starknet 3.12.1 → 3.13.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 (65) hide show
  1. package/.github/workflows/pr.yml +3 -0
  2. package/.github/workflows/release.yml +4 -0
  3. package/CHANGELOG.md +39 -0
  4. package/README.md +2 -0
  5. package/__mocks__/Account.json +25468 -0
  6. package/__tests__/account.test.ts +102 -65
  7. package/__tests__/contract.test.ts +23 -65
  8. package/__tests__/fixtures.ts +21 -1
  9. package/__tests__/jest.setup.ts +23 -4
  10. package/__tests__/provider.test.ts +20 -1
  11. package/account/index.js +10 -6
  12. package/contract/default.js +20 -21
  13. package/contract/index.js +10 -6
  14. package/dist/account/index.js +5 -1
  15. package/dist/contract/default.js +18 -18
  16. package/dist/contract/index.js +5 -1
  17. package/dist/index.js +5 -1
  18. package/dist/provider/default.d.ts +1 -1
  19. package/dist/provider/default.js +35 -49
  20. package/dist/provider/index.js +5 -1
  21. package/dist/provider/interface.d.ts +1 -1
  22. package/dist/provider/utils.js +5 -5
  23. package/dist/signer/index.js +5 -1
  24. package/dist/types/api.d.ts +1 -1
  25. package/dist/types/index.js +5 -1
  26. package/dist/utils/ellipticCurve.js +1 -1
  27. package/dist/utils/encode.js +1 -1
  28. package/dist/utils/hash.js +1 -1
  29. package/dist/utils/number.js +8 -4
  30. package/dist/utils/shortString.js +2 -2
  31. package/dist/utils/typedData/index.d.ts +2 -36
  32. package/dist/utils/typedData/index.js +8 -4
  33. package/dist/utils/typedData/types.d.ts +15 -70
  34. package/dist/utils/typedData/types.js +0 -45
  35. package/dist/utils/typedData/utils.d.ts +2 -18
  36. package/dist/utils/typedData/utils.js +4 -3
  37. package/index.js +10 -6
  38. package/package.json +33 -31
  39. package/provider/default.d.ts +1 -1
  40. package/provider/default.js +44 -65
  41. package/provider/index.js +10 -6
  42. package/provider/interface.d.ts +1 -1
  43. package/provider/utils.js +5 -5
  44. package/signer/index.js +10 -6
  45. package/src/provider/default.ts +24 -31
  46. package/src/provider/interface.ts +1 -1
  47. package/src/types/api.ts +1 -1
  48. package/src/utils/typedData/types.ts +15 -68
  49. package/src/utils/typedData/utils.ts +7 -4
  50. package/types/api.d.ts +1 -1
  51. package/types/index.js +10 -6
  52. package/utils/ellipticCurve.js +1 -1
  53. package/utils/encode.js +1 -1
  54. package/utils/hash.js +1 -1
  55. package/utils/number.js +13 -9
  56. package/utils/shortString.js +2 -2
  57. package/utils/typedData/index.d.ts +2 -46
  58. package/utils/typedData/index.js +15 -13
  59. package/utils/typedData/types.d.ts +15 -91
  60. package/utils/typedData/types.js +0 -55
  61. package/utils/typedData/utils.d.ts +2 -21
  62. package/utils/typedData/utils.js +4 -3
  63. package/www/guides/account.md +21 -7
  64. package/www/guides/erc20.md +15 -27
  65. package/__tests__/accountContract.test.ts +0 -110
@@ -1,8 +1,6 @@
1
1
  'use strict';
2
2
  Object.defineProperty(exports, '__esModule', { value: true });
3
3
  exports.validateTypedData = void 0;
4
- var superstruct_1 = require('superstruct');
5
- var types_1 = require('./types');
6
4
  /**
7
5
  * Validates that `data` matches the EIP-712 JSON schema.
8
6
  *
@@ -10,6 +8,9 @@ var types_1 = require('./types');
10
8
  * @return {boolean}
11
9
  */
12
10
  var validateTypedData = function (data) {
13
- return (0, superstruct_1.is)(data, types_1.STARKNET_TYPED_DATA_TYPE);
11
+ var typedData = data;
12
+ // Validate that the data matches the EIP-712 JSON schema
13
+ var valid = Boolean(typedData.types && typedData.primaryType && typedData.message);
14
+ return valid;
14
15
  };
15
16
  exports.validateTypedData = validateTypedData;
@@ -14,7 +14,15 @@ Install the latest version of starknet with `npm install starknet@next`
14
14
 
15
15
  ```javascript
16
16
  import fs from "fs";
17
- import * as starknet from "starknet";
17
+ import fs from "fs";
18
+ import {
19
+ Account,
20
+ Contract,
21
+ defaultProvider,
22
+ ec,
23
+ json,
24
+ number,
25
+ } from "starknet";
18
26
  ```
19
27
 
20
28
  ## Generate random key pair.
@@ -44,17 +52,23 @@ const accountResponse = await defaultProvider.deployContract({
44
52
 
45
53
  Wait for the deployment transaction to be accepted and assign the address of the deployed account to the Account object.
46
54
 
47
- Use your new account object to sign transactions, messages or verify signatures!
48
-
49
55
  ```javascript
50
56
  await defaultProvider.waitForTransaction(accountResponse.transaction_hash);
51
57
  const accountContract = new Contract(
52
58
  compiledArgentAccount.abi,
53
59
  accountResponse.address
54
60
  );
55
- const { transaction_hash: initializeTxHash } = await accountContract.initialize(
56
- starkKeyPub,
57
- "0"
61
+ const initializeResponse = await accountContract.initialize(starkKeyPub, "0");
62
+
63
+ await defaultProvider.waitForTransaction(initializeResponse.transaction_hash);
64
+ ```
65
+
66
+ Once account contract is initialized [Account](../docs/API/account.md) instance can be created. Use your new account instance to sign transactions, messages or verify signatures!
67
+
68
+ ```js
69
+ const account = new Account(
70
+ defaultProvider,
71
+ accountResponse.address,
72
+ starkKeyPair
58
73
  );
59
- await defaultProvider.waitForTransaction(initializeTxHash);
60
74
  ```
@@ -4,7 +4,7 @@ sidebar_position: 3
4
4
 
5
5
  # Deploy an ERC20 Contract
6
6
 
7
- Dpeploy the contract and wait for deployment transaction to be accepted on StarkNet
7
+ Deploy the contract and wait for deployment transaction to be accepted on StarkNet
8
8
 
9
9
  ```javascript
10
10
  const compiledErc20 = json.parse(
@@ -26,9 +26,11 @@ const erc20 = new Contract(compiledErc20.abi, erc20Address);
26
26
 
27
27
  ## Mint tokens to an account address
28
28
 
29
+ Make sure you created the `Account` instance following the [Creating an Account](./account.md) guide.
30
+
29
31
  ```javascript
30
32
  const { transaction_hash: mintTxHash } = await erc20.mint(
31
- accountContract.address,
33
+ account.address,
32
34
  "1000"
33
35
  );
34
36
  console.log(`Waiting for Tx to be Accepted on Starknet - Minting...`);
@@ -38,11 +40,11 @@ await defaultProvider.waitForTransaction(mintTxHash);
38
40
  ## Check balance after mint
39
41
 
40
42
  ```javascript
41
- console.log(`Calling StarkNet for accountContract balance...`);
42
- const balanceBeforeTransfer = await erc20.balance_of(accountContract.address);
43
+ console.log(`Calling StarkNet for account balance...`);
44
+ const balanceBeforeTransfer = await erc20.balance_of(account.address);
43
45
 
44
46
  console.log(
45
- `accountContract Address ${accountContract.address} has a balance of:`,
47
+ `account Address ${account.address} has a balance of:`,
46
48
  number.toBN(balanceBeforeTransfer.res, 16).toString()
47
49
  );
48
50
  ```
@@ -50,30 +52,16 @@ console.log(
50
52
  ## Transfer tokens
51
53
 
52
54
  ```javascript
53
- // Get the nonce of the account and prepare transfer tx
54
- console.log(`Calling StarkNet for accountContract nonce...`);
55
- const nonce = (await accountContract.call("get_nonce")).nonce.toString();
56
- const calls = [
55
+ // Execute tx transfer of 10 tokens
56
+ console.log(`Invoke Tx - Transfer 10 tokens back to erc20 contract...`);
57
+ const { transaction_hash: transferTxHash } = await account.execute(
57
58
  {
58
59
  contractAddress: erc20Address,
59
60
  entrypoint: "transfer",
60
61
  calldata: [erc20Address, "10"],
61
62
  },
62
- ];
63
- const msgHash = hash.hashMulticall(accountContract.address, calls, nonce, "0");
64
-
65
- const { callArray, calldata } = transformCallsToMulticallArrays(calls);
66
-
67
- // sign tx to transfer 10 tokens
68
- const signature = ec.sign(starkKeyPair, msgHash);
69
-
70
- // Execute tx transfer of 10 tokens
71
- console.log(`Invoke Tx - Transfer 10 tokens back to erc20 contract...`);
72
- const { transaction_hash: transferTxHash } = await accountContract.__execute__(
73
- callArray,
74
- calldata,
75
- nonce,
76
- signature
63
+ undefined,
64
+ { maxFee: "0" }
77
65
  );
78
66
 
79
67
  // Wait for the invoke transaction to be accepted on StarkNet
@@ -85,11 +73,11 @@ await defaultProvider.waitForTransaction(transferTxHash);
85
73
 
86
74
  ```javascript
87
75
  // Check balance after transfer - should be 990
88
- console.log(`Calling StarkNet for accountContract balance...`);
89
- const balanceAfterTransfer = await erc20.balance_of(accountContract.address);
76
+ console.log(`Calling StarkNet for account balance...`);
77
+ const balanceAfterTransfer = await erc20.balance_of(account.address);
90
78
 
91
79
  console.log(
92
- `accountContract Address ${accountContract.address} has a balance of:`,
80
+ `account Address ${account.address} has a balance of:`,
93
81
  number.toBN(balanceAfterTransfer.res, 16).toString()
94
82
  );
95
83
  ```
@@ -1,110 +0,0 @@
1
- import { Contract, defaultProvider, ec, hash, number, stark } from '../src';
2
- import { StarknetChainId } from '../src/constants';
3
- import {
4
- calculcateTransactionHash,
5
- getSelectorFromName,
6
- transactionVersion,
7
- } from '../src/utils/hash';
8
- import { fromCallsToExecuteCalldataWithNonce } from '../src/utils/transaction';
9
- import { compiledArgentAccount, compiledErc20 } from './fixtures';
10
-
11
- describe('getStarkAccountFromPrivateKey()', () => {
12
- test('it works with valid privateKey', () => {
13
- const privateKey = '0xb696427c0d79c5d28a1fa6f748bae1b98b3f4b86bd1a2505bab144673c856fa9';
14
-
15
- const starkKeyPair = ec.getKeyPair(privateKey);
16
- const starkKey = ec.getStarkKey(starkKeyPair);
17
-
18
- expect(starkKey).toBe('0x060d46f8d7ef3d83ed05f3ed9beb91e22f9529289b9d863683fd71eafaf28035');
19
- });
20
- test('it works with valid privateKey', () => {
21
- const privateKey = '0x5f65099e269b080000000000000000000000000000000000000000000000000';
22
-
23
- const starkKeyPair = ec.getKeyPair(privateKey);
24
- const starkKey = ec.getStarkKey(starkKeyPair);
25
-
26
- expect(starkKey).toBe('0xf321e59b257a577836d8313150aabd21f412491358c329966218df76bab591');
27
- });
28
- });
29
-
30
- test('build tx', async () => {
31
- const privateKey = '0x1B69B4BE052FAB1';
32
- const keyPair = ec.getKeyPair(privateKey);
33
- const address = ec.getStarkKey(keyPair);
34
-
35
- expect(address).toBe('0x04024999b9574cb7623679ce049a609db62a95098982c5b28ac61abdebd1c82b');
36
-
37
- const selector = hash.getSelectorFromName('transfer');
38
-
39
- expect(selector).toMatchInlineSnapshot(
40
- `"0x83afd3f4caedc6eebf44246fe54e38c95e3179a5ec9ea81740eca5b482d12e"`
41
- );
42
-
43
- const calls = [{ contractAddress: '1', entrypoint: 'transfer', calldata: ['6', '7'] }];
44
- const calldata = fromCallsToExecuteCalldataWithNonce(calls, 0);
45
-
46
- const msgHash = calculcateTransactionHash(
47
- address,
48
- transactionVersion,
49
- getSelectorFromName('__execute__'),
50
- calldata,
51
- 0,
52
- StarknetChainId.TESTNET
53
- );
54
- expect(number.toBN(msgHash).toString()).toMatchInlineSnapshot(
55
- `"235855380881994314533025886817815774848495061484535023348790852315407085619"`
56
- );
57
-
58
- const [r, s] = ec.sign(keyPair, msgHash);
59
- expect(r.toString()).toMatchInlineSnapshot(
60
- `"181489288548431284937202760565682158657883789985879744111612429574110648095"`
61
- );
62
- expect(s.toString()).toMatchInlineSnapshot(
63
- `"2055384802167699202203509702082340762385659879831017273872106910763470114538"`
64
- );
65
- });
66
-
67
- describe('deploy and test Wallet', () => {
68
- const privateKey = stark.randomAddress();
69
-
70
- const starkKeyPair = ec.getKeyPair(privateKey);
71
- const starkKeyPub = ec.getStarkKey(starkKeyPair);
72
- let accountContract: Contract;
73
- let erc20: Contract;
74
- let erc20Address: string;
75
-
76
- beforeAll(async () => {
77
- const accountResponse = await defaultProvider.deployContract({
78
- contract: compiledArgentAccount,
79
- addressSalt: starkKeyPub,
80
- });
81
- accountContract = new Contract(compiledArgentAccount.abi, accountResponse.address);
82
- expect(accountResponse.code).toBe('TRANSACTION_RECEIVED');
83
-
84
- const initializeResponse = await accountContract.initialize(starkKeyPub, '0');
85
- expect(initializeResponse.code).toBe('TRANSACTION_RECEIVED');
86
-
87
- const erc20Response = await defaultProvider.deployContract({
88
- contract: compiledErc20,
89
- });
90
- erc20Address = erc20Response.address;
91
- erc20 = new Contract(compiledErc20.abi, erc20Address);
92
- expect(erc20Response.code).toBe('TRANSACTION_RECEIVED');
93
-
94
- const mintResponse = await erc20.mint(accountContract.address, '1000');
95
- expect(mintResponse.code).toBe('TRANSACTION_RECEIVED');
96
- await defaultProvider.waitForTransaction(mintResponse.transaction_hash);
97
- });
98
-
99
- test('read nonce', async () => {
100
- const { nonce } = await accountContract.get_nonce();
101
-
102
- expect(number.toBN(nonce as string).toString()).toStrictEqual(number.toBN(0).toString());
103
- });
104
-
105
- test('read balance of wallet', async () => {
106
- const { res } = await erc20.balance_of(accountContract.address);
107
-
108
- expect(res).toStrictEqual(number.toBN(1000));
109
- });
110
- });