forked from gregsantos/fcl-next-harness
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update: add FCLCrypto contract and deploy instructions
- Loading branch information
Greg Santos
committed
Jul 11, 2022
1 parent
fc91ed0
commit 2e2e37c
Showing
4 changed files
with
151 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"emulators": { | ||
"default": { | ||
"port": 3569, | ||
"serviceAccount": "emulator-account" | ||
} | ||
}, | ||
"contracts": { | ||
"FCLCrypto": "./flow/cadence/contracts/FCLCrypto.cdc" | ||
}, | ||
"networks": { | ||
"emulator": "127.0.0.1:3569", | ||
"mainnet": "access.mainnet.nodes.onflow.org:9000", | ||
"testnet": "access.devnet.nodes.onflow.org:9000" | ||
}, | ||
"accounts": { | ||
"emulator-account": { | ||
"address": "f8d6e0586b0a20c7", | ||
"key": "28b9aa18adda109686c9d4937a58e4b3044ab57bac55679b1050ef9898bf4bba" | ||
} | ||
}, | ||
"deployments": { | ||
"emulator": { | ||
"emulator-account": ["FCLCrypto"] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
pub contract FCLCrypto { | ||
|
||
pub fun verifyUserSignatures( | ||
address: Address, | ||
message: String, | ||
keyIndices: [Int], | ||
signatures: [String] | ||
): Bool { | ||
return self.verifySignatures( | ||
address: address, | ||
message: message, | ||
keyIndices: keyIndices, | ||
signatures: signatures, | ||
domainSeparationTag: self.domainSeparationTagFlowUser, | ||
) | ||
} | ||
|
||
pub fun verifyAccountProofSignatures( | ||
address: Address, | ||
message: String, | ||
keyIndices: [Int], | ||
signatures: [String] | ||
): Bool { | ||
return self.verifySignatures( | ||
address: address, | ||
message: message, | ||
keyIndices: keyIndices, | ||
signatures: signatures, | ||
domainSeparationTag: self.domainSeparationTagAccountProof, | ||
) || | ||
self.verifySignatures( | ||
address: address, | ||
message: message, | ||
keyIndices: keyIndices, | ||
signatures: signatures, | ||
domainSeparationTag: self.domainSeparationTagFlowUser, | ||
) | ||
} | ||
|
||
priv fun verifySignatures( | ||
address: Address, | ||
message: String, | ||
keyIndices: [Int], | ||
signatures: [String], | ||
domainSeparationTag: String, | ||
): Bool { | ||
pre { | ||
keyIndices.length == signatures.length : "Key index list length does not match signature list length" | ||
} | ||
|
||
let account = getAccount(address) | ||
let messageBytes = message.decodeHex() | ||
|
||
var totalWeight: UFix64 = 0.0 | ||
let seenKeyIndices: {Int: Bool} = {} | ||
|
||
var i = 0 | ||
|
||
for keyIndex in keyIndices { | ||
|
||
let accountKey = account.keys.get(keyIndex: keyIndex) ?? panic("Key provided does not exist on account") | ||
let signature = signatures[i].decodeHex() | ||
|
||
// Ensure this key index has not already been seen | ||
if seenKeyIndices[accountKey.keyIndex] ?? false { | ||
return false | ||
} | ||
|
||
// Record the key index was seen | ||
seenKeyIndices[accountKey.keyIndex] = true | ||
|
||
// Ensure the key is not revoked | ||
if accountKey.isRevoked { | ||
return false | ||
} | ||
|
||
// Ensure the signature is valid | ||
if !accountKey.publicKey.verify( | ||
signature: signature, | ||
signedData: messageBytes, | ||
domainSeparationTag: domainSeparationTag, | ||
hashAlgorithm: accountKey.hashAlgorithm | ||
) { | ||
return false | ||
} | ||
|
||
totalWeight = totalWeight + accountKey.weight | ||
|
||
i = i + 1 | ||
} | ||
|
||
return totalWeight >= 1000.0 | ||
} | ||
|
||
priv let domainSeparationTagFlowUser: String | ||
priv let domainSeparationTagFCLUser: String | ||
priv let domainSeparationTagAccountProof: String | ||
|
||
init() { | ||
self.domainSeparationTagFlowUser = "FLOW-V0.0-user" | ||
self.domainSeparationTagFCLUser = "FCL-USER-V0.0" | ||
self.domainSeparationTagAccountProof = "FCL-ACCOUNT-PROOF-V0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters