|
| 1 | +import { encodeType, getMessageHash, getStructHash, getTypeHash } from '../../src/utils/typedData'; |
| 2 | + |
| 3 | +const typedDataExample = { |
| 4 | + types: { |
| 5 | + StarkNetDomain: [ |
| 6 | + { name: 'name', type: 'felt' }, |
| 7 | + { name: 'version', type: 'felt' }, |
| 8 | + { name: 'chainId', type: 'felt' }, |
| 9 | + ], |
| 10 | + Person: [ |
| 11 | + { name: 'name', type: 'felt' }, |
| 12 | + { name: 'wallet', type: 'felt' }, |
| 13 | + ], |
| 14 | + Mail: [ |
| 15 | + { name: 'from', type: 'Person' }, |
| 16 | + { name: 'to', type: 'Person' }, |
| 17 | + { name: 'contents', type: 'felt' }, |
| 18 | + ], |
| 19 | + }, |
| 20 | + primaryType: 'Mail', |
| 21 | + domain: { |
| 22 | + name: 'StarkNet Mail', |
| 23 | + version: '1', |
| 24 | + chainId: 1, |
| 25 | + }, |
| 26 | + message: { |
| 27 | + from: { |
| 28 | + name: 'Cow', |
| 29 | + wallet: '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826', |
| 30 | + }, |
| 31 | + to: { |
| 32 | + name: 'Bob', |
| 33 | + wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB', |
| 34 | + }, |
| 35 | + contents: 'Hello, Bob!', |
| 36 | + }, |
| 37 | +}; |
| 38 | + |
| 39 | +describe('typedData', () => { |
| 40 | + test('should get right type encoding', () => { |
| 41 | + const typeEncoding = encodeType(typedDataExample, 'Mail'); |
| 42 | + expect(typeEncoding).toMatchInlineSnapshot( |
| 43 | + `"Mail(from:Person,to:Person,contents:felt)Person(name:felt,wallet:felt)"` |
| 44 | + ); |
| 45 | + }); |
| 46 | + test('should get right type hash', () => { |
| 47 | + const typeHashDomain = getTypeHash(typedDataExample, 'StarkNetDomain'); |
| 48 | + expect(typeHashDomain).toMatchInlineSnapshot( |
| 49 | + `"0x1bfc207425a47a5dfa1a50a4f5241203f50624ca5fdf5e18755765416b8e288"` |
| 50 | + ); |
| 51 | + const typeHashPerson = getTypeHash(typedDataExample, 'Person'); |
| 52 | + expect(typeHashPerson).toMatchInlineSnapshot( |
| 53 | + `"0x2896dbe4b96a67110f454c01e5336edc5bbc3635537efd690f122f4809cc855"` |
| 54 | + ); |
| 55 | + const typeHashMail = getTypeHash(typedDataExample, 'Mail'); |
| 56 | + expect(typeHashMail).toMatchInlineSnapshot( |
| 57 | + `"0x13d89452df9512bf750f539ba3001b945576243288137ddb6c788457d4b2f79"` |
| 58 | + ); |
| 59 | + }); |
| 60 | + test('should get right hash for StarkNetDomain', () => { |
| 61 | + const hash = getStructHash(typedDataExample, 'StarkNetDomain', typedDataExample.domain as any); |
| 62 | + expect(hash).toMatchInlineSnapshot( |
| 63 | + `"0x54833b121883a3e3aebff48ec08a962f5742e5f7b973469c1f8f4f55d470b07"` |
| 64 | + ); |
| 65 | + }); |
| 66 | + test('should get right hash for entire message', () => { |
| 67 | + const hash = getMessageHash(typedDataExample, '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826'); |
| 68 | + expect(hash).toMatchInlineSnapshot( |
| 69 | + `"0x6fcff244f63e38b9d88b9e3378d44757710d1b244282b435cb472053c8d78d0"` |
| 70 | + ); |
| 71 | + }); |
| 72 | +}); |
0 commit comments