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.
- package/.github/workflows/pr.yml +3 -0
- package/.github/workflows/release.yml +4 -0
- package/CHANGELOG.md +39 -0
- package/README.md +2 -0
- package/__mocks__/Account.json +25468 -0
- package/__tests__/account.test.ts +102 -65
- package/__tests__/contract.test.ts +23 -65
- package/__tests__/fixtures.ts +21 -1
- package/__tests__/jest.setup.ts +23 -4
- package/__tests__/provider.test.ts +20 -1
- package/account/index.js +10 -6
- package/contract/default.js +20 -21
- package/contract/index.js +10 -6
- package/dist/account/index.js +5 -1
- package/dist/contract/default.js +18 -18
- package/dist/contract/index.js +5 -1
- package/dist/index.js +5 -1
- package/dist/provider/default.d.ts +1 -1
- package/dist/provider/default.js +35 -49
- package/dist/provider/index.js +5 -1
- package/dist/provider/interface.d.ts +1 -1
- package/dist/provider/utils.js +5 -5
- package/dist/signer/index.js +5 -1
- package/dist/types/api.d.ts +1 -1
- package/dist/types/index.js +5 -1
- package/dist/utils/ellipticCurve.js +1 -1
- package/dist/utils/encode.js +1 -1
- package/dist/utils/hash.js +1 -1
- package/dist/utils/number.js +8 -4
- package/dist/utils/shortString.js +2 -2
- package/dist/utils/typedData/index.d.ts +2 -36
- package/dist/utils/typedData/index.js +8 -4
- package/dist/utils/typedData/types.d.ts +15 -70
- package/dist/utils/typedData/types.js +0 -45
- package/dist/utils/typedData/utils.d.ts +2 -18
- package/dist/utils/typedData/utils.js +4 -3
- package/index.js +10 -6
- package/package.json +33 -31
- package/provider/default.d.ts +1 -1
- package/provider/default.js +44 -65
- package/provider/index.js +10 -6
- package/provider/interface.d.ts +1 -1
- package/provider/utils.js +5 -5
- package/signer/index.js +10 -6
- package/src/provider/default.ts +24 -31
- package/src/provider/interface.ts +1 -1
- package/src/types/api.ts +1 -1
- package/src/utils/typedData/types.ts +15 -68
- package/src/utils/typedData/utils.ts +7 -4
- package/types/api.d.ts +1 -1
- package/types/index.js +10 -6
- package/utils/ellipticCurve.js +1 -1
- package/utils/encode.js +1 -1
- package/utils/hash.js +1 -1
- package/utils/number.js +13 -9
- package/utils/shortString.js +2 -2
- package/utils/typedData/index.d.ts +2 -46
- package/utils/typedData/index.js +15 -13
- package/utils/typedData/types.d.ts +15 -91
- package/utils/typedData/types.js +0 -55
- package/utils/typedData/utils.d.ts +2 -21
- package/utils/typedData/utils.js +4 -3
- package/www/guides/account.md +21 -7
- package/www/guides/erc20.md +15 -27
- package/__tests__/accountContract.test.ts +0 -110
package/utils/typedData/utils.js
CHANGED
|
@@ -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
|
-
|
|
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;
|
package/www/guides/account.md
CHANGED
|
@@ -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
|
|
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
|
|
56
|
-
|
|
57
|
-
|
|
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
|
```
|
package/www/guides/erc20.md
CHANGED
|
@@ -4,7 +4,7 @@ sidebar_position: 3
|
|
|
4
4
|
|
|
5
5
|
# Deploy an ERC20 Contract
|
|
6
6
|
|
|
7
|
-
|
|
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
|
-
|
|
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
|
|
42
|
-
const balanceBeforeTransfer = await erc20.balance_of(
|
|
43
|
+
console.log(`Calling StarkNet for account balance...`);
|
|
44
|
+
const balanceBeforeTransfer = await erc20.balance_of(account.address);
|
|
43
45
|
|
|
44
46
|
console.log(
|
|
45
|
-
`
|
|
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
|
-
//
|
|
54
|
-
console.log(`
|
|
55
|
-
const
|
|
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
|
-
|
|
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
|
|
89
|
-
const balanceAfterTransfer = await erc20.balance_of(
|
|
76
|
+
console.log(`Calling StarkNet for account balance...`);
|
|
77
|
+
const balanceAfterTransfer = await erc20.balance_of(account.address);
|
|
90
78
|
|
|
91
79
|
console.log(
|
|
92
|
-
`
|
|
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
|
-
});
|