Skip to content

Commit

Permalink
feat: expand structured data hashing in line with SNIP-12 (#920)
Browse files Browse the repository at this point in the history
* feat: expand structured data hashing in line with SNIP-12

* chore: reduce revision overlap

* chore: allow hash method override for the Merkle tree class

* chore: simplify enum and array handling
  • Loading branch information
penovicp authored Feb 1, 2024
1 parent 418bc11 commit cb20590
Show file tree
Hide file tree
Showing 14 changed files with 637 additions and 224 deletions.
File renamed without changes.
39 changes: 39 additions & 0 deletions __mocks__/typedData/example_baseTypes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"types": {
"StarknetDomain": [
{ "name": "name", "type": "shortstring" },
{ "name": "version", "type": "shortstring" },
{ "name": "chainId", "type": "shortstring" },
{ "name": "revision", "type": "shortstring" }
],
"Example": [
{ "name": "n0", "type": "felt" },
{ "name": "n1", "type": "bool" },
{ "name": "n2", "type": "string" },
{ "name": "n3", "type": "selector" },
{ "name": "n4", "type": "u128" },
{ "name": "n5", "type": "ContractAddress" },
{ "name": "n6", "type": "ClassHash" },
{ "name": "n7", "type": "timestamp" },
{ "name": "n8", "type": "shortstring" }
]
},
"primaryType": "Example",
"domain": {
"name": "StarkNet Mail",
"version": "1",
"chainId": "1",
"revision": "1"
},
"message": {
"n0": "0x3e8",
"n1": true,
"n2": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
"n3": "transfer",
"n4": "0x3e8",
"n5": "0x3e8",
"n6": "0x3e8",
"n7": 1000,
"n8": "transfer"
}
}
28 changes: 28 additions & 0 deletions __mocks__/typedData/example_enum.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"types": {
"StarknetDomain": [
{ "name": "name", "type": "shortstring" },
{ "name": "version", "type": "shortstring" },
{ "name": "chainId", "type": "shortstring" },
{ "name": "revision", "type": "shortstring" }
],
"Example": [{ "name": "someEnum", "type": "enum", "contains": "MyEnum" }],
"MyEnum": [
{ "name": "Variant 1", "type": "()" },
{ "name": "Variant 2", "type": "(u128,u128*)" },
{ "name": "Variant 3", "type": "(u128)" }
]
},
"primaryType": "Example",
"domain": {
"name": "StarkNet Mail",
"version": "1",
"chainId": "1",
"revision": "1"
},
"message": {
"someEnum": {
"Variant 2": [2, [0, 1]]
}
}
}
37 changes: 37 additions & 0 deletions __mocks__/typedData/example_presetTypes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"types": {
"StarknetDomain": [
{ "name": "name", "type": "shortstring" },
{ "name": "version", "type": "shortstring" },
{ "name": "chainId", "type": "shortstring" },
{ "name": "revision", "type": "shortstring" }
],
"Example": [
{ "name": "n0", "type": "TokenAmount" },
{ "name": "n1", "type": "NftId" }
]
},
"primaryType": "Example",
"domain": {
"name": "StarkNet Mail",
"version": "1",
"chainId": "1",
"revision": "1"
},
"message": {
"n0": {
"token_address": "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
"amount": {
"low": "0x3e8",
"high": "0x0"
}
},
"n1": {
"collection_address": "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
"token_id": {
"low": "0x3e8",
"high": "0x0"
}
}
}
}
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion __tests__/account.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import typedDataExample from '../__mocks__/typedDataExample.json';
import typedDataExample from '../__mocks__/typedData/baseExample.json';
import {
Account,
Contract,
Expand Down
Loading

0 comments on commit cb20590

Please sign in to comment.