Skip to content

Commit

Permalink
Merge pull request #223 from onflow/stable-cadence
Browse files Browse the repository at this point in the history
Stable cadence
  • Loading branch information
jribbink authored Sep 17, 2024
2 parents 2959756 + da55d0a commit bb33f8f
Show file tree
Hide file tree
Showing 37 changed files with 2,424 additions and 1,060 deletions.
4 changes: 0 additions & 4 deletions .env.development
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
# We recommend to use the service account defined in the flow.json file your emulator is using.

FLOW_INIT_ACCOUNTS=0
CONTRACT_FUNGIBLE_TOKEN=0xee82856bf20e2aa6
CONTRACT_FLOW_TOKEN=0x0ae53cb6e3f42a79
CONTRACT_FUSD=0xf8d6e0586b0a20c7
CONTRACT_FCL_CRYPTO=0xf8d6e0586b0a20c7
FLOW_AVATAR_URL=https://avatars.onflow.org/avatar/

# EMULATOR REST API ENDPOINT
Expand Down
4 changes: 0 additions & 4 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
# We recommend to use the service account definied in the flow.json file your emulator is using.

FLOW_INIT_ACCOUNTS=0
CONTRACT_FUNGIBLE_TOKEN=0xee82856bf20e2aa6
CONTRACT_FLOW_TOKEN=0x0ae53cb6e3f42a79
CONTRACT_FUSD=0xf8d6e0586b0a20c7
CONTRACT_FCL_CRYPTO=0xf8d6e0586b0a20c7
FLOW_AVATAR_URL=https://avatars.onflow.org/avatar/

# EMULATOR REST API ENDPOINT
Expand Down
83 changes: 55 additions & 28 deletions cadence/contracts/FCL.cdc
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
pub contract FCL {
pub let storagePath: StoragePath
access(all) contract FCL {
access(all) let storagePath: StoragePath

pub struct Account {
pub let type: String
pub let address: Address
pub let keyId: Int
pub var label: String
pub var scopes: [String]
access(all) struct FCLKey {
access(all) let publicKey: [UInt8]
access(all) let signatureAlgorithm: UInt8
access(all) let hashAlgorithm: UInt8

init(publicKey: [UInt8], signatureAlgorithm: UInt8, hashAlgorithm: UInt8) {
self.publicKey = publicKey
self.signatureAlgorithm = signatureAlgorithm
self.hashAlgorithm = hashAlgorithm
}
}

access(all) struct FCLAccount {
access(all) let type: String
access(all) let address: Address
access(all) let keyId: Int
access(all) var label: String
access(all) var scopes: [String]

init(address: Address, label: String, scopes: [String]) {
self.type = "ACCOUNT"
Expand All @@ -16,59 +28,74 @@ pub contract FCL {
self.scopes = scopes
}

pub fun update(label: String, scopes: [String]) {
access(all) fun update(label: String, scopes: [String]) {
self.label = label
self.scopes = scopes
}
}

pub resource Root {
pub let key: [UInt8]
pub let accounts: {Address: Account}
access(all) resource Root {
access(all) let key: FCLKey
access(all) let accounts: {Address: FCLAccount}

init (_ key: String) {
self.key = key.decodeHex()
init (_ key: FCLKey) {
self.key = key
self.accounts = {}
}

pub fun add(_ acct: Account) {
access(all) fun add(_ acct: FCLAccount) {
self.accounts[acct.address] = acct
}

pub fun update(address: Address, label: String, scopes: [String]) {
access(all) fun update(address: Address, label: String, scopes: [String]) {
let acct = self.accounts[address]
acct!.update(label: label, scopes: scopes)
self.accounts[address] = acct
}
}

pub fun accounts(): {Address: Account} {
return self.account.borrow<&Root>(from: self.storagePath)!.accounts
access(all) fun accounts(): &{Address: FCLAccount} {
return self.account.storage.borrow<&Root>(from: self.storagePath)!.accounts
}

pub fun getServiceKey(): [UInt8] {
return self.account.borrow<&Root>(from: self.storagePath)!.key
access(all) fun getServiceKey(): &FCLKey {
return self.account.storage.borrow<&Root>(from: self.storagePath)!.key
}

pub fun new(label: String, scopes: [String], address: Address?): AuthAccount {
let acct = AuthAccount(payer: self.account)
acct.addPublicKey(self.getServiceKey())
access(all) fun new(label: String, scopes: [String], address: Address?): &Account {
let acct = Account(payer: self.account)
let key = self.getServiceKey()

acct.keys.add(
publicKey: PublicKey(
publicKey: key.publicKey.map(fun (_ byte: UInt8): UInt8 {
return byte
}),
signatureAlgorithm: SignatureAlgorithm(key.signatureAlgorithm)!,
),
hashAlgorithm: HashAlgorithm(key.hashAlgorithm)!,
weight: 1000.0
)

self.account
.storage
.borrow<&Root>(from: self.storagePath)!
.add(Account(address: address ?? acct.address, label: label, scopes: scopes))
.add(FCLAccount(address: address ?? acct.address, label: label, scopes: scopes))

return acct
}

pub fun update(address: Address, label: String, scopes: [String]) {
self.account.borrow<&Root>(from: self.storagePath)!
access(all) fun update(address: Address, label: String, scopes: [String]) {
self.account.storage.borrow<&Root>(from: self.storagePath)!
.update(address: address, label: label, scopes: scopes)
}

init (key: String, initAccountsLabels: [String]) {
init (publicKey: String, hashAlgorithm: UInt8, signAlgorithm: UInt8, initAccountsLabels: [String]) {
let keyByteArray = publicKey.decodeHex()
let key = FCLKey(publicKey: keyByteArray, signatureAlgorithm: signAlgorithm, hashAlgorithm: hashAlgorithm)

self.storagePath = /storage/FCL_DEV_WALLET
self.account.save(<- create Root(key), to: self.storagePath)
self.account.storage.save(<- create Root(key), to: self.storagePath)

self.new(label: initAccountsLabels[0], scopes: [], address: self.account.address)
var acctInitIndex = 1
Expand Down
51 changes: 43 additions & 8 deletions cadence/contracts/FCLCrypto.cdc
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
pub contract FCLCrypto {

pub fun verifyUserSignatures(
/*
FCLCrypto
The FCLCrypto contract provides functions which allow to verify signatures and check for signing power.
*/

access(all) contract FCLCrypto {

/// verifyUserSignatures allows to verify the user signatures for the given account.
///
/// @param address: The address of the account
/// @param message: The signed data
/// @param keyIndices: This integer array maps the signatures to the account keys by index
/// @param signatures: The signatures belonging to the account keys
///
/// @return Whether all signatures are valid and the combined total key weight reaches signing power
///
access(all) fun verifyUserSignatures(
address: Address,
message: String,
keyIndices: [Int],
Expand All @@ -15,7 +30,16 @@ pub contract FCLCrypto {
)
}

pub fun verifyAccountProofSignatures(
/// verifyAccountProofSignatures allows to verify the account proof signatures for the given account.
///
/// @param address: The address of the account
/// @param message: The signed data
/// @param keyIndices: This integer array maps the signatures to the account keys by index
/// @param signatures: The signatures belonging to the account keys
///
/// @return Whether all signatures are valid and the combined total key weight reaches signing power
///
access(all) fun verifyAccountProofSignatures(
address: Address,
message: String,
keyIndices: [Int],
Expand All @@ -37,7 +61,18 @@ pub contract FCLCrypto {
)
}

priv fun verifySignatures(
/// verifySignatures is a private function which provides the functionality to verify
/// signatures for the public functions.
///
/// @param address: The address of the account
/// @param message: The signed data
/// @param keyIndices: This integer array maps the signatures to the account keys by index
/// @param signatures: The signatures belonging to the account keys
/// @param domainSeparationTag: The domain tag originally used for the signatures
///
/// @return Whether all signatures are valid and the combined total key weight reaches signing power
///
access(self) fun verifySignatures(
address: Address,
message: String,
keyIndices: [Int],
Expand Down Expand Up @@ -96,9 +131,9 @@ pub contract FCLCrypto {
return totalWeight >= 1000.0
}

priv let domainSeparationTagFlowUser: String
priv let domainSeparationTagFCLUser: String
priv let domainSeparationTagAccountProof: String
access(self) let domainSeparationTagFlowUser: String
access(self) let domainSeparationTagFCLUser: String
access(self) let domainSeparationTagAccountProof: String

init() {
self.domainSeparationTagFlowUser = "FLOW-V0.0-user"
Expand Down
Loading

0 comments on commit bb33f8f

Please sign in to comment.