Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent committed Oct 21, 2024
1 parent 8622482 commit 98fb01f
Show file tree
Hide file tree
Showing 2 changed files with 372 additions and 0 deletions.
342 changes: 342 additions & 0 deletions contracts/Crypto_test.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,342 @@
import Test
import Crypto from "Crypto.cdc"

access(all)
fun setup() {
let err = Test.deployContract(
name: "Crypto",
path: "Crypto.cdc",
arguments: []
)

Test.expect(err, Test.beNil())
}

access(all)
fun testCryptoHash() {
let hash = Crypto.hash([1, 2, 3], algorithm: HashAlgorithm.SHA3_256)
Test.assertEqual(32, hash.length)
}

access(all)
fun testCryptoHashWithTag() {
let hash = Crypto.hashWithTag(
[1, 2, 3],
tag: "v0.1.tag",
algorithm: HashAlgorithm.SHA3_256
)
Test.assertEqual(32, hash.length)
}

access(all)
fun testAddKeyToKeyList() {
let keyList = Crypto.KeyList()

let publicKey = PublicKey(
publicKey:
"db04940e18ec414664ccfd31d5d2d4ece3985acb8cb17a2025b2f1673427267968e52e2bbf3599059649d4b2cce98fdb8a3048e68abf5abe3e710129e90696ca".decodeHex(),
signatureAlgorithm: SignatureAlgorithm.ECDSA_P256
)
keyList.add(
publicKey,
hashAlgorithm: HashAlgorithm.SHA3_256,
weight: 1.0
)

Test.assert(keyList.get(keyIndex: 0) != nil)
}

access(all)
fun testGetKeyFromList() {
let keyList = Crypto.KeyList()

let publicKey = PublicKey(
publicKey:
"db04940e18ec414664ccfd31d5d2d4ece3985acb8cb17a2025b2f1673427267968e52e2bbf3599059649d4b2cce98fdb8a3048e68abf5abe3e710129e90696ca".decodeHex(),
signatureAlgorithm: SignatureAlgorithm.ECDSA_P256
)
keyList.add(
publicKey,
hashAlgorithm: HashAlgorithm.SHA3_256,
weight: 1.0
)

Test.assert(keyList.get(keyIndex: 0) != nil)
Test.assert(keyList.get(keyIndex: 2) == nil)
}

access(all)
fun testRevokeKeyFromList() {
let keyList = Crypto.KeyList()

let publicKey = PublicKey(
publicKey:
"db04940e18ec414664ccfd31d5d2d4ece3985acb8cb17a2025b2f1673427267968e52e2bbf3599059649d4b2cce98fdb8a3048e68abf5abe3e710129e90696ca".decodeHex(),
signatureAlgorithm: SignatureAlgorithm.ECDSA_P256
)
keyList.add(
publicKey,
hashAlgorithm: HashAlgorithm.SHA3_256,
weight: 0.5
)

keyList.revoke(keyIndex: 0)
keyList.revoke(keyIndex: 2)

Test.assert(keyList.get(keyIndex: 0)!.isRevoked)
Test.assert(keyList.get(keyIndex: 2) == nil)
}

access(all)
fun testKeyListVerify() {
let keyList = Crypto.KeyList()

let publicKeyA = PublicKey(
publicKey:
"db04940e18ec414664ccfd31d5d2d4ece3985acb8cb17a2025b2f1673427267968e52e2bbf3599059649d4b2cce98fdb8a3048e68abf5abe3e710129e90696ca".decodeHex(),
signatureAlgorithm: SignatureAlgorithm.ECDSA_P256
)

keyList.add(
publicKeyA,
hashAlgorithm: HashAlgorithm.SHA3_256,
weight: 0.5
)

let publicKeyB = PublicKey(
publicKey:
"df9609ee588dd4a6f7789df8d56f03f545d4516f0c99b200d73b9a3afafc14de5d21a4fc7a2a2015719dc95c9e756cfa44f2a445151aaf42479e7120d83df956".decodeHex(),
signatureAlgorithm: SignatureAlgorithm.ECDSA_P256
)

keyList.add(
publicKeyB,
hashAlgorithm: HashAlgorithm.SHA3_256,
weight: 0.5
)

let signatureSet = [
Crypto.KeyListSignature(
keyIndex: 0,
signature:
"8870a8cbe6f44932ba59e0d15a706214cc4ad2538deb12c0cf718d86f32c47765462a92ce2da15d4a29eb4e2b6fa05d08c7db5d5b2a2cd8c2cb98ded73da31f6".decodeHex()
),
Crypto.KeyListSignature(
keyIndex: 1,
signature:
"bbdc5591c3f937a730d4f6c0a6fde61a0a6ceaa531ccb367c3559335ab9734f4f2b9da8adbe371f1f7da913b5a3fdd96a871e04f078928ca89a83d841c72fadf".decodeHex()
)
]

// "foo", encoded as UTF-8, in hex representation
let signedData = "666f6f".decodeHex()

let isValid = keyList.verify(
signatureSet: signatureSet,
signedData: signedData,
domainSeparationTag: "FLOW-V0.0-user"
)

Test.assert(isValid)
}

access(all)
fun testKeyListVerifyInsufficientWeights() {
let keyList = Crypto.KeyList()

let publicKeyA = PublicKey(
publicKey:
"db04940e18ec414664ccfd31d5d2d4ece3985acb8cb17a2025b2f1673427267968e52e2bbf3599059649d4b2cce98fdb8a3048e68abf5abe3e710129e90696ca".decodeHex(),
signatureAlgorithm: SignatureAlgorithm.ECDSA_P256
)

keyList.add(
publicKeyA,
hashAlgorithm: HashAlgorithm.SHA3_256,
weight: 0.4
)

let publicKeyB = PublicKey(
publicKey:
"df9609ee588dd4a6f7789df8d56f03f545d4516f0c99b200d73b9a3afafc14de5d21a4fc7a2a2015719dc95c9e756cfa44f2a445151aaf42479e7120d83df956".decodeHex(),
signatureAlgorithm: SignatureAlgorithm.ECDSA_P256
)

keyList.add(
publicKeyB,
hashAlgorithm: HashAlgorithm.SHA3_256,
weight: 0.5
)

let signatureSet = [
Crypto.KeyListSignature(
keyIndex: 0,
signature:
"8870a8cbe6f44932ba59e0d15a706214cc4ad2538deb12c0cf718d86f32c47765462a92ce2da15d4a29eb4e2b6fa05d08c7db5d5b2a2cd8c2cb98ded73da31f6".decodeHex()
),
Crypto.KeyListSignature(
keyIndex: 1,
signature:
"bbdc5591c3f937a730d4f6c0a6fde61a0a6ceaa531ccb367c3559335ab9734f4f2b9da8adbe371f1f7da913b5a3fdd96a871e04f078928ca89a83d841c72fadf".decodeHex()
)
]

// "foo", encoded as UTF-8, in hex representation
let signedData = "666f6f".decodeHex()

let isValid = keyList.verify(
signatureSet: signatureSet,
signedData: signedData,
domainSeparationTag: "FLOW-V0.0-user"
)

Test.assert(!isValid)
}

access(all)
fun testKeyListVerifyWithRevokedKey() {
let keyList = Crypto.KeyList()

let publicKey = PublicKey(
publicKey:
"db04940e18ec414664ccfd31d5d2d4ece3985acb8cb17a2025b2f1673427267968e52e2bbf3599059649d4b2cce98fdb8a3048e68abf5abe3e710129e90696ca".decodeHex(),
signatureAlgorithm: SignatureAlgorithm.ECDSA_P256
)
keyList.add(
publicKey,
hashAlgorithm: HashAlgorithm.SHA3_256,
weight: 0.5
)

let signatureSet = [
Crypto.KeyListSignature(
keyIndex: 0,
signature:
"8870a8cbe6f44932ba59e0d15a706214cc4ad2538deb12c0cf718d86f32c47765462a92ce2da15d4a29eb4e2b6fa05d08c7db5d5b2a2cd8c2cb98ded73da31f6".decodeHex()
)
]

// "foo", encoded as UTF-8, in hex representation
let signedData = "666f6f".decodeHex()

keyList.revoke(keyIndex: 0)

let isValid = keyList.verify(
signatureSet: signatureSet,
signedData: signedData,
domainSeparationTag: "FLOW-V0.0-user"
)

Test.assert(!isValid)
}

access(all)
fun testKeyListVerifyWithMissingSignature() {
let keyList = Crypto.KeyList()

let publicKey = PublicKey(
publicKey:
"db04940e18ec414664ccfd31d5d2d4ece3985acb8cb17a2025b2f1673427267968e52e2bbf3599059649d4b2cce98fdb8a3048e68abf5abe3e710129e90696ca".decodeHex(),
signatureAlgorithm: SignatureAlgorithm.ECDSA_P256
)
keyList.add(
publicKey,
hashAlgorithm: HashAlgorithm.SHA3_256,
weight: 0.5
)

let signatureSet = [
Crypto.KeyListSignature(
keyIndex: 1,
signature:
"8870a8cbe6f44932ba59e0d15a706214cc4ad2538deb12c0cf718d86f32c47765462a92ce2da15d4a29eb4e2b6fa05d08c7db5d5b2a2cd8c2cb98ded73da31f6".decodeHex()
)
]

// "foo", encoded as UTF-8, in hex representation
let signedData = "666f6f".decodeHex()

let isValid = keyList.verify(
signatureSet: signatureSet,
signedData: signedData,
domainSeparationTag: "FLOW-V0.0-user"
)

Test.assert(!isValid)
}

access(all)
fun testKeyListVerifyDuplicateSignature() {
let keyList = Crypto.KeyList()

let publicKey = PublicKey(
publicKey:
"db04940e18ec414664ccfd31d5d2d4ece3985acb8cb17a2025b2f1673427267968e52e2bbf3599059649d4b2cce98fdb8a3048e68abf5abe3e710129e90696ca".decodeHex(),
signatureAlgorithm: SignatureAlgorithm.ECDSA_P256
)
keyList.add(
publicKey,
hashAlgorithm: HashAlgorithm.SHA3_256,
weight: 0.5
)

let signatureSet = [
Crypto.KeyListSignature(
keyIndex: 0,
signature:
"8870a8cbe6f44932ba59e0d15a706214cc4ad2538deb12c0cf718d86f32c47765462a92ce2da15d4a29eb4e2b6fa05d08c7db5d5b2a2cd8c2cb98ded73da31f6".decodeHex()
),
Crypto.KeyListSignature(
keyIndex: 0,
signature:
"8870a8cbe6f44932ba59e0d15a706214cc4ad2538deb12c0cf718d86f32c47765462a92ce2da15d4a29eb4e2b6fa05d08c7db5d5b2a2cd8c2cb98ded73da31f6".decodeHex()
)
]

// "foo", encoded as UTF-8, in hex representation
let signedData = "666f6f".decodeHex()

let isValid = keyList.verify(
signatureSet: signatureSet,
signedData: signedData,
domainSeparationTag: "FLOW-V0.0-user"
)

Test.assert(!isValid)
}

access(all)
fun testKeyListVerifyInvalidSignature() {
let keyList = Crypto.KeyList()

let publicKey = PublicKey(
publicKey:
"db04940e18ec414664ccfd31d5d2d4ece3985acb8cb17a2025b2f1673427267968e52e2bbf3599059649d4b2cce98fdb8a3048e68abf5abe3e710129e90696ca".decodeHex(),
signatureAlgorithm: SignatureAlgorithm.ECDSA_P256
)
keyList.add(
publicKey,
hashAlgorithm: HashAlgorithm.SHA3_256,
weight: 0.5
)

let signatureSet = [
Crypto.KeyListSignature(
keyIndex: 0,
signature:
"db70a8cbe6f44932ba59e0d15a706214cc4ad2538deb12c0cf718d86f32c47765462a92ce2da15d4a29eb4e2b6fa05d08c7db5d5b2a2cd8c2cb98ded73da31f6".decodeHex()
)
]

// "foo", encoded as UTF-8, in hex representation
let signedData = "666f6f".decodeHex()

let isValid = keyList.verify(
signatureSet: signatureSet,
signedData: signedData,
domainSeparationTag: "FLOW-V0.0-user"
)

Test.assert(!isValid)
}

30 changes: 30 additions & 0 deletions contracts/flow.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"networks": {
"emulator": "127.0.0.1:3569",
"testing": "127.0.0.1:3569",
"mainnet": "access.mainnet.nodes.onflow.org:9000",
"sandboxnet": "access.sandboxnet.nodes.onflow.org:9000",
"testnet": "access.devnet.nodes.onflow.org:9000"
},
"contracts": {
"Crypto": {
"source": "crypto.cdc",
"aliases": {
"testing": "0x0000000000000007"
}
}
},
"deployments": {
"emulator": {
"emulator-account": [
"Crypto"
]
}
},
"accounts": {
"emulator-account": {
"address": "f8d6e0586b0a20c7",
"key": "b775d6da04a0b4819903d89a25395bfd90eb8559182255690c7c141edaeae923"
}
}
}

0 comments on commit 98fb01f

Please sign in to comment.