starknet 3.8.0 → 3.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/CHANGELOG.md +35 -0
  2. package/README.md +18 -53
  3. package/__mocks__/ArgentAccount.json +32022 -38726
  4. package/__tests__/accountContract.test.ts +42 -32
  5. package/__tests__/contract.test.ts +20 -6
  6. package/__tests__/utils/__snapshots__/utils.browser.test.ts.snap +2 -2
  7. package/__tests__/utils/__snapshots__/utils.test.ts.snap +2 -2
  8. package/__tests__/utils/ellipticalCurve.test.ts +26 -8
  9. package/__tests__/utils/transactionHash.test.ts +17 -0
  10. package/__tests__/utils/utils.test.ts +10 -0
  11. package/account/default.d.ts +10 -6
  12. package/account/default.js +32 -39
  13. package/account/interface.d.ts +2 -0
  14. package/constants.d.ts +9 -0
  15. package/constants.js +13 -0
  16. package/contract/contractFactory.d.ts +5 -5
  17. package/contract/default.js +14 -2
  18. package/dist/account/default.d.ts +6 -6
  19. package/dist/account/default.js +32 -23
  20. package/dist/account/interface.d.ts +2 -0
  21. package/dist/constants.d.ts +9 -0
  22. package/dist/constants.js +12 -1
  23. package/dist/contract/contractFactory.d.ts +5 -5
  24. package/dist/contract/default.js +14 -2
  25. package/dist/index.d.ts +1 -0
  26. package/dist/index.js +2 -1
  27. package/dist/provider/default.d.ts +4 -1
  28. package/dist/provider/default.js +20 -1
  29. package/dist/provider/interface.d.ts +2 -0
  30. package/dist/signer/default.js +4 -2
  31. package/dist/signer/ledger.js +4 -2
  32. package/dist/types/api.d.ts +1 -0
  33. package/dist/types/lib.d.ts +1 -0
  34. package/dist/types/signer.d.ts +2 -0
  35. package/dist/utils/hash.d.ts +5 -3
  36. package/dist/utils/hash.js +25 -23
  37. package/dist/utils/stark.d.ts +3 -0
  38. package/dist/utils/stark.js +8 -1
  39. package/dist/utils/transaction.d.ts +2 -0
  40. package/dist/utils/transaction.js +5 -1
  41. package/dist/utils/typedData/index.d.ts +2 -2
  42. package/dist/utils/typedData/types.d.ts +3 -3
  43. package/dist/utils/typedData/utils.d.ts +1 -1
  44. package/index.d.ts +1 -0
  45. package/index.js +2 -0
  46. package/package.json +1 -1
  47. package/provider/default.d.ts +4 -1
  48. package/provider/default.js +21 -1
  49. package/provider/interface.d.ts +2 -0
  50. package/signer/default.js +12 -5
  51. package/signer/ledger.js +12 -5
  52. package/src/account/default.ts +38 -17
  53. package/src/account/interface.ts +3 -0
  54. package/src/constants.ts +10 -0
  55. package/src/contract/contractFactory.ts +5 -5
  56. package/src/contract/default.ts +13 -2
  57. package/src/index.ts +1 -0
  58. package/src/provider/default.ts +23 -2
  59. package/src/provider/interface.ts +3 -0
  60. package/src/signer/default.ts +10 -5
  61. package/src/signer/ledger.ts +10 -5
  62. package/src/types/api.ts +1 -0
  63. package/src/types/lib.ts +1 -0
  64. package/src/types/signer.ts +2 -0
  65. package/src/utils/hash.ts +70 -26
  66. package/src/utils/stark.ts +8 -1
  67. package/src/utils/transaction.ts +7 -0
  68. package/types/api.d.ts +1 -0
  69. package/types/lib.d.ts +1 -0
  70. package/types/signer.d.ts +2 -0
  71. package/utils/hash.d.ts +25 -7
  72. package/utils/hash.js +60 -26
  73. package/utils/stark.d.ts +4 -0
  74. package/utils/stark.js +13 -1
  75. package/utils/transaction.d.ts +5 -0
  76. package/utils/transaction.js +12 -1
  77. package/utils/typedData/index.d.ts +2 -2
  78. package/utils/typedData/types.d.ts +3 -3
  79. package/utils/typedData/utils.d.ts +1 -1
  80. package/__tests__/constancts.ts +0 -2
@@ -1,7 +1,15 @@
1
+ import { StarknetChainId } from '../../src/constants';
1
2
  import { ec, getKeyPair, getStarkKey, sign, verify } from '../../src/utils/ellipticCurve';
2
3
  import { removeHexPrefix } from '../../src/utils/encode';
3
- import { computeHashOnElements, hashMulticall, pedersen } from '../../src/utils/hash';
4
+ import {
5
+ calculcateTransactionHash,
6
+ computeHashOnElements,
7
+ getSelectorFromName,
8
+ pedersen,
9
+ transactionVersion,
10
+ } from '../../src/utils/hash';
4
11
  import { toBN, toHex } from '../../src/utils/number';
12
+ import { fromCallsToExecuteCalldataWithNonce } from '../../src/utils/transaction';
5
13
 
6
14
  test('getKeyPair()', () => {
7
15
  const privateKey = '0x019800ea6a9a73f94aee6a3d2edf018fc770443e90c7ba121e8303ec6b349279';
@@ -42,17 +50,27 @@ test('hashMessage()', () => {
42
50
  ];
43
51
  const nonce = '3';
44
52
  const maxFee = '0';
45
- const hashMsg = hashMulticall(account, transactions, nonce, maxFee);
46
- expect(hashMsg).toBe(
47
- toHex(toBN('1608351043472325350463069815257733118091727529101532499046754244230898025592'))
53
+ const calldata = fromCallsToExecuteCalldataWithNonce(transactions, nonce);
54
+
55
+ const hashMsg = calculcateTransactionHash(
56
+ account,
57
+ transactionVersion,
58
+ getSelectorFromName('__execute__'),
59
+ calldata,
60
+ maxFee,
61
+ StarknetChainId.TESTNET
62
+ );
63
+
64
+ expect(hashMsg).toMatchInlineSnapshot(
65
+ `"0x4c337c6bf32b2cf2b8ae54064e4b982c214660e8d0423b431a3fde10b9b9c02"`
48
66
  );
49
67
  const keyPair = getKeyPair(privateKey);
50
68
  const [r, s] = sign(keyPair, removeHexPrefix(hashMsg));
51
- expect(r.toString()).toStrictEqual(
52
- toBN('1079537730825246752292590270213864261175133133352510235773017189606850691611').toString()
69
+ expect(r.toString()).toMatchInlineSnapshot(
70
+ `"1944132633844378384908742523072599391732300777648030785844673145513474741467"`
53
71
  );
54
- expect(s.toString()).toStrictEqual(
55
- toBN('2904560423220491364719171767721067837294296476624248675613584621502231297000').toString()
72
+ expect(s.toString()).toMatchInlineSnapshot(
73
+ `"1067771353159635307522498807851959257107695451405842425488451092336556917559"`
56
74
  );
57
75
  });
58
76
 
@@ -0,0 +1,17 @@
1
+ import { StarknetChainId, TransactionHashPrefix } from '../../src/constants';
2
+ import { calculateTransactionHashCommon } from '../../src/utils/hash';
3
+
4
+ describe('calculateTransactionHashCommon()', () => {
5
+ test('should match most simple python output', () => {
6
+ const result = calculateTransactionHashCommon(
7
+ TransactionHashPrefix.INVOKE,
8
+ '0x0',
9
+ '0x2a',
10
+ '0x64',
11
+ [],
12
+ '0x0',
13
+ StarknetChainId.TESTNET
14
+ );
15
+ expect(result).toBe('0x7d260744de9d8c55e7675a34512d1951a7b262c79e685d26599edd2948de959');
16
+ });
17
+ });
@@ -77,3 +77,13 @@ describe('computeHashOnElements()', () => {
77
77
  );
78
78
  });
79
79
  });
80
+ describe('estimatedFeeToMaxFee()', () => {
81
+ test('should return maxFee for 0', () => {
82
+ const res = stark.estimatedFeeToMaxFee(0, 0.15).toNumber();
83
+ expect(res).toBe(0);
84
+ });
85
+ test('should return maxFee for 10_000', () => {
86
+ const res = stark.estimatedFeeToMaxFee(10_000, 0.15).toNumber();
87
+ expect(res).toBe(11_500);
88
+ });
89
+ });
@@ -1,4 +1,4 @@
1
- import { Provider } from '../provider';
1
+ import { Provider, ProviderInterface } from '../provider';
2
2
  import { BlockIdentifier } from '../provider/utils';
3
3
  import { SignerInterface } from '../signer';
4
4
  import {
@@ -16,8 +16,12 @@ import { TypedData } from '../utils/typedData';
16
16
  import { AccountInterface } from './interface';
17
17
  export declare class Account extends Provider implements AccountInterface {
18
18
  address: string;
19
- private signer;
20
- constructor(provider: Provider, address: string, keyPairOrSigner: KeyPair | SignerInterface);
19
+ signer: SignerInterface;
20
+ constructor(
21
+ provider: ProviderInterface,
22
+ address: string,
23
+ keyPairOrSigner: KeyPair | SignerInterface
24
+ );
21
25
  getNonce(): Promise<string>;
22
26
  estimateFee(
23
27
  calls: Call | Call[],
@@ -64,7 +68,8 @@ export declare class Account extends Provider implements AccountInterface {
64
68
  */
65
69
  hashMessage(typedData: TypedData): Promise<string>;
66
70
  /**
67
- * Verify a signature of a JSON object
71
+ * Verify a signature of a given hash
72
+ * @warning This method is not recommended, use verifyMessage instead
68
73
  *
69
74
  * @param hash - JSON object to be verified
70
75
  * @param signature - signature of the JSON object
@@ -73,8 +78,7 @@ export declare class Account extends Provider implements AccountInterface {
73
78
  */
74
79
  verifyMessageHash(hash: BigNumberish, signature: Signature): Promise<boolean>;
75
80
  /**
76
- * Verify a signature of a given hash
77
- * @warning This method is not recommended, use verifyMessage instead
81
+ * Verify a signature of a JSON object
78
82
  *
79
83
  * @param hash - hash to be verified
80
84
  * @param signature - signature of the hash
@@ -197,11 +197,13 @@ var __importDefault =
197
197
  Object.defineProperty(exports, '__esModule', { value: true });
198
198
  exports.Account = void 0;
199
199
  var minimalistic_assert_1 = __importDefault(require('minimalistic-assert'));
200
+ var constants_1 = require('../constants');
200
201
  var provider_1 = require('../provider');
201
202
  var signer_1 = require('../signer');
202
203
  var ellipticCurve_1 = require('../utils/ellipticCurve');
203
204
  var hash_1 = require('../utils/hash');
204
205
  var number_1 = require('../utils/number');
206
+ var shortString_1 = require('../utils/shortString');
205
207
  var stark_1 = require('../utils/stark');
206
208
  var transaction_1 = require('../utils/transaction');
207
209
  var typedData_1 = require('../utils/typedData');
@@ -240,7 +242,7 @@ var Account = /** @class */ (function (_super) {
240
242
  _c = _b.blockIdentifier,
241
243
  blockIdentifier = _c === void 0 ? 'pending' : _c;
242
244
  return __awaiter(this, void 0, void 0, function () {
243
- var transactions, nonce, _d, signerDetails, signature, calldata;
245
+ var transactions, nonce, _d, version, signerDetails, signature, calldata;
244
246
  return __generator(this, function (_e) {
245
247
  switch (_e.label) {
246
248
  case 0:
@@ -255,23 +257,18 @@ var Account = /** @class */ (function (_super) {
255
257
  _e.label = 3;
256
258
  case 3:
257
259
  nonce = _d;
260
+ version = (0, number_1.toBN)(hash_1.feeTransactionVersion);
258
261
  signerDetails = {
259
262
  walletAddress: this.address,
260
263
  nonce: (0, number_1.toBN)(nonce),
261
- maxFee: (0, number_1.toBN)('0'),
264
+ maxFee: constants_1.ZERO,
265
+ version: version,
266
+ chainId: this.chainId,
262
267
  };
263
268
  return [4 /*yield*/, this.signer.signTransaction(transactions, signerDetails)];
264
269
  case 4:
265
270
  signature = _e.sent();
266
- calldata = __spreadArray(
267
- __spreadArray(
268
- [],
269
- __read((0, transaction_1.fromCallsToExecuteCalldata)(transactions)),
270
- false
271
- ),
272
- [signerDetails.nonce.toString()],
273
- false
274
- );
271
+ calldata = (0, transaction_1.fromCallsToExecuteCalldataWithNonce)(transactions, nonce);
275
272
  return [
276
273
  2 /*return*/,
277
274
  this.fetchEndpoint(
@@ -281,6 +278,7 @@ var Account = /** @class */ (function (_super) {
281
278
  contract_address: this.address,
282
279
  entry_point_selector: (0, hash_1.getSelectorFromName)('__execute__'),
283
280
  calldata: calldata,
281
+ version: (0, number_1.toHex)(version),
284
282
  signature: (0, number_1.bigNumberishArrayToDecimalStringArray)(signature),
285
283
  }
286
284
  ),
@@ -298,7 +296,7 @@ var Account = /** @class */ (function (_super) {
298
296
  * @returns a confirmation of invoking a function on the starknet contract
299
297
  */
300
298
  Account.prototype.execute = function (calls, abis, transactionsDetail) {
301
- var _a, _b;
299
+ var _a;
302
300
  if (abis === void 0) {
303
301
  abis = undefined;
304
302
  }
@@ -306,51 +304,46 @@ var Account = /** @class */ (function (_super) {
306
304
  transactionsDetail = {};
307
305
  }
308
306
  return __awaiter(this, void 0, void 0, function () {
309
- var transactions, nonce, _c, _d, maxFee, _e, signerDetails, signature, calldata;
310
- return __generator(this, function (_f) {
311
- switch (_f.label) {
307
+ var transactions, nonce, _b, _c, maxFee, estimatedFee, signerDetails, signature, calldata;
308
+ return __generator(this, function (_d) {
309
+ switch (_d.label) {
312
310
  case 0:
313
311
  transactions = Array.isArray(calls) ? calls : [calls];
314
- _c = number_1.toBN;
312
+ _b = number_1.toBN;
315
313
  if (!((_a = transactionsDetail.nonce) !== null && _a !== void 0))
316
314
  return [3 /*break*/, 1];
317
- _d = _a;
315
+ _c = _a;
318
316
  return [3 /*break*/, 3];
319
317
  case 1:
320
318
  return [4 /*yield*/, this.getNonce()];
321
319
  case 2:
322
- _d = _f.sent();
323
- _f.label = 3;
320
+ _c = _d.sent();
321
+ _d.label = 3;
324
322
  case 3:
325
- nonce = _c.apply(void 0, [_d]);
326
- if (!((_b = transactionsDetail.maxFee) !== null && _b !== void 0))
323
+ nonce = _b.apply(void 0, [_c]);
324
+ maxFee = '0';
325
+ if (!(transactionsDetail.maxFee || transactionsDetail.maxFee === 0))
327
326
  return [3 /*break*/, 4];
328
- _e = _b;
327
+ maxFee = transactionsDetail.maxFee;
329
328
  return [3 /*break*/, 6];
330
329
  case 4:
331
330
  return [4 /*yield*/, this.estimateFee(transactions, { nonce: nonce })];
332
331
  case 5:
333
- _e = _f.sent().amount;
334
- _f.label = 6;
332
+ estimatedFee = _d.sent().amount;
333
+ maxFee = (0, stark_1.estimatedFeeToMaxFee)(estimatedFee).toString();
334
+ _d.label = 6;
335
335
  case 6:
336
- maxFee = _e;
337
336
  signerDetails = {
338
337
  walletAddress: this.address,
339
338
  nonce: nonce,
340
339
  maxFee: maxFee,
340
+ version: (0, number_1.toBN)(hash_1.transactionVersion),
341
+ chainId: this.chainId,
341
342
  };
342
343
  return [4 /*yield*/, this.signer.signTransaction(transactions, signerDetails, abis)];
343
344
  case 7:
344
- signature = _f.sent();
345
- calldata = __spreadArray(
346
- __spreadArray(
347
- [],
348
- __read((0, transaction_1.fromCallsToExecuteCalldata)(transactions)),
349
- false
350
- ),
351
- [signerDetails.nonce.toString()],
352
- false
353
- );
345
+ signature = _d.sent();
346
+ calldata = (0, transaction_1.fromCallsToExecuteCalldataWithNonce)(transactions, nonce);
354
347
  return [
355
348
  2 /*return*/,
356
349
  this.fetchEndpoint('add_transaction', undefined, {
@@ -387,7 +380,7 @@ var Account = /** @class */ (function (_super) {
387
380
  .map(number_1.bigNumberishArrayToDecimalStringArray)
388
381
  .map(hash_1.computeHashOnElements);
389
382
  return (0,
390
- hash_1.computeHashOnElements)([hash_1.transactionPrefix, account, (0, hash_1.computeHashOnElements)(hashArray), nonce, maxFee, hash_1.transactionVersion]);
383
+ hash_1.computeHashOnElements)([(0, shortString_1.encodeShortString)('StarkNet Transaction'), account, (0, hash_1.computeHashOnElements)(hashArray), nonce, maxFee, hash_1.transactionVersion]);
391
384
  }
392
385
  var nonceBn,
393
386
  result,
@@ -517,7 +510,8 @@ var Account = /** @class */ (function (_super) {
517
510
  });
518
511
  };
519
512
  /**
520
- * Verify a signature of a JSON object
513
+ * Verify a signature of a given hash
514
+ * @warning This method is not recommended, use verifyMessage instead
521
515
  *
522
516
  * @param hash - JSON object to be verified
523
517
  * @param signature - signature of the JSON object
@@ -557,8 +551,7 @@ var Account = /** @class */ (function (_super) {
557
551
  });
558
552
  };
559
553
  /**
560
- * Verify a signature of a given hash
561
- * @warning This method is not recommended, use verifyMessage instead
554
+ * Verify a signature of a JSON object
562
555
  *
563
556
  * @param hash - hash to be verified
564
557
  * @param signature - signature of the hash
@@ -1,4 +1,5 @@
1
1
  import { ProviderInterface } from '../provider';
2
+ import { SignerInterface } from '../signer';
2
3
  import {
3
4
  Abi,
4
5
  AddTransactionResponse,
@@ -13,6 +14,7 @@ import { BigNumberish } from '../utils/number';
13
14
  import { TypedData } from '../utils/typedData/types';
14
15
  export declare abstract class AccountInterface extends ProviderInterface {
15
16
  abstract address: string;
17
+ abstract signer: SignerInterface;
16
18
  /**
17
19
  * Deploys a given compiled contract (json) to starknet
18
20
  *
package/constants.d.ts CHANGED
@@ -5,6 +5,15 @@ export declare const ONE: import('bn.js');
5
5
  export declare const TWO: import('bn.js');
6
6
  export declare const MASK_250: import('bn.js');
7
7
  export declare const MASK_251: import('bn.js');
8
+ export declare enum StarknetChainId {
9
+ MAINNET = '0x534e5f4d41494e',
10
+ TESTNET = '0x534e5f474f45524c49',
11
+ }
12
+ export declare enum TransactionHashPrefix {
13
+ DEPLOY = '0x6465706c6f79',
14
+ INVOKE = '0x696e766f6b65',
15
+ L1_HANDLER = '0x6c315f68616e646c6572',
16
+ }
8
17
  /**
9
18
  * The following is taken from https://github.com/starkware-libs/starkex-resources/blob/master/crypto/starkware/crypto/signature/pedersen_params.json but converted to hex, because JS is very bad handling big integers by default
10
19
  * Please do not edit until the JSON changes.
package/constants.js CHANGED
@@ -8,6 +8,8 @@ exports.CONSTANT_POINTS =
8
8
  exports.FIELD_SIZE =
9
9
  exports.FIELD_GEN =
10
10
  exports.FIELD_PRIME =
11
+ exports.TransactionHashPrefix =
12
+ exports.StarknetChainId =
11
13
  exports.MASK_251 =
12
14
  exports.MASK_250 =
13
15
  exports.TWO =
@@ -28,6 +30,17 @@ exports.ONE = (0, number_1.toBN)(1);
28
30
  exports.TWO = (0, number_1.toBN)(2);
29
31
  exports.MASK_250 = exports.TWO.pow((0, number_1.toBN)(250)).sub(exports.ONE); // 2 ** 250 - 1
30
32
  exports.MASK_251 = exports.TWO.pow((0, number_1.toBN)(251));
33
+ var StarknetChainId;
34
+ (function (StarknetChainId) {
35
+ StarknetChainId['MAINNET'] = '0x534e5f4d41494e';
36
+ StarknetChainId['TESTNET'] = '0x534e5f474f45524c49';
37
+ })((StarknetChainId = exports.StarknetChainId || (exports.StarknetChainId = {})));
38
+ var TransactionHashPrefix;
39
+ (function (TransactionHashPrefix) {
40
+ TransactionHashPrefix['DEPLOY'] = '0x6465706c6f79';
41
+ TransactionHashPrefix['INVOKE'] = '0x696e766f6b65';
42
+ TransactionHashPrefix['L1_HANDLER'] = '0x6c315f68616e646c6572';
43
+ })((TransactionHashPrefix = exports.TransactionHashPrefix || (exports.TransactionHashPrefix = {})));
31
44
  /**
32
45
  * The following is taken from https://github.com/starkware-libs/starkex-resources/blob/master/crypto/starkware/crypto/signature/pedersen_params.json but converted to hex, because JS is very bad handling big integers by default
33
46
  * Please do not edit until the JSON changes.
@@ -1,15 +1,15 @@
1
- import { Account } from '../account';
2
- import { Provider } from '../provider';
1
+ import { AccountInterface } from '../account';
2
+ import { ProviderInterface } from '../provider';
3
3
  import { Abi, CompiledContract, RawCalldata } from '../types';
4
4
  import { BigNumberish } from '../utils/number';
5
5
  import { Contract } from './default';
6
6
  export declare class ContractFactory {
7
7
  abi: Abi;
8
8
  compiledContract: CompiledContract;
9
- providerOrAccount: Provider | Account;
9
+ providerOrAccount: ProviderInterface | AccountInterface;
10
10
  constructor(
11
11
  compiledContract: CompiledContract,
12
- providerOrAccount?: Provider | Account,
12
+ providerOrAccount?: ProviderInterface | AccountInterface,
13
13
  abi?: Abi
14
14
  );
15
15
  /**
@@ -25,7 +25,7 @@ export declare class ContractFactory {
25
25
  *
26
26
  * @param providerOrAccount - new Provider or Account to attach to
27
27
  */
28
- connect(providerOrAccount: Provider | Account): ContractFactory;
28
+ connect(providerOrAccount: ProviderInterface | AccountInterface): ContractFactory;
29
29
  /**
30
30
  * Attaches current abi and provider or account to the new address
31
31
  *
@@ -224,8 +224,20 @@ function buildInvoke(contract, functionAbi) {
224
224
  args[_i] = arguments[_i];
225
225
  }
226
226
  return __awaiter(this, void 0, void 0, function () {
227
+ var inputs, inputsLength, options;
227
228
  return __generator(this, function (_a) {
228
- return [2 /*return*/, contract.invoke(functionAbi.name, args)];
229
+ inputs = functionAbi.inputs;
230
+ inputsLength = inputs.reduce(function (acc, input) {
231
+ if (!/_len$/.test(input.name)) {
232
+ return acc + 1;
233
+ }
234
+ return acc;
235
+ }, 0);
236
+ options = {};
237
+ if (inputsLength + 1 === args.length && typeof args[args.length - 1] === 'object') {
238
+ Object.assign(options, args.pop());
239
+ }
240
+ return [2 /*return*/, contract.invoke(functionAbi.name, args, options)];
229
241
  });
230
242
  });
231
243
  };
@@ -510,7 +522,7 @@ var Contract = /** @class */ (function () {
510
522
  if (member.type === 'felt') {
511
523
  return acc + 1;
512
524
  }
513
- return acc + _this.structMemberNum(member.type);
525
+ return acc + _this.calculateStructMembers(member.type);
514
526
  }, 0);
515
527
  };
516
528
  /**
@@ -1,4 +1,4 @@
1
- import { Provider } from '../provider';
1
+ import { Provider, ProviderInterface } from '../provider';
2
2
  import { BlockIdentifier } from '../provider/utils';
3
3
  import { SignerInterface } from '../signer';
4
4
  import { Abi, AddTransactionResponse, Call, EstimateFeeResponse, InvocationsDetails, KeyPair, Signature, Transaction } from '../types';
@@ -7,8 +7,8 @@ import { TypedData } from '../utils/typedData';
7
7
  import { AccountInterface } from './interface';
8
8
  export declare class Account extends Provider implements AccountInterface {
9
9
  address: string;
10
- private signer;
11
- constructor(provider: Provider, address: string, keyPairOrSigner: KeyPair | SignerInterface);
10
+ signer: SignerInterface;
11
+ constructor(provider: ProviderInterface, address: string, keyPairOrSigner: KeyPair | SignerInterface);
12
12
  getNonce(): Promise<string>;
13
13
  estimateFee(calls: Call | Call[], { nonce: providedNonce, blockIdentifier, }?: {
14
14
  nonce?: BigNumberish;
@@ -45,7 +45,8 @@ export declare class Account extends Provider implements AccountInterface {
45
45
  */
46
46
  hashMessage(typedData: TypedData): Promise<string>;
47
47
  /**
48
- * Verify a signature of a JSON object
48
+ * Verify a signature of a given hash
49
+ * @warning This method is not recommended, use verifyMessage instead
49
50
  *
50
51
  * @param hash - JSON object to be verified
51
52
  * @param signature - signature of the JSON object
@@ -54,8 +55,7 @@ export declare class Account extends Provider implements AccountInterface {
54
55
  */
55
56
  verifyMessageHash(hash: BigNumberish, signature: Signature): Promise<boolean>;
56
57
  /**
57
- * Verify a signature of a given hash
58
- * @warning This method is not recommended, use verifyMessage instead
58
+ * Verify a signature of a JSON object
59
59
  *
60
60
  * @param hash - hash to be verified
61
61
  * @param signature - signature of the hash
@@ -81,11 +81,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
81
81
  Object.defineProperty(exports, "__esModule", { value: true });
82
82
  exports.Account = void 0;
83
83
  var minimalistic_assert_1 = __importDefault(require("minimalistic-assert"));
84
+ var constants_1 = require("../constants");
84
85
  var provider_1 = require("../provider");
85
86
  var signer_1 = require("../signer");
86
87
  var ellipticCurve_1 = require("../utils/ellipticCurve");
87
88
  var hash_1 = require("../utils/hash");
88
89
  var number_1 = require("../utils/number");
90
+ var shortString_1 = require("../utils/shortString");
89
91
  var stark_1 = require("../utils/stark");
90
92
  var transaction_1 = require("../utils/transaction");
91
93
  var typedData_1 = require("../utils/typedData");
@@ -117,7 +119,7 @@ var Account = /** @class */ (function (_super) {
117
119
  Account.prototype.estimateFee = function (calls, _a) {
118
120
  var _b = _a === void 0 ? {} : _a, providedNonce = _b.nonce, _c = _b.blockIdentifier, blockIdentifier = _c === void 0 ? 'pending' : _c;
119
121
  return __awaiter(this, void 0, void 0, function () {
120
- var transactions, nonce, _d, signerDetails, signature, calldata;
122
+ var transactions, nonce, _d, version, signerDetails, signature, calldata;
121
123
  return __generator(this, function (_e) {
122
124
  switch (_e.label) {
123
125
  case 0:
@@ -131,19 +133,23 @@ var Account = /** @class */ (function (_super) {
131
133
  _e.label = 3;
132
134
  case 3:
133
135
  nonce = _d;
136
+ version = (0, number_1.toBN)(hash_1.feeTransactionVersion);
134
137
  signerDetails = {
135
138
  walletAddress: this.address,
136
139
  nonce: (0, number_1.toBN)(nonce),
137
- maxFee: (0, number_1.toBN)('0'),
140
+ maxFee: constants_1.ZERO,
141
+ version: version,
142
+ chainId: this.chainId,
138
143
  };
139
144
  return [4 /*yield*/, this.signer.signTransaction(transactions, signerDetails)];
140
145
  case 4:
141
146
  signature = _e.sent();
142
- calldata = __spreadArray(__spreadArray([], __read((0, transaction_1.fromCallsToExecuteCalldata)(transactions)), false), [signerDetails.nonce.toString()], false);
147
+ calldata = (0, transaction_1.fromCallsToExecuteCalldataWithNonce)(transactions, nonce);
143
148
  return [2 /*return*/, this.fetchEndpoint('estimate_fee', { blockIdentifier: blockIdentifier }, {
144
149
  contract_address: this.address,
145
150
  entry_point_selector: (0, hash_1.getSelectorFromName)('__execute__'),
146
151
  calldata: calldata,
152
+ version: (0, number_1.toHex)(version),
147
153
  signature: (0, number_1.bigNumberishArrayToDecimalStringArray)(signature),
148
154
  })];
149
155
  }
@@ -159,43 +165,46 @@ var Account = /** @class */ (function (_super) {
159
165
  * @returns a confirmation of invoking a function on the starknet contract
160
166
  */
161
167
  Account.prototype.execute = function (calls, abis, transactionsDetail) {
162
- var _a, _b;
168
+ var _a;
163
169
  if (abis === void 0) { abis = undefined; }
164
170
  if (transactionsDetail === void 0) { transactionsDetail = {}; }
165
171
  return __awaiter(this, void 0, void 0, function () {
166
- var transactions, nonce, _c, _d, maxFee, _e, signerDetails, signature, calldata;
167
- return __generator(this, function (_f) {
168
- switch (_f.label) {
172
+ var transactions, nonce, _b, _c, maxFee, estimatedFee, signerDetails, signature, calldata;
173
+ return __generator(this, function (_d) {
174
+ switch (_d.label) {
169
175
  case 0:
170
176
  transactions = Array.isArray(calls) ? calls : [calls];
171
- _c = number_1.toBN;
177
+ _b = number_1.toBN;
172
178
  if (!((_a = transactionsDetail.nonce) !== null && _a !== void 0)) return [3 /*break*/, 1];
173
- _d = _a;
179
+ _c = _a;
174
180
  return [3 /*break*/, 3];
175
181
  case 1: return [4 /*yield*/, this.getNonce()];
176
182
  case 2:
177
- _d = (_f.sent());
178
- _f.label = 3;
183
+ _c = (_d.sent());
184
+ _d.label = 3;
179
185
  case 3:
180
- nonce = _c.apply(void 0, [_d]);
181
- if (!((_b = transactionsDetail.maxFee) !== null && _b !== void 0)) return [3 /*break*/, 4];
182
- _e = _b;
186
+ nonce = _b.apply(void 0, [_c]);
187
+ maxFee = '0';
188
+ if (!(transactionsDetail.maxFee || transactionsDetail.maxFee === 0)) return [3 /*break*/, 4];
189
+ maxFee = transactionsDetail.maxFee;
183
190
  return [3 /*break*/, 6];
184
191
  case 4: return [4 /*yield*/, this.estimateFee(transactions, { nonce: nonce })];
185
192
  case 5:
186
- _e = (_f.sent()).amount;
187
- _f.label = 6;
193
+ estimatedFee = (_d.sent()).amount;
194
+ maxFee = (0, stark_1.estimatedFeeToMaxFee)(estimatedFee).toString();
195
+ _d.label = 6;
188
196
  case 6:
189
- maxFee = _e;
190
197
  signerDetails = {
191
198
  walletAddress: this.address,
192
199
  nonce: nonce,
193
200
  maxFee: maxFee,
201
+ version: (0, number_1.toBN)(hash_1.transactionVersion),
202
+ chainId: this.chainId,
194
203
  };
195
204
  return [4 /*yield*/, this.signer.signTransaction(transactions, signerDetails, abis)];
196
205
  case 7:
197
- signature = _f.sent();
198
- calldata = __spreadArray(__spreadArray([], __read((0, transaction_1.fromCallsToExecuteCalldata)(transactions)), false), [signerDetails.nonce.toString()], false);
206
+ signature = _d.sent();
207
+ calldata = (0, transaction_1.fromCallsToExecuteCalldataWithNonce)(transactions, nonce);
199
208
  return [2 /*return*/, this.fetchEndpoint('add_transaction', undefined, {
200
209
  type: 'INVOKE_FUNCTION',
201
210
  contract_address: this.address,
@@ -227,7 +236,7 @@ var Account = /** @class */ (function (_super) {
227
236
  .map(number_1.bigNumberishArrayToDecimalStringArray)
228
237
  .map(hash_1.computeHashOnElements);
229
238
  return (0, hash_1.computeHashOnElements)([
230
- hash_1.transactionPrefix,
239
+ (0, shortString_1.encodeShortString)('StarkNet Transaction'),
231
240
  account,
232
241
  (0, hash_1.computeHashOnElements)(hashArray),
233
242
  nonce,
@@ -331,7 +340,8 @@ var Account = /** @class */ (function (_super) {
331
340
  });
332
341
  };
333
342
  /**
334
- * Verify a signature of a JSON object
343
+ * Verify a signature of a given hash
344
+ * @warning This method is not recommended, use verifyMessage instead
335
345
  *
336
346
  * @param hash - JSON object to be verified
337
347
  * @param signature - signature of the JSON object
@@ -365,8 +375,7 @@ var Account = /** @class */ (function (_super) {
365
375
  });
366
376
  };
367
377
  /**
368
- * Verify a signature of a given hash
369
- * @warning This method is not recommended, use verifyMessage instead
378
+ * Verify a signature of a JSON object
370
379
  *
371
380
  * @param hash - hash to be verified
372
381
  * @param signature - signature of the hash
@@ -1,9 +1,11 @@
1
1
  import { ProviderInterface } from '../provider';
2
+ import { SignerInterface } from '../signer';
2
3
  import { Abi, AddTransactionResponse, Call, DeployContractPayload, EstimateFeeResponse, Invocation, InvocationsDetails, Signature } from '../types';
3
4
  import { BigNumberish } from '../utils/number';
4
5
  import { TypedData } from '../utils/typedData/types';
5
6
  export declare abstract class AccountInterface extends ProviderInterface {
6
7
  abstract address: string;
8
+ abstract signer: SignerInterface;
7
9
  /**
8
10
  * Deploys a given compiled contract (json) to starknet
9
11
  *
@@ -5,6 +5,15 @@ export declare const ONE: import("bn.js");
5
5
  export declare const TWO: import("bn.js");
6
6
  export declare const MASK_250: import("bn.js");
7
7
  export declare const MASK_251: import("bn.js");
8
+ export declare enum StarknetChainId {
9
+ MAINNET = "0x534e5f4d41494e",
10
+ TESTNET = "0x534e5f474f45524c49"
11
+ }
12
+ export declare enum TransactionHashPrefix {
13
+ DEPLOY = "0x6465706c6f79",
14
+ INVOKE = "0x696e766f6b65",
15
+ L1_HANDLER = "0x6c315f68616e646c6572"
16
+ }
8
17
  /**
9
18
  * The following is taken from https://github.com/starkware-libs/starkex-resources/blob/master/crypto/starkware/crypto/signature/pedersen_params.json but converted to hex, because JS is very bad handling big integers by default
10
19
  * Please do not edit until the JSON changes.
package/dist/constants.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CONSTANT_POINTS = exports.MAX_ECDSA_VAL = exports.BETA = exports.ALPHA = exports.EC_ORDER = exports.FIELD_SIZE = exports.FIELD_GEN = exports.FIELD_PRIME = exports.MASK_251 = exports.MASK_250 = exports.TWO = exports.ONE = exports.ZERO = exports.IS_BROWSER = void 0;
3
+ exports.CONSTANT_POINTS = exports.MAX_ECDSA_VAL = exports.BETA = exports.ALPHA = exports.EC_ORDER = exports.FIELD_SIZE = exports.FIELD_GEN = exports.FIELD_PRIME = exports.TransactionHashPrefix = exports.StarknetChainId = exports.MASK_251 = exports.MASK_250 = exports.TWO = exports.ONE = exports.ZERO = exports.IS_BROWSER = void 0;
4
4
  var number_1 = require("./utils/number");
5
5
  var encode_1 = require("./utils/encode");
6
6
  Object.defineProperty(exports, "IS_BROWSER", { enumerable: true, get: function () { return encode_1.IS_BROWSER; } });
@@ -9,6 +9,17 @@ exports.ONE = (0, number_1.toBN)(1);
9
9
  exports.TWO = (0, number_1.toBN)(2);
10
10
  exports.MASK_250 = exports.TWO.pow((0, number_1.toBN)(250)).sub(exports.ONE); // 2 ** 250 - 1
11
11
  exports.MASK_251 = exports.TWO.pow((0, number_1.toBN)(251));
12
+ var StarknetChainId;
13
+ (function (StarknetChainId) {
14
+ StarknetChainId["MAINNET"] = "0x534e5f4d41494e";
15
+ StarknetChainId["TESTNET"] = "0x534e5f474f45524c49";
16
+ })(StarknetChainId = exports.StarknetChainId || (exports.StarknetChainId = {}));
17
+ var TransactionHashPrefix;
18
+ (function (TransactionHashPrefix) {
19
+ TransactionHashPrefix["DEPLOY"] = "0x6465706c6f79";
20
+ TransactionHashPrefix["INVOKE"] = "0x696e766f6b65";
21
+ TransactionHashPrefix["L1_HANDLER"] = "0x6c315f68616e646c6572";
22
+ })(TransactionHashPrefix = exports.TransactionHashPrefix || (exports.TransactionHashPrefix = {}));
12
23
  /**
13
24
  * The following is taken from https://github.com/starkware-libs/starkex-resources/blob/master/crypto/starkware/crypto/signature/pedersen_params.json but converted to hex, because JS is very bad handling big integers by default
14
25
  * Please do not edit until the JSON changes.