|
| 1 | +import {account, config} from "@onflow/fcl" |
| 2 | +import { |
| 3 | + createAccount, |
| 4 | + emulator, |
| 5 | + getAccountAddress, |
| 6 | + getServiceAddress, |
| 7 | + init, |
| 8 | + sendTransaction, |
| 9 | + shallPass, |
| 10 | + signUserMessage, |
| 11 | + verifyUserSignatures, |
| 12 | +} from "../src" |
| 13 | +import { |
| 14 | + prependDomainTag, |
| 15 | + resolveHashAlgoKey, |
| 16 | + resolveSignAlgoKey, |
| 17 | +} from "../src/crypto" |
| 18 | + |
| 19 | +beforeEach(async () => { |
| 20 | + await init() |
| 21 | + await emulator.start() |
| 22 | +}) |
| 23 | +afterEach(async () => { |
| 24 | + await emulator.stop() |
| 25 | +}) |
| 26 | + |
| 27 | +describe("cryptography tests", () => { |
| 28 | + test("signUserMessage - sign with address", async () => { |
| 29 | + const Alice = await getAccountAddress("Alice") |
| 30 | + const msgHex = "a1b2c3" |
| 31 | + |
| 32 | + const signature = await signUserMessage(msgHex, Alice) |
| 33 | + |
| 34 | + expect(Object.keys(signature).length).toBe(3) |
| 35 | + expect(signature.addr).toBe(Alice) |
| 36 | + expect(signature.keyId).toBe(0) |
| 37 | + expect(await verifyUserSignatures(msgHex, [signature])).toBe(true) |
| 38 | + }) |
| 39 | + |
| 40 | + test("signUserMessage - sign with KeyObject", async () => { |
| 41 | + const hashAlgorithm = "SHA3_256" |
| 42 | + const signatureAlgorithm = "ECDSA_P256" |
| 43 | + |
| 44 | + const privateKey = "1234" |
| 45 | + const Adam = await createAccount({ |
| 46 | + name: "Adam", |
| 47 | + keys: [ |
| 48 | + { |
| 49 | + privateKey, |
| 50 | + hashAlgorithm, |
| 51 | + signatureAlgorithm, |
| 52 | + weight: 1000, |
| 53 | + }, |
| 54 | + ], |
| 55 | + }) |
| 56 | + |
| 57 | + const signer = { |
| 58 | + addr: Adam, |
| 59 | + keyId: 0, |
| 60 | + privateKey, |
| 61 | + hashAlgorithm, |
| 62 | + signatureAlgorithm, |
| 63 | + } |
| 64 | + |
| 65 | + const msgHex = "a1b2c3" |
| 66 | + const signature = await signUserMessage(msgHex, signer) |
| 67 | + |
| 68 | + expect(Object.keys(signature).length).toBe(3) |
| 69 | + expect(signature.addr).toBe(Adam) |
| 70 | + expect(signature.keyId).toBe(0) |
| 71 | + expect(await verifyUserSignatures(msgHex, [signature])).toBe(true) |
| 72 | + }) |
| 73 | + |
| 74 | + test("signUserMessage - sign with domain separation tag", async () => { |
| 75 | + const Alice = await getAccountAddress("Alice") |
| 76 | + const msgHex = "a1b2c3" |
| 77 | + |
| 78 | + const signature = await signUserMessage(msgHex, Alice, "foo") |
| 79 | + |
| 80 | + expect(Object.keys(signature).length).toBe(3) |
| 81 | + expect(signature.addr).toBe(Alice) |
| 82 | + expect(signature.keyId).toBe(0) |
| 83 | + expect(await verifyUserSignatures(msgHex, [signature], "foo")).toBe(true) |
| 84 | + }) |
| 85 | + |
| 86 | + test("signUserMessage - sign with service key", async () => { |
| 87 | + const Alice = await getServiceAddress() |
| 88 | + const msgHex = "a1b2c3" |
| 89 | + |
| 90 | + const signature = await signUserMessage(msgHex, Alice) |
| 91 | + |
| 92 | + expect(Object.keys(signature).length).toBe(3) |
| 93 | + expect(signature.addr).toBe(Alice) |
| 94 | + expect(signature.keyId).toBe(0) |
| 95 | + expect(await verifyUserSignatures(msgHex, [signature])).toBe(true) |
| 96 | + }) |
| 97 | + |
| 98 | + test("verifyUserSignature & signUserMessage - work with Buffer messageHex", async () => { |
| 99 | + const Alice = await getAccountAddress("Alice") |
| 100 | + const msgHex = Buffer.from([0xa1, 0xb2, 0xc3]) |
| 101 | + const signature = await signUserMessage(msgHex, Alice) |
| 102 | + |
| 103 | + expect(await verifyUserSignatures(msgHex, [signature])).toBe(true) |
| 104 | + }) |
| 105 | + |
| 106 | + test("verifyUserSignature - fails with bad signature", async () => { |
| 107 | + const Alice = await getAccountAddress("Alice") |
| 108 | + const msgHex = "a1b2c3" |
| 109 | + |
| 110 | + const badSignature = { |
| 111 | + addr: Alice, |
| 112 | + keyId: 0, |
| 113 | + signature: "a1b2c3", |
| 114 | + } |
| 115 | + |
| 116 | + expect(await verifyUserSignatures(msgHex, [badSignature])).toBe(false) |
| 117 | + }) |
| 118 | + |
| 119 | + test("verifyUserSignature - fails if weight < 1000", async () => { |
| 120 | + const Alice = await createAccount({ |
| 121 | + name: "Alice", |
| 122 | + keys: [ |
| 123 | + { |
| 124 | + privateKey: await config().get("PRIVATE_KEY"), |
| 125 | + weight: 123, |
| 126 | + }, |
| 127 | + ], |
| 128 | + }) |
| 129 | + const msgHex = "a1b2c3" |
| 130 | + const signature = await signUserMessage(msgHex, Alice) |
| 131 | + |
| 132 | + expect(await verifyUserSignatures(msgHex, [signature])).toBe(false) |
| 133 | + }) |
| 134 | + |
| 135 | + test("verifyUserSignatures - throws with null signature object", async () => { |
| 136 | + const msgHex = "a1b2c3" |
| 137 | + |
| 138 | + await expect(verifyUserSignatures(msgHex, null)).rejects.toThrow( |
| 139 | + "INVARIANT One or mores signatures must be provided" |
| 140 | + ) |
| 141 | + }) |
| 142 | + |
| 143 | + test("verifyUserSignatures - throws with no signatures in array", async () => { |
| 144 | + const msgHex = "a1b2c3" |
| 145 | + |
| 146 | + await expect(verifyUserSignatures(msgHex, [])).rejects.toThrow( |
| 147 | + "INVARIANT One or mores signatures must be provided" |
| 148 | + ) |
| 149 | + }) |
| 150 | + |
| 151 | + test("verifyUserSignatures - throws with different account signatures", async () => { |
| 152 | + const Alice = await getAccountAddress("Alice") |
| 153 | + const Bob = await getAccountAddress("Bob") |
| 154 | + const msgHex = "a1b2c3" |
| 155 | + |
| 156 | + const signatureAlice = await signUserMessage(msgHex, Alice) |
| 157 | + const signatureBob = await signUserMessage(msgHex, Bob) |
| 158 | + |
| 159 | + await expect( |
| 160 | + verifyUserSignatures(msgHex, [signatureAlice, signatureBob]) |
| 161 | + ).rejects.toThrow("INVARIANT Signatures must belong to the same address") |
| 162 | + }) |
| 163 | + |
| 164 | + test("verifyUserSignatures - throws with invalid signature format", async () => { |
| 165 | + const msgHex = "a1b2c3" |
| 166 | + const signature = { |
| 167 | + foo: "bar", |
| 168 | + } |
| 169 | + |
| 170 | + await expect(verifyUserSignatures(msgHex, [signature])).rejects.toThrow( |
| 171 | + "INVARIANT One or more signature is invalid. Valid signatures have the following keys: addr, keyId, siganture" |
| 172 | + ) |
| 173 | + }) |
| 174 | + |
| 175 | + test("verifyUserSignatures - throws with non-existant key", async () => { |
| 176 | + const Alice = await getAccountAddress("Alice") |
| 177 | + const msgHex = "a1b2c3" |
| 178 | + |
| 179 | + const signature = await signUserMessage(msgHex, Alice) |
| 180 | + signature.keyId = 42 |
| 181 | + |
| 182 | + await expect(verifyUserSignatures(msgHex, [signature])).rejects.toThrow( |
| 183 | + `INVARIANT Key index ${signature.keyId} does not exist on account ${Alice}` |
| 184 | + ) |
| 185 | + }) |
| 186 | + |
| 187 | + test("prependDomainTag prepends a domain tag to a given msgHex", () => { |
| 188 | + const msgHex = "a1b2c3" |
| 189 | + const domainTag = "hello world" |
| 190 | + const paddedDomainTagHex = |
| 191 | + "00000000000000000000000000000000000000000068656c6c6f20776f726c64" |
| 192 | + |
| 193 | + const result = prependDomainTag(msgHex, domainTag) |
| 194 | + const expected = paddedDomainTagHex + msgHex |
| 195 | + |
| 196 | + expect(result).toEqual(expected) |
| 197 | + }) |
| 198 | + |
| 199 | + test("resolveHashAlgoKey - unsupported hash algorithm", () => { |
| 200 | + const hashAlgorithm = "ABC123" |
| 201 | + expect(() => resolveHashAlgoKey(hashAlgorithm)).toThrow( |
| 202 | + `Provided hash algorithm "${hashAlgorithm}" is not currently supported` |
| 203 | + ) |
| 204 | + }) |
| 205 | + |
| 206 | + test("resolveHashAlgoKey - unsupported signature algorithm", () => { |
| 207 | + const signatureAlgorithm = "ABC123" |
| 208 | + expect(() => resolveSignAlgoKey(signatureAlgorithm)).toThrow( |
| 209 | + `Provided signature algorithm "${signatureAlgorithm}" is not currently supported` |
| 210 | + ) |
| 211 | + }) |
| 212 | +}) |
0 commit comments