starknet 2.3.0 → 2.6.0

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.
Files changed (61) hide show
  1. package/CHANGELOG.md +42 -0
  2. package/__tests__/account.test.ts +3 -3
  3. package/__tests__/provider.test.ts +3 -3
  4. package/__tests__/signer.test.ts +17 -0
  5. package/__tests__/utils/ellipticalCurve.test.ts +1 -1
  6. package/__tests__/utils/typedData.test.ts +72 -0
  7. package/contract.d.ts +9 -3
  8. package/contract.js +23 -5
  9. package/dist/contract.d.ts +6 -3
  10. package/dist/contract.js +19 -5
  11. package/dist/index.d.ts +1 -0
  12. package/dist/index.js +2 -1
  13. package/dist/provider/default.d.ts +17 -17
  14. package/dist/provider/default.js +37 -32
  15. package/dist/provider/interface.d.ts +16 -16
  16. package/dist/signer/default.d.ts +20 -3
  17. package/dist/signer/default.js +61 -20
  18. package/dist/signer/interface.d.ts +22 -3
  19. package/dist/types.d.ts +10 -7
  20. package/dist/utils/ellipticCurve.d.ts +8 -1
  21. package/dist/utils/ellipticCurve.js +48 -9
  22. package/dist/utils/stark.d.ts +2 -6
  23. package/dist/utils/stark.js +1 -5
  24. package/dist/utils/typedData/index.d.ts +91 -0
  25. package/dist/utils/typedData/index.js +183 -0
  26. package/dist/utils/typedData/types.d.ts +82 -0
  27. package/dist/utils/typedData/types.js +47 -0
  28. package/dist/utils/typedData/utils.d.ts +24 -0
  29. package/dist/utils/typedData/utils.js +15 -0
  30. package/index.d.ts +1 -0
  31. package/index.js +3 -1
  32. package/package.json +3 -1
  33. package/provider/default.d.ts +21 -16
  34. package/provider/default.js +50 -39
  35. package/provider/interface.d.ts +22 -16
  36. package/signer/default.d.ts +20 -3
  37. package/signer/default.js +60 -17
  38. package/signer/interface.d.ts +22 -3
  39. package/src/contract.ts +17 -14
  40. package/src/index.ts +1 -0
  41. package/src/provider/default.ts +37 -31
  42. package/src/provider/interface.ts +21 -16
  43. package/src/signer/default.ts +49 -17
  44. package/src/signer/interface.ts +26 -3
  45. package/src/types.ts +16 -7
  46. package/src/utils/ellipticCurve.ts +31 -9
  47. package/src/utils/stark.ts +4 -8
  48. package/src/utils/typedData/index.ts +176 -0
  49. package/src/utils/typedData/types.ts +82 -0
  50. package/src/utils/typedData/utils.ts +13 -0
  51. package/types.d.ts +12 -8
  52. package/utils/ellipticCurve.d.ts +12 -1
  53. package/utils/ellipticCurve.js +72 -23
  54. package/utils/stark.d.ts +2 -8
  55. package/utils/stark.js +1 -6
  56. package/utils/typedData/index.d.ts +113 -0
  57. package/utils/typedData/index.js +247 -0
  58. package/utils/typedData/types.d.ts +103 -0
  59. package/utils/typedData/types.js +57 -0
  60. package/utils/typedData/utils.d.ts +27 -0
  61. package/utils/typedData/utils.js +15 -0
@@ -0,0 +1,247 @@
1
+ 'use strict';
2
+ var __createBinding =
3
+ (this && this.__createBinding) ||
4
+ (Object.create
5
+ ? function (o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ Object.defineProperty(o, k2, {
8
+ enumerable: true,
9
+ get: function () {
10
+ return m[k];
11
+ },
12
+ });
13
+ }
14
+ : function (o, m, k, k2) {
15
+ if (k2 === undefined) k2 = k;
16
+ o[k2] = m[k];
17
+ });
18
+ var __exportStar =
19
+ (this && this.__exportStar) ||
20
+ function (m, exports) {
21
+ for (var p in m)
22
+ if (p !== 'default' && !Object.prototype.hasOwnProperty.call(exports, p))
23
+ __createBinding(exports, m, p);
24
+ };
25
+ var __read =
26
+ (this && this.__read) ||
27
+ function (o, n) {
28
+ var m = typeof Symbol === 'function' && o[Symbol.iterator];
29
+ if (!m) return o;
30
+ var i = m.call(o),
31
+ r,
32
+ ar = [],
33
+ e;
34
+ try {
35
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
36
+ } catch (error) {
37
+ e = { error: error };
38
+ } finally {
39
+ try {
40
+ if (r && !r.done && (m = i['return'])) m.call(i);
41
+ } finally {
42
+ if (e) throw e.error;
43
+ }
44
+ }
45
+ return ar;
46
+ };
47
+ var __spreadArray =
48
+ (this && this.__spreadArray) ||
49
+ function (to, from, pack) {
50
+ if (pack || arguments.length === 2)
51
+ for (var i = 0, l = from.length, ar; i < l; i++) {
52
+ if (ar || !(i in from)) {
53
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
54
+ ar[i] = from[i];
55
+ }
56
+ }
57
+ return to.concat(ar || Array.prototype.slice.call(from));
58
+ };
59
+ Object.defineProperty(exports, '__esModule', { value: true });
60
+ exports.getMessageHash =
61
+ exports.getStructHash =
62
+ exports.encodeData =
63
+ exports.getTypeHash =
64
+ exports.encodeType =
65
+ exports.getDependencies =
66
+ void 0;
67
+ var hash_1 = require('../hash');
68
+ var number_1 = require('../number');
69
+ var shortString_1 = require('../shortString');
70
+ var stark_1 = require('../stark');
71
+ var utils_1 = require('./utils');
72
+ __exportStar(require('./types'), exports);
73
+ function getHex(value) {
74
+ try {
75
+ return (0, number_1.toHex)((0, number_1.toBN)(value));
76
+ } catch (e) {
77
+ if (typeof value === 'string') {
78
+ return (0, number_1.toHex)((0, number_1.toBN)((0, shortString_1.encodeShortString)(value)));
79
+ }
80
+ throw new Error('Invalid BigNumberish: ' + value);
81
+ }
82
+ }
83
+ /**
84
+ * Get the dependencies of a struct type. If a struct has the same dependency multiple times, it's only included once
85
+ * in the resulting array.
86
+ *
87
+ * @param {TypedData} typedData
88
+ * @param {string} type
89
+ * @param {string[]} [dependencies]
90
+ * @return {string[]}
91
+ */
92
+ var getDependencies = function (typedData, type, dependencies) {
93
+ if (dependencies === void 0) {
94
+ dependencies = [];
95
+ }
96
+ // `getDependencies` is called by most other functions, so we validate the JSON schema here
97
+ if (!(0, utils_1.validateTypedData)(typedData)) {
98
+ throw new Error('Typed data does not match JSON schema');
99
+ }
100
+ if (dependencies.includes(type)) {
101
+ return dependencies;
102
+ }
103
+ if (!typedData.types[type]) {
104
+ return dependencies;
105
+ }
106
+ return __spreadArray(
107
+ [type],
108
+ __read(
109
+ typedData.types[type].reduce(function (previous, t) {
110
+ return __spreadArray(
111
+ __spreadArray([], __read(previous), false),
112
+ __read(
113
+ (0, exports.getDependencies)(typedData, t.type, previous).filter(function (dependency) {
114
+ return !previous.includes(dependency);
115
+ })
116
+ ),
117
+ false
118
+ );
119
+ }, [])
120
+ ),
121
+ false
122
+ );
123
+ };
124
+ exports.getDependencies = getDependencies;
125
+ /**
126
+ * Encode a type to a string. All dependant types are alphabetically sorted.
127
+ *
128
+ * @param {TypedData} typedData
129
+ * @param {string} type
130
+ * @return {string}
131
+ */
132
+ var encodeType = function (typedData, type) {
133
+ var _a = __read((0, exports.getDependencies)(typedData, type)),
134
+ primary = _a[0],
135
+ dependencies = _a.slice(1);
136
+ var types = __spreadArray([primary], __read(dependencies.sort()), false);
137
+ return types
138
+ .map(function (dependency) {
139
+ return (
140
+ dependency +
141
+ '(' +
142
+ typedData.types[dependency].map(function (t) {
143
+ return t.name + ':' + t.type;
144
+ }) +
145
+ ')'
146
+ );
147
+ })
148
+ .join('');
149
+ };
150
+ exports.encodeType = encodeType;
151
+ /**
152
+ * Get a type string as hash.
153
+ *
154
+ * @param {TypedData} typedData
155
+ * @param {string} type
156
+ * @return {string}
157
+ */
158
+ var getTypeHash = function (typedData, type) {
159
+ return (0, stark_1.getSelectorFromName)((0, exports.encodeType)(typedData, type));
160
+ };
161
+ exports.getTypeHash = getTypeHash;
162
+ /**
163
+ * Encodes a single value to an ABI serialisable string, number or Buffer. Returns the data as tuple, which consists of
164
+ * an array of ABI compatible types, and an array of corresponding values.
165
+ *
166
+ * @param {TypedData} typedData
167
+ * @param {string} type
168
+ * @param {any} data
169
+ * @returns {[string, string]}
170
+ */
171
+ var encodeValue = function (typedData, type, data) {
172
+ if (typedData.types[type]) {
173
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
174
+ return [type, (0, exports.getStructHash)(typedData, type, data)];
175
+ }
176
+ if (type === 'felt*') {
177
+ return ['felt*', (0, hash_1.computeHashOnElements)(data)];
178
+ }
179
+ return [type, getHex(data)];
180
+ };
181
+ /**
182
+ * Encode the data to an ABI encoded Buffer. The data should be a key -> value object with all the required values. All
183
+ * dependant types are automatically encoded.
184
+ *
185
+ * @param {TypedData} typedData
186
+ * @param {string} type
187
+ * @param {Record<string, any>} data
188
+ */
189
+ var encodeData = function (typedData, type, data) {
190
+ var _a = __read(
191
+ typedData.types[type].reduce(
192
+ function (_a, field) {
193
+ var _b = __read(_a, 2),
194
+ ts = _b[0],
195
+ vs = _b[1];
196
+ if (data[field.name] === undefined || data[field.name] === null) {
197
+ throw new Error("Cannot encode data: missing data for '" + field.name + "'");
198
+ }
199
+ var value = data[field.name];
200
+ var _c = __read(encodeValue(typedData, field.type, value), 2),
201
+ t = _c[0],
202
+ encodedValue = _c[1];
203
+ return [
204
+ __spreadArray(__spreadArray([], __read(ts), false), [t], false),
205
+ __spreadArray(__spreadArray([], __read(vs), false), [encodedValue], false),
206
+ ];
207
+ },
208
+ [['felt'], [(0, exports.getTypeHash)(typedData, type)]]
209
+ ),
210
+ 2
211
+ ),
212
+ types = _a[0],
213
+ values = _a[1];
214
+ return [types, values];
215
+ };
216
+ exports.encodeData = encodeData;
217
+ /**
218
+ * Get encoded data as a hash. The data should be a key -> value object with all the required values. All dependant
219
+ * types are automatically encoded.
220
+ *
221
+ * @param {TypedData} typedData
222
+ * @param {string} type
223
+ * @param {Record<string, any>} data
224
+ * @return {Buffer}
225
+ */
226
+ var getStructHash = function (typedData, type, data) {
227
+ return (0, hash_1.computeHashOnElements)((0, exports.encodeData)(typedData, type, data)[1]);
228
+ };
229
+ exports.getStructHash = getStructHash;
230
+ /**
231
+ * Get the EIP-191 encoded message to sign, from the typedData object. If `hash` is enabled, the message will be hashed
232
+ * with Keccak256.
233
+ *
234
+ * @param {TypedData} typedData
235
+ * @param {boolean} hash
236
+ * @return {string}
237
+ */
238
+ var getMessageHash = function (typedData, account) {
239
+ var message = [
240
+ (0, shortString_1.encodeShortString)('StarkNet Message'),
241
+ (0, exports.getStructHash)(typedData, 'StarkNetDomain', typedData.domain),
242
+ account,
243
+ (0, exports.getStructHash)(typedData, typedData.primaryType, typedData.message),
244
+ ];
245
+ return (0, hash_1.computeHashOnElements)(message);
246
+ };
247
+ exports.getMessageHash = getMessageHash;
@@ -0,0 +1,103 @@
1
+ import { Infer } from 'superstruct';
2
+ export declare const ATOMIC_TYPES: string[];
3
+ /**
4
+ * Checks if a type is valid with the given `typedData`. The following types are valid:
5
+ * - Atomic types: felt, felt*
6
+ * - Reference types: struct type (e.g. SomeStruct)
7
+ *
8
+ * @param {Record<string, unknown>} types
9
+ * @param {string} type
10
+ * @return {boolean}
11
+ */
12
+ export declare const isValidType: (types: Record<string, unknown>, type: string) => boolean;
13
+ export declare const STARKNET_TYPE: import('superstruct').Struct<
14
+ {
15
+ type: string;
16
+ name: string;
17
+ },
18
+ {
19
+ name: import('superstruct').Struct<string, null>;
20
+ type: import('superstruct').Struct<string, null>;
21
+ }
22
+ >;
23
+ /**
24
+ * A single type, as part of a struct. The `type` field can be any of the EIP-712 supported types.
25
+ *
26
+ * Note that the `uint` and `int` aliases like in Solidity, and fixed point numbers are not supported by the EIP-712
27
+ * standard.
28
+ */
29
+ export declare type StarkNetType = Infer<typeof STARKNET_TYPE>;
30
+ export declare const STARKNET_DOMAIN_TYPE: import('superstruct').Struct<
31
+ {
32
+ name?: string | undefined;
33
+ version?: string | undefined;
34
+ chainId?: string | number | undefined;
35
+ },
36
+ {
37
+ name: import('superstruct').Struct<string | undefined, null>;
38
+ version: import('superstruct').Struct<string | undefined, null>;
39
+ chainId: import('superstruct').Struct<string | number | undefined, null>;
40
+ }
41
+ >;
42
+ /**
43
+ * The EIP712 domain struct. Any of these fields are optional, but it must contain at least one field.
44
+ */
45
+ export declare type StarkNetDomain = Infer<typeof STARKNET_DOMAIN_TYPE>;
46
+ export declare const STARKNET_TYPED_DATA_TYPE: import('superstruct').Struct<
47
+ {
48
+ types: {
49
+ StarkNetDomain: {
50
+ type: string;
51
+ name: string;
52
+ }[];
53
+ } & Record<
54
+ string,
55
+ {
56
+ type: string;
57
+ name: string;
58
+ }[]
59
+ >;
60
+ primaryType: string;
61
+ domain: {
62
+ name?: string | undefined;
63
+ version?: string | undefined;
64
+ chainId?: string | number | undefined;
65
+ };
66
+ message: Record<string, unknown>;
67
+ },
68
+ {
69
+ types: import('superstruct').Struct<
70
+ {
71
+ StarkNetDomain: {
72
+ type: string;
73
+ name: string;
74
+ }[];
75
+ } & Record<
76
+ string,
77
+ {
78
+ type: string;
79
+ name: string;
80
+ }[]
81
+ >,
82
+ null
83
+ >;
84
+ primaryType: import('superstruct').Struct<string, null>;
85
+ domain: import('superstruct').Struct<
86
+ {
87
+ name?: string | undefined;
88
+ version?: string | undefined;
89
+ chainId?: string | number | undefined;
90
+ },
91
+ {
92
+ name: import('superstruct').Struct<string | undefined, null>;
93
+ version: import('superstruct').Struct<string | undefined, null>;
94
+ chainId: import('superstruct').Struct<string | number | undefined, null>;
95
+ }
96
+ >;
97
+ message: import('superstruct').Struct<Record<string, unknown>, null>;
98
+ }
99
+ >;
100
+ /**
101
+ * The complete typed data, with all the structs, domain data, primary type of the message, and the message itself.
102
+ */
103
+ export declare type TypedData = Infer<typeof STARKNET_TYPED_DATA_TYPE>;
@@ -0,0 +1,57 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, '__esModule', { value: true });
3
+ exports.STARKNET_TYPED_DATA_TYPE =
4
+ exports.STARKNET_DOMAIN_TYPE =
5
+ exports.STARKNET_TYPE =
6
+ exports.isValidType =
7
+ exports.ATOMIC_TYPES =
8
+ void 0;
9
+ var superstruct_1 = require('superstruct');
10
+ exports.ATOMIC_TYPES = ['felt', 'felt*'];
11
+ // Source: https://github.com/Mrtenz/eip-712/blob/master/src/eip-712.ts
12
+ // and modified to support starknet types
13
+ /**
14
+ * Checks if a type is valid with the given `typedData`. The following types are valid:
15
+ * - Atomic types: felt, felt*
16
+ * - Reference types: struct type (e.g. SomeStruct)
17
+ *
18
+ * @param {Record<string, unknown>} types
19
+ * @param {string} type
20
+ * @return {boolean}
21
+ */
22
+ var isValidType = function (types, type) {
23
+ if (exports.ATOMIC_TYPES.includes(type)) {
24
+ return true;
25
+ }
26
+ if (types[type]) {
27
+ return true;
28
+ }
29
+ return false;
30
+ };
31
+ exports.isValidType = isValidType;
32
+ var TYPE = (0, superstruct_1.refine)((0, superstruct_1.string)(), 'Type', function (type, context) {
33
+ return (0, exports.isValidType)(context.branch[0].types, type);
34
+ });
35
+ exports.STARKNET_TYPE = (0, superstruct_1.object)({
36
+ name: (0, superstruct_1.string)(),
37
+ type: TYPE,
38
+ });
39
+ exports.STARKNET_DOMAIN_TYPE = (0, superstruct_1.object)({
40
+ name: (0, superstruct_1.optional)((0, superstruct_1.string)()),
41
+ version: (0, superstruct_1.optional)((0, superstruct_1.string)()),
42
+ chainId: (0, superstruct_1.optional)(
43
+ (0, superstruct_1.union)([(0, superstruct_1.string)(), (0, superstruct_1.number)()])
44
+ ),
45
+ });
46
+ exports.STARKNET_TYPED_DATA_TYPE = (0, superstruct_1.object)({
47
+ types: (0, superstruct_1.intersection)([
48
+ (0, superstruct_1.type)({ StarkNetDomain: (0, superstruct_1.array)(exports.STARKNET_TYPE) }),
49
+ (0, superstruct_1.record)(
50
+ (0, superstruct_1.string)(),
51
+ (0, superstruct_1.array)(exports.STARKNET_TYPE)
52
+ ),
53
+ ]),
54
+ primaryType: (0, superstruct_1.string)(),
55
+ domain: exports.STARKNET_DOMAIN_TYPE,
56
+ message: (0, superstruct_1.object)(),
57
+ });
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Validates that `data` matches the EIP-712 JSON schema.
3
+ *
4
+ * @param {any} data
5
+ * @return {boolean}
6
+ */
7
+ export declare const validateTypedData: (data: unknown) => data is {
8
+ types: {
9
+ StarkNetDomain: {
10
+ type: string;
11
+ name: string;
12
+ }[];
13
+ } & Record<
14
+ string,
15
+ {
16
+ type: string;
17
+ name: string;
18
+ }[]
19
+ >;
20
+ primaryType: string;
21
+ domain: {
22
+ name?: string | undefined;
23
+ version?: string | undefined;
24
+ chainId?: string | number | undefined;
25
+ };
26
+ message: Record<string, unknown>;
27
+ };
@@ -0,0 +1,15 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, '__esModule', { value: true });
3
+ exports.validateTypedData = void 0;
4
+ var superstruct_1 = require('superstruct');
5
+ var types_1 = require('./types');
6
+ /**
7
+ * Validates that `data` matches the EIP-712 JSON schema.
8
+ *
9
+ * @param {any} data
10
+ * @return {boolean}
11
+ */
12
+ var validateTypedData = function (data) {
13
+ return (0, superstruct_1.is)(data, types_1.STARKNET_TYPED_DATA_TYPE);
14
+ };
15
+ exports.validateTypedData = validateTypedData;