starknet 3.18.0 → 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 (163) hide show
  1. package/CHANGELOG.md +65 -0
  2. package/README.md +1 -0
  3. package/__mocks__/typedDataStructArrayExample.json +44 -0
  4. package/__tests__/account.test.ts +11 -56
  5. package/__tests__/contract.test.ts +11 -49
  6. package/__tests__/defaultProvider.test.ts +321 -0
  7. package/__tests__/fixtures.ts +32 -11
  8. package/__tests__/rpcProvider.test.ts +17 -0
  9. package/__tests__/sequencerProvider.test.ts +45 -0
  10. package/__tests__/utils/typedData.test.ts +24 -0
  11. package/account/default.d.ts +54 -77
  12. package/account/default.js +271 -596
  13. package/account/index.js +18 -31
  14. package/account/interface.d.ts +66 -95
  15. package/account/interface.js +20 -30
  16. package/constants.d.ts +17 -19
  17. package/constants.js +2038 -2059
  18. package/contract/contractFactory.d.ts +25 -29
  19. package/contract/contractFactory.js +94 -210
  20. package/contract/default.d.ts +117 -146
  21. package/contract/default.js +582 -776
  22. package/contract/index.js +19 -32
  23. package/contract/interface.d.ts +72 -92
  24. package/contract/interface.js +6 -5
  25. package/dist/account/default.d.ts +5 -9
  26. package/dist/account/default.js +35 -169
  27. package/dist/account/interface.d.ts +3 -15
  28. package/dist/contract/contractFactory.js +4 -4
  29. package/dist/contract/default.d.ts +3 -3
  30. package/dist/contract/default.js +3 -2
  31. package/dist/contract/interface.d.ts +2 -2
  32. package/dist/provider/default.d.ts +18 -134
  33. package/dist/provider/default.js +47 -410
  34. package/dist/provider/index.d.ts +2 -0
  35. package/dist/provider/index.js +2 -0
  36. package/dist/provider/interface.d.ts +45 -50
  37. package/dist/provider/rpc.d.ts +57 -0
  38. package/dist/provider/rpc.js +364 -0
  39. package/dist/provider/sequencer.d.ts +66 -0
  40. package/dist/provider/sequencer.js +443 -0
  41. package/dist/types/account.d.ts +2 -3
  42. package/dist/types/api/index.d.ts +16 -0
  43. package/dist/types/api/index.js +18 -0
  44. package/dist/types/api/rpc.d.ts +221 -0
  45. package/dist/types/{api.js → api/rpc.js} +0 -0
  46. package/dist/types/api/sequencer.d.ts +289 -0
  47. package/dist/types/api/sequencer.js +2 -0
  48. package/dist/types/index.d.ts +3 -1
  49. package/dist/types/index.js +15 -1
  50. package/dist/types/lib.d.ts +3 -1
  51. package/dist/types/provider.d.ts +86 -0
  52. package/dist/types/provider.js +2 -0
  53. package/dist/utils/provider.d.ts +4 -0
  54. package/dist/utils/provider.js +38 -0
  55. package/dist/utils/responseParser/index.d.ts +11 -0
  56. package/dist/utils/responseParser/index.js +9 -0
  57. package/dist/utils/responseParser/rpc.d.ts +13 -0
  58. package/dist/utils/responseParser/rpc.js +96 -0
  59. package/dist/utils/responseParser/sequencer.d.ts +13 -0
  60. package/dist/utils/responseParser/sequencer.js +124 -0
  61. package/dist/utils/typedData/index.js +14 -0
  62. package/index.js +42 -75
  63. package/package.json +1 -1
  64. package/provider/default.d.ts +21 -175
  65. package/provider/default.js +139 -703
  66. package/provider/errors.d.ts +4 -4
  67. package/provider/errors.js +30 -40
  68. package/provider/index.d.ts +2 -0
  69. package/provider/index.js +22 -33
  70. package/provider/interface.d.ts +104 -131
  71. package/provider/interface.js +6 -5
  72. package/provider/rpc.d.ts +57 -0
  73. package/provider/rpc.js +364 -0
  74. package/provider/sequencer.d.ts +66 -0
  75. package/provider/sequencer.js +443 -0
  76. package/provider/utils.d.ts +7 -9
  77. package/provider/utils.js +39 -44
  78. package/signer/default.d.ts +5 -9
  79. package/signer/default.js +72 -177
  80. package/signer/index.js +18 -31
  81. package/signer/interface.d.ts +29 -33
  82. package/signer/interface.js +6 -5
  83. package/src/account/default.ts +26 -146
  84. package/src/account/interface.ts +5 -20
  85. package/src/contract/contractFactory.ts +3 -6
  86. package/src/contract/default.ts +6 -4
  87. package/src/contract/interface.ts +2 -2
  88. package/src/provider/default.ts +63 -394
  89. package/src/provider/index.ts +2 -0
  90. package/src/provider/interface.ts +68 -63
  91. package/src/provider/rpc.ts +300 -0
  92. package/src/provider/sequencer.ts +384 -0
  93. package/src/types/account.ts +2 -3
  94. package/src/types/api/index.ts +17 -0
  95. package/src/types/api/rpc.ts +247 -0
  96. package/src/types/api/sequencer.ts +331 -0
  97. package/src/types/index.ts +3 -1
  98. package/src/types/lib.ts +3 -1
  99. package/src/types/provider.ts +108 -0
  100. package/src/utils/provider.ts +28 -0
  101. package/src/utils/responseParser/index.ts +28 -0
  102. package/src/utils/responseParser/rpc.ts +93 -0
  103. package/src/utils/responseParser/sequencer.ts +127 -0
  104. package/src/utils/typedData/index.ts +18 -0
  105. package/types/account.d.ts +5 -7
  106. package/types/account.js +2 -2
  107. package/types/api/index.d.ts +16 -0
  108. package/types/api/index.js +18 -0
  109. package/types/api/rpc.d.ts +221 -0
  110. package/types/api/rpc.js +2 -0
  111. package/types/api/sequencer.d.ts +289 -0
  112. package/types/api/sequencer.js +2 -0
  113. package/types/contract.d.ts +1 -1
  114. package/types/contract.js +2 -2
  115. package/types/index.d.ts +3 -1
  116. package/types/index.js +35 -34
  117. package/types/lib.d.ts +36 -41
  118. package/types/lib.js +2 -2
  119. package/types/provider.d.ts +86 -0
  120. package/types/provider.js +2 -0
  121. package/types/signer.d.ts +2 -2
  122. package/types/signer.js +2 -2
  123. package/utils/address.js +26 -37
  124. package/utils/ellipticCurve.d.ts +1 -6
  125. package/utils/ellipticCurve.js +73 -137
  126. package/utils/encode.js +49 -85
  127. package/utils/hash.d.ts +4 -31
  128. package/utils/hash.js +76 -141
  129. package/utils/json.d.ts +13 -45
  130. package/utils/json.js +15 -22
  131. package/utils/number.d.ts +2 -9
  132. package/utils/number.js +47 -81
  133. package/utils/provider.d.ts +4 -0
  134. package/utils/provider.js +38 -0
  135. package/utils/responseParser/index.d.ts +11 -0
  136. package/utils/responseParser/index.js +9 -0
  137. package/utils/responseParser/rpc.d.ts +13 -0
  138. package/utils/responseParser/rpc.js +96 -0
  139. package/utils/responseParser/sequencer.d.ts +13 -0
  140. package/utils/responseParser/sequencer.js +124 -0
  141. package/utils/shortString.js +13 -21
  142. package/utils/stark.d.ts +0 -1
  143. package/utils/stark.js +59 -93
  144. package/utils/transaction.d.ts +3 -6
  145. package/utils/transaction.js +50 -81
  146. package/utils/typedData/index.d.ts +3 -15
  147. package/utils/typedData/index.js +109 -157
  148. package/utils/typedData/types.d.ts +9 -9
  149. package/utils/typedData/types.js +2 -2
  150. package/utils/typedData/utils.js +6 -6
  151. package/utils/uint256.d.ts +5 -5
  152. package/utils/uint256.js +16 -26
  153. package/www/docs/API/account.md +3 -4
  154. package/www/docs/API/contract.md +2 -2
  155. package/www/docs/API/contractFactory.md +2 -2
  156. package/www/docs/API/provider.md +185 -74
  157. package/www/guides/account.md +1 -8
  158. package/www/guides/erc20.md +3 -0
  159. package/__tests__/provider.test.ts +0 -168
  160. package/dist/types/api.d.ts +0 -261
  161. package/src/types/api.ts +0 -303
  162. package/types/api.d.ts +0 -287
  163. package/types/api.js +0 -2
@@ -0,0 +1,321 @@
1
+ import {
2
+ BlockNumber,
3
+ DeclareContractResponse,
4
+ DeployContractResponse,
5
+ GetBlockResponse,
6
+ stark,
7
+ } from '../src';
8
+ import { toBN } from '../src/utils/number';
9
+ import {
10
+ compiledErc20,
11
+ compiledOpenZeppelinAccount,
12
+ describeIfNotDevnet,
13
+ getTestProvider,
14
+ } from './fixtures';
15
+
16
+ const { compileCalldata } = stark;
17
+
18
+ const testProvider = getTestProvider();
19
+
20
+ describe('defaultProvider', () => {
21
+ let exampleTransactionHash: string;
22
+ let exampleContractAddress: string;
23
+
24
+ let exampleBlock: GetBlockResponse;
25
+ let exampleBlockNumber: BlockNumber;
26
+ let exampleBlockHash: string;
27
+
28
+ beforeAll(async () => {
29
+ const { transaction_hash, contract_address } = await testProvider.deployContract({
30
+ contract: compiledErc20,
31
+ });
32
+ await testProvider.waitForTransaction(transaction_hash);
33
+ exampleTransactionHash = transaction_hash;
34
+ exampleContractAddress = contract_address;
35
+
36
+ exampleBlock = await testProvider.getBlock('latest');
37
+ exampleBlockHash = exampleBlock.block_hash;
38
+ exampleBlockNumber = exampleBlock.block_number;
39
+ });
40
+
41
+ describe('endpoints', () => {
42
+ test(`getBlock(blockHash=undefined, blockNumber=${exampleBlockNumber})`, () => {
43
+ return expect(testProvider.getBlock(exampleBlockNumber)).resolves.not.toThrow();
44
+ });
45
+
46
+ test(`getBlock(blockHash=${exampleBlockHash}, blockNumber=undefined)`, () => {
47
+ return expect(testProvider.getBlock(exampleBlockHash)).resolves.not.toThrow();
48
+ });
49
+
50
+ test('getBlock(blockIdentifier=latest)', async () => {
51
+ expect(exampleBlock).not.toBeNull();
52
+
53
+ const { block_number, accepted_time } = exampleBlock;
54
+
55
+ expect(typeof block_number).toEqual('number');
56
+
57
+ return expect(typeof accepted_time).toEqual('number');
58
+ });
59
+
60
+ test('getBlock() -> { blockNumber }', async () => {
61
+ const block = await testProvider.getBlock();
62
+ return expect(block).toHaveProperty('block_number');
63
+ });
64
+
65
+ describe('getStorageAt', () => {
66
+ test('with "key" type of number', () => {
67
+ return expect(testProvider.getStorageAt(exampleContractAddress, 0)).resolves.not.toThrow();
68
+ });
69
+
70
+ test('"key" type of string', () => {
71
+ return expect(
72
+ testProvider.getStorageAt(exampleContractAddress, '0x0')
73
+ ).resolves.not.toThrow();
74
+ });
75
+
76
+ test('with "key" type of BN', () => {
77
+ return expect(
78
+ testProvider.getStorageAt(exampleContractAddress, toBN('0x0'))
79
+ ).resolves.not.toThrow();
80
+ });
81
+ });
82
+
83
+ test('getTransaction() - successful transaction', async () => {
84
+ const transaction = await testProvider.getTransaction(exampleTransactionHash);
85
+
86
+ expect(transaction).toHaveProperty('transaction_hash');
87
+ });
88
+
89
+ test('getTransactionReceipt() - successful transaction', async () => {
90
+ const transactionReceipt = await testProvider.getTransactionReceipt(exampleTransactionHash);
91
+
92
+ return expect(transactionReceipt).toHaveProperty('actual_fee');
93
+ });
94
+
95
+ test('callContract()', () => {
96
+ return expect(
97
+ testProvider.callContract({
98
+ contractAddress: exampleContractAddress,
99
+ entrypoint: 'balance_of',
100
+ calldata: compileCalldata({
101
+ user: '0x9ff64f4ab0e1fe88df4465ade98d1ea99d5732761c39279b8e1374fa943e9b',
102
+ }),
103
+ })
104
+ ).resolves.not.toThrow();
105
+ });
106
+
107
+ test('callContract() - gateway error', async () => {
108
+ return expect(
109
+ testProvider.callContract({
110
+ contractAddress: exampleContractAddress,
111
+ entrypoint: 'non_existent_entrypoint',
112
+ calldata: compileCalldata({
113
+ user: '0xdeadbeef',
114
+ }),
115
+ })
116
+ ).rejects.toThrowError();
117
+ });
118
+ });
119
+
120
+ describe('addTransaction()', () => {
121
+ test('declareContract()', async () => {
122
+ const response = await testProvider.declareContract({
123
+ contract: compiledErc20,
124
+ });
125
+
126
+ expect(response.transaction_hash).toBeDefined();
127
+ expect(response.class_hash).toBeDefined();
128
+ });
129
+
130
+ test('deployContract()', async () => {
131
+ const response = await testProvider.deployContract({
132
+ contract: compiledOpenZeppelinAccount,
133
+ });
134
+
135
+ expect(response.transaction_hash).toBeDefined();
136
+ expect(response.contract_address).toBeDefined();
137
+ });
138
+ });
139
+
140
+ describeIfNotDevnet('Provider', () => {
141
+ const provider = getTestProvider();
142
+ describe(`Provider methods`, () => {
143
+ describe('getBlock', () => {
144
+ test('pending', async () => {
145
+ const latestBlock = await provider.getBlock();
146
+ expect(latestBlock).toHaveProperty('block_hash');
147
+ expect(latestBlock).toHaveProperty('parent_hash');
148
+ expect(latestBlock).toHaveProperty('block_number');
149
+ expect(latestBlock).toHaveProperty('status');
150
+ expect(latestBlock).toHaveProperty('sequencer');
151
+ expect(latestBlock).toHaveProperty('new_root');
152
+ expect(latestBlock).toHaveProperty('old_root');
153
+ expect(latestBlock).toHaveProperty('accepted_time');
154
+ expect(latestBlock).toHaveProperty('gas_price');
155
+ expect(latestBlock).toHaveProperty('transactions');
156
+ expect(Array.isArray(latestBlock.transactions)).toBe(true);
157
+ });
158
+
159
+ test('Block Hash 0x8a30a1212d142cb0053fe9921e1dbf64f651d328565bd2e7ac24059c270f43', async () => {
160
+ const block = await provider.getBlock(
161
+ '0x8a30a1212d142cb0053fe9921e1dbf64f651d328565bd2e7ac24059c270f43'
162
+ );
163
+
164
+ expect(block).toHaveProperty('block_hash');
165
+ expect(block).toHaveProperty('parent_hash');
166
+ expect(block).toHaveProperty('block_number');
167
+ expect(block).toHaveProperty('status');
168
+ expect(block).toHaveProperty('sequencer');
169
+ expect(block).toHaveProperty('new_root');
170
+ expect(block).toHaveProperty('old_root');
171
+ expect(block).toHaveProperty('accepted_time');
172
+ expect(block).toHaveProperty('gas_price');
173
+ expect(block).toHaveProperty('transactions');
174
+ expect(Array.isArray(block.transactions)).toBe(true);
175
+ });
176
+
177
+ test('Block Number 102634', async () => {
178
+ const block = await provider.getBlock(102634);
179
+ expect(block).toHaveProperty('block_hash');
180
+ expect(block).toHaveProperty('parent_hash');
181
+ expect(block).toHaveProperty('block_number');
182
+ expect(block).toHaveProperty('status');
183
+ expect(block).toHaveProperty('sequencer');
184
+ expect(block).toHaveProperty('new_root');
185
+ expect(block).toHaveProperty('old_root');
186
+ expect(block).toHaveProperty('accepted_time');
187
+ expect(block).toHaveProperty('gas_price');
188
+ expect(block).toHaveProperty('transactions');
189
+ expect(Array.isArray(block.transactions)).toBe(true);
190
+ });
191
+ });
192
+
193
+ describe('getStorageAt', () => {
194
+ test('pending', async () => {
195
+ const storage = await provider.getStorageAt(
196
+ '0x01d1f307c073bb786a66e6e042ec2a9bdc385a3373bb3738d95b966d5ce56166',
197
+ 0
198
+ );
199
+ expect(typeof storage).toBe('string');
200
+ });
201
+
202
+ test('Block Hash 0x7104702055c2a5773a870ceada9552ec659d69c18053b14078983f07527dea8', async () => {
203
+ const storage = await provider.getStorageAt(
204
+ '0x01d1f307c073bb786a66e6e042ec2a9bdc385a3373bb3738d95b966d5ce56166',
205
+ 0,
206
+ '0x7225762c7ff5e7e5f0867f0a8e73594df4f44f05a65375339a76398e8ae3e64'
207
+ );
208
+ expect(typeof storage).toBe('string');
209
+ });
210
+ });
211
+
212
+ describe('getTransaction', () => {
213
+ test('Deploy Transaction Hash 0x37013e1cb9c133e6fe51b4b371b76b317a480f56d80576730754c1662582348', async () => {
214
+ const transaction = await provider.getTransaction(
215
+ '0x37013e1cb9c133e6fe51b4b371b76b317a480f56d80576730754c1662582348'
216
+ );
217
+
218
+ expect(transaction.transaction_hash).toBeTruthy();
219
+ expect(transaction.contract_address).toBeTruthy();
220
+ });
221
+
222
+ test('Invoke Transaction Hash 0x2a56c636f45761c99a67ecdf0f185a6d5fe5239924ed9a4886fddbfaf3227b', async () => {
223
+ const transaction = await provider.getTransaction(
224
+ '0x2a56c636f45761c99a67ecdf0f185a6d5fe5239924ed9a4886fddbfaf3227b'
225
+ );
226
+
227
+ expect(transaction.transaction_hash).toBeTruthy();
228
+ expect(transaction.contract_address).toBeTruthy();
229
+ expect(Array.isArray(transaction.calldata)).toBe(true);
230
+ expect(transaction.entry_point_selector).toBeTruthy();
231
+ expect(Array.isArray(transaction.signature)).toBe(true);
232
+ expect(transaction.max_fee).toBeTruthy();
233
+ });
234
+
235
+ test('Declare Transaction Hash 0x79b130f2e808db6ab4b83f0182f016a128d73752b849e5b0221c2b3a35a87ea', async () => {
236
+ const transaction = await provider.getTransaction(
237
+ '0x79b130f2e808db6ab4b83f0182f016a128d73752b849e5b0221c2b3a35a87ea'
238
+ );
239
+
240
+ expect(transaction.max_fee).toBeTruthy();
241
+ expect(transaction.transaction_hash).toBeTruthy();
242
+ expect(transaction).toHaveProperty('nonce');
243
+ expect(transaction).toHaveProperty('sender_address');
244
+ expect(transaction).toHaveProperty('version');
245
+ });
246
+ });
247
+
248
+ describe('getTransactionReceipt', () => {
249
+ test('Transaction Hash 0x37013e1cb9c133e6fe51b4b371b76b317a480f56d80576730754c1662582348', async () => {
250
+ const receipt = await provider.getTransactionReceipt(
251
+ '0x37013e1cb9c133e6fe51b4b371b76b317a480f56d80576730754c1662582348'
252
+ );
253
+
254
+ expect(receipt).toHaveProperty('transaction_hash');
255
+ expect(receipt).toHaveProperty('status');
256
+ expect(receipt).toHaveProperty('status_data');
257
+ expect(receipt).toHaveProperty('messages_sent');
258
+ expect(receipt).toHaveProperty('l1_origin_message');
259
+ expect(receipt).toHaveProperty('events');
260
+ });
261
+ });
262
+
263
+ describe('Contract methods', () => {
264
+ let contractAddress: string;
265
+ let deployResponse: DeployContractResponse;
266
+ let declareResponse: DeclareContractResponse;
267
+
268
+ beforeAll(async () => {
269
+ deployResponse = await provider.deployContract({ contract: compiledErc20 });
270
+ contractAddress = deployResponse.contract_address;
271
+ declareResponse = await provider.declareContract({ contract: compiledErc20 });
272
+ await Promise.all([
273
+ provider.waitForTransaction(deployResponse.transaction_hash),
274
+ provider.waitForTransaction(declareResponse.transaction_hash),
275
+ ]);
276
+ });
277
+
278
+ describe('deployContract', () => {
279
+ test('response', () => {
280
+ expect(deployResponse.contract_address).toBeTruthy();
281
+ expect(deployResponse.transaction_hash).toBeTruthy();
282
+ });
283
+ });
284
+
285
+ describe('declareContract', () => {
286
+ test('response', async () => {
287
+ expect(declareResponse.class_hash).toBeTruthy();
288
+ expect(declareResponse.transaction_hash).toBeTruthy();
289
+ });
290
+ });
291
+
292
+ describe('getClassAt', () => {
293
+ test('response', async () => {
294
+ const classResponse = await provider.getClassAt(contractAddress);
295
+
296
+ expect(classResponse).toHaveProperty('program');
297
+ expect(classResponse).toHaveProperty('entry_points_by_type');
298
+ });
299
+ });
300
+
301
+ describe('callContract', () => {
302
+ test('result', () => {
303
+ return expect(
304
+ provider
305
+ .callContract({
306
+ contractAddress: deployResponse.contract_address,
307
+ entrypoint: 'balance_of',
308
+ calldata: compileCalldata({
309
+ user: '0x9ff64f4ab0e1fe88df4465ade98d1ea99d5732761c39279b8e1374fa943e9b',
310
+ }),
311
+ })
312
+ .then((res) => {
313
+ expect(Array.isArray(res.result)).toBe(true);
314
+ })
315
+ ).resolves.not.toThrow();
316
+ });
317
+ });
318
+ });
319
+ });
320
+ });
321
+ });
@@ -1,6 +1,6 @@
1
1
  import fs from 'fs';
2
2
 
3
- import { Account, Provider, ec, json } from '../src';
3
+ import { Account, ProviderInterface, RpcProvider, SequencerProvider, ec, json } from '../src';
4
4
  import { CompiledContract } from '../src/types';
5
5
 
6
6
  const readContract = (name: string): CompiledContract =>
@@ -18,10 +18,20 @@ const DEFAULT_TEST_ACCOUNT_ADDRESS = // run `starknet-devnet --seed 0` and this
18
18
  const DEFAULT_TEST_ACCOUNT_PRIVATE_KEY = '0xe3e70682c2094cac629f6fbed82c07cd';
19
19
 
20
20
  const BASE_URL = process.env.TEST_PROVIDER_BASE_URL || DEFAULT_TEST_PROVIDER_BASE_URL;
21
- export const IS_DEVNET = !BASE_URL.includes('starknet.io');
21
+ const RPC_URL = process.env.TEST_RPC_URL;
22
+
23
+ const IS_RPC = !!RPC_URL;
24
+ const IS_RPC_DEVNET = Boolean(
25
+ RPC_URL && (RPC_URL.includes('localhost') || RPC_URL.includes('127.0.0.1'))
26
+ );
27
+ const IS_SEQUENCER = !IS_RPC;
28
+ const IS_SEQUENCER_DEVNET = !BASE_URL.includes('starknet.io');
29
+ export const IS_DEVNET = IS_SEQUENCER ? IS_SEQUENCER_DEVNET : IS_RPC_DEVNET;
22
30
 
23
31
  export const getTestProvider = () => {
24
- const provider = new Provider({ baseUrl: BASE_URL });
32
+ const provider = RPC_URL
33
+ ? new RpcProvider({ nodeUrl: RPC_URL })
34
+ : new SequencerProvider({ baseUrl: BASE_URL });
25
35
 
26
36
  if (IS_DEVNET) {
27
37
  // accelerate the tests when running locally
@@ -35,16 +45,27 @@ export const getTestProvider = () => {
35
45
  };
36
46
 
37
47
  // test account with fee token balance
38
- export const getTestAccount = () => {
39
- const provider = getTestProvider();
48
+ export const getTestAccount = (provider: ProviderInterface) => {
49
+ let testAccountAddress = process.env.TEST_ACCOUNT_ADDRESS;
50
+ let testAccountPrivateKey = process.env.TEST_ACCOUNT_PRIVATE_KEY;
51
+
52
+ if (!IS_DEVNET) {
53
+ if (!testAccountPrivateKey) {
54
+ throw new Error('TEST_ACCOUNT_PRIVATE_KEY is not set');
55
+ }
40
56
 
41
- const testAccountAddress = process.env.TEST_ACCOUNT_ADDRESS || DEFAULT_TEST_ACCOUNT_ADDRESS;
42
- const testAccountPrivateKey =
43
- process.env.TEST_ACCOUNT_PRIVATE_KEY || DEFAULT_TEST_ACCOUNT_PRIVATE_KEY;
57
+ if (!testAccountAddress) {
58
+ throw new Error('TEST_ACCOUNT_ADDRESS is not set');
59
+ }
60
+ } else {
61
+ testAccountAddress = DEFAULT_TEST_ACCOUNT_ADDRESS;
62
+ testAccountPrivateKey = DEFAULT_TEST_ACCOUNT_PRIVATE_KEY;
63
+ }
44
64
 
45
65
  return new Account(provider, testAccountAddress, ec.getKeyPair(testAccountPrivateKey));
46
66
  };
47
67
 
48
- export const testIf = (condition: boolean) => (condition ? test : test.skip);
49
- export const testIfDevnet = testIf(IS_DEVNET);
50
- export const testIfNotDevnet = testIf(!IS_DEVNET);
68
+ const describeIf = (condition: boolean) => (condition ? describe : describe.skip);
69
+ export const describeIfSequencer = describeIf(IS_DEVNET);
70
+ export const describeIfRpc = describeIf(IS_RPC);
71
+ export const describeIfNotDevnet = describeIf(!IS_DEVNET);
@@ -0,0 +1,17 @@
1
+ import { RpcProvider } from '../src';
2
+ import { describeIfRpc, getTestProvider } from './fixtures';
3
+
4
+ describeIfRpc('RPCProvider', () => {
5
+ let provider: RpcProvider;
6
+
7
+ beforeAll(async () => {
8
+ provider = getTestProvider() as RpcProvider;
9
+ });
10
+
11
+ describe('RPC methods', () => {
12
+ test('getChainId', async () => {
13
+ const chainId = await provider.getChainId();
14
+ expect(chainId).toBe('0x534e5f474f45524c49');
15
+ });
16
+ });
17
+ });
@@ -0,0 +1,45 @@
1
+ import { SequencerProvider } from '../src';
2
+ import {
3
+ compiledErc20,
4
+ describeIfNotDevnet,
5
+ describeIfSequencer,
6
+ getTestProvider,
7
+ } from './fixtures';
8
+
9
+ describeIfSequencer('SequencerProvider', () => {
10
+ let provider: SequencerProvider;
11
+
12
+ beforeAll(async () => {
13
+ provider = getTestProvider() as SequencerProvider;
14
+ });
15
+
16
+ describe('Gateway specific methods', () => {
17
+ let exampleTransactionHash: string;
18
+
19
+ beforeAll(async () => {
20
+ const { transaction_hash } = await provider.deployContract({
21
+ contract: compiledErc20,
22
+ });
23
+ await provider.waitForTransaction(transaction_hash);
24
+ exampleTransactionHash = transaction_hash;
25
+ });
26
+
27
+ test('getTransactionStatus()', async () => {
28
+ return expect(provider.getTransactionStatus(exampleTransactionHash)).resolves.not.toThrow();
29
+ });
30
+
31
+ test('transaction trace', async () => {
32
+ const transactionTrace = await provider.getTransactionTrace(exampleTransactionHash);
33
+ expect(transactionTrace).toHaveProperty('function_invocation');
34
+ expect(transactionTrace).toHaveProperty('signature');
35
+ });
36
+
37
+ describeIfNotDevnet('which are not available on devnet', () => {
38
+ test('getContractAddresses()', async () => {
39
+ const { GpsStatementVerifier, Starknet } = await provider.getContractAddresses();
40
+ expect(typeof GpsStatementVerifier).toBe('string');
41
+ expect(typeof Starknet).toBe('string');
42
+ });
43
+ });
44
+ });
45
+ });
@@ -1,4 +1,5 @@
1
1
  import typedDataExample from '../../__mocks__/typedDataExample.json';
2
+ import typedDataStructArrayExample from '../../__mocks__/typedDataStructArrayExample.json';
2
3
  import { number } from '../../src';
3
4
  import { BigNumberish } from '../../src/utils/number';
4
5
  import { encodeType, getMessageHash, getStructHash, getTypeHash } from '../../src/utils/typedData';
@@ -9,7 +10,12 @@ describe('typedData', () => {
9
10
  expect(typeEncoding).toMatchInlineSnapshot(
10
11
  `"Mail(from:Person,to:Person,contents:felt)Person(name:felt,wallet:felt)"`
11
12
  );
13
+ const typeEncodingStructArr = encodeType(typedDataStructArrayExample, 'Mail');
14
+ expect(typeEncodingStructArr).toMatchInlineSnapshot(
15
+ `"Mail(from:Person,to:Person,posts_len:felt,posts:Post*)Person(name:felt,wallet:felt)Post(title:felt,content:felt)"`
16
+ );
12
17
  });
18
+
13
19
  test('should get right type hash', () => {
14
20
  const typeHashDomain = getTypeHash(typedDataExample, 'StarkNetDomain');
15
21
  expect(typeHashDomain).toMatchInlineSnapshot(
@@ -23,18 +29,36 @@ describe('typedData', () => {
23
29
  expect(typeHashMail).toMatchInlineSnapshot(
24
30
  `"0x13d89452df9512bf750f539ba3001b945576243288137ddb6c788457d4b2f79"`
25
31
  );
32
+ const typeHashPost = getTypeHash(typedDataStructArrayExample, 'Post');
33
+ expect(typeHashPost).toMatchInlineSnapshot(
34
+ `"0x1d71e69bf476486b43cdcfaf5a85c00bb2d954c042b281040e513080388356d"`
35
+ );
36
+ const typeHashMailWithStructArray = getTypeHash(typedDataStructArrayExample, 'Mail');
37
+ expect(typeHashMailWithStructArray).toMatchInlineSnapshot(
38
+ `"0x873b878e35e258fc99e3085d5aaad3a81a0c821f189c08b30def2cde55ff27"`
39
+ );
26
40
  });
41
+
27
42
  test('should get right hash for StarkNetDomain', () => {
28
43
  const hash = getStructHash(typedDataExample, 'StarkNetDomain', typedDataExample.domain as any);
29
44
  expect(hash).toMatchInlineSnapshot(
30
45
  `"0x54833b121883a3e3aebff48ec08a962f5742e5f7b973469c1f8f4f55d470b07"`
31
46
  );
32
47
  });
48
+
33
49
  test('should get right hash for entire message', () => {
34
50
  const hash = getMessageHash(typedDataExample, '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826');
35
51
  expect(hash).toMatchInlineSnapshot(
36
52
  `"0x6fcff244f63e38b9d88b9e3378d44757710d1b244282b435cb472053c8d78d0"`
37
53
  );
54
+
55
+ const hashStructArr = getMessageHash(
56
+ typedDataStructArrayExample,
57
+ '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826'
58
+ );
59
+ expect(hashStructArr).toMatchInlineSnapshot(
60
+ `"0x5914ed2764eca2e6a41eb037feefd3d2e33d9af6225a9e7fe31ac943ff712c"`
61
+ );
38
62
  });
39
63
 
40
64
  interface StringStruct {
@@ -1,84 +1,61 @@
1
- import { Provider, ProviderInterface } from '../provider';
1
+ import { ProviderInterface, ProviderOptions } from '../provider';
2
+ import { Provider } from '../provider/default';
2
3
  import { SignerInterface } from '../signer';
3
- import {
4
- Abi,
5
- AddTransactionResponse,
6
- Call,
7
- InvocationsDetails,
8
- KeyPair,
9
- Signature,
10
- Transaction,
11
- } from '../types';
4
+ import { Abi, Call, InvocationsDetails, InvokeFunctionResponse, KeyPair, Signature } from '../types';
12
5
  import { EstimateFee, EstimateFeeDetails } from '../types/account';
13
6
  import { BigNumberish } from '../utils/number';
14
7
  import { TypedData } from '../utils/typedData';
15
8
  import { AccountInterface } from './interface';
16
9
  export declare class Account extends Provider implements AccountInterface {
17
- address: string;
18
- signer: SignerInterface;
19
- constructor(
20
- provider: ProviderInterface,
21
- address: string,
22
- keyPairOrSigner: KeyPair | SignerInterface
23
- );
24
- getNonce(): Promise<string>;
25
- estimateFee(
26
- calls: Call | Call[],
27
- { nonce: providedNonce, blockIdentifier }?: EstimateFeeDetails
28
- ): Promise<EstimateFee>;
29
- /**
30
- * Invoke execute function in account contract
31
- *
32
- * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/gateway/gateway_client.py#L13-L17)
33
- *
34
- * @param calls - one or more calls to be executed
35
- * @param abis - one or more abis which can be used to display the calls
36
- * @param transactionsDetail - optional transaction details
37
- * @returns a confirmation of invoking a function on the starknet contract
38
- */
39
- execute(
40
- calls: Call | Call[],
41
- abis?: Abi[] | undefined,
42
- transactionsDetail?: InvocationsDetails
43
- ): Promise<AddTransactionResponse>;
44
- /**
45
- * Temporary method to allow dapps on starknet.js v2 to work with Argent X v3
46
- * @deprecated to remove ASAP
47
- */
48
- LEGACY_addTransaction(transaction: Transaction): Promise<AddTransactionResponse>;
49
- /**
50
- * Sign an JSON object with the starknet private key and return the signature
51
- *
52
- * @param json - JSON object to be signed
53
- * @returns the signature of the JSON object
54
- * @throws {Error} if the JSON object is not a valid JSON
55
- */
56
- signMessage(typedData: TypedData): Promise<Signature>;
57
- /**
58
- * Hash a JSON object with pederson hash and return the hash
59
- *
60
- * @param json - JSON object to be hashed
61
- * @returns the hash of the JSON object
62
- * @throws {Error} if the JSON object is not a valid JSON
63
- */
64
- hashMessage(typedData: TypedData): Promise<string>;
65
- /**
66
- * Verify a signature of a given hash
67
- * @warning This method is not recommended, use verifyMessage instead
68
- *
69
- * @param hash - JSON object to be verified
70
- * @param signature - signature of the JSON object
71
- * @returns true if the signature is valid, false otherwise
72
- * @throws {Error} if the JSON object is not a valid JSON or the signature is not a valid signature
73
- */
74
- verifyMessageHash(hash: BigNumberish, signature: Signature): Promise<boolean>;
75
- /**
76
- * Verify a signature of a JSON object
77
- *
78
- * @param hash - hash to be verified
79
- * @param signature - signature of the hash
80
- * @returns true if the signature is valid, false otherwise
81
- * @throws {Error} if the signature is not a valid signature
82
- */
83
- verifyMessage(typedData: TypedData, signature: Signature): Promise<boolean>;
10
+ address: string;
11
+ signer: SignerInterface;
12
+ constructor(providerOrOptions: ProviderOptions | ProviderInterface, address: string, keyPairOrSigner: KeyPair | SignerInterface);
13
+ getNonce(): Promise<string>;
14
+ estimateFee(calls: Call | Call[], { nonce: providedNonce, blockIdentifier }?: EstimateFeeDetails): Promise<EstimateFee>;
15
+ /**
16
+ * Invoke execute function in account contract
17
+ *
18
+ * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/gateway/gateway_client.py#L13-L17)
19
+ *
20
+ * @param calls - one or more calls to be executed
21
+ * @param abis - one or more abis which can be used to display the calls
22
+ * @param transactionsDetail - optional transaction details
23
+ * @returns a confirmation of invoking a function on the starknet contract
24
+ */
25
+ execute(calls: Call | Call[], abis?: Abi[] | undefined, transactionsDetail?: InvocationsDetails): Promise<InvokeFunctionResponse>;
26
+ /**
27
+ * Sign an JSON object with the starknet private key and return the signature
28
+ *
29
+ * @param json - JSON object to be signed
30
+ * @returns the signature of the JSON object
31
+ * @throws {Error} if the JSON object is not a valid JSON
32
+ */
33
+ signMessage(typedData: TypedData): Promise<Signature>;
34
+ /**
35
+ * Hash a JSON object with pederson hash and return the hash
36
+ *
37
+ * @param json - JSON object to be hashed
38
+ * @returns the hash of the JSON object
39
+ * @throws {Error} if the JSON object is not a valid JSON
40
+ */
41
+ hashMessage(typedData: TypedData): Promise<string>;
42
+ /**
43
+ * Verify a signature of a given hash
44
+ * @warning This method is not recommended, use verifyMessage instead
45
+ *
46
+ * @param hash - JSON object to be verified
47
+ * @param signature - signature of the JSON object
48
+ * @returns true if the signature is valid, false otherwise
49
+ * @throws {Error} if the JSON object is not a valid JSON or the signature is not a valid signature
50
+ */
51
+ verifyMessageHash(hash: BigNumberish, signature: Signature): Promise<boolean>;
52
+ /**
53
+ * Verify a signature of a JSON object
54
+ *
55
+ * @param hash - hash to be verified
56
+ * @param signature - signature of the hash
57
+ * @returns true if the signature is valid, false otherwise
58
+ * @throws {Error} if the signature is not a valid signature
59
+ */
60
+ verifyMessage(typedData: TypedData, signature: Signature): Promise<boolean>;
84
61
  }