starknet 3.18.2 → 3.19.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 (158) hide show
  1. package/CHANGELOG.md +53 -0
  2. package/__tests__/account.test.ts +11 -56
  3. package/__tests__/contract.test.ts +11 -49
  4. package/__tests__/defaultProvider.test.ts +321 -0
  5. package/__tests__/fixtures.ts +32 -11
  6. package/__tests__/rpcProvider.test.ts +17 -0
  7. package/__tests__/sequencerProvider.test.ts +45 -0
  8. package/account/default.d.ts +54 -77
  9. package/account/default.js +271 -596
  10. package/account/index.js +18 -31
  11. package/account/interface.d.ts +66 -95
  12. package/account/interface.js +20 -30
  13. package/constants.d.ts +17 -19
  14. package/constants.js +2038 -2059
  15. package/contract/contractFactory.d.ts +25 -29
  16. package/contract/contractFactory.js +94 -210
  17. package/contract/default.d.ts +117 -146
  18. package/contract/default.js +582 -776
  19. package/contract/index.js +19 -32
  20. package/contract/interface.d.ts +72 -92
  21. package/contract/interface.js +6 -5
  22. package/dist/account/default.d.ts +5 -9
  23. package/dist/account/default.js +35 -169
  24. package/dist/account/interface.d.ts +3 -15
  25. package/dist/contract/contractFactory.js +4 -4
  26. package/dist/contract/default.d.ts +3 -3
  27. package/dist/contract/default.js +3 -2
  28. package/dist/contract/interface.d.ts +2 -2
  29. package/dist/provider/default.d.ts +18 -134
  30. package/dist/provider/default.js +47 -411
  31. package/dist/provider/index.d.ts +2 -0
  32. package/dist/provider/index.js +2 -0
  33. package/dist/provider/interface.d.ts +45 -50
  34. package/dist/provider/rpc.d.ts +57 -0
  35. package/dist/provider/rpc.js +364 -0
  36. package/dist/provider/sequencer.d.ts +66 -0
  37. package/dist/provider/sequencer.js +443 -0
  38. package/dist/types/account.d.ts +2 -3
  39. package/dist/types/api/index.d.ts +16 -0
  40. package/dist/types/api/index.js +18 -0
  41. package/dist/types/api/rpc.d.ts +221 -0
  42. package/dist/types/{api.js → api/rpc.js} +0 -0
  43. package/dist/types/api/sequencer.d.ts +289 -0
  44. package/dist/types/api/sequencer.js +2 -0
  45. package/dist/types/index.d.ts +3 -1
  46. package/dist/types/index.js +15 -1
  47. package/dist/types/lib.d.ts +3 -1
  48. package/dist/types/provider.d.ts +86 -0
  49. package/dist/types/provider.js +2 -0
  50. package/dist/utils/provider.d.ts +4 -0
  51. package/dist/utils/provider.js +38 -0
  52. package/dist/utils/responseParser/index.d.ts +11 -0
  53. package/dist/utils/responseParser/index.js +9 -0
  54. package/dist/utils/responseParser/rpc.d.ts +13 -0
  55. package/dist/utils/responseParser/rpc.js +96 -0
  56. package/dist/utils/responseParser/sequencer.d.ts +13 -0
  57. package/dist/utils/responseParser/sequencer.js +124 -0
  58. package/index.js +42 -75
  59. package/package.json +1 -1
  60. package/provider/default.d.ts +21 -175
  61. package/provider/default.js +139 -704
  62. package/provider/errors.d.ts +4 -4
  63. package/provider/errors.js +30 -40
  64. package/provider/index.d.ts +2 -0
  65. package/provider/index.js +22 -33
  66. package/provider/interface.d.ts +104 -131
  67. package/provider/interface.js +6 -5
  68. package/provider/rpc.d.ts +57 -0
  69. package/provider/rpc.js +364 -0
  70. package/provider/sequencer.d.ts +66 -0
  71. package/provider/sequencer.js +443 -0
  72. package/provider/utils.d.ts +7 -9
  73. package/provider/utils.js +39 -44
  74. package/signer/default.d.ts +5 -9
  75. package/signer/default.js +72 -177
  76. package/signer/index.js +18 -31
  77. package/signer/interface.d.ts +29 -33
  78. package/signer/interface.js +6 -5
  79. package/src/account/default.ts +26 -146
  80. package/src/account/interface.ts +5 -20
  81. package/src/contract/contractFactory.ts +3 -6
  82. package/src/contract/default.ts +6 -4
  83. package/src/contract/interface.ts +2 -2
  84. package/src/provider/default.ts +63 -395
  85. package/src/provider/index.ts +2 -0
  86. package/src/provider/interface.ts +68 -63
  87. package/src/provider/rpc.ts +300 -0
  88. package/src/provider/sequencer.ts +384 -0
  89. package/src/types/account.ts +2 -3
  90. package/src/types/api/index.ts +17 -0
  91. package/src/types/api/rpc.ts +247 -0
  92. package/src/types/api/sequencer.ts +331 -0
  93. package/src/types/index.ts +3 -1
  94. package/src/types/lib.ts +3 -1
  95. package/src/types/provider.ts +108 -0
  96. package/src/utils/provider.ts +28 -0
  97. package/src/utils/responseParser/index.ts +28 -0
  98. package/src/utils/responseParser/rpc.ts +93 -0
  99. package/src/utils/responseParser/sequencer.ts +127 -0
  100. package/types/account.d.ts +5 -7
  101. package/types/account.js +2 -2
  102. package/types/api/index.d.ts +16 -0
  103. package/types/api/index.js +18 -0
  104. package/types/api/rpc.d.ts +221 -0
  105. package/types/api/rpc.js +2 -0
  106. package/types/api/sequencer.d.ts +289 -0
  107. package/types/api/sequencer.js +2 -0
  108. package/types/contract.d.ts +1 -1
  109. package/types/contract.js +2 -2
  110. package/types/index.d.ts +3 -1
  111. package/types/index.js +35 -34
  112. package/types/lib.d.ts +36 -41
  113. package/types/lib.js +2 -2
  114. package/types/provider.d.ts +86 -0
  115. package/types/provider.js +2 -0
  116. package/types/signer.d.ts +2 -2
  117. package/types/signer.js +2 -2
  118. package/utils/address.js +26 -37
  119. package/utils/ellipticCurve.d.ts +1 -6
  120. package/utils/ellipticCurve.js +73 -137
  121. package/utils/encode.js +49 -85
  122. package/utils/hash.d.ts +4 -31
  123. package/utils/hash.js +76 -141
  124. package/utils/json.d.ts +13 -45
  125. package/utils/json.js +15 -22
  126. package/utils/number.d.ts +2 -9
  127. package/utils/number.js +47 -81
  128. package/utils/provider.d.ts +4 -0
  129. package/utils/provider.js +38 -0
  130. package/utils/responseParser/index.d.ts +11 -0
  131. package/utils/responseParser/index.js +9 -0
  132. package/utils/responseParser/rpc.d.ts +13 -0
  133. package/utils/responseParser/rpc.js +96 -0
  134. package/utils/responseParser/sequencer.d.ts +13 -0
  135. package/utils/responseParser/sequencer.js +124 -0
  136. package/utils/shortString.js +13 -21
  137. package/utils/stark.d.ts +0 -1
  138. package/utils/stark.js +59 -93
  139. package/utils/transaction.d.ts +3 -6
  140. package/utils/transaction.js +50 -81
  141. package/utils/typedData/index.d.ts +3 -15
  142. package/utils/typedData/index.js +109 -175
  143. package/utils/typedData/types.d.ts +9 -9
  144. package/utils/typedData/types.js +2 -2
  145. package/utils/typedData/utils.js +6 -6
  146. package/utils/uint256.d.ts +5 -5
  147. package/utils/uint256.js +16 -26
  148. package/www/docs/API/account.md +3 -4
  149. package/www/docs/API/contract.md +2 -2
  150. package/www/docs/API/contractFactory.md +2 -2
  151. package/www/docs/API/provider.md +185 -74
  152. package/www/guides/account.md +1 -8
  153. package/www/guides/erc20.md +3 -0
  154. package/__tests__/provider.test.ts +0 -168
  155. package/dist/types/api.d.ts +0 -261
  156. package/src/types/api.ts +0 -303
  157. package/types/api.d.ts +0 -287
  158. package/types/api.js +0 -2
@@ -1,608 +1,283 @@
1
- 'use strict';
2
- var __extends =
3
- (this && this.__extends) ||
4
- (function () {
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
5
3
  var extendStatics = function (d, b) {
6
- extendStatics =
7
- Object.setPrototypeOf ||
8
- ({ __proto__: [] } instanceof Array &&
9
- function (d, b) {
10
- d.__proto__ = b;
11
- }) ||
12
- function (d, b) {
13
- for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p];
14
- };
15
- return extendStatics(d, b);
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
16
8
  };
17
9
  return function (d, b) {
18
- if (typeof b !== 'function' && b !== null)
19
- throw new TypeError('Class extends value ' + String(b) + ' is not a constructor or null');
20
- extendStatics(d, b);
21
- function __() {
22
- this.constructor = d;
23
- }
24
- d.prototype = b === null ? Object.create(b) : ((__.prototype = b.prototype), new __());
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
25
15
  };
26
- })();
27
- var __awaiter =
28
- (this && this.__awaiter) ||
29
- function (thisArg, _arguments, P, generator) {
30
- function adopt(value) {
31
- return value instanceof P
32
- ? value
33
- : new P(function (resolve) {
34
- resolve(value);
35
- });
36
- }
37
- return new (P || (P = Promise))(function (resolve, reject) {
38
- function fulfilled(value) {
39
- try {
40
- step(generator.next(value));
41
- } catch (e) {
42
- reject(e);
43
- }
44
- }
45
- function rejected(value) {
46
- try {
47
- step(generator['throw'](value));
48
- } catch (e) {
49
- reject(e);
16
+ })();
17
+ var __assign = (this && this.__assign) || function () {
18
+ __assign = Object.assign || function(t) {
19
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
20
+ s = arguments[i];
21
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
22
+ t[p] = s[p];
50
23
  }
51
- }
52
- function step(result) {
53
- result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
54
- }
55
- step((generator = generator.apply(thisArg, _arguments || [])).next());
24
+ return t;
25
+ };
26
+ return __assign.apply(this, arguments);
27
+ };
28
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
29
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
30
+ return new (P || (P = Promise))(function (resolve, reject) {
31
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
32
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
33
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
34
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
56
35
  });
57
- };
58
- var __generator =
59
- (this && this.__generator) ||
60
- function (thisArg, body) {
61
- var _ = {
62
- label: 0,
63
- sent: function () {
64
- if (t[0] & 1) throw t[1];
65
- return t[1];
66
- },
67
- trys: [],
68
- ops: [],
69
- },
70
- f,
71
- y,
72
- t,
73
- g;
74
- return (
75
- (g = { next: verb(0), throw: verb(1), return: verb(2) }),
76
- typeof Symbol === 'function' &&
77
- (g[Symbol.iterator] = function () {
78
- return this;
79
- }),
80
- g
81
- );
82
- function verb(n) {
83
- return function (v) {
84
- return step([n, v]);
85
- };
86
- }
36
+ };
37
+ var __generator = (this && this.__generator) || function (thisArg, body) {
38
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
39
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
40
+ function verb(n) { return function (v) { return step([n, v]); }; }
87
41
  function step(op) {
88
- if (f) throw new TypeError('Generator is already executing.');
89
- while (_)
90
- try {
91
- if (
92
- ((f = 1),
93
- y &&
94
- (t =
95
- op[0] & 2
96
- ? y['return']
97
- : op[0]
98
- ? y['throw'] || ((t = y['return']) && t.call(y), 0)
99
- : y.next) &&
100
- !(t = t.call(y, op[1])).done)
101
- )
102
- return t;
103
- if (((y = 0), t)) op = [op[0] & 2, t.value];
104
- switch (op[0]) {
105
- case 0:
106
- case 1:
107
- t = op;
108
- break;
109
- case 4:
110
- _.label++;
111
- return { value: op[1], done: false };
112
- case 5:
113
- _.label++;
114
- y = op[1];
115
- op = [0];
116
- continue;
117
- case 7:
118
- op = _.ops.pop();
119
- _.trys.pop();
120
- continue;
121
- default:
122
- if (
123
- !((t = _.trys), (t = t.length > 0 && t[t.length - 1])) &&
124
- (op[0] === 6 || op[0] === 2)
125
- ) {
126
- _ = 0;
127
- continue;
128
- }
129
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) {
130
- _.label = op[1];
131
- break;
132
- }
133
- if (op[0] === 6 && _.label < t[1]) {
134
- _.label = t[1];
135
- t = op;
136
- break;
137
- }
138
- if (t && _.label < t[2]) {
139
- _.label = t[2];
140
- _.ops.push(op);
141
- break;
142
- }
143
- if (t[2]) _.ops.pop();
144
- _.trys.pop();
145
- continue;
146
- }
147
- op = body.call(thisArg, _);
148
- } catch (e) {
149
- op = [6, e];
150
- y = 0;
151
- } finally {
152
- f = t = 0;
153
- }
154
- if (op[0] & 5) throw op[1];
155
- return { value: op[0] ? op[1] : void 0, done: true };
156
- }
157
- };
158
- var __read =
159
- (this && this.__read) ||
160
- function (o, n) {
161
- var m = typeof Symbol === 'function' && o[Symbol.iterator];
162
- if (!m) return o;
163
- var i = m.call(o),
164
- r,
165
- ar = [],
166
- e;
167
- try {
168
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
169
- } catch (error) {
170
- e = { error: error };
171
- } finally {
172
- try {
173
- if (r && !r.done && (m = i['return'])) m.call(i);
174
- } finally {
175
- if (e) throw e.error;
176
- }
42
+ if (f) throw new TypeError("Generator is already executing.");
43
+ while (_) try {
44
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
45
+ if (y = 0, t) op = [op[0] & 2, t.value];
46
+ switch (op[0]) {
47
+ case 0: case 1: t = op; break;
48
+ case 4: _.label++; return { value: op[1], done: false };
49
+ case 5: _.label++; y = op[1]; op = [0]; continue;
50
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
51
+ default:
52
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
53
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
54
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
55
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
56
+ if (t[2]) _.ops.pop();
57
+ _.trys.pop(); continue;
58
+ }
59
+ op = body.call(thisArg, _);
60
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
61
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
177
62
  }
178
- return ar;
179
- };
180
- var __spreadArray =
181
- (this && this.__spreadArray) ||
182
- function (to, from, pack) {
183
- if (pack || arguments.length === 2)
184
- for (var i = 0, l = from.length, ar; i < l; i++) {
185
- if (ar || !(i in from)) {
186
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
187
- ar[i] = from[i];
188
- }
189
- }
190
- return to.concat(ar || Array.prototype.slice.call(from));
191
- };
192
- var __importDefault =
193
- (this && this.__importDefault) ||
194
- function (mod) {
195
- return mod && mod.__esModule ? mod : { default: mod };
196
- };
197
- Object.defineProperty(exports, '__esModule', { value: true });
63
+ };
64
+ Object.defineProperty(exports, "__esModule", { value: true });
198
65
  exports.Account = void 0;
199
- var minimalistic_assert_1 = __importDefault(require('minimalistic-assert'));
200
- var constants_1 = require('../constants');
201
- var provider_1 = require('../provider');
202
- var signer_1 = require('../signer');
203
- var ellipticCurve_1 = require('../utils/ellipticCurve');
204
- var hash_1 = require('../utils/hash');
205
- var number_1 = require('../utils/number');
206
- var shortString_1 = require('../utils/shortString');
207
- var stark_1 = require('../utils/stark');
208
- var transaction_1 = require('../utils/transaction');
209
- var typedData_1 = require('../utils/typedData');
66
+ var constants_1 = require("../constants");
67
+ var default_1 = require("../provider/default");
68
+ var signer_1 = require("../signer");
69
+ var hash_1 = require("../utils/hash");
70
+ var number_1 = require("../utils/number");
71
+ var stark_1 = require("../utils/stark");
72
+ var transaction_1 = require("../utils/transaction");
73
+ var typedData_1 = require("../utils/typedData");
210
74
  var Account = /** @class */ (function (_super) {
211
- __extends(Account, _super);
212
- function Account(provider, address, keyPairOrSigner) {
213
- var _this = _super.call(this, provider) || this;
214
- _this.signer =
215
- 'getPubKey' in keyPairOrSigner ? keyPairOrSigner : new signer_1.Signer(keyPairOrSigner);
216
- _this.address = address;
217
- return _this;
218
- }
219
- Account.prototype.getNonce = function () {
220
- return __awaiter(this, void 0, void 0, function () {
221
- var result;
222
- return __generator(this, function (_a) {
223
- switch (_a.label) {
224
- case 0:
225
- return [
226
- 4 /*yield*/,
227
- this.callContract({
228
- contractAddress: this.address,
229
- entrypoint: 'get_nonce',
230
- }),
231
- ];
232
- case 1:
233
- result = _a.sent().result;
234
- return [2 /*return*/, (0, number_1.toHex)((0, number_1.toBN)(result[0]))];
235
- }
236
- });
237
- });
238
- };
239
- Account.prototype.estimateFee = function (calls, _a) {
240
- var _b;
241
- var _c = _a === void 0 ? {} : _a,
242
- providedNonce = _c.nonce,
243
- _d = _c.blockIdentifier,
244
- blockIdentifier = _d === void 0 ? 'pending' : _d;
245
- return __awaiter(this, void 0, void 0, function () {
246
- var transactions,
247
- nonce,
248
- _e,
249
- version,
250
- signerDetails,
251
- signature,
252
- calldata,
253
- fetchedEstimate,
254
- fee,
255
- suggestedMaxFee;
256
- return __generator(this, function (_f) {
257
- switch (_f.label) {
258
- case 0:
259
- transactions = Array.isArray(calls) ? calls : [calls];
260
- if (!(providedNonce !== null && providedNonce !== void 0)) return [3 /*break*/, 1];
261
- _e = providedNonce;
262
- return [3 /*break*/, 3];
263
- case 1:
264
- return [4 /*yield*/, this.getNonce()];
265
- case 2:
266
- _e = _f.sent();
267
- _f.label = 3;
268
- case 3:
269
- nonce = _e;
270
- version = (0, number_1.toBN)(hash_1.feeTransactionVersion);
271
- signerDetails = {
272
- walletAddress: this.address,
273
- nonce: (0, number_1.toBN)(nonce),
274
- maxFee: constants_1.ZERO,
275
- version: version,
276
- chainId: this.chainId,
277
- };
278
- return [4 /*yield*/, this.signer.signTransaction(transactions, signerDetails)];
279
- case 4:
280
- signature = _f.sent();
281
- calldata = (0, transaction_1.fromCallsToExecuteCalldataWithNonce)(transactions, nonce);
282
- return [
283
- 4 /*yield*/,
284
- this.fetchEndpoint(
285
- 'estimate_fee',
286
- { blockIdentifier: blockIdentifier },
287
- {
288
- contract_address: this.address,
289
- entry_point_selector: (0, hash_1.getSelectorFromName)('__execute__'),
290
- calldata: calldata,
291
- version: (0, number_1.toHex)(version),
292
- signature: (0, number_1.bigNumberishArrayToDecimalStringArray)(signature),
293
- }
294
- ),
295
- ];
296
- case 5:
297
- fetchedEstimate = _f.sent();
298
- fee =
299
- (_b = fetchedEstimate.overall_fee) !== null && _b !== void 0
300
- ? _b
301
- : fetchedEstimate.amount;
302
- if (fee === undefined) {
303
- throw new Error('Expected either amount or overall_fee in estimate_fee response');
304
- }
305
- suggestedMaxFee = (0, stark_1.estimatedFeeToMaxFee)(fee);
306
- return [
307
- 2 /*return*/,
308
- {
309
- amount: fee,
310
- unit: fetchedEstimate.unit,
311
- suggestedMaxFee: suggestedMaxFee,
312
- },
313
- ];
314
- }
315
- });
316
- });
317
- };
318
- /**
319
- * Invoke execute function in account contract
320
- *
321
- * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/gateway/gateway_client.py#L13-L17)
322
- *
323
- * @param calls - one or more calls to be executed
324
- * @param abis - one or more abis which can be used to display the calls
325
- * @param transactionsDetail - optional transaction details
326
- * @returns a confirmation of invoking a function on the starknet contract
327
- */
328
- Account.prototype.execute = function (calls, abis, transactionsDetail) {
329
- var _a;
330
- if (abis === void 0) {
331
- abis = undefined;
332
- }
333
- if (transactionsDetail === void 0) {
334
- transactionsDetail = {};
75
+ __extends(Account, _super);
76
+ function Account(providerOrOptions, address, keyPairOrSigner) {
77
+ var _this = _super.call(this, providerOrOptions) || this;
78
+ _this.address = address;
79
+ _this.signer =
80
+ 'getPubKey' in keyPairOrSigner ? keyPairOrSigner : new signer_1.Signer(keyPairOrSigner);
81
+ return _this;
335
82
  }
336
- return __awaiter(this, void 0, void 0, function () {
337
- var transactions, nonce, _b, _c, maxFee, suggestedMaxFee, signerDetails, signature, calldata;
338
- return __generator(this, function (_d) {
339
- switch (_d.label) {
340
- case 0:
341
- transactions = Array.isArray(calls) ? calls : [calls];
342
- _b = number_1.toBN;
343
- if (!((_a = transactionsDetail.nonce) !== null && _a !== void 0))
344
- return [3 /*break*/, 1];
345
- _c = _a;
346
- return [3 /*break*/, 3];
347
- case 1:
348
- return [4 /*yield*/, this.getNonce()];
349
- case 2:
350
- _c = _d.sent();
351
- _d.label = 3;
352
- case 3:
353
- nonce = _b.apply(void 0, [_c]);
354
- maxFee = '0';
355
- if (!(transactionsDetail.maxFee || transactionsDetail.maxFee === 0))
356
- return [3 /*break*/, 4];
357
- maxFee = transactionsDetail.maxFee;
358
- return [3 /*break*/, 6];
359
- case 4:
360
- return [4 /*yield*/, this.estimateFee(transactions, { nonce: nonce })];
361
- case 5:
362
- suggestedMaxFee = _d.sent().suggestedMaxFee;
363
- maxFee = suggestedMaxFee.toString();
364
- _d.label = 6;
365
- case 6:
366
- signerDetails = {
367
- walletAddress: this.address,
368
- nonce: nonce,
369
- maxFee: maxFee,
370
- version: (0, number_1.toBN)(hash_1.transactionVersion),
371
- chainId: this.chainId,
372
- };
373
- return [4 /*yield*/, this.signer.signTransaction(transactions, signerDetails, abis)];
374
- case 7:
375
- signature = _d.sent();
376
- calldata = (0, transaction_1.fromCallsToExecuteCalldataWithNonce)(transactions, nonce);
377
- return [
378
- 2 /*return*/,
379
- this.fetchEndpoint('add_transaction', undefined, {
380
- type: 'INVOKE_FUNCTION',
381
- contract_address: this.address,
382
- entry_point_selector: (0, hash_1.getSelectorFromName)('__execute__'),
383
- calldata: calldata,
384
- signature: (0, number_1.bigNumberishArrayToDecimalStringArray)(signature),
385
- max_fee: (0, number_1.toHex)((0, number_1.toBN)(maxFee)),
386
- }),
387
- ];
388
- }
389
- });
390
- });
391
- };
392
- /**
393
- * Temporary method to allow dapps on starknet.js v2 to work with Argent X v3
394
- * @deprecated to remove ASAP
395
- */
396
- Account.prototype.LEGACY_addTransaction = function (transaction) {
397
- return __awaiter(this, void 0, void 0, function () {
398
- function hashMulticall(account, transactions, nonce, maxFee) {
399
- var hashArray = transactions
400
- .map(function (_a) {
401
- var contract_address = _a.contract_address,
402
- entry_point_selector = _a.entry_point_selector,
403
- calldata = _a.calldata;
404
- return [
405
- contract_address,
406
- entry_point_selector,
407
- (0, hash_1.computeHashOnElements)(calldata || []),
408
- ];
409
- })
410
- .map(number_1.bigNumberishArrayToDecimalStringArray)
411
- .map(hash_1.computeHashOnElements);
412
- return (0,
413
- hash_1.computeHashOnElements)([(0, shortString_1.encodeShortString)('StarkNet Transaction'), account, (0, hash_1.computeHashOnElements)(hashArray), nonce, maxFee, hash_1.transactionVersion]);
414
- }
415
- var nonceBn,
416
- result,
417
- msgHash,
418
- signature,
419
- transformCallsToMulticallArrays,
420
- fromCallsToExecuteCalldata2,
421
- calldata;
422
- return __generator(this, function (_a) {
423
- switch (_a.label) {
424
- case 0:
425
- if (transaction.type === 'DEPLOY') throw new Error('No DEPLOYS');
426
- if (transaction.type === 'DECLARE') throw new Error('No DECLARES');
427
- (0,
428
- minimalistic_assert_1.default)(!transaction.signature, "Adding signatures to a signer transaction currently isn't supported");
429
- if (!transaction.nonce) return [3 /*break*/, 1];
430
- nonceBn = (0, number_1.toBN)(transaction.nonce);
431
- return [3 /*break*/, 3];
432
- case 1:
433
- return [
434
- 4 /*yield*/,
435
- this.callContract({
436
- contractAddress: this.address,
437
- entrypoint: 'get_nonce',
438
- }),
439
- ];
440
- case 2:
441
- result = _a.sent().result;
442
- nonceBn = (0, number_1.toBN)(result[0]);
443
- _a.label = 3;
444
- case 3:
445
- msgHash = hashMulticall(this.address, [transaction], nonceBn.toString(), '0');
446
- if (!('keyPair' in this.signer)) {
447
- throw new Error('No keyPair');
448
- }
449
- signature = (0, ellipticCurve_1.sign)(this.signer.keyPair, msgHash);
450
- transformCallsToMulticallArrays = function (calls) {
451
- var callArray = [];
452
- var calldata = [];
453
- calls.forEach(function (call) {
454
- var data = call.calldata || [];
455
- callArray.push({
456
- to: (0, number_1.toBN)(call.contract_address).toString(10),
457
- selector: (0, number_1.toBN)(call.entry_point_selector).toString(10),
458
- data_offset: calldata.length.toString(),
459
- data_len: data.length.toString(),
460
- });
461
- calldata.push.apply(calldata, __spreadArray([], __read(data), false));
462
- });
463
- return {
464
- callArray: callArray,
465
- calldata: (0, number_1.bigNumberishArrayToDecimalStringArray)(calldata),
466
- };
467
- };
468
- fromCallsToExecuteCalldata2 = function (calls) {
469
- var _a = transformCallsToMulticallArrays(calls),
470
- callArray = _a.callArray,
471
- calldata = _a.calldata;
472
- return __spreadArray(
473
- __spreadArray(
474
- __spreadArray(
475
- [callArray.length.toString()],
476
- __read(
477
- callArray
478
- .map(function (_a) {
479
- var to = _a.to,
480
- selector = _a.selector,
481
- data_offset = _a.data_offset,
482
- data_len = _a.data_len;
483
- return [to, selector, data_offset, data_len];
484
- })
485
- .flat()
486
- ),
487
- false
488
- ),
489
- [calldata.length.toString()],
490
- false
491
- ),
492
- __read(calldata),
493
- false
494
- );
495
- };
496
- calldata = __spreadArray(
497
- __spreadArray([], __read(fromCallsToExecuteCalldata2([transaction])), false),
498
- [nonceBn.toString()],
499
- false
500
- );
501
- return [
502
- 2 /*return*/,
503
- this.fetchEndpoint('add_transaction', undefined, {
504
- type: 'INVOKE_FUNCTION',
505
- contract_address: this.address,
506
- entry_point_selector: (0, hash_1.getSelectorFromName)('__execute__'),
507
- calldata: calldata,
508
- signature: (0, number_1.bigNumberishArrayToDecimalStringArray)(signature),
509
- }),
510
- ];
511
- }
512
- });
513
- });
514
- };
515
- /**
516
- * Sign an JSON object with the starknet private key and return the signature
517
- *
518
- * @param json - JSON object to be signed
519
- * @returns the signature of the JSON object
520
- * @throws {Error} if the JSON object is not a valid JSON
521
- */
522
- Account.prototype.signMessage = function (typedData) {
523
- return __awaiter(this, void 0, void 0, function () {
524
- return __generator(this, function (_a) {
525
- return [2 /*return*/, this.signer.signMessage(typedData, this.address)];
526
- });
527
- });
528
- };
529
- /**
530
- * Hash a JSON object with pederson hash and return the hash
531
- *
532
- * @param json - JSON object to be hashed
533
- * @returns the hash of the JSON object
534
- * @throws {Error} if the JSON object is not a valid JSON
535
- */
536
- Account.prototype.hashMessage = function (typedData) {
537
- return __awaiter(this, void 0, void 0, function () {
538
- return __generator(this, function (_a) {
539
- return [2 /*return*/, (0, typedData_1.getMessageHash)(typedData, this.address)];
540
- });
541
- });
542
- };
543
- /**
544
- * Verify a signature of a given hash
545
- * @warning This method is not recommended, use verifyMessage instead
546
- *
547
- * @param hash - JSON object to be verified
548
- * @param signature - signature of the JSON object
549
- * @returns true if the signature is valid, false otherwise
550
- * @throws {Error} if the JSON object is not a valid JSON or the signature is not a valid signature
551
- */
552
- Account.prototype.verifyMessageHash = function (hash, signature) {
553
- return __awaiter(this, void 0, void 0, function () {
554
- var _a;
555
- return __generator(this, function (_b) {
556
- switch (_b.label) {
557
- case 0:
558
- _b.trys.push([0, 2, , 3]);
559
- return [
560
- 4 /*yield*/,
561
- this.callContract({
562
- contractAddress: this.address,
563
- entrypoint: 'is_valid_signature',
564
- calldata: (0, stark_1.compileCalldata)({
565
- hash: (0, number_1.toBN)(hash).toString(),
566
- signature: signature.map(function (x) {
567
- return (0, number_1.toBN)(x).toString();
568
- }),
569
- }),
570
- }),
571
- ];
572
- case 1:
573
- _b.sent();
574
- return [2 /*return*/, true];
575
- case 2:
576
- _a = _b.sent();
577
- return [2 /*return*/, false];
578
- case 3:
579
- return [2 /*return*/];
580
- }
581
- });
582
- });
583
- };
584
- /**
585
- * Verify a signature of a JSON object
586
- *
587
- * @param hash - hash to be verified
588
- * @param signature - signature of the hash
589
- * @returns true if the signature is valid, false otherwise
590
- * @throws {Error} if the signature is not a valid signature
591
- */
592
- Account.prototype.verifyMessage = function (typedData, signature) {
593
- return __awaiter(this, void 0, void 0, function () {
594
- var hash;
595
- return __generator(this, function (_a) {
596
- switch (_a.label) {
597
- case 0:
598
- return [4 /*yield*/, this.hashMessage(typedData)];
599
- case 1:
600
- hash = _a.sent();
601
- return [2 /*return*/, this.verifyMessageHash(hash, signature)];
602
- }
603
- });
604
- });
605
- };
606
- return Account;
607
- })(provider_1.Provider);
83
+ Account.prototype.getNonce = function () {
84
+ return __awaiter(this, void 0, void 0, function () {
85
+ var result;
86
+ return __generator(this, function (_a) {
87
+ switch (_a.label) {
88
+ case 0: return [4 /*yield*/, this.callContract({
89
+ contractAddress: this.address,
90
+ entrypoint: 'get_nonce',
91
+ })];
92
+ case 1:
93
+ result = (_a.sent()).result;
94
+ return [2 /*return*/, (0, number_1.toHex)((0, number_1.toBN)(result[0]))];
95
+ }
96
+ });
97
+ });
98
+ };
99
+ Account.prototype.estimateFee = function (calls, _a) {
100
+ var _b = _a === void 0 ? {} : _a, providedNonce = _b.nonce, blockIdentifier = _b.blockIdentifier;
101
+ return __awaiter(this, void 0, void 0, function () {
102
+ var transactions, nonce, _c, version, signerDetails, signature, calldata, response, suggestedMaxFee;
103
+ return __generator(this, function (_d) {
104
+ switch (_d.label) {
105
+ case 0:
106
+ transactions = Array.isArray(calls) ? calls : [calls];
107
+ if (!(providedNonce !== null && providedNonce !== void 0)) return [3 /*break*/, 1];
108
+ _c = providedNonce;
109
+ return [3 /*break*/, 3];
110
+ case 1: return [4 /*yield*/, this.getNonce()];
111
+ case 2:
112
+ _c = (_d.sent());
113
+ _d.label = 3;
114
+ case 3:
115
+ nonce = _c;
116
+ version = (0, number_1.toBN)(hash_1.feeTransactionVersion);
117
+ signerDetails = {
118
+ walletAddress: this.address,
119
+ nonce: (0, number_1.toBN)(nonce),
120
+ maxFee: constants_1.ZERO,
121
+ version: version,
122
+ chainId: this.chainId,
123
+ };
124
+ return [4 /*yield*/, this.signer.signTransaction(transactions, signerDetails)];
125
+ case 4:
126
+ signature = _d.sent();
127
+ calldata = (0, transaction_1.fromCallsToExecuteCalldataWithNonce)(transactions, nonce);
128
+ return [4 /*yield*/, _super.prototype.getEstimateFee.call(this, { contractAddress: this.address, entrypoint: '__execute__', calldata: calldata, signature: signature }, blockIdentifier, { version: version })];
129
+ case 5:
130
+ response = _d.sent();
131
+ suggestedMaxFee = (0, stark_1.estimatedFeeToMaxFee)(response.overall_fee);
132
+ return [2 /*return*/, __assign(__assign({}, response), { suggestedMaxFee: suggestedMaxFee })];
133
+ }
134
+ });
135
+ });
136
+ };
137
+ /**
138
+ * Invoke execute function in account contract
139
+ *
140
+ * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/gateway/gateway_client.py#L13-L17)
141
+ *
142
+ * @param calls - one or more calls to be executed
143
+ * @param abis - one or more abis which can be used to display the calls
144
+ * @param transactionsDetail - optional transaction details
145
+ * @returns a confirmation of invoking a function on the starknet contract
146
+ */
147
+ Account.prototype.execute = function (calls, abis, transactionsDetail) {
148
+ var _a;
149
+ if (abis === void 0) { abis = undefined; }
150
+ if (transactionsDetail === void 0) { transactionsDetail = {}; }
151
+ return __awaiter(this, void 0, void 0, function () {
152
+ var transactions, nonce, _b, _c, maxFee, suggestedMaxFee, version, signerDetails, signature, calldata;
153
+ return __generator(this, function (_d) {
154
+ switch (_d.label) {
155
+ case 0:
156
+ transactions = Array.isArray(calls) ? calls : [calls];
157
+ _b = number_1.toBN;
158
+ if (!((_a = transactionsDetail.nonce) !== null && _a !== void 0)) return [3 /*break*/, 1];
159
+ _c = _a;
160
+ return [3 /*break*/, 3];
161
+ case 1: return [4 /*yield*/, this.getNonce()];
162
+ case 2:
163
+ _c = (_d.sent());
164
+ _d.label = 3;
165
+ case 3:
166
+ nonce = _b.apply(void 0, [_c]);
167
+ maxFee = '0';
168
+ if (!(transactionsDetail.maxFee || transactionsDetail.maxFee === 0)) return [3 /*break*/, 4];
169
+ maxFee = transactionsDetail.maxFee;
170
+ return [3 /*break*/, 6];
171
+ case 4: return [4 /*yield*/, this.estimateFee(transactions, { nonce: nonce })];
172
+ case 5:
173
+ suggestedMaxFee = (_d.sent()).suggestedMaxFee;
174
+ maxFee = suggestedMaxFee.toString();
175
+ _d.label = 6;
176
+ case 6:
177
+ version = (0, number_1.toBN)(hash_1.transactionVersion);
178
+ signerDetails = {
179
+ walletAddress: this.address,
180
+ nonce: nonce,
181
+ maxFee: maxFee,
182
+ version: version,
183
+ chainId: this.chainId,
184
+ };
185
+ return [4 /*yield*/, this.signer.signTransaction(transactions, signerDetails, abis)];
186
+ case 7:
187
+ signature = _d.sent();
188
+ calldata = (0, transaction_1.fromCallsToExecuteCalldataWithNonce)(transactions, nonce);
189
+ return [2 /*return*/, this.invokeFunction({ contractAddress: this.address, entrypoint: '__execute__', calldata: calldata, signature: signature }, {
190
+ maxFee: maxFee,
191
+ version: version,
192
+ })];
193
+ }
194
+ });
195
+ });
196
+ };
197
+ /**
198
+ * Sign an JSON object with the starknet private key and return the signature
199
+ *
200
+ * @param json - JSON object to be signed
201
+ * @returns the signature of the JSON object
202
+ * @throws {Error} if the JSON object is not a valid JSON
203
+ */
204
+ Account.prototype.signMessage = function (typedData) {
205
+ return __awaiter(this, void 0, void 0, function () {
206
+ return __generator(this, function (_a) {
207
+ return [2 /*return*/, this.signer.signMessage(typedData, this.address)];
208
+ });
209
+ });
210
+ };
211
+ /**
212
+ * Hash a JSON object with pederson hash and return the hash
213
+ *
214
+ * @param json - JSON object to be hashed
215
+ * @returns the hash of the JSON object
216
+ * @throws {Error} if the JSON object is not a valid JSON
217
+ */
218
+ Account.prototype.hashMessage = function (typedData) {
219
+ return __awaiter(this, void 0, void 0, function () {
220
+ return __generator(this, function (_a) {
221
+ return [2 /*return*/, (0, typedData_1.getMessageHash)(typedData, this.address)];
222
+ });
223
+ });
224
+ };
225
+ /**
226
+ * Verify a signature of a given hash
227
+ * @warning This method is not recommended, use verifyMessage instead
228
+ *
229
+ * @param hash - JSON object to be verified
230
+ * @param signature - signature of the JSON object
231
+ * @returns true if the signature is valid, false otherwise
232
+ * @throws {Error} if the JSON object is not a valid JSON or the signature is not a valid signature
233
+ */
234
+ Account.prototype.verifyMessageHash = function (hash, signature) {
235
+ return __awaiter(this, void 0, void 0, function () {
236
+ var _a;
237
+ return __generator(this, function (_b) {
238
+ switch (_b.label) {
239
+ case 0:
240
+ _b.trys.push([0, 2, , 3]);
241
+ return [4 /*yield*/, this.callContract({
242
+ contractAddress: this.address,
243
+ entrypoint: 'is_valid_signature',
244
+ calldata: (0, stark_1.compileCalldata)({
245
+ hash: (0, number_1.toBN)(hash).toString(),
246
+ signature: signature.map(function (x) { return (0, number_1.toBN)(x).toString(); }),
247
+ }),
248
+ })];
249
+ case 1:
250
+ _b.sent();
251
+ return [2 /*return*/, true];
252
+ case 2:
253
+ _a = _b.sent();
254
+ return [2 /*return*/, false];
255
+ case 3: return [2 /*return*/];
256
+ }
257
+ });
258
+ });
259
+ };
260
+ /**
261
+ * Verify a signature of a JSON object
262
+ *
263
+ * @param hash - hash to be verified
264
+ * @param signature - signature of the hash
265
+ * @returns true if the signature is valid, false otherwise
266
+ * @throws {Error} if the signature is not a valid signature
267
+ */
268
+ Account.prototype.verifyMessage = function (typedData, signature) {
269
+ return __awaiter(this, void 0, void 0, function () {
270
+ var hash;
271
+ return __generator(this, function (_a) {
272
+ switch (_a.label) {
273
+ case 0: return [4 /*yield*/, this.hashMessage(typedData)];
274
+ case 1:
275
+ hash = _a.sent();
276
+ return [2 /*return*/, this.verifyMessageHash(hash, signature)];
277
+ }
278
+ });
279
+ });
280
+ };
281
+ return Account;
282
+ }(default_1.Provider));
608
283
  exports.Account = Account;