starknet 4.7.0 → 4.9.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 (95) hide show
  1. package/CHANGELOG.md +43 -0
  2. package/CONTRIBUTING.md +2 -2
  3. package/__mocks__/ERC20.json +32561 -29055
  4. package/__mocks__/l1l2_compiled.json +10107 -0
  5. package/__tests__/account.test.ts +32 -24
  6. package/__tests__/contract.test.ts +25 -14
  7. package/__tests__/defaultProvider.test.ts +19 -49
  8. package/__tests__/fixtures.ts +11 -1
  9. package/__tests__/rpcProvider.test.ts +6 -15
  10. package/__tests__/sequencerProvider.test.ts +57 -11
  11. package/__tests__/utils/merkle.test.ts +113 -3
  12. package/__tests__/utils/typedData.test.ts +3 -3
  13. package/account/default.d.ts +10 -44
  14. package/account/default.js +255 -61
  15. package/account/interface.d.ts +78 -7
  16. package/constants.d.ts +1 -0
  17. package/constants.js +1 -0
  18. package/contract/default.js +6 -6
  19. package/dist/account/default.d.ts +10 -44
  20. package/dist/account/default.js +255 -61
  21. package/dist/account/interface.d.ts +78 -7
  22. package/dist/constants.d.ts +1 -0
  23. package/dist/constants.js +1 -0
  24. package/dist/contract/default.js +6 -6
  25. package/dist/provider/default.d.ts +8 -3
  26. package/dist/provider/default.js +31 -4
  27. package/dist/provider/interface.d.ts +67 -5
  28. package/dist/provider/rpc.d.ts +7 -2
  29. package/dist/provider/rpc.js +83 -8
  30. package/dist/provider/sequencer.d.ts +9 -3
  31. package/dist/provider/sequencer.js +93 -14
  32. package/dist/signer/default.d.ts +4 -1
  33. package/dist/signer/default.js +22 -0
  34. package/dist/signer/interface.d.ts +27 -2
  35. package/dist/types/api/openrpc.d.ts +24 -2
  36. package/dist/types/api/sequencer.d.ts +43 -23
  37. package/dist/types/lib.d.ts +23 -2
  38. package/dist/types/provider.d.ts +19 -10
  39. package/dist/types/signer.d.ts +14 -1
  40. package/dist/utils/hash.d.ts +8 -0
  41. package/dist/utils/hash.js +28 -2
  42. package/dist/utils/merkle.js +4 -5
  43. package/dist/utils/number.d.ts +5 -0
  44. package/dist/utils/number.js +29 -1
  45. package/dist/utils/responseParser/rpc.d.ts +2 -6
  46. package/dist/utils/responseParser/rpc.js +0 -11
  47. package/dist/utils/responseParser/sequencer.js +11 -33
  48. package/package.json +1 -1
  49. package/provider/default.d.ts +8 -3
  50. package/provider/default.js +31 -4
  51. package/provider/interface.d.ts +67 -5
  52. package/provider/rpc.d.ts +7 -2
  53. package/provider/rpc.js +83 -8
  54. package/provider/sequencer.d.ts +9 -3
  55. package/provider/sequencer.js +93 -14
  56. package/signer/default.d.ts +4 -1
  57. package/signer/default.js +22 -0
  58. package/signer/interface.d.ts +27 -2
  59. package/src/account/default.ts +201 -53
  60. package/src/account/interface.ts +104 -6
  61. package/src/constants.ts +1 -0
  62. package/src/contract/default.ts +6 -6
  63. package/src/provider/default.ts +43 -5
  64. package/src/provider/interface.ts +92 -7
  65. package/src/provider/rpc.ts +86 -12
  66. package/src/provider/sequencer.ts +105 -13
  67. package/src/signer/default.ts +54 -2
  68. package/src/signer/interface.ts +31 -2
  69. package/src/types/api/openrpc.ts +28 -2
  70. package/src/types/api/sequencer.ts +54 -25
  71. package/src/types/lib.ts +30 -2
  72. package/src/types/provider.ts +31 -11
  73. package/src/types/signer.ts +18 -1
  74. package/src/utils/hash.ts +70 -2
  75. package/src/utils/merkle.ts +4 -5
  76. package/src/utils/number.ts +27 -0
  77. package/src/utils/responseParser/rpc.ts +4 -20
  78. package/src/utils/responseParser/sequencer.ts +14 -7
  79. package/types/api/openrpc.d.ts +24 -2
  80. package/types/api/sequencer.d.ts +43 -23
  81. package/types/lib.d.ts +23 -2
  82. package/types/provider.d.ts +19 -10
  83. package/types/signer.d.ts +14 -1
  84. package/utils/hash.d.ts +8 -0
  85. package/utils/hash.js +28 -2
  86. package/utils/merkle.js +4 -5
  87. package/utils/number.d.ts +5 -0
  88. package/utils/number.js +29 -1
  89. package/utils/responseParser/rpc.d.ts +2 -6
  90. package/utils/responseParser/rpc.js +0 -11
  91. package/utils/responseParser/sequencer.js +11 -33
  92. package/www/docs/API/account.md +60 -1
  93. package/www/docs/API/provider.md +320 -23
  94. package/www/guides/account.md +1 -1
  95. package/www/guides/erc20.md +13 -7
@@ -1,6 +1,21 @@
1
+ import { pedersen } from '../../src/utils/hash';
1
2
  import { MerkleTree, proofMerklePath } from '../../src/utils/merkle';
3
+ import { toBN } from '../../src/utils/number';
2
4
 
3
5
  describe('MerkleTree class', () => {
6
+ describe('calculate hashes', () => {
7
+ test('should generate hash with sorted arguments', async () => {
8
+ let leaves = ['0x12', '0xa']; // 18, 10
9
+ let merkleHash = MerkleTree.hash(leaves[0], leaves[1]);
10
+ let rawHash = pedersen([toBN(leaves[1]), toBN(leaves[0])]);
11
+ expect(merkleHash).toBe(rawHash);
12
+
13
+ leaves = ['0x5bb9440e27889a364bcb678b1f679ecd1347acdedcbf36e83494f857cc58026', '0x3'];
14
+ merkleHash = MerkleTree.hash(leaves[0], leaves[1]);
15
+ rawHash = pedersen([toBN(leaves[1]), toBN(leaves[0])]);
16
+ expect(merkleHash).toBe(rawHash);
17
+ });
18
+ });
4
19
  describe('generate roots', () => {
5
20
  test('should generate valid root for 1 elements', async () => {
6
21
  const leaves = ['0x1'];
@@ -38,7 +53,7 @@ describe('MerkleTree class', () => {
38
53
  MerkleTree.hash(leaves[0], leaves[1]),
39
54
  MerkleTree.hash(leaves[2], leaves[3])
40
55
  ),
41
- MerkleTree.hash(leaves[4], leaves[5])
56
+ MerkleTree.hash(MerkleTree.hash(leaves[4], leaves[5]), '0x0')
42
57
  );
43
58
 
44
59
  expect(tree.root).toBe(manualMerkle);
@@ -52,7 +67,7 @@ describe('MerkleTree class', () => {
52
67
  MerkleTree.hash(leaves[0], leaves[1]),
53
68
  MerkleTree.hash(leaves[2], leaves[3])
54
69
  ),
55
- MerkleTree.hash(MerkleTree.hash(leaves[4], leaves[5]), leaves[6])
70
+ MerkleTree.hash(MerkleTree.hash(leaves[4], leaves[5]), MerkleTree.hash(leaves[6], '0x0'))
56
71
  );
57
72
 
58
73
  expect(tree.root).toBe(manualMerkle);
@@ -70,7 +85,7 @@ describe('MerkleTree class', () => {
70
85
  const manualProof = [
71
86
  '0x4',
72
87
  MerkleTree.hash('0x1', '0x2'),
73
- MerkleTree.hash(MerkleTree.hash('0x5', '0x6'), '0x7'),
88
+ MerkleTree.hash(MerkleTree.hash('0x5', '0x6'), MerkleTree.hash('0x7', '0x0')),
74
89
  ];
75
90
 
76
91
  expect(proof).toEqual(manualProof);
@@ -86,6 +101,17 @@ describe('MerkleTree class', () => {
86
101
 
87
102
  expect(proof).toEqual(manualProof);
88
103
  });
104
+ test('should return proof path for valid child', async () => {
105
+ const proof = tree.getProof('0x5');
106
+
107
+ const manualProof = [
108
+ '0x6',
109
+ MerkleTree.hash('0x7', '0x0'), // tree should be padded with 0x0 so that all proofs are equals in size
110
+ MerkleTree.hash(MerkleTree.hash('0x1', '0x2'), MerkleTree.hash('0x3', '0x4')),
111
+ ];
112
+
113
+ expect(proof).toEqual(manualProof);
114
+ });
89
115
  test('should throw for invalid child', () => {
90
116
  expect(() => tree.getProof('0x8')).toThrow('leaf not found');
91
117
  });
@@ -99,6 +125,7 @@ describe('MerkleTree class', () => {
99
125
 
100
126
  test('should return true for valid manual proof', async () => {
101
127
  const manualProof = [
128
+ '0x0', // tree should be padded with 0x0 so that all proofs are equals in size
102
129
  MerkleTree.hash('0x5', '0x6'),
103
130
  MerkleTree.hash(MerkleTree.hash('0x1', '0x2'), MerkleTree.hash('0x3', '0x4')),
104
131
  ];
@@ -107,6 +134,17 @@ describe('MerkleTree class', () => {
107
134
 
108
135
  expect(proofMerklePath(root, leaf, manualProof)).toBe(true);
109
136
  });
137
+ test('should return true for valid manual proof', async () => {
138
+ const manualProof = [
139
+ '0x6',
140
+ MerkleTree.hash('0x7', '0x0'), // tree should be padded with 0x0 so that all proofs are equals in size
141
+ MerkleTree.hash(MerkleTree.hash('0x1', '0x2'), MerkleTree.hash('0x3', '0x4')),
142
+ ];
143
+ const leaf = '0x5';
144
+ const { root } = tree;
145
+
146
+ expect(proofMerklePath(root, leaf, manualProof)).toBe(true);
147
+ });
110
148
  test('should return true for valid proof', async () => {
111
149
  const proof = tree.getProof('0x3');
112
150
  const leaf = '0x3';
@@ -143,4 +181,76 @@ describe('MerkleTree class', () => {
143
181
  expect(proofMerklePath(root, leaf, proof)).toBe(false);
144
182
  });
145
183
  });
184
+ describe('verify 2-deep tree with empty data on the right', () => {
185
+ let tree: MerkleTree;
186
+ beforeAll(() => {
187
+ const leaves = ['0x1', '0x2', '0x3'];
188
+ tree = new MerkleTree(leaves);
189
+ });
190
+ test('should return 1-length proof in a 2-length tree', async () => {
191
+ const proof = tree.getProof('0x3');
192
+ const manualProof = ['0x0', MerkleTree.hash('0x1', '0x2')];
193
+ expect(proof).toEqual(manualProof);
194
+ });
195
+ test('should check the previous proof works fine', async () => {
196
+ const manualMerkle = MerkleTree.hash(
197
+ MerkleTree.hash('0x3', '0x0'),
198
+ MerkleTree.hash('0x1', '0x2')
199
+ );
200
+ expect(tree.root).toBe(manualMerkle);
201
+ });
202
+ });
203
+ describe('verify 3-deep tree with empty data on the right', () => {
204
+ let tree: MerkleTree;
205
+ beforeAll(() => {
206
+ const leaves = ['0x1', '0x2', '0x3', '0x4', '0x5', '0x6'];
207
+ tree = new MerkleTree(leaves);
208
+ });
209
+ test('should return 2-length proof with the 2nd place skipped', async () => {
210
+ const proof = tree.getProof('0x5');
211
+ const manualProof = [
212
+ '0x6',
213
+ '0x0',
214
+ MerkleTree.hash(MerkleTree.hash('0x1', '0x2'), MerkleTree.hash('0x3', '0x4')),
215
+ ];
216
+ expect(proof).toEqual(manualProof);
217
+ });
218
+ test('should check the previous proof works fine', async () => {
219
+ const manualMerkle = MerkleTree.hash(
220
+ MerkleTree.hash(MerkleTree.hash('0x5', '0x6'), '0x0'),
221
+ MerkleTree.hash(MerkleTree.hash('0x1', '0x2'), MerkleTree.hash('0x3', '0x4'))
222
+ );
223
+ expect(tree.root).toBe(manualMerkle);
224
+ });
225
+ });
226
+ describe('verify 4-deep tree with empty data on the right', () => {
227
+ let tree: MerkleTree;
228
+ beforeAll(() => {
229
+ const leaves = ['0x1', '0x2', '0x3', '0x4', '0x5', '0x6', '0x7', '0x8', '0x9'];
230
+ tree = new MerkleTree(leaves);
231
+ });
232
+ test('should return 2-length proof with the 2nd place skipped', async () => {
233
+ const proof = tree.getProof('0x9');
234
+ const manualProof = [
235
+ '0x0',
236
+ '0x0',
237
+ '0x0',
238
+ MerkleTree.hash(
239
+ MerkleTree.hash(MerkleTree.hash('0x1', '0x2'), MerkleTree.hash('0x3', '0x4')),
240
+ MerkleTree.hash(MerkleTree.hash('0x5', '0x6'), MerkleTree.hash('0x7', '0x8'))
241
+ ),
242
+ ];
243
+ expect(proof).toEqual(manualProof);
244
+ });
245
+ test('should check the previous proof works fine', async () => {
246
+ const manualMerkle = MerkleTree.hash(
247
+ MerkleTree.hash(MerkleTree.hash(MerkleTree.hash('0x9', '0x0'), '0x0'), '0x0'),
248
+ MerkleTree.hash(
249
+ MerkleTree.hash(MerkleTree.hash('0x1', '0x2'), MerkleTree.hash('0x3', '0x4')),
250
+ MerkleTree.hash(MerkleTree.hash('0x5', '0x6'), MerkleTree.hash('0x7', '0x8'))
251
+ )
252
+ );
253
+ expect(tree.root).toBe(manualMerkle);
254
+ });
255
+ });
146
256
  });
@@ -72,7 +72,7 @@ describe('typedData', () => {
72
72
  const [, merkleTreeHash] = encodeValue({}, 'merkletree', tree.leaves);
73
73
  expect(merkleTreeHash).toBe(tree.root);
74
74
  expect(merkleTreeHash).toMatchInlineSnapshot(
75
- `"0x551b4adb6c35d49c686a00b9192da9332b18c9b262507cad0ece37f3b6918d2"`
75
+ `"0x15ac9e457789ef0c56e5d559809e7336a909c14ee2511503fa7af69be1ba639"`
76
76
  );
77
77
  });
78
78
 
@@ -119,7 +119,7 @@ describe('typedData', () => {
119
119
  );
120
120
  expect(merkleTreeHash).toBe(tree.root);
121
121
  expect(merkleTreeHash).toMatchInlineSnapshot(
122
- `"0x75c4f467f4527a5348f3e302007228a6b0057fc4c015f981ffb5b3ace475727"`
122
+ `"0x12354b159e3799dc0ebe86d62dde4ce7b300538d471e5a7fef23dcbac076011"`
123
123
  );
124
124
  });
125
125
 
@@ -217,7 +217,7 @@ describe('typedData', () => {
217
217
  '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826'
218
218
  );
219
219
  expect(hash).toMatchInlineSnapshot(
220
- `"0x1ad0330a62a4a94eae5ea1a7ad96388179d2e4d33e6f909d17421d315110653"`
220
+ `"0x751fb7d98545f7649d0d0eadc80d770fcd88d8cfaa55590b284f4e1b701ef0a"`
221
221
  );
222
222
  });
223
223
  });
@@ -2,8 +2,9 @@ import { ProviderInterface, ProviderOptions } from '../provider';
2
2
  import { Provider } from '../provider/default';
3
3
  import { BlockIdentifier } from '../provider/utils';
4
4
  import { SignerInterface } from '../signer';
5
- import { Abi, Call, InvocationsDetails, InvokeFunctionResponse, KeyPair, Signature } from '../types';
5
+ import { Abi, Call, DeclareContractResponse, DeployContractResponse, EstimateFeeAction, InvocationsDetails, InvokeFunctionResponse, KeyPair, Signature } from '../types';
6
6
  import { EstimateFee, EstimateFeeDetails } from '../types/account';
7
+ import { AllowArray, DeclareContractPayload, DeployAccountContractPayload } from '../types/lib';
7
8
  import { BigNumberish } from '../utils/number';
8
9
  import { TypedData } from '../utils/typedData';
9
10
  import { AccountInterface } from './interface';
@@ -12,51 +13,16 @@ export declare class Account extends Provider implements AccountInterface {
12
13
  address: string;
13
14
  constructor(providerOrOptions: ProviderOptions | ProviderInterface, address: string, keyPairOrSigner: KeyPair | SignerInterface);
14
15
  getNonce(blockIdentifier?: BlockIdentifier): Promise<BigNumberish>;
15
- estimateFee(calls: Call | Call[], { nonce: providedNonce, blockIdentifier }?: EstimateFeeDetails): Promise<EstimateFee>;
16
- /**
17
- * Invoke execute function in account contract
18
- *
19
- * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/gateway/gateway_client.py#L13-L17)
20
- *
21
- * @param calls - one or more calls to be executed
22
- * @param abis - one or more abis which can be used to display the calls
23
- * @param transactionsDetail - optional transaction details
24
- * @returns a confirmation of invoking a function on the starknet contract
25
- */
26
- execute(calls: Call | Call[], abis?: Abi[] | undefined, transactionsDetail?: InvocationsDetails): Promise<InvokeFunctionResponse>;
27
- /**
28
- * Sign an JSON object with the starknet private key and return the signature
29
- *
30
- * @param json - JSON object to be signed
31
- * @returns the signature of the JSON object
32
- * @throws {Error} if the JSON object is not a valid JSON
33
- */
16
+ estimateFee(calls: AllowArray<Call>, estimateFeeDetails?: EstimateFeeDetails | undefined): Promise<EstimateFee>;
17
+ estimateInvokeFee(calls: AllowArray<Call>, { nonce: providedNonce, blockIdentifier }?: EstimateFeeDetails): Promise<EstimateFee>;
18
+ estimateDeclareFee({ classHash, contract }: DeclareContractPayload, { blockIdentifier, nonce: providedNonce }?: EstimateFeeDetails): Promise<EstimateFee>;
19
+ estimateAccountDeployFee({ classHash, addressSalt, constructorCalldata, contractAddress: providedContractAddress, }: DeployAccountContractPayload, { blockIdentifier, nonce: providedNonce }?: EstimateFeeDetails): Promise<EstimateFee>;
20
+ execute(calls: AllowArray<Call>, abis?: Abi[] | undefined, transactionsDetail?: InvocationsDetails): Promise<InvokeFunctionResponse>;
21
+ declare({ classHash, contract }: DeclareContractPayload, transactionsDetail?: InvocationsDetails): Promise<DeclareContractResponse>;
22
+ deployAccount({ classHash, constructorCalldata, addressSalt, contractAddress: providedContractAddress, }: DeployAccountContractPayload, transactionsDetail?: InvocationsDetails): Promise<DeployContractResponse>;
34
23
  signMessage(typedData: TypedData): Promise<Signature>;
35
- /**
36
- * Hash a JSON object with pederson hash and return the hash
37
- *
38
- * @param json - JSON object to be hashed
39
- * @returns the hash of the JSON object
40
- * @throws {Error} if the JSON object is not a valid JSON
41
- */
42
24
  hashMessage(typedData: TypedData): Promise<string>;
43
- /**
44
- * Verify a signature of a given hash
45
- * @warning This method is not recommended, use verifyMessage instead
46
- *
47
- * @param hash - JSON object to be verified
48
- * @param signature - signature of the JSON object
49
- * @returns true if the signature is valid, false otherwise
50
- * @throws {Error} if the JSON object is not a valid JSON or the signature is not a valid signature
51
- */
52
25
  verifyMessageHash(hash: BigNumberish, signature: Signature): Promise<boolean>;
53
- /**
54
- * Verify a signature of a JSON object
55
- *
56
- * @param hash - hash to be verified
57
- * @param signature - signature of the hash
58
- * @returns true if the signature is valid, false otherwise
59
- * @throws {Error} if the signature is not a valid signature
60
- */
61
26
  verifyMessage(typedData: TypedData, signature: Signature): Promise<boolean>;
27
+ getSuggestedMaxFee(estimateFeeAction: EstimateFeeAction, details: EstimateFeeDetails): Promise<string>;
62
28
  }