Skip to content

Commit

Permalink
Update Cadence code to stable cadence
Browse files Browse the repository at this point in the history
  • Loading branch information
jribbink committed Jan 11, 2024
1 parent f3533e5 commit 4121feb
Show file tree
Hide file tree
Showing 22 changed files with 1,823 additions and 285 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"editor.formatOnSave": true
}
64 changes: 39 additions & 25 deletions cadence/contracts/FCL.cdc
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
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 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 +16,73 @@ 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: [UInt8]
access(all) let accounts: {Address: FCLAccount}

init (_ key: String) {
self.key = key.decodeHex()
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(): [UInt8] {
let keyRef = self.account.storage.borrow<&Root>(from: self.storagePath)!.key
return keyRef.map(fun (_ x: UInt8): UInt8 {
return x
})
}

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 rawKey = self.getServiceKey()
let decodedKey = RLP.decodeList(rawKey)

acct.keys.add(
publicKey: PublicKey(
publicKey: decodedKey[0].slice(from: 2, upTo: decodedKey[0].length),
signatureAlgorithm: SignatureAlgorithm.ECDSA_P256,
),
hashAlgorithm: HashAlgorithm(decodedKey[2][0])!,
weight: UFix64.fromBigEndianBytes(decodedKey[3])!
)

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]) {
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
199 changes: 0 additions & 199 deletions cadence/contracts/FungibleToken.cdc

This file was deleted.

Loading

0 comments on commit 4121feb

Please sign in to comment.