|
1 | 1 | import typedDataExample from '../../__mocks__/typedDataExample.json'; |
| 2 | +import typedDataSessionExample from '../../__mocks__/typedDataSessionExample.json'; |
2 | 3 | import typedDataStructArrayExample from '../../__mocks__/typedDataStructArrayExample.json'; |
3 | 4 | import { number } from '../../src'; |
| 5 | +import { getSelectorFromName } from '../../src/utils/hash'; |
| 6 | +import { MerkleTree } from '../../src/utils/merkle'; |
4 | 7 | import { BigNumberish } from '../../src/utils/number'; |
5 | | -import { encodeType, getMessageHash, getStructHash, getTypeHash } from '../../src/utils/typedData'; |
| 8 | +import { |
| 9 | + StarkNetDomain, |
| 10 | + encodeType, |
| 11 | + encodeValue, |
| 12 | + getMessageHash, |
| 13 | + getStructHash, |
| 14 | + getTypeHash, |
| 15 | +} from '../../src/utils/typedData'; |
6 | 16 |
|
7 | 17 | describe('typedData', () => { |
8 | 18 | test('should get right type encoding', () => { |
9 | | - const typeEncoding = encodeType(typedDataExample, 'Mail'); |
| 19 | + const typeEncoding = encodeType(typedDataExample.types, 'Mail'); |
10 | 20 | expect(typeEncoding).toMatchInlineSnapshot( |
11 | 21 | `"Mail(from:Person,to:Person,contents:felt)Person(name:felt,wallet:felt)"` |
12 | 22 | ); |
13 | | - const typeEncodingStructArr = encodeType(typedDataStructArrayExample, 'Mail'); |
| 23 | + const typeEncodingStructArr = encodeType(typedDataStructArrayExample.types, 'Mail'); |
14 | 24 | expect(typeEncodingStructArr).toMatchInlineSnapshot( |
15 | 25 | `"Mail(from:Person,to:Person,posts_len:felt,posts:Post*)Person(name:felt,wallet:felt)Post(title:felt,content:felt)"` |
16 | 26 | ); |
17 | 27 | }); |
18 | 28 |
|
19 | 29 | test('should get right type hash', () => { |
20 | | - const typeHashDomain = getTypeHash(typedDataExample, 'StarkNetDomain'); |
| 30 | + const typeHashDomain = getTypeHash(typedDataExample.types, 'StarkNetDomain'); |
21 | 31 | expect(typeHashDomain).toMatchInlineSnapshot( |
22 | 32 | `"0x1bfc207425a47a5dfa1a50a4f5241203f50624ca5fdf5e18755765416b8e288"` |
23 | 33 | ); |
24 | | - const typeHashPerson = getTypeHash(typedDataExample, 'Person'); |
| 34 | + const typeHashPerson = getTypeHash(typedDataExample.types, 'Person'); |
25 | 35 | expect(typeHashPerson).toMatchInlineSnapshot( |
26 | 36 | `"0x2896dbe4b96a67110f454c01e5336edc5bbc3635537efd690f122f4809cc855"` |
27 | 37 | ); |
28 | | - const typeHashMail = getTypeHash(typedDataExample, 'Mail'); |
| 38 | + const typeHashMail = getTypeHash(typedDataExample.types, 'Mail'); |
29 | 39 | expect(typeHashMail).toMatchInlineSnapshot( |
30 | 40 | `"0x13d89452df9512bf750f539ba3001b945576243288137ddb6c788457d4b2f79"` |
31 | 41 | ); |
32 | | - const typeHashPost = getTypeHash(typedDataStructArrayExample, 'Post'); |
| 42 | + const typeHashPost = getTypeHash(typedDataStructArrayExample.types, 'Post'); |
33 | 43 | expect(typeHashPost).toMatchInlineSnapshot( |
34 | 44 | `"0x1d71e69bf476486b43cdcfaf5a85c00bb2d954c042b281040e513080388356d"` |
35 | 45 | ); |
36 | | - const typeHashMailWithStructArray = getTypeHash(typedDataStructArrayExample, 'Mail'); |
| 46 | + const typeHashMailWithStructArray = getTypeHash(typedDataStructArrayExample.types, 'Mail'); |
37 | 47 | expect(typeHashMailWithStructArray).toMatchInlineSnapshot( |
38 | 48 | `"0x873b878e35e258fc99e3085d5aaad3a81a0c821f189c08b30def2cde55ff27"` |
39 | 49 | ); |
| 50 | + const selectorTypeHash = getTypeHash({}, 'selector'); |
| 51 | + expect(selectorTypeHash).toMatchInlineSnapshot( |
| 52 | + `"0x1d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"` |
| 53 | + ); |
| 54 | + }); |
| 55 | + |
| 56 | + test('should transform type selector', () => { |
| 57 | + const selector = 'transfer'; |
| 58 | + const selectorHash = getSelectorFromName(selector); |
| 59 | + const rawSelectorValueHash = encodeValue({}, 'raw', selectorHash); |
| 60 | + const selectorValueHash = encodeValue({}, 'selector', selector); |
| 61 | + expect(selectorValueHash).toEqual(rawSelectorValueHash); |
| 62 | + expect(selectorValueHash).toMatchInlineSnapshot(` |
| 63 | + Array [ |
| 64 | + "felt", |
| 65 | + "0x83afd3f4caedc6eebf44246fe54e38c95e3179a5ec9ea81740eca5b482d12e", |
| 66 | + ] |
| 67 | + `); |
| 68 | + }); |
| 69 | + |
| 70 | + test('should transform merkle tree', () => { |
| 71 | + const tree = new MerkleTree(['0x1', '0x2', '0x3']); |
| 72 | + const [, merkleTreeHash] = encodeValue({}, 'merkletree', tree.leaves); |
| 73 | + expect(merkleTreeHash).toBe(tree.root); |
| 74 | + expect(merkleTreeHash).toMatchInlineSnapshot( |
| 75 | + `"0x551b4adb6c35d49c686a00b9192da9332b18c9b262507cad0ece37f3b6918d2"` |
| 76 | + ); |
| 77 | + }); |
| 78 | + |
| 79 | + test('should transform merkle tree with custom types', () => { |
| 80 | + const leaves = [ |
| 81 | + { |
| 82 | + contractAddress: '0x1', |
| 83 | + selector: 'transfer', |
| 84 | + }, |
| 85 | + { |
| 86 | + contractAddress: '0x2', |
| 87 | + selector: 'transfer', |
| 88 | + }, |
| 89 | + { |
| 90 | + contractAddress: '0x3', |
| 91 | + selector: 'transfer', |
| 92 | + }, |
| 93 | + ]; |
| 94 | + const hashedLeaves = leaves.map( |
| 95 | + (leaf) => |
| 96 | + encodeValue( |
| 97 | + { |
| 98 | + Policy: [ |
| 99 | + { name: 'contractAddress', type: 'felt' }, |
| 100 | + { name: 'selector', type: 'selector' }, |
| 101 | + ], |
| 102 | + }, |
| 103 | + 'Policy', |
| 104 | + leaf |
| 105 | + )[1] |
| 106 | + ); |
| 107 | + const tree = new MerkleTree(hashedLeaves); |
| 108 | + const [, merkleTreeHash] = encodeValue( |
| 109 | + { |
| 110 | + Parent: [{ name: 'root', type: 'merkletree', contains: 'Policy' }], |
| 111 | + Policy: [ |
| 112 | + { name: 'contractAddress', type: 'felt' }, |
| 113 | + { name: 'selector', type: 'selector' }, |
| 114 | + ], |
| 115 | + }, |
| 116 | + 'merkletree', |
| 117 | + leaves, |
| 118 | + { key: 'root', parent: 'Parent' } |
| 119 | + ); |
| 120 | + expect(merkleTreeHash).toBe(tree.root); |
| 121 | + expect(merkleTreeHash).toMatchInlineSnapshot( |
| 122 | + `"0x75c4f467f4527a5348f3e302007228a6b0057fc4c015f981ffb5b3ace475727"` |
| 123 | + ); |
40 | 124 | }); |
41 | 125 |
|
42 | 126 | test('should get right hash for StarkNetDomain', () => { |
43 | | - const hash = getStructHash(typedDataExample, 'StarkNetDomain', typedDataExample.domain as any); |
| 127 | + const hash = getStructHash( |
| 128 | + typedDataExample.types, |
| 129 | + 'StarkNetDomain', |
| 130 | + typedDataExample.domain as StarkNetDomain |
| 131 | + ); |
44 | 132 | expect(hash).toMatchInlineSnapshot( |
45 | 133 | `"0x54833b121883a3e3aebff48ec08a962f5742e5f7b973469c1f8f4f55d470b07"` |
46 | 134 | ); |
@@ -122,4 +210,14 @@ describe('typedData', () => { |
122 | 210 | `"0x70338fb11b8f70b68b261de8a322bcb004bd85e88ac47d9147982c7f5ac66fd"` |
123 | 211 | ); |
124 | 212 | }); |
| 213 | + |
| 214 | + test('should transform session message correctly', () => { |
| 215 | + const hash = getMessageHash( |
| 216 | + typedDataSessionExample, |
| 217 | + '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826' |
| 218 | + ); |
| 219 | + expect(hash).toMatchInlineSnapshot( |
| 220 | + `"0x1ad0330a62a4a94eae5ea1a7ad96388179d2e4d33e6f909d17421d315110653"` |
| 221 | + ); |
| 222 | + }); |
125 | 223 | }); |
0 commit comments