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
@@ -175,6 +175,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
175
175
  exports.Provider = void 0;
176
176
  var axios_1 = __importDefault(require('axios'));
177
177
  var url_join_1 = __importDefault(require('url-join'));
178
+ var hash_1 = require('../utils/hash');
178
179
  var json_1 = require('../utils/json');
179
180
  var number_1 = require('../utils/number');
180
181
  var stark_1 = require('../utils/stark');
@@ -260,8 +261,9 @@ var Provider = /** @class */ (function () {
260
261
  };
261
262
  // typesafe fetch
262
263
  Provider.prototype.fetchEndpoint = function (endpoint) {
264
+ var _a;
263
265
  // typescript type magiuc to create a nice fitting function interface
264
- var _a = []; // when both query and request are needed, we cant omit anything
266
+ var _b = []; // when both query and request are needed, we cant omit anything
265
267
  for (
266
268
  // typescript type magiuc to create a nice fitting function interface
267
269
  var _i = 1; // when both query and request are needed, we cant omit anything
@@ -271,21 +273,24 @@ var Provider = /** @class */ (function () {
271
273
  _i++ // when both query and request are needed, we cant omit anything
272
274
  ) {
273
275
  // typescript type magiuc to create a nice fitting function interface
274
- _a[_i - 1] = arguments[_i]; // when both query and request are needed, we cant omit anything
276
+ _b[_i - 1] = arguments[_i]; // when both query and request are needed, we cant omit anything
275
277
  }
276
278
  // typescript type magiuc to create a nice fitting function interface
277
- var _b = __read(_a, 2),
278
- query = _b[0],
279
- request = _b[1]; // when both query and request are needed, we cant omit anything
279
+ var _c = __read(_b, 2),
280
+ query = _c[0],
281
+ request = _c[1]; // when both query and request are needed, we cant omit anything
280
282
  return __awaiter(this, void 0, void 0, function () {
281
- var baseUrl, method, queryString, headers, data;
282
- return __generator(this, function (_c) {
283
- switch (_c.label) {
283
+ var baseUrl, method, queryString, headers, data, error_1, data;
284
+ return __generator(this, function (_d) {
285
+ switch (_d.label) {
284
286
  case 0:
285
287
  baseUrl = this.getFetchUrl(endpoint);
286
288
  method = this.getFetchMethod(endpoint);
287
289
  queryString = this.getQueryString(query);
288
290
  headers = this.getHeaders(method);
291
+ _d.label = 1;
292
+ case 1:
293
+ _d.trys.push([1, 3, , 4]);
289
294
  return [
290
295
  4 /*yield*/,
291
296
  axios_1.default.request({
@@ -295,9 +300,22 @@ var Provider = /** @class */ (function () {
295
300
  headers: headers,
296
301
  }),
297
302
  ];
298
- case 1:
299
- data = _c.sent().data;
303
+ case 2:
304
+ data = _d.sent().data;
300
305
  return [2 /*return*/, data];
306
+ case 3:
307
+ error_1 = _d.sent();
308
+ data =
309
+ (_a = error_1 === null || error_1 === void 0 ? void 0 : error_1.response) === null ||
310
+ _a === void 0
311
+ ? void 0
312
+ : _a.data;
313
+ if (data === null || data === void 0 ? void 0 : data.message) {
314
+ throw new Error(data.code + ': ' + data.message);
315
+ }
316
+ throw error_1;
317
+ case 4:
318
+ return [2 /*return*/];
301
319
  }
302
320
  });
303
321
  });
@@ -345,7 +363,7 @@ var Provider = /** @class */ (function () {
345
363
  {
346
364
  signature: [],
347
365
  contract_address: contractAddress,
348
- entry_point_selector: (0, stark_1.getSelectorFromName)(entrypoint),
366
+ entry_point_selector: (0, hash_1.getSelectorFromName)(entrypoint),
349
367
  calldata: calldata,
350
368
  }
351
369
  ),
@@ -532,10 +550,8 @@ var Provider = /** @class */ (function () {
532
550
  * Invokes a function on starknet
533
551
  * @deprecated This method wont be supported as soon as fees are mandatory
534
552
  *
535
- * @param contractAddress - target contract address for invoke
536
- * @param entrypointSelector - target entrypoint selector for
537
- * @param calldata - (optional, default []) calldata
538
- * @param signature - (optional) signature to send along
553
+ * @param invocation
554
+ * @param _abi - (optional) signature to send along
539
555
  * @returns response from addTransaction
540
556
  */
541
557
  Provider.prototype.invokeFunction = function (invocation, _abi) {
@@ -543,7 +559,7 @@ var Provider = /** @class */ (function () {
543
559
  return this.fetchEndpoint('add_transaction', undefined, {
544
560
  type: 'INVOKE_FUNCTION',
545
561
  contract_address: invocation.contractAddress,
546
- entry_point_selector: (0, stark_1.getSelectorFromName)(invocation.entrypoint),
562
+ entry_point_selector: (0, hash_1.getSelectorFromName)(invocation.entrypoint),
547
563
  calldata: (0, number_1.bigNumberishArrayToDecimalStringArray)(
548
564
  (_a = invocation.calldata) !== null && _a !== void 0 ? _a : []
549
565
  ),
@@ -552,12 +568,40 @@ var Provider = /** @class */ (function () {
552
568
  ),
553
569
  });
554
570
  };
555
- Provider.prototype.waitForTx = function (txHash, retryInterval) {
571
+ /**
572
+ * Invokes a function on starknet
573
+ * @deprecated This method wont be supported as soon as fees are mandatory
574
+ *
575
+ * @param contractAddress - target contract address for invoke
576
+ * @param entrypointSelector - target entrypoint selector for
577
+ * @param calldata - (optional, default []) calldata
578
+ * @param signature - (optional) signature to send along
579
+ * @returns response from addTransaction
580
+ */
581
+ Provider.prototype.LEGACY_invokeFunction = function (
582
+ contractAddress,
583
+ entrypointSelector,
584
+ calldata,
585
+ signature
586
+ ) {
587
+ return this.fetchEndpoint('add_transaction', undefined, {
588
+ type: 'INVOKE_FUNCTION',
589
+ contract_address: contractAddress,
590
+ entry_point_selector: entrypointSelector,
591
+ calldata: (0, number_1.bigNumberishArrayToDecimalStringArray)(
592
+ calldata !== null && calldata !== void 0 ? calldata : []
593
+ ),
594
+ signature: (0, number_1.bigNumberishArrayToDecimalStringArray)(
595
+ signature !== null && signature !== void 0 ? signature : []
596
+ ),
597
+ });
598
+ };
599
+ Provider.prototype.waitForTransaction = function (txHash, retryInterval) {
556
600
  if (retryInterval === void 0) {
557
601
  retryInterval = 8000;
558
602
  }
559
603
  return __awaiter(this, void 0, void 0, function () {
560
- var onchain, res, error;
604
+ var onchain, res, message, error;
561
605
  return __generator(this, function (_a) {
562
606
  switch (_a.label) {
563
607
  case 0:
@@ -579,7 +623,14 @@ var Provider = /** @class */ (function () {
579
623
  if (res.tx_status === 'ACCEPTED_ON_L1' || res.tx_status === 'ACCEPTED_ON_L2') {
580
624
  onchain = true;
581
625
  } else if (res.tx_status === 'REJECTED' || res.tx_status === 'NOT_RECEIVED') {
582
- error = Error(res.tx_status);
626
+ message = res.tx_failure_reason
627
+ ? res.tx_status +
628
+ ': ' +
629
+ res.tx_failure_reason.code +
630
+ '\n' +
631
+ res.tx_failure_reason.error_message
632
+ : res.tx_status;
633
+ error = new Error(message);
583
634
  error.response = res;
584
635
  throw error;
585
636
  }
@@ -590,6 +641,19 @@ var Provider = /** @class */ (function () {
590
641
  });
591
642
  });
592
643
  };
644
+ /**
645
+ * @deprecated use `waitForTransaction` instead
646
+ */
647
+ Provider.prototype.waitForTx = function (txHash, retryInterval) {
648
+ if (retryInterval === void 0) {
649
+ retryInterval = 8000;
650
+ }
651
+ return __awaiter(this, void 0, void 0, function () {
652
+ return __generator(this, function (_a) {
653
+ return [2 /*return*/, this.waitForTransaction(txHash, retryInterval)];
654
+ });
655
+ });
656
+ };
593
657
  return Provider;
594
658
  })();
595
659
  exports.Provider = Provider;
@@ -122,5 +122,9 @@ export declare abstract class ProviderInterface {
122
122
  * @returns response from addTransaction
123
123
  */
124
124
  abstract invokeFunction(invocation: Invocation): Promise<AddTransactionResponse>;
125
- abstract waitForTx(txHash: BigNumberish, retryInterval?: number): Promise<void>;
125
+ abstract waitForTransaction(txHash: BigNumberish, retryInterval?: number): Promise<void>;
126
+ /**
127
+ * @deprecated use `waitForTransaction` instead
128
+ */
129
+ abstract waitForTransaction(txHash: BigNumberish, retryInterval?: number): Promise<void>;
126
130
  }
@@ -10,5 +10,5 @@ export declare class Signer implements SignerInterface {
10
10
  transactionsDetail: InvocationsSignerDetails,
11
11
  abis?: Abi[]
12
12
  ): Promise<Signature>;
13
- signMessage(typedData: TypedData, walletAddress: string): Promise<Signature>;
13
+ signMessage(typedData: TypedData, accountAddress: string): Promise<Signature>;
14
14
  }
package/signer/default.js CHANGED
@@ -133,10 +133,7 @@ var __generator =
133
133
  Object.defineProperty(exports, '__esModule', { value: true });
134
134
  exports.Signer = void 0;
135
135
  var ellipticCurve_1 = require('../utils/ellipticCurve');
136
- var encode_1 = require('../utils/encode');
137
136
  var hash_1 = require('../utils/hash');
138
- var number_1 = require('../utils/number');
139
- var stark_1 = require('../utils/stark');
140
137
  var typedData_1 = require('../utils/typedData');
141
138
  var Signer = /** @class */ (function () {
142
139
  function Signer(keyPair) {
@@ -150,58 +147,27 @@ var Signer = /** @class */ (function () {
150
147
  });
151
148
  };
152
149
  Signer.prototype.signTransaction = function (transactions, transactionsDetail, abis) {
153
- if (abis === void 0) {
154
- abis = [];
155
- }
156
150
  return __awaiter(this, void 0, void 0, function () {
157
- var _a,
158
- contractAddress,
159
- entrypoint,
160
- _b,
161
- calldata,
162
- nonce,
163
- walletAddress,
164
- nonceBn,
165
- entrypointSelector,
166
- calldataDecimal,
167
- msgHash;
168
- return __generator(this, function (_c) {
169
- if (transactions.length !== 1) {
170
- throw new Error('Only one transaction at a time is currently supported by this signer');
171
- }
172
- if (
173
- (abis === null || abis === void 0 ? void 0 : abis.length) !== 0 &&
174
- abis.length !== transactions.length
175
- ) {
151
+ var msgHash;
152
+ return __generator(this, function (_a) {
153
+ if (abis && abis.length !== transactions.length) {
176
154
  throw new Error('ABI must be provided for each transaction or no transaction');
177
155
  }
178
- (_a = transactions[0]),
179
- (contractAddress = _a.contractAddress),
180
- (entrypoint = _a.entrypoint),
181
- (_b = _a.calldata),
182
- (calldata = _b === void 0 ? [] : _b);
183
- (nonce = transactionsDetail.nonce), (walletAddress = transactionsDetail.walletAddress);
184
- nonceBn = (0, number_1.toBN)(nonce);
185
- entrypointSelector = (0, stark_1.getSelectorFromName)(entrypoint);
186
- calldataDecimal = (0, number_1.bigNumberishArrayToDecimalStringArray)(calldata);
187
- msgHash = (0, encode_1.addHexPrefix)(
188
- (0, hash_1.hashMessage)(
189
- walletAddress,
190
- contractAddress,
191
- entrypointSelector,
192
- calldataDecimal,
193
- nonceBn.toString()
194
- )
156
+ msgHash = (0, hash_1.hashMulticall)(
157
+ transactionsDetail.walletAddress,
158
+ transactions,
159
+ transactionsDetail.nonce.toString(),
160
+ transactionsDetail.maxFee.toString()
195
161
  );
196
162
  return [2 /*return*/, (0, ellipticCurve_1.sign)(this.keyPair, msgHash)];
197
163
  });
198
164
  });
199
165
  };
200
- Signer.prototype.signMessage = function (typedData, walletAddress) {
166
+ Signer.prototype.signMessage = function (typedData, accountAddress) {
201
167
  return __awaiter(this, void 0, void 0, function () {
202
168
  var msgHash;
203
169
  return __generator(this, function (_a) {
204
- msgHash = (0, typedData_1.getMessageHash)(typedData, walletAddress);
170
+ msgHash = (0, typedData_1.getMessageHash)(typedData, accountAddress);
205
171
  return [2 /*return*/, (0, ellipticCurve_1.sign)(this.keyPair, msgHash)];
206
172
  });
207
173
  });
@@ -11,11 +11,12 @@ export declare abstract class SignerInterface {
11
11
  * Sign an JSON object for off-chain usage with the starknet private key and return the signature
12
12
  * This adds a message prefix so it cant be interchanged with transactions
13
13
  *
14
- * @param json - JSON object to be signed
14
+ * @param typedData - JSON object to be signed
15
+ * @param accountAddress - account
15
16
  * @returns the signature of the JSON object
16
17
  * @throws {Error} if the JSON object is not a valid JSON
17
18
  */
18
- abstract signMessage(typedData: TypedData, walletAddress: string): Promise<Signature>;
19
+ abstract signMessage(typedData: TypedData, accountAddress: string): Promise<Signature>;
19
20
  /**
20
21
  * Signs a transaction with the starknet private key and returns the signature
21
22
  *
@@ -1,16 +1,17 @@
1
- import { compileCalldata } from '../contract';
2
1
  import { Provider } from '../provider';
3
2
  import { Signer, SignerInterface } from '../signer';
4
3
  import {
5
4
  Abi,
6
5
  AddTransactionResponse,
7
- ExecuteInvocation,
6
+ Call,
8
7
  InvocationsDetails,
9
8
  KeyPair,
10
9
  Signature,
11
10
  } from '../types';
11
+ import { getSelectorFromName } from '../utils/hash';
12
12
  import { BigNumberish, bigNumberishArrayToDecimalStringArray, toBN, toHex } from '../utils/number';
13
- import { getSelectorFromName } from '../utils/stark';
13
+ import { compileCalldata } from '../utils/stark';
14
+ import { fromCallsToExecuteCalldata } from '../utils/transaction';
14
15
  import { TypedData, getMessageHash } from '../utils/typedData';
15
16
  import { AccountInterface } from './interface';
16
17
 
@@ -42,51 +43,28 @@ export class Account extends Provider implements AccountInterface {
42
43
  * @returns a confirmation of invoking a function on the starknet contract
43
44
  */
44
45
  public async execute(
45
- transactions: ExecuteInvocation | ExecuteInvocation[],
46
- abis: Abi[] = [],
46
+ calls: Call | Call[],
47
+ abis: Abi[] | undefined = undefined,
47
48
  transactionsDetail: InvocationsDetails = {}
48
49
  ): Promise<AddTransactionResponse> {
49
- if (Array.isArray(transactions) && transactions.length !== 1) {
50
- throw new Error('Only one transaction at a time is currently supported');
51
- }
52
-
53
- const {
54
- contractAddress,
55
- calldata = [],
56
- entrypoint,
57
- ...invocation
58
- } = Array.isArray(transactions) ? transactions[0] : transactions;
59
- const { nonce } = transactionsDetail;
50
+ const transactions = Array.isArray(calls) ? calls : [calls];
60
51
 
61
- const nonceBn = toBN(nonce ?? (await this.getNonce()));
62
- const calldataDecimal = bigNumberishArrayToDecimalStringArray(calldata);
52
+ const signerDetails = {
53
+ walletAddress: this.address,
54
+ nonce: toBN(transactionsDetail.nonce ?? (await this.getNonce())),
55
+ maxFee: toBN(transactionsDetail.maxFee ?? '0'),
56
+ };
63
57
 
64
- const signature = await this.signer.signTransaction(
65
- [
66
- {
67
- ...invocation,
68
- contractAddress,
69
- calldata: calldataDecimal,
70
- entrypoint,
71
- },
72
- ],
73
- { walletAddress: this.address, nonce: nonceBn },
74
- abis
75
- );
58
+ const signature = await this.signer.signTransaction(transactions, signerDetails, abis);
76
59
 
77
- const entrypointSelector = getSelectorFromName(entrypoint);
60
+ const calldata = [...fromCallsToExecuteCalldata(transactions), signerDetails.nonce.toString()];
78
61
 
79
- return super.invokeFunction({
80
- contractAddress: this.address,
81
- entrypoint: 'execute',
82
- calldata: [
83
- contractAddress,
84
- entrypointSelector,
85
- calldataDecimal.length.toString(),
86
- ...calldataDecimal,
87
- nonceBn.toString(),
88
- ],
89
- signature,
62
+ return this.fetchEndpoint('add_transaction', undefined, {
63
+ type: 'INVOKE_FUNCTION',
64
+ contract_address: this.address,
65
+ entry_point_selector: getSelectorFromName('__execute__'),
66
+ calldata,
67
+ signature: bigNumberishArrayToDecimalStringArray(signature),
90
68
  });
91
69
  }
92
70
 
@@ -115,7 +93,7 @@ export class Account extends Provider implements AccountInterface {
115
93
  /**
116
94
  * Verify a signature of a JSON object
117
95
  *
118
- * @param json - JSON object to be verified
96
+ * @param hash - JSON object to be verified
119
97
  * @param signature - signature of the JSON object
120
98
  * @returns true if the signature is valid, false otherwise
121
99
  * @throws {Error} if the JSON object is not a valid JSON or the signature is not a valid signature
@@ -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';
@@ -41,7 +41,7 @@ export abstract class AccountInterface extends ProviderInterface {
41
41
  * @returns response from addTransaction
42
42
  */
43
43
  public abstract execute(
44
- transactions: ExecuteInvocation | ExecuteInvocation[],
44
+ transactions: Call | Call[],
45
45
  abis?: Abi[],
46
46
  transactionsDetail?: InvocationsDetails
47
47
  ): Promise<AddTransactionResponse>;