starknet 3.0.0 → 3.3.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 (83) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/__mocks__/ArgentAccount.json +68548 -51944
  3. package/__mocks__/TestDapp.json +12962 -0
  4. package/__mocks__/contract.json +33191 -0
  5. package/__mocks__/multicall.json +8139 -0
  6. package/__tests__/account.test.ts +63 -49
  7. package/__tests__/accountContract.test.ts +51 -70
  8. package/__tests__/contract.test.ts +182 -35
  9. package/__tests__/fixtures.ts +13 -0
  10. package/__tests__/provider.test.ts +4 -14
  11. package/__tests__/utils/__snapshots__/utils.browser.test.ts.snap +2 -2
  12. package/__tests__/utils/__snapshots__/utils.test.ts.snap +2 -2
  13. package/__tests__/utils/ellipticalCurve.test.ts +20 -13
  14. package/__tests__/utils/utils.test.ts +3 -3
  15. package/account/default.d.ts +4 -4
  16. package/account/default.js +43 -92
  17. package/account/interface.d.ts +2 -2
  18. package/contract.d.ts +68 -9
  19. package/contract.js +229 -77
  20. package/dist/account/default.d.ts +3 -3
  21. package/dist/account/default.js +31 -57
  22. package/dist/account/interface.d.ts +2 -2
  23. package/dist/contract.d.ts +68 -6
  24. package/dist/contract.js +207 -55
  25. package/dist/index.d.ts +1 -0
  26. package/dist/index.js +1 -0
  27. package/dist/provider/default.d.ts +15 -2
  28. package/dist/provider/default.js +61 -17
  29. package/dist/provider/interface.d.ts +5 -1
  30. package/dist/signer/default.d.ts +1 -1
  31. package/dist/signer/default.js +6 -18
  32. package/dist/signer/interface.d.ts +3 -2
  33. package/dist/types/api.d.ts +12 -0
  34. package/dist/types/lib.d.ts +3 -3
  35. package/dist/utils/ellipticCurve.js +1 -1
  36. package/dist/utils/hash.d.ts +12 -2
  37. package/dist/utils/hash.js +37 -9
  38. package/dist/utils/number.d.ts +1 -0
  39. package/dist/utils/number.js +28 -2
  40. package/dist/utils/stark.d.ts +2 -9
  41. package/dist/utils/stark.js +44 -14
  42. package/dist/utils/transaction.d.ts +19 -0
  43. package/dist/utils/transaction.js +75 -0
  44. package/dist/utils/typedData/index.d.ts +1 -1
  45. package/dist/utils/typedData/index.js +2 -3
  46. package/index.d.ts +1 -0
  47. package/index.js +1 -0
  48. package/package.json +2 -2
  49. package/provider/default.d.ts +20 -1
  50. package/provider/default.js +83 -19
  51. package/provider/interface.d.ts +5 -1
  52. package/signer/default.d.ts +1 -1
  53. package/signer/default.js +10 -44
  54. package/signer/interface.d.ts +3 -2
  55. package/src/account/default.ts +21 -43
  56. package/src/account/interface.ts +2 -2
  57. package/src/contract.ts +232 -62
  58. package/src/index.ts +1 -0
  59. package/src/provider/default.ts +58 -22
  60. package/src/provider/interface.ts +6 -1
  61. package/src/signer/default.ts +10 -26
  62. package/src/signer/interface.ts +3 -2
  63. package/src/types/api.ts +11 -0
  64. package/src/types/lib.ts +3 -4
  65. package/src/utils/ellipticCurve.ts +1 -1
  66. package/src/utils/hash.ts +39 -12
  67. package/src/utils/number.ts +8 -1
  68. package/src/utils/stark.ts +14 -15
  69. package/src/utils/transaction.ts +50 -0
  70. package/src/utils/typedData/index.ts +2 -3
  71. package/types/api.d.ts +15 -0
  72. package/types/lib.d.ts +3 -3
  73. package/utils/ellipticCurve.js +1 -1
  74. package/utils/hash.d.ts +15 -6
  75. package/utils/hash.js +42 -10
  76. package/utils/number.d.ts +1 -0
  77. package/utils/number.js +46 -1
  78. package/utils/stark.d.ts +2 -9
  79. package/utils/stark.js +64 -15
  80. package/utils/transaction.d.ts +19 -0
  81. package/utils/transaction.js +99 -0
  82. package/utils/typedData/index.d.ts +1 -1
  83. package/utils/typedData/index.js +2 -3
@@ -1,6 +1,6 @@
1
1
  import { ec, getKeyPair, getStarkKey, sign, verify } from '../../src/utils/ellipticCurve';
2
2
  import { removeHexPrefix } from '../../src/utils/encode';
3
- import { hashCalldata, hashMessage, pedersen } from '../../src/utils/hash';
3
+ import { computeHashOnElements, hashMulticall, pedersen } from '../../src/utils/hash';
4
4
  import { toBN, toHex } from '../../src/utils/number';
5
5
 
6
6
  test('getKeyPair()', () => {
@@ -17,35 +17,42 @@ test('pedersen()', () => {
17
17
  expect(own).toMatchSnapshot();
18
18
  });
19
19
 
20
- test('hashCalldata()', () => {
20
+ test('computeHashOnElements()', () => {
21
21
  const array = ['1', '2', '3', '4'];
22
- expect(hashCalldata(array)).toBe(
22
+ expect(computeHashOnElements(array)).toBe(
23
23
  '0x66bd4335902683054d08a0572747ea78ebd9e531536fb43125424ca9f902084'
24
24
  );
25
25
  expect(array).toStrictEqual(['1', '2', '3', '4']);
26
26
 
27
- expect(hashCalldata(['1', '2'])).toBe(
27
+ expect(computeHashOnElements(['1', '2'])).toBe(
28
28
  '0x501a3a8e6cd4f5241c639c74052aaa34557aafa84dd4ba983d6443c590ab7df'
29
29
  );
30
30
  });
31
31
 
32
32
  test('hashMessage()', () => {
33
33
  const privateKey = '0x019800ea6a9a73f94aee6a3d2edf018fc770443e90c7ba121e8303ec6b349279';
34
- const hashMsg = hashMessage(
35
- '0x33f45f07e1bd1a51b45fc24ec8c8c9908db9e42191be9e169bfcac0c0d99745',
36
- '5',
37
- '6',
38
- ['1', '2'],
39
- '2'
34
+ const account = '2007067565103695475819120104515800035851923905855118399071773059478896040938';
35
+ const transactions = [
36
+ {
37
+ contractAddress:
38
+ '3290661298119599979891444342541795905081168856323302956721669397616389152866',
39
+ entrypoint: 'set_number',
40
+ calldata: ['47'],
41
+ },
42
+ ];
43
+ const nonce = '3';
44
+ const maxFee = '0';
45
+ const hashMsg = hashMulticall(account, transactions, nonce, maxFee);
46
+ expect(hashMsg).toBe(
47
+ toHex(toBN('1608351043472325350463069815257733118091727529101532499046754244230898025592'))
40
48
  );
41
- expect(hashMsg).toBe('0x7f15c38ea577a26f4f553282fcfe4f1feeb8ecfaad8f221ae41abf8224cbddd');
42
49
  const keyPair = getKeyPair(privateKey);
43
50
  const [r, s] = sign(keyPair, removeHexPrefix(hashMsg));
44
51
  expect(r.toString()).toStrictEqual(
45
- toBN('2458502865976494910213617956670505342647705497324144349552978333078363662855').toString()
52
+ toBN('1079537730825246752292590270213864261175133133352510235773017189606850691611').toString()
46
53
  );
47
54
  expect(s.toString()).toStrictEqual(
48
- toBN('3439514492576562277095748549117516048613512930236865921315982886313695689433').toString()
55
+ toBN('2904560423220491364719171767721067837294296476624248675613584621502231297000').toString()
49
56
  );
50
57
  });
51
58
 
@@ -44,17 +44,17 @@ describe('makeAddress()', () => {
44
44
  });
45
45
  describe('getSelectorFromName()', () => {
46
46
  test('hash works for value="test"', () => {
47
- expect(stark.getSelectorFromName('test')).toBe(
47
+ expect(hash.getSelectorFromName('test')).toBe(
48
48
  '0x22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658'
49
49
  );
50
50
  });
51
51
  test('hash works for value="initialize"', () => {
52
- expect(stark.getSelectorFromName('initialize')).toBe(
52
+ expect(hash.getSelectorFromName('initialize')).toBe(
53
53
  '0x79dc0da7c54b95f10aa182ad0a46400db63156920adb65eca2654c0945a463'
54
54
  );
55
55
  });
56
56
  test('hash works for value="mint"', () => {
57
- expect(stark.getSelectorFromName('mint')).toBe(
57
+ expect(hash.getSelectorFromName('mint')).toBe(
58
58
  '0x2f0b3c5710379609eb5495f1ecd348cb28167711b73609fe565a72734550354'
59
59
  );
60
60
  });
@@ -2,7 +2,7 @@ import { Provider } from '../provider';
2
2
  import {
3
3
  Abi,
4
4
  AddTransactionResponse,
5
- ExecuteInvocation,
5
+ Call,
6
6
  InvocationsDetails,
7
7
  KeyPair,
8
8
  Signature,
@@ -24,8 +24,8 @@ export declare class Account extends Provider implements AccountInterface {
24
24
  * @returns a confirmation of invoking a function on the starknet contract
25
25
  */
26
26
  execute(
27
- transactions: ExecuteInvocation | ExecuteInvocation[],
28
- abis?: Abi[],
27
+ calls: Call | Call[],
28
+ abis?: Abi[] | undefined,
29
29
  transactionsDetail?: InvocationsDetails
30
30
  ): Promise<AddTransactionResponse>;
31
31
  /**
@@ -47,7 +47,7 @@ export declare class Account extends Provider implements AccountInterface {
47
47
  /**
48
48
  * Verify a signature of a JSON object
49
49
  *
50
- * @param json - JSON object to be verified
50
+ * @param hash - JSON object to be verified
51
51
  * @param signature - signature of the JSON object
52
52
  * @returns true if the signature is valid, false otherwise
53
53
  * @throws {Error} if the JSON object is not a valid JSON or the signature is not a valid signature
@@ -24,20 +24,6 @@ var __extends =
24
24
  d.prototype = b === null ? Object.create(b) : ((__.prototype = b.prototype), new __());
25
25
  };
26
26
  })();
27
- var __assign =
28
- (this && this.__assign) ||
29
- function () {
30
- __assign =
31
- Object.assign ||
32
- function (t) {
33
- for (var s, i = 1, n = arguments.length; i < n; i++) {
34
- s = arguments[i];
35
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
36
- }
37
- return t;
38
- };
39
- return __assign.apply(this, arguments);
40
- };
41
27
  var __awaiter =
42
28
  (this && this.__awaiter) ||
43
29
  function (thisArg, _arguments, P, generator) {
@@ -169,19 +155,6 @@ var __generator =
169
155
  return { value: op[0] ? op[1] : void 0, done: true };
170
156
  }
171
157
  };
172
- var __rest =
173
- (this && this.__rest) ||
174
- function (s, e) {
175
- var t = {};
176
- for (var p in s)
177
- if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
178
- if (s != null && typeof Object.getOwnPropertySymbols === 'function')
179
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
180
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
181
- t[p[i]] = s[p[i]];
182
- }
183
- return t;
184
- };
185
158
  var __read =
186
159
  (this && this.__read) ||
187
160
  function (o, n) {
@@ -218,11 +191,12 @@ var __spreadArray =
218
191
  };
219
192
  Object.defineProperty(exports, '__esModule', { value: true });
220
193
  exports.Account = void 0;
221
- var contract_1 = require('../contract');
222
194
  var provider_1 = require('../provider');
223
195
  var signer_1 = require('../signer');
196
+ var hash_1 = require('../utils/hash');
224
197
  var number_1 = require('../utils/number');
225
198
  var stark_1 = require('../utils/stark');
199
+ var transaction_1 = require('../utils/transaction');
226
200
  var typedData_1 = require('../utils/typedData');
227
201
  var Account = /** @class */ (function (_super) {
228
202
  __extends(Account, _super);
@@ -260,84 +234,61 @@ var Account = /** @class */ (function (_super) {
260
234
  * @param transaction - transaction to be invoked
261
235
  * @returns a confirmation of invoking a function on the starknet contract
262
236
  */
263
- Account.prototype.execute = function (transactions, abis, transactionsDetail) {
237
+ Account.prototype.execute = function (calls, abis, transactionsDetail) {
238
+ var _a, _b;
264
239
  if (abis === void 0) {
265
- abis = [];
240
+ abis = undefined;
266
241
  }
267
242
  if (transactionsDetail === void 0) {
268
243
  transactionsDetail = {};
269
244
  }
270
245
  return __awaiter(this, void 0, void 0, function () {
271
- var _a,
272
- contractAddress,
273
- _b,
274
- calldata,
275
- entrypoint,
276
- invocation,
277
- nonce,
278
- nonceBn,
279
- _c,
280
- _d,
281
- calldataDecimal,
282
- signature,
283
- entrypointSelector;
284
- return __generator(this, function (_e) {
285
- switch (_e.label) {
246
+ var transactions, signerDetails, _c, _d, signature, calldata;
247
+ var _e;
248
+ return __generator(this, function (_f) {
249
+ switch (_f.label) {
286
250
  case 0:
287
- if (Array.isArray(transactions) && transactions.length !== 1) {
288
- throw new Error('Only one transaction at a time is currently supported');
289
- }
290
- (_a = Array.isArray(transactions) ? transactions[0] : transactions),
291
- (contractAddress = _a.contractAddress),
292
- (_b = _a.calldata),
293
- (calldata = _b === void 0 ? [] : _b),
294
- (entrypoint = _a.entrypoint),
295
- (invocation = __rest(_a, ['contractAddress', 'calldata', 'entrypoint']));
296
- nonce = transactionsDetail.nonce;
251
+ transactions = Array.isArray(calls) ? calls : [calls];
252
+ _e = {
253
+ walletAddress: this.address,
254
+ };
297
255
  _c = number_1.toBN;
298
- if (!(nonce !== null && nonce !== void 0)) return [3 /*break*/, 1];
299
- _d = nonce;
256
+ if (!((_a = transactionsDetail.nonce) !== null && _a !== void 0))
257
+ return [3 /*break*/, 1];
258
+ _d = _a;
300
259
  return [3 /*break*/, 3];
301
260
  case 1:
302
261
  return [4 /*yield*/, this.getNonce()];
303
262
  case 2:
304
- _d = _e.sent();
305
- _e.label = 3;
263
+ _d = _f.sent();
264
+ _f.label = 3;
306
265
  case 3:
307
- nonceBn = _c.apply(void 0, [_d]);
308
- calldataDecimal = (0, number_1.bigNumberishArrayToDecimalStringArray)(calldata);
309
- return [
310
- 4 /*yield*/,
311
- this.signer.signTransaction(
312
- [
313
- __assign(__assign({}, invocation), {
314
- contractAddress: contractAddress,
315
- calldata: calldataDecimal,
316
- entrypoint: entrypoint,
317
- }),
318
- ],
319
- { walletAddress: this.address, nonce: nonceBn },
320
- abis
321
- ),
322
- ];
266
+ signerDetails =
267
+ ((_e.nonce = _c.apply(void 0, [_d])),
268
+ (_e.maxFee = (0, number_1.toBN)(
269
+ (_b = transactionsDetail.maxFee) !== null && _b !== void 0 ? _b : '0'
270
+ )),
271
+ _e);
272
+ return [4 /*yield*/, this.signer.signTransaction(transactions, signerDetails, abis)];
323
273
  case 4:
324
- signature = _e.sent();
325
- entrypointSelector = (0, stark_1.getSelectorFromName)(entrypoint);
274
+ signature = _f.sent();
275
+ calldata = __spreadArray(
276
+ __spreadArray(
277
+ [],
278
+ __read((0, transaction_1.fromCallsToExecuteCalldata)(transactions)),
279
+ false
280
+ ),
281
+ [signerDetails.nonce.toString()],
282
+ false
283
+ );
326
284
  return [
327
285
  2 /*return*/,
328
- _super.prototype.invokeFunction.call(this, {
329
- contractAddress: this.address,
330
- entrypoint: 'execute',
331
- calldata: __spreadArray(
332
- __spreadArray(
333
- [contractAddress, entrypointSelector, calldataDecimal.length.toString()],
334
- __read(calldataDecimal),
335
- false
336
- ),
337
- [nonceBn.toString()],
338
- false
339
- ),
340
- signature: signature,
286
+ this.fetchEndpoint('add_transaction', undefined, {
287
+ type: 'INVOKE_FUNCTION',
288
+ contract_address: this.address,
289
+ entry_point_selector: (0, hash_1.getSelectorFromName)('__execute__'),
290
+ calldata: calldata,
291
+ signature: (0, number_1.bigNumberishArrayToDecimalStringArray)(signature),
341
292
  }),
342
293
  ];
343
294
  }
@@ -375,7 +326,7 @@ var Account = /** @class */ (function (_super) {
375
326
  /**
376
327
  * Verify a signature of a JSON object
377
328
  *
378
- * @param json - JSON object to be verified
329
+ * @param hash - JSON object to be verified
379
330
  * @param signature - signature of the JSON object
380
331
  * @returns true if the signature is valid, false otherwise
381
332
  * @throws {Error} if the JSON object is not a valid JSON or the signature is not a valid signature
@@ -392,7 +343,7 @@ var Account = /** @class */ (function (_super) {
392
343
  this.callContract({
393
344
  contractAddress: this.address,
394
345
  entrypoint: 'is_valid_signature',
395
- calldata: (0, contract_1.compileCalldata)({
346
+ calldata: (0, stark_1.compileCalldata)({
396
347
  hash: (0, number_1.toBN)(hash).toString(),
397
348
  signature: signature.map(function (x) {
398
349
  return (0, number_1.toBN)(x).toString();
@@ -2,8 +2,8 @@ import { ProviderInterface } from '../provider';
2
2
  import {
3
3
  Abi,
4
4
  AddTransactionResponse,
5
+ Call,
5
6
  DeployContractPayload,
6
- ExecuteInvocation,
7
7
  InvocationsDetails,
8
8
  Signature,
9
9
  } from '../types';
@@ -38,7 +38,7 @@ export declare abstract class AccountInterface extends ProviderInterface {
38
38
  * @returns response from addTransaction
39
39
  */
40
40
  abstract execute(
41
- transactions: ExecuteInvocation | ExecuteInvocation[],
41
+ transactions: Call | Call[],
42
42
  abis?: Abi[],
43
43
  transactionsDetail?: InvocationsDetails
44
44
  ): Promise<AddTransactionResponse>;
package/contract.d.ts CHANGED
@@ -1,17 +1,17 @@
1
1
  import { Provider } from './provider';
2
2
  import { BlockIdentifier } from './provider/utils';
3
- import { Abi, RawCalldata, Signature, StructAbi } from './types';
3
+ import { Abi, Signature, StructAbi } from './types';
4
4
  import { BigNumberish } from './utils/number';
5
+ export declare type Struct = {
6
+ type: 'struct';
7
+ [k: string]: BigNumberish;
8
+ };
9
+ export declare type ParsedStruct = {
10
+ [key: string]: BigNumberish | ParsedStruct;
11
+ };
5
12
  export declare type Args = {
6
- [inputName: string]:
7
- | string
8
- | string[]
9
- | {
10
- type: 'struct';
11
- [k: string]: BigNumberish;
12
- };
13
+ [inputName: string]: BigNumberish | BigNumberish[] | ParsedStruct | ParsedStruct[];
13
14
  };
14
- export declare function compileCalldata(args: Args): RawCalldata;
15
15
  export declare class Contract {
16
16
  connectedTo: string | null;
17
17
  abi: Abi;
@@ -26,9 +26,68 @@ export declare class Contract {
26
26
  * @param address (optional) - address to connect to
27
27
  */
28
28
  constructor(abi: Abi, address?: string | null, provider?: Provider);
29
+ /**
30
+ * Saves the address of the contract deployed on network that will be used for interaction
31
+ *
32
+ * @param address - address of the contract
33
+ * @returns Contract
34
+ */
29
35
  connect(address: string): Contract;
36
+ /**
37
+ * Validates if all arguments that are passed to the method are corresponding to the ones in the abi
38
+ *
39
+ * @param type - type of the method
40
+ * @param method - name of the method
41
+ * @param args - arguments that are passed to the method
42
+ */
30
43
  private validateMethodAndArgs;
44
+ /**
45
+ * Deep parse of the object that has been passed to the method
46
+ *
47
+ * @param element - element that needs to be parsed
48
+ * @param type - name of the method
49
+ * @return {string | string[]} - parsed arguments in format that contract is expecting
50
+ */
51
+ private parseCalldataObject;
52
+ /**
53
+ * Parse of the response elements that are converted to Object (Struct) by using the abi
54
+ *
55
+ * @param responseIterator - iterator of the response
56
+ * @param type - type of the struct
57
+ * @return {BigNumberish | ParsedStruct} - parsed arguments in format that contract is expecting
58
+ */
59
+ private parseResponseStruct;
60
+ /**
61
+ * Parse one field of the calldata by using input field from the abi for that method
62
+ *
63
+ * @param args - value of the field
64
+ * @param input - input(field) information from the abi that will be used to parse the data
65
+ * @return {string | string[]} - parsed arguments in format that contract is expecting
66
+ */
67
+ private parsCalldataField;
68
+ /**
69
+ * Parse the calldata by using input fields from the abi for that method
70
+ *
71
+ * @param args - arguments passed the the method
72
+ * @param inputs - list of inputs(fields) that are in the abi
73
+ * @return {Calldata} - parsed arguments in format that contract is expecting
74
+ */
75
+ private compileCalldata;
76
+ /**
77
+ * Parse elements of the response and structuring them into one field by using output property from the abi for that method
78
+ *
79
+ * @param responseIterator - iterator of the response
80
+ * @param output - output(field) information from the abi that will be used to parse the data
81
+ * @return - parsed response corresponding to the abi structure of the field
82
+ */
31
83
  private parseResponseField;
84
+ /**
85
+ * Parse elements of the response array and structuring them into response object
86
+ *
87
+ * @param method - method name
88
+ * @param response - response from the method
89
+ * @return - parsed response corresponding to the abi
90
+ */
32
91
  private parseResponse;
33
92
  invoke(
34
93
  method: string,