starknet 3.18.2 → 3.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (158) hide show
  1. package/CHANGELOG.md +53 -0
  2. package/__tests__/account.test.ts +11 -56
  3. package/__tests__/contract.test.ts +11 -49
  4. package/__tests__/defaultProvider.test.ts +321 -0
  5. package/__tests__/fixtures.ts +32 -11
  6. package/__tests__/rpcProvider.test.ts +17 -0
  7. package/__tests__/sequencerProvider.test.ts +45 -0
  8. package/account/default.d.ts +54 -77
  9. package/account/default.js +271 -596
  10. package/account/index.js +18 -31
  11. package/account/interface.d.ts +66 -95
  12. package/account/interface.js +20 -30
  13. package/constants.d.ts +17 -19
  14. package/constants.js +2038 -2059
  15. package/contract/contractFactory.d.ts +25 -29
  16. package/contract/contractFactory.js +94 -210
  17. package/contract/default.d.ts +117 -146
  18. package/contract/default.js +582 -776
  19. package/contract/index.js +19 -32
  20. package/contract/interface.d.ts +72 -92
  21. package/contract/interface.js +6 -5
  22. package/dist/account/default.d.ts +5 -9
  23. package/dist/account/default.js +35 -169
  24. package/dist/account/interface.d.ts +3 -15
  25. package/dist/contract/contractFactory.js +4 -4
  26. package/dist/contract/default.d.ts +3 -3
  27. package/dist/contract/default.js +3 -2
  28. package/dist/contract/interface.d.ts +2 -2
  29. package/dist/provider/default.d.ts +18 -134
  30. package/dist/provider/default.js +47 -411
  31. package/dist/provider/index.d.ts +2 -0
  32. package/dist/provider/index.js +2 -0
  33. package/dist/provider/interface.d.ts +45 -50
  34. package/dist/provider/rpc.d.ts +57 -0
  35. package/dist/provider/rpc.js +364 -0
  36. package/dist/provider/sequencer.d.ts +66 -0
  37. package/dist/provider/sequencer.js +443 -0
  38. package/dist/types/account.d.ts +2 -3
  39. package/dist/types/api/index.d.ts +16 -0
  40. package/dist/types/api/index.js +18 -0
  41. package/dist/types/api/rpc.d.ts +221 -0
  42. package/dist/types/{api.js → api/rpc.js} +0 -0
  43. package/dist/types/api/sequencer.d.ts +289 -0
  44. package/dist/types/api/sequencer.js +2 -0
  45. package/dist/types/index.d.ts +3 -1
  46. package/dist/types/index.js +15 -1
  47. package/dist/types/lib.d.ts +3 -1
  48. package/dist/types/provider.d.ts +86 -0
  49. package/dist/types/provider.js +2 -0
  50. package/dist/utils/provider.d.ts +4 -0
  51. package/dist/utils/provider.js +38 -0
  52. package/dist/utils/responseParser/index.d.ts +11 -0
  53. package/dist/utils/responseParser/index.js +9 -0
  54. package/dist/utils/responseParser/rpc.d.ts +13 -0
  55. package/dist/utils/responseParser/rpc.js +96 -0
  56. package/dist/utils/responseParser/sequencer.d.ts +13 -0
  57. package/dist/utils/responseParser/sequencer.js +124 -0
  58. package/index.js +42 -75
  59. package/package.json +1 -1
  60. package/provider/default.d.ts +21 -175
  61. package/provider/default.js +139 -704
  62. package/provider/errors.d.ts +4 -4
  63. package/provider/errors.js +30 -40
  64. package/provider/index.d.ts +2 -0
  65. package/provider/index.js +22 -33
  66. package/provider/interface.d.ts +104 -131
  67. package/provider/interface.js +6 -5
  68. package/provider/rpc.d.ts +57 -0
  69. package/provider/rpc.js +364 -0
  70. package/provider/sequencer.d.ts +66 -0
  71. package/provider/sequencer.js +443 -0
  72. package/provider/utils.d.ts +7 -9
  73. package/provider/utils.js +39 -44
  74. package/signer/default.d.ts +5 -9
  75. package/signer/default.js +72 -177
  76. package/signer/index.js +18 -31
  77. package/signer/interface.d.ts +29 -33
  78. package/signer/interface.js +6 -5
  79. package/src/account/default.ts +26 -146
  80. package/src/account/interface.ts +5 -20
  81. package/src/contract/contractFactory.ts +3 -6
  82. package/src/contract/default.ts +6 -4
  83. package/src/contract/interface.ts +2 -2
  84. package/src/provider/default.ts +63 -395
  85. package/src/provider/index.ts +2 -0
  86. package/src/provider/interface.ts +68 -63
  87. package/src/provider/rpc.ts +300 -0
  88. package/src/provider/sequencer.ts +384 -0
  89. package/src/types/account.ts +2 -3
  90. package/src/types/api/index.ts +17 -0
  91. package/src/types/api/rpc.ts +247 -0
  92. package/src/types/api/sequencer.ts +331 -0
  93. package/src/types/index.ts +3 -1
  94. package/src/types/lib.ts +3 -1
  95. package/src/types/provider.ts +108 -0
  96. package/src/utils/provider.ts +28 -0
  97. package/src/utils/responseParser/index.ts +28 -0
  98. package/src/utils/responseParser/rpc.ts +93 -0
  99. package/src/utils/responseParser/sequencer.ts +127 -0
  100. package/types/account.d.ts +5 -7
  101. package/types/account.js +2 -2
  102. package/types/api/index.d.ts +16 -0
  103. package/types/api/index.js +18 -0
  104. package/types/api/rpc.d.ts +221 -0
  105. package/types/api/rpc.js +2 -0
  106. package/types/api/sequencer.d.ts +289 -0
  107. package/types/api/sequencer.js +2 -0
  108. package/types/contract.d.ts +1 -1
  109. package/types/contract.js +2 -2
  110. package/types/index.d.ts +3 -1
  111. package/types/index.js +35 -34
  112. package/types/lib.d.ts +36 -41
  113. package/types/lib.js +2 -2
  114. package/types/provider.d.ts +86 -0
  115. package/types/provider.js +2 -0
  116. package/types/signer.d.ts +2 -2
  117. package/types/signer.js +2 -2
  118. package/utils/address.js +26 -37
  119. package/utils/ellipticCurve.d.ts +1 -6
  120. package/utils/ellipticCurve.js +73 -137
  121. package/utils/encode.js +49 -85
  122. package/utils/hash.d.ts +4 -31
  123. package/utils/hash.js +76 -141
  124. package/utils/json.d.ts +13 -45
  125. package/utils/json.js +15 -22
  126. package/utils/number.d.ts +2 -9
  127. package/utils/number.js +47 -81
  128. package/utils/provider.d.ts +4 -0
  129. package/utils/provider.js +38 -0
  130. package/utils/responseParser/index.d.ts +11 -0
  131. package/utils/responseParser/index.js +9 -0
  132. package/utils/responseParser/rpc.d.ts +13 -0
  133. package/utils/responseParser/rpc.js +96 -0
  134. package/utils/responseParser/sequencer.d.ts +13 -0
  135. package/utils/responseParser/sequencer.js +124 -0
  136. package/utils/shortString.js +13 -21
  137. package/utils/stark.d.ts +0 -1
  138. package/utils/stark.js +59 -93
  139. package/utils/transaction.d.ts +3 -6
  140. package/utils/transaction.js +50 -81
  141. package/utils/typedData/index.d.ts +3 -15
  142. package/utils/typedData/index.js +109 -175
  143. package/utils/typedData/types.d.ts +9 -9
  144. package/utils/typedData/types.js +2 -2
  145. package/utils/typedData/utils.js +6 -6
  146. package/utils/uint256.d.ts +5 -5
  147. package/utils/uint256.js +16 -26
  148. package/www/docs/API/account.md +3 -4
  149. package/www/docs/API/contract.md +2 -2
  150. package/www/docs/API/contractFactory.md +2 -2
  151. package/www/docs/API/provider.md +185 -74
  152. package/www/guides/account.md +1 -8
  153. package/www/guides/erc20.md +3 -0
  154. package/__tests__/provider.test.ts +0 -168
  155. package/dist/types/api.d.ts +0 -261
  156. package/src/types/api.ts +0 -303
  157. package/types/api.d.ts +0 -287
  158. package/types/api.js +0 -2
@@ -1,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,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
  }