starknet 3.7.0 → 3.10.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 (79) hide show
  1. package/CHANGELOG.md +38 -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 +11 -1
  12. package/account/default.js +58 -50
  13. package/constants.d.ts +9 -0
  14. package/constants.js +13 -0
  15. package/contract/default.d.ts +12 -2
  16. package/contract/default.js +27 -20
  17. package/contract/interface.d.ts +21 -3
  18. package/dist/account/default.d.ts +5 -1
  19. package/dist/account/default.js +46 -30
  20. package/dist/constants.d.ts +9 -0
  21. package/dist/constants.js +12 -1
  22. package/dist/contract/default.d.ts +6 -3
  23. package/dist/contract/default.js +23 -20
  24. package/dist/contract/interface.d.ts +9 -4
  25. package/dist/provider/default.d.ts +5 -2
  26. package/dist/provider/default.js +36 -19
  27. package/dist/provider/interface.d.ts +2 -0
  28. package/dist/provider/utils.d.ts +1 -2
  29. package/dist/provider/utils.js +7 -8
  30. package/dist/signer/default.js +4 -2
  31. package/dist/signer/ledger.js +4 -2
  32. package/dist/types/api.d.ts +4 -1
  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/package.json +1 -1
  45. package/provider/default.d.ts +7 -2
  46. package/provider/default.js +49 -27
  47. package/provider/interface.d.ts +2 -0
  48. package/provider/utils.d.ts +1 -2
  49. package/provider/utils.js +7 -8
  50. package/signer/default.js +12 -5
  51. package/signer/ledger.js +12 -5
  52. package/src/account/default.ts +47 -18
  53. package/src/constants.ts +10 -0
  54. package/src/contract/default.ts +32 -19
  55. package/src/contract/interface.ts +21 -3
  56. package/src/provider/default.ts +37 -12
  57. package/src/provider/interface.ts +3 -0
  58. package/src/provider/utils.ts +7 -8
  59. package/src/signer/default.ts +10 -5
  60. package/src/signer/ledger.ts +10 -5
  61. package/src/types/api.ts +4 -1
  62. package/src/types/lib.ts +1 -0
  63. package/src/types/signer.ts +2 -0
  64. package/src/utils/hash.ts +70 -26
  65. package/src/utils/stark.ts +8 -1
  66. package/src/utils/transaction.ts +7 -0
  67. package/types/api.d.ts +4 -1
  68. package/types/lib.d.ts +1 -0
  69. package/types/signer.d.ts +2 -0
  70. package/utils/hash.d.ts +25 -7
  71. package/utils/hash.js +60 -26
  72. package/utils/stark.d.ts +4 -0
  73. package/utils/stark.js +13 -1
  74. package/utils/transaction.d.ts +5 -0
  75. package/utils/transaction.js +12 -1
  76. package/utils/typedData/index.d.ts +2 -2
  77. package/utils/typedData/types.d.ts +3 -3
  78. package/utils/typedData/utils.d.ts +1 -1
  79. package/__tests__/constancts.ts +0 -2
@@ -1,10 +1,11 @@
1
+ import BN from 'bn.js';
1
2
  import { gzip } from 'pako';
2
3
 
3
4
  import { Calldata, CompressedProgram, Program, RawArgs, Signature } from '../types';
4
5
  import { genKeyPair, getStarkKey } from './ellipticCurve';
5
6
  import { addHexPrefix, btoaUniversal } from './encode';
6
7
  import { stringify } from './json';
7
- import { toBN } from './number';
8
+ import { BigNumberish, toBN } from './number';
8
9
 
9
10
  /**
10
11
  * Function to compress compiled cairo program
@@ -48,3 +49,9 @@ export function compileCalldata(args: RawArgs): Calldata {
48
49
  return toBN(value).toString();
49
50
  });
50
51
  }
52
+
53
+ export function estimatedFeeToMaxFee(estimatedFee: BigNumberish, overhead: number = 0.15): BN {
54
+ // BN can only handle Integers, so we need to do all calulations with integers
55
+ const overHeadPercent = Math.round((1 + overhead) * 100);
56
+ return toBN(estimatedFee).mul(toBN(overHeadPercent)).div(toBN(100));
57
+ }
@@ -47,3 +47,10 @@ export const fromCallsToExecuteCalldata = (calls: Call[]): string[] => {
47
47
  ...calldata,
48
48
  ];
49
49
  };
50
+
51
+ export const fromCallsToExecuteCalldataWithNonce = (
52
+ calls: Call[],
53
+ nonce: BigNumberish
54
+ ): string[] => {
55
+ return [...fromCallsToExecuteCalldata(calls), toBN(nonce).toString()];
56
+ };
package/types/api.d.ts CHANGED
@@ -74,7 +74,9 @@ export declare type Endpoints = {
74
74
  RESPONSE: CallContractResponse;
75
75
  };
76
76
  estimate_fee: {
77
- QUERY: never;
77
+ QUERY: {
78
+ blockIdentifier: BlockIdentifier;
79
+ };
78
80
  REQUEST: CallContractTransaction;
79
81
  RESPONSE: EstimateFeeResponse;
80
82
  };
@@ -99,6 +101,7 @@ export declare type InvokeFunctionTransaction = {
99
101
  calldata?: RawCalldata;
100
102
  nonce?: BigNumberish;
101
103
  max_fee?: BigNumberish;
104
+ version?: BigNumberish;
102
105
  };
103
106
  export declare type InvokeFunctionTrace = {
104
107
  caller_address: string;
package/types/lib.d.ts CHANGED
@@ -19,6 +19,7 @@ export declare type Call = Omit<Invocation, 'signature'>;
19
19
  export declare type InvocationsDetails = {
20
20
  nonce?: BigNumberish;
21
21
  maxFee?: BigNumberish;
22
+ version?: BigNumberish;
22
23
  };
23
24
  export declare type Status =
24
25
  | 'NOT_RECEIVED'
package/types/signer.d.ts CHANGED
@@ -1,4 +1,6 @@
1
+ import { StarknetChainId } from '../constants';
1
2
  import { InvocationsDetails } from './lib';
2
3
  export interface InvocationsSignerDetails extends Required<InvocationsDetails> {
3
4
  walletAddress: string;
5
+ chainId: StarknetChainId;
4
6
  }
package/utils/hash.d.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import BN from 'bn.js';
2
2
 
3
- import { Call } from '../types';
3
+ import { StarknetChainId, TransactionHashPrefix } from '../constants';
4
4
  import { BigNumberish } from './number';
5
- export declare const transactionPrefix: string;
6
5
  export declare const transactionVersion = 0;
6
+ export declare const feeTransactionVersion: BN;
7
7
  /**
8
8
  * Function to get the starknet keccak hash from a string
9
9
  *
@@ -22,9 +22,27 @@ export declare function starknetKeccak(value: string): BN;
22
22
  export declare function getSelectorFromName(funcName: string): string;
23
23
  export declare function pedersen(input: [BigNumberish, BigNumberish]): string;
24
24
  export declare function computeHashOnElements(data: BigNumberish[]): string;
25
- export declare function hashMulticall(
26
- account: string,
27
- transactions: Call[],
28
- nonce: string,
29
- maxFee: string
25
+ export declare function calculateTransactionHashCommon(
26
+ txHashPrefix: TransactionHashPrefix,
27
+ version: BigNumberish,
28
+ contractAddress: BigNumberish,
29
+ entryPointSelector: BigNumberish,
30
+ calldata: BigNumberish[],
31
+ maxFee: BigNumberish,
32
+ chainId: StarknetChainId,
33
+ additionalData?: BigNumberish[]
34
+ ): string;
35
+ export declare function calculateDeployTransactionHash(
36
+ contractAddress: BigNumberish,
37
+ constructorCalldata: BigNumberish[],
38
+ version: BigNumberish,
39
+ chainId: StarknetChainId
40
+ ): string;
41
+ export declare function calculcateTransactionHash(
42
+ contractAddress: BigNumberish,
43
+ version: BigNumberish,
44
+ entryPointSelector: BigNumberish,
45
+ calldata: BigNumberish[],
46
+ maxFee: BigNumberish,
47
+ chainId: StarknetChainId
30
48
  ): string;
package/utils/hash.js CHANGED
@@ -39,13 +39,15 @@ var __importDefault =
39
39
  return mod && mod.__esModule ? mod : { default: mod };
40
40
  };
41
41
  Object.defineProperty(exports, '__esModule', { value: true });
42
- exports.hashMulticall =
42
+ exports.calculcateTransactionHash =
43
+ exports.calculateDeployTransactionHash =
44
+ exports.calculateTransactionHashCommon =
43
45
  exports.computeHashOnElements =
44
46
  exports.pedersen =
45
47
  exports.getSelectorFromName =
46
48
  exports.starknetKeccak =
49
+ exports.feeTransactionVersion =
47
50
  exports.transactionVersion =
48
- exports.transactionPrefix =
49
51
  void 0;
50
52
  var keccak_1 = require('ethereum-cryptography/keccak');
51
53
  var minimalistic_assert_1 = __importDefault(require('minimalistic-assert'));
@@ -53,9 +55,10 @@ var constants_1 = require('../constants');
53
55
  var ellipticCurve_1 = require('./ellipticCurve');
54
56
  var encode_1 = require('./encode');
55
57
  var number_1 = require('./number');
56
- var shortString_1 = require('./shortString');
57
- exports.transactionPrefix = (0, shortString_1.encodeShortString)('StarkNet Transaction');
58
58
  exports.transactionVersion = 0;
59
+ exports.feeTransactionVersion = (0, number_1.toBN)(2)
60
+ .pow((0, number_1.toBN)(128))
61
+ .add((0, number_1.toBN)(exports.transactionVersion));
59
62
  function keccakHex(value) {
60
63
  return (0, encode_1.addHexPrefix)(
61
64
  (0, encode_1.buf2hex)((0, keccak_1.keccak256)((0, encode_1.utf8ToArray)(value)))
@@ -116,27 +119,58 @@ function computeHashOnElements(data) {
116
119
  .toString();
117
120
  }
118
121
  exports.computeHashOnElements = computeHashOnElements;
119
- function hashMulticall(account, transactions, nonce, maxFee) {
120
- var hashArray = transactions
121
- .map(function (_a) {
122
- var contractAddress = _a.contractAddress,
123
- entrypoint = _a.entrypoint,
124
- calldata = _a.calldata;
125
- return [
126
- contractAddress,
127
- getSelectorFromName(entrypoint),
128
- computeHashOnElements(calldata || []),
129
- ];
130
- })
131
- .map(number_1.bigNumberishArrayToDecimalStringArray)
132
- .map(computeHashOnElements);
133
- return computeHashOnElements([
134
- exports.transactionPrefix,
135
- account,
136
- computeHashOnElements(hashArray),
137
- nonce,
122
+ // following implementation is based on this python implementation:
123
+ // https://github.com/starkware-libs/cairo-lang/blob/b614d1867c64f3fb2cf4a4879348cfcf87c3a5a7/src/starkware/starknet/core/os/transaction_hash/transaction_hash.py
124
+ function calculateTransactionHashCommon(
125
+ txHashPrefix,
126
+ version,
127
+ contractAddress,
128
+ entryPointSelector,
129
+ calldata,
130
+ maxFee,
131
+ chainId,
132
+ additionalData
133
+ ) {
134
+ if (additionalData === void 0) {
135
+ additionalData = [];
136
+ }
137
+ var calldataHash = computeHashOnElements(calldata);
138
+ var dataToHash = __spreadArray(
139
+ [txHashPrefix, version, contractAddress, entryPointSelector, calldataHash, maxFee, chainId],
140
+ __read(additionalData),
141
+ false
142
+ );
143
+ return computeHashOnElements(dataToHash);
144
+ }
145
+ exports.calculateTransactionHashCommon = calculateTransactionHashCommon;
146
+ function calculateDeployTransactionHash(contractAddress, constructorCalldata, version, chainId) {
147
+ return calculateTransactionHashCommon(
148
+ constants_1.TransactionHashPrefix.DEPLOY,
149
+ version,
150
+ contractAddress,
151
+ getSelectorFromName('constructor'),
152
+ constructorCalldata,
153
+ constants_1.ZERO,
154
+ chainId
155
+ );
156
+ }
157
+ exports.calculateDeployTransactionHash = calculateDeployTransactionHash;
158
+ function calculcateTransactionHash(
159
+ contractAddress,
160
+ version,
161
+ entryPointSelector,
162
+ calldata,
163
+ maxFee,
164
+ chainId
165
+ ) {
166
+ return calculateTransactionHashCommon(
167
+ constants_1.TransactionHashPrefix.INVOKE,
168
+ version,
169
+ contractAddress,
170
+ entryPointSelector,
171
+ calldata,
138
172
  maxFee,
139
- exports.transactionVersion,
140
- ]);
173
+ chainId
174
+ );
141
175
  }
142
- exports.hashMulticall = hashMulticall;
176
+ exports.calculcateTransactionHash = calculcateTransactionHash;
package/utils/stark.d.ts CHANGED
@@ -1,4 +1,7 @@
1
+ import BN from 'bn.js';
2
+
1
3
  import { Calldata, CompressedProgram, Program, RawArgs, Signature } from '../types';
4
+ import { BigNumberish } from './number';
2
5
  /**
3
6
  * Function to compress compiled cairo program
4
7
  *
@@ -11,3 +14,4 @@ export declare function randomAddress(): string;
11
14
  export declare function makeAddress(input: string): string;
12
15
  export declare function formatSignature(sig?: Signature): string[];
13
16
  export declare function compileCalldata(args: RawArgs): Calldata;
17
+ export declare function estimatedFeeToMaxFee(estimatedFee: BigNumberish, overhead?: number): BN;
package/utils/stark.js CHANGED
@@ -34,7 +34,8 @@ var __spreadArray =
34
34
  return to.concat(ar || Array.prototype.slice.call(from));
35
35
  };
36
36
  Object.defineProperty(exports, '__esModule', { value: true });
37
- exports.compileCalldata =
37
+ exports.estimatedFeeToMaxFee =
38
+ exports.compileCalldata =
38
39
  exports.formatSignature =
39
40
  exports.makeAddress =
40
41
  exports.randomAddress =
@@ -111,3 +112,14 @@ function compileCalldata(args) {
111
112
  });
112
113
  }
113
114
  exports.compileCalldata = compileCalldata;
115
+ function estimatedFeeToMaxFee(estimatedFee, overhead) {
116
+ if (overhead === void 0) {
117
+ overhead = 0.15;
118
+ }
119
+ // BN can only handle Integers, so we need to do all calulations with integers
120
+ var overHeadPercent = Math.round((1 + overhead) * 100);
121
+ return (0, number_1.toBN)(estimatedFee)
122
+ .mul((0, number_1.toBN)(overHeadPercent))
123
+ .div((0, number_1.toBN)(100));
124
+ }
125
+ exports.estimatedFeeToMaxFee = estimatedFeeToMaxFee;
@@ -1,4 +1,5 @@
1
1
  import { Call, ParsedStruct } from '../types';
2
+ import { BigNumberish } from './number';
2
3
  /**
3
4
  * Transforms a list of Calls, each with their own calldata, into
4
5
  * two arrays: one with the entrypoints, and one with the concatenated calldata.
@@ -16,3 +17,7 @@ export declare const transformCallsToMulticallArrays: (calls: Call[]) => {
16
17
  * @returns
17
18
  */
18
19
  export declare const fromCallsToExecuteCalldata: (calls: Call[]) => string[];
20
+ export declare const fromCallsToExecuteCalldataWithNonce: (
21
+ calls: Call[],
22
+ nonce: BigNumberish
23
+ ) => string[];
@@ -34,7 +34,10 @@ var __spreadArray =
34
34
  return to.concat(ar || Array.prototype.slice.call(from));
35
35
  };
36
36
  Object.defineProperty(exports, '__esModule', { value: true });
37
- exports.fromCallsToExecuteCalldata = exports.transformCallsToMulticallArrays = void 0;
37
+ exports.fromCallsToExecuteCalldataWithNonce =
38
+ exports.fromCallsToExecuteCalldata =
39
+ exports.transformCallsToMulticallArrays =
40
+ void 0;
38
41
  var hash_1 = require('./hash');
39
42
  var number_1 = require('./number');
40
43
  /**
@@ -97,3 +100,11 @@ var fromCallsToExecuteCalldata = function (calls) {
97
100
  );
98
101
  };
99
102
  exports.fromCallsToExecuteCalldata = fromCallsToExecuteCalldata;
103
+ var fromCallsToExecuteCalldataWithNonce = function (calls, nonce) {
104
+ return __spreadArray(
105
+ __spreadArray([], __read((0, exports.fromCallsToExecuteCalldata)(calls)), false),
106
+ [(0, number_1.toBN)(nonce).toString()],
107
+ false
108
+ );
109
+ };
110
+ exports.fromCallsToExecuteCalldataWithNonce = fromCallsToExecuteCalldataWithNonce;
@@ -55,9 +55,9 @@ export declare const encodeData: <
55
55
  >;
56
56
  primaryType: string;
57
57
  domain: {
58
- name?: string | undefined;
59
58
  version?: string | undefined;
60
59
  chainId?: string | number | undefined;
60
+ name?: string | undefined;
61
61
  };
62
62
  message: Record<string, unknown>;
63
63
  }
@@ -91,9 +91,9 @@ export declare const getStructHash: <
91
91
  >;
92
92
  primaryType: string;
93
93
  domain: {
94
- name?: string | undefined;
95
94
  version?: string | undefined;
96
95
  chainId?: string | number | undefined;
96
+ name?: string | undefined;
97
97
  };
98
98
  message: Record<string, unknown>;
99
99
  }
@@ -29,9 +29,9 @@ export declare const STARKNET_TYPE: import('superstruct').Struct<
29
29
  export declare type StarkNetType = Infer<typeof STARKNET_TYPE>;
30
30
  export declare const STARKNET_DOMAIN_TYPE: import('superstruct').Struct<
31
31
  {
32
- name?: string | undefined;
33
32
  version?: string | undefined;
34
33
  chainId?: string | number | undefined;
34
+ name?: string | undefined;
35
35
  },
36
36
  {
37
37
  name: import('superstruct').Struct<string | undefined, null>;
@@ -59,9 +59,9 @@ export declare const STARKNET_TYPED_DATA_TYPE: import('superstruct').Struct<
59
59
  >;
60
60
  primaryType: string;
61
61
  domain: {
62
- name?: string | undefined;
63
62
  version?: string | undefined;
64
63
  chainId?: string | number | undefined;
64
+ name?: string | undefined;
65
65
  };
66
66
  message: Record<string, unknown>;
67
67
  },
@@ -84,9 +84,9 @@ export declare const STARKNET_TYPED_DATA_TYPE: import('superstruct').Struct<
84
84
  primaryType: import('superstruct').Struct<string, null>;
85
85
  domain: import('superstruct').Struct<
86
86
  {
87
- name?: string | undefined;
88
87
  version?: string | undefined;
89
88
  chainId?: string | number | undefined;
89
+ name?: string | undefined;
90
90
  },
91
91
  {
92
92
  name: import('superstruct').Struct<string | undefined, null>;
@@ -19,9 +19,9 @@ export declare const validateTypedData: (data: unknown) => data is {
19
19
  >;
20
20
  primaryType: string;
21
21
  domain: {
22
- name?: string | undefined;
23
22
  version?: string | undefined;
24
23
  chainId?: string | number | undefined;
24
+ name?: string | undefined;
25
25
  };
26
26
  message: Record<string, unknown>;
27
27
  };
@@ -1,2 +0,0 @@
1
- export const ACCOUNT_ADDRESS = '0x4e5f6f6020ffa7e5761cbe96e932b7c1cbbcace70c0fe22767c5f0abf851a17';
2
- export const PRIVATE_KEY = '0x0529868a6dc774b36342024c207686af16e84fc157c5eeb3a9d31fa13c0d44c5';