starknet 3.18.0 → 3.18.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## [3.18.1](https://github.com/0xs34n/starknet.js/compare/v3.18.0...v3.18.1) (2022-07-23)
2
+
3
+ ### Bug Fixes
4
+
5
+ - struct array hashing ([e1f82df](https://github.com/0xs34n/starknet.js/commit/e1f82dfd575be4c84b291c33f8169bf367493603))
6
+
1
7
  # [3.18.0](https://github.com/0xs34n/starknet.js/compare/v3.17.0...v3.18.0) (2022-07-23)
2
8
 
3
9
  ### Features
@@ -0,0 +1,44 @@
1
+ {
2
+ "types": {
3
+ "StarkNetDomain": [
4
+ { "name": "name", "type": "felt" },
5
+ { "name": "version", "type": "felt" },
6
+ { "name": "chainId", "type": "felt" }
7
+ ],
8
+ "Person": [
9
+ { "name": "name", "type": "felt" },
10
+ { "name": "wallet", "type": "felt" }
11
+ ],
12
+ "Post": [
13
+ { "name": "title", "type": "felt" },
14
+ { "name": "content", "type": "felt" }
15
+ ],
16
+ "Mail": [
17
+ { "name": "from", "type": "Person" },
18
+ { "name": "to", "type": "Person" },
19
+ { "name": "posts_len", "type": "felt" },
20
+ { "name": "posts", "type": "Post*" }
21
+ ]
22
+ },
23
+ "primaryType": "Mail",
24
+ "domain": {
25
+ "name": "StarkNet Mail",
26
+ "version": "1",
27
+ "chainId": 1
28
+ },
29
+ "message": {
30
+ "from": {
31
+ "name": "Cow",
32
+ "wallet": "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826"
33
+ },
34
+ "to": {
35
+ "name": "Bob",
36
+ "wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB"
37
+ },
38
+ "posts_len": 2,
39
+ "posts": [
40
+ { "title": "Greeting", "content": "Hello, Bob!" },
41
+ { "title": "Farewell", "content": "Goodbye, Bob!" }
42
+ ]
43
+ }
44
+ }
@@ -1,4 +1,5 @@
1
1
  import typedDataExample from '../../__mocks__/typedDataExample.json';
2
+ import typedDataStructArrayExample from '../../__mocks__/typedDataStructArrayExample.json';
2
3
  import { number } from '../../src';
3
4
  import { BigNumberish } from '../../src/utils/number';
4
5
  import { encodeType, getMessageHash, getStructHash, getTypeHash } from '../../src/utils/typedData';
@@ -9,7 +10,12 @@ describe('typedData', () => {
9
10
  expect(typeEncoding).toMatchInlineSnapshot(
10
11
  `"Mail(from:Person,to:Person,contents:felt)Person(name:felt,wallet:felt)"`
11
12
  );
13
+ const typeEncodingStructArr = encodeType(typedDataStructArrayExample, 'Mail');
14
+ expect(typeEncodingStructArr).toMatchInlineSnapshot(
15
+ `"Mail(from:Person,to:Person,posts_len:felt,posts:Post*)Person(name:felt,wallet:felt)Post(title:felt,content:felt)"`
16
+ );
12
17
  });
18
+
13
19
  test('should get right type hash', () => {
14
20
  const typeHashDomain = getTypeHash(typedDataExample, 'StarkNetDomain');
15
21
  expect(typeHashDomain).toMatchInlineSnapshot(
@@ -23,18 +29,36 @@ describe('typedData', () => {
23
29
  expect(typeHashMail).toMatchInlineSnapshot(
24
30
  `"0x13d89452df9512bf750f539ba3001b945576243288137ddb6c788457d4b2f79"`
25
31
  );
32
+ const typeHashPost = getTypeHash(typedDataStructArrayExample, 'Post');
33
+ expect(typeHashPost).toMatchInlineSnapshot(
34
+ `"0x1d71e69bf476486b43cdcfaf5a85c00bb2d954c042b281040e513080388356d"`
35
+ );
36
+ const typeHashMailWithStructArray = getTypeHash(typedDataStructArrayExample, 'Mail');
37
+ expect(typeHashMailWithStructArray).toMatchInlineSnapshot(
38
+ `"0x873b878e35e258fc99e3085d5aaad3a81a0c821f189c08b30def2cde55ff27"`
39
+ );
26
40
  });
41
+
27
42
  test('should get right hash for StarkNetDomain', () => {
28
43
  const hash = getStructHash(typedDataExample, 'StarkNetDomain', typedDataExample.domain as any);
29
44
  expect(hash).toMatchInlineSnapshot(
30
45
  `"0x54833b121883a3e3aebff48ec08a962f5742e5f7b973469c1f8f4f55d470b07"`
31
46
  );
32
47
  });
48
+
33
49
  test('should get right hash for entire message', () => {
34
50
  const hash = getMessageHash(typedDataExample, '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826');
35
51
  expect(hash).toMatchInlineSnapshot(
36
52
  `"0x6fcff244f63e38b9d88b9e3378d44757710d1b244282b435cb472053c8d78d0"`
37
53
  );
54
+
55
+ const hashStructArr = getMessageHash(
56
+ typedDataStructArrayExample,
57
+ '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826'
58
+ );
59
+ expect(hashStructArr).toMatchInlineSnapshot(
60
+ `"0x5914ed2764eca2e6a41eb037feefd3d2e33d9af6225a9e7fe31ac943ff712c"`
61
+ );
38
62
  });
39
63
 
40
64
  interface StringStruct {
@@ -71,6 +71,11 @@ var getDependencies = function (typedData, type, dependencies) {
71
71
  if (!(0, utils_1.validateTypedData)(typedData)) {
72
72
  throw new Error('Typed data does not match JSON schema');
73
73
  }
74
+ // Include pointers (struct arrays)
75
+ if (type[type.length - 1] === '*') {
76
+ // eslint-disable-next-line no-param-reassign
77
+ type = type.slice(0, -1);
78
+ }
74
79
  if (dependencies.includes(type)) {
75
80
  return dependencies;
76
81
  }
@@ -124,6 +129,15 @@ var encodeValue = function (typedData, type, data) {
124
129
  // eslint-disable-next-line @typescript-eslint/no-use-before-define
125
130
  return [type, (0, exports.getStructHash)(typedData, type, data)];
126
131
  }
132
+ if (Object.keys(typedData.types)
133
+ .map(function (x) { return "".concat(x, "*"); })
134
+ .includes(type)) {
135
+ var structHashes = data.map(function (struct) {
136
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
137
+ return (0, exports.getStructHash)(typedData, type.slice(0, -1), struct);
138
+ });
139
+ return [type, (0, hash_1.computeHashOnElements)(structHashes)];
140
+ }
127
141
  if (type === 'felt*') {
128
142
  return ['felt*', (0, hash_1.computeHashOnElements)(data)];
129
143
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starknet",
3
- "version": "3.18.0",
3
+ "version": "3.18.1",
4
4
  "description": "JavaScript library for StarkNet",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -36,6 +36,12 @@ export const getDependencies = (
36
36
  throw new Error('Typed data does not match JSON schema');
37
37
  }
38
38
 
39
+ // Include pointers (struct arrays)
40
+ if (type[type.length - 1] === '*') {
41
+ // eslint-disable-next-line no-param-reassign
42
+ type = type.slice(0, -1);
43
+ }
44
+
39
45
  if (dependencies.includes(type)) {
40
46
  return dependencies;
41
47
  }
@@ -102,6 +108,18 @@ const encodeValue = (typedData: TypedData, type: string, data: unknown): [string
102
108
  return [type, getStructHash(typedData, type, data as Record<string, unknown>)];
103
109
  }
104
110
 
111
+ if (
112
+ Object.keys(typedData.types)
113
+ .map((x) => `${x}*`)
114
+ .includes(type)
115
+ ) {
116
+ const structHashes: string[] = (data as unknown[]).map((struct) => {
117
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
118
+ return getStructHash(typedData, type.slice(0, -1), struct as Record<string, unknown>);
119
+ });
120
+ return [type, computeHashOnElements(structHashes)];
121
+ }
122
+
105
123
  if (type === 'felt*') {
106
124
  return ['felt*', computeHashOnElements(data as string[])];
107
125
  }
@@ -100,6 +100,11 @@ var getDependencies = function (typedData, type, dependencies) {
100
100
  if (!(0, utils_1.validateTypedData)(typedData)) {
101
101
  throw new Error('Typed data does not match JSON schema');
102
102
  }
103
+ // Include pointers (struct arrays)
104
+ if (type[type.length - 1] === '*') {
105
+ // eslint-disable-next-line no-param-reassign
106
+ type = type.slice(0, -1);
107
+ }
103
108
  if (dependencies.includes(type)) {
104
109
  return dependencies;
105
110
  }
@@ -174,6 +179,19 @@ var encodeValue = function (typedData, type, data) {
174
179
  // eslint-disable-next-line @typescript-eslint/no-use-before-define
175
180
  return [type, (0, exports.getStructHash)(typedData, type, data)];
176
181
  }
182
+ if (
183
+ Object.keys(typedData.types)
184
+ .map(function (x) {
185
+ return ''.concat(x, '*');
186
+ })
187
+ .includes(type)
188
+ ) {
189
+ var structHashes = data.map(function (struct) {
190
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
191
+ return (0, exports.getStructHash)(typedData, type.slice(0, -1), struct);
192
+ });
193
+ return [type, (0, hash_1.computeHashOnElements)(structHashes)];
194
+ }
177
195
  if (type === 'felt*') {
178
196
  return ['felt*', (0, hash_1.computeHashOnElements)(data)];
179
197
  }