starknet 2.7.1 → 3.0.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/.eslintrc +3 -1
- package/CHANGELOG.md +47 -0
- package/CONTRIBUTING.md +1 -1
- package/README.md +18 -16
- package/__mocks__/typedDataExample.json +35 -0
- package/__tests__/account.test.ts +52 -87
- package/__tests__/accountContract.test.ts +160 -0
- package/__tests__/contract.test.ts +3 -1
- package/__tests__/jest.setup.ts +9 -0
- package/__tests__/provider.test.ts +16 -33
- package/__tests__/utils/address.test.ts +16 -0
- package/__tests__/utils/typedData.test.ts +1 -36
- package/account/default.d.ts +66 -0
- package/account/default.js +440 -0
- package/account/index.d.ts +2 -0
- package/account/index.js +27 -0
- package/account/interface.d.ts +83 -0
- package/account/interface.js +37 -0
- package/constants.d.ts +2 -0
- package/constants.js +4 -0
- package/contract.d.ts +6 -6
- package/contract.js +16 -14
- package/dist/account/default.d.ts +55 -0
- package/dist/account/default.js +272 -0
- package/dist/account/index.d.ts +2 -0
- package/dist/account/index.js +14 -0
- package/dist/account/interface.d.ts +69 -0
- package/dist/account/interface.js +27 -0
- package/dist/constants.d.ts +2 -0
- package/dist/constants.js +3 -1
- package/dist/contract.d.ts +6 -6
- package/dist/contract.js +9 -12
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/provider/default.d.ts +27 -16
- package/dist/provider/default.js +157 -100
- package/dist/provider/interface.d.ts +29 -32
- package/dist/provider/utils.d.ts +21 -5
- package/dist/provider/utils.js +53 -10
- package/dist/signer/default.d.ts +7 -31
- package/dist/signer/default.js +25 -121
- package/dist/signer/index.d.ts +1 -1
- package/dist/signer/index.js +1 -1
- package/dist/signer/interface.d.ts +17 -18
- package/dist/signer/interface.js +2 -20
- package/dist/{types.d.ts → types/api.d.ts} +72 -41
- package/dist/{types.js → types/api.js} +0 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/index.js +15 -0
- package/dist/types/lib.d.ts +57 -0
- package/dist/types/lib.js +2 -0
- package/dist/types/signer.d.ts +4 -0
- package/dist/types/signer.js +2 -0
- package/dist/utils/address.d.ts +2 -0
- package/dist/utils/address.js +22 -0
- package/dist/utils/number.d.ts +1 -0
- package/dist/utils/number.js +5 -1
- package/index.d.ts +2 -1
- package/index.js +2 -1
- package/package.json +8 -2
- package/provider/default.d.ts +45 -36
- package/provider/default.js +216 -201
- package/provider/interface.d.ts +36 -49
- package/provider/utils.d.ts +23 -8
- package/provider/utils.js +57 -11
- package/signer/default.d.ts +11 -31
- package/signer/default.js +52 -169
- package/signer/index.d.ts +1 -1
- package/signer/index.js +1 -1
- package/signer/interface.d.ts +21 -18
- package/signer/interface.js +3 -32
- package/src/account/default.ts +152 -0
- package/src/account/index.ts +2 -0
- package/src/account/interface.ts +91 -0
- package/src/constants.ts +2 -0
- package/src/contract.ts +17 -18
- package/src/index.ts +2 -1
- package/src/provider/default.ts +141 -110
- package/src/provider/interface.ts +36 -52
- package/src/provider/utils.ts +60 -13
- package/src/signer/default.ts +33 -76
- package/src/signer/index.ts +1 -1
- package/src/signer/interface.ts +21 -20
- package/src/types/api.ts +165 -0
- package/src/types/index.ts +3 -0
- package/src/types/lib.ts +73 -0
- package/src/types/signer.ts +5 -0
- package/src/utils/address.ts +23 -0
- package/src/utils/number.ts +4 -0
- package/types/api.d.ts +152 -0
- package/{types.js → types/api.js} +0 -0
- package/types/index.d.ts +3 -0
- package/types/index.js +28 -0
- package/types/lib.d.ts +64 -0
- package/types/lib.js +2 -0
- package/types/signer.d.ts +4 -0
- package/types/signer.js +2 -0
- package/utils/address.d.ts +2 -0
- package/utils/address.js +22 -0
- package/utils/number.d.ts +3 -0
- package/utils/number.js +8 -1
- package/__tests__/signer.test.ts +0 -119
- package/src/types.ts +0 -131
- package/types.d.ts +0 -116
package/.eslintrc
CHANGED
|
@@ -18,7 +18,9 @@
|
|
|
18
18
|
},
|
|
19
19
|
"plugins": ["@typescript-eslint"],
|
|
20
20
|
"rules": {
|
|
21
|
+
"class-methods-use-this": 0,
|
|
21
22
|
"import/prefer-default-export": 0,
|
|
22
|
-
"@typescript-eslint/naming-convention": 0
|
|
23
|
+
"@typescript-eslint/naming-convention": 0,
|
|
24
|
+
"@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_" }]
|
|
23
25
|
}
|
|
24
26
|
}
|
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,50 @@
|
|
|
1
|
+
# [3.0.0](https://github.com/seanjameshan/starknet.js/compare/v2.9.0...v3.0.0) (2022-02-10)
|
|
2
|
+
|
|
3
|
+
### Bug Fixes
|
|
4
|
+
|
|
5
|
+
- **account:** dont allow additional signatures in execute ([ac02d46](https://github.com/seanjameshan/starknet.js/commit/ac02d46c5759904886f56a7b9e7e2686dbf52d4c))
|
|
6
|
+
- comments ([f261120](https://github.com/seanjameshan/starknet.js/commit/f261120e1971710cde8f95bd17a5869860db8160))
|
|
7
|
+
- review comments ([ee93bd6](https://github.com/seanjameshan/starknet.js/commit/ee93bd6e51eabcf3507a77d21c5bdc6fe93d5d1c))
|
|
8
|
+
- **test:** remove only for getTransactionReceipt test ([eb11d3b](https://github.com/seanjameshan/starknet.js/commit/eb11d3b60f434f81390f4d13c0462ad8c7ed7321))
|
|
9
|
+
- **types:** change type of `block_number` from `number` to `BlockNumber` in TransactionReceipt ([7a48ff8](https://github.com/seanjameshan/starknet.js/commit/7a48ff8e15a93a778fec62fdff10f92b1a073dec))
|
|
10
|
+
- **types:** update TransactionReceipt type ([9f9f6a2](https://github.com/seanjameshan/starknet.js/commit/9f9f6a2f4cf3b8bfb438edda06ec38bd1471c03d))
|
|
11
|
+
|
|
12
|
+
### Features
|
|
13
|
+
|
|
14
|
+
- **account:** introduce execute method ([0be78c6](https://github.com/seanjameshan/starknet.js/commit/0be78c67a40d0b457c571a57a8b49008f5e423de))
|
|
15
|
+
- **contract:** add an option to pass block identifier ([e34dd86](https://github.com/seanjameshan/starknet.js/commit/e34dd86c07495b7b8c38ee410838e15226a7b8d6))
|
|
16
|
+
- new signer and provider interface ([4b2a71c](https://github.com/seanjameshan/starknet.js/commit/4b2a71c578e363b11abb1c3adb74472bbb9f73fe))
|
|
17
|
+
- **provider:** add getTransactionReceipt() ([4267a5f](https://github.com/seanjameshan/starknet.js/commit/4267a5f5cec77bb4b09b2e8b4979ec0ee611d416))
|
|
18
|
+
- split account and signer ([ca4ad9d](https://github.com/seanjameshan/starknet.js/commit/ca4ad9d62aee8b0637096eb3059e96dd4407f663))
|
|
19
|
+
- update readme ([ed021dc](https://github.com/seanjameshan/starknet.js/commit/ed021dc25a394affec8ea92ed79eed4c68a040d8))
|
|
20
|
+
|
|
21
|
+
### BREAKING CHANGES
|
|
22
|
+
|
|
23
|
+
- new provider and signer interfaces
|
|
24
|
+
|
|
25
|
+
# [2.9.0](https://github.com/seanjameshan/starknet.js/compare/v2.8.0...v2.9.0) (2022-02-04)
|
|
26
|
+
|
|
27
|
+
### Bug Fixes
|
|
28
|
+
|
|
29
|
+
- introduce block identifier type ([75599a9](https://github.com/seanjameshan/starknet.js/commit/75599a99bbcb5723cfc8575b5fbf994a0bbf5b67))
|
|
30
|
+
|
|
31
|
+
### Features
|
|
32
|
+
|
|
33
|
+
- **utils:** add validateAndParseAddress function ([c067fc4](https://github.com/seanjameshan/starknet.js/commit/c067fc443e4dc9c22b78ed6c093978a2f37debde))
|
|
34
|
+
|
|
35
|
+
# [2.8.0](https://github.com/seanjameshan/starknet.js/compare/v2.7.2...v2.8.0) (2022-02-02)
|
|
36
|
+
|
|
37
|
+
### Features
|
|
38
|
+
|
|
39
|
+
- add tests ([e495d48](https://github.com/seanjameshan/starknet.js/commit/e495d4899141a79fe310d4fe76f70df03b1551ca))
|
|
40
|
+
- implement verifyMessage and verifyMessageHash ([bc9c4e9](https://github.com/seanjameshan/starknet.js/commit/bc9c4e9574cc453af35705eb4488602ea33cc2cb))
|
|
41
|
+
|
|
42
|
+
## [2.7.2](https://github.com/seanjameshan/starknet.js/compare/v2.7.1...v2.7.2) (2022-01-20)
|
|
43
|
+
|
|
44
|
+
### Bug Fixes
|
|
45
|
+
|
|
46
|
+
- **CONTRIBUTING:** wrong link ([2622a6c](https://github.com/seanjameshan/starknet.js/commit/2622a6c984259a6928e9ab02892b8de60b8c749e))
|
|
47
|
+
|
|
1
48
|
## [2.7.1](https://github.com/seanjameshan/starknet.js/compare/v2.7.0...v2.7.1) (2022-01-04)
|
|
2
49
|
|
|
3
50
|
### Bug Fixes
|
package/CONTRIBUTING.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
We love pull requests. And following this guidelines will make your pull request easier to merge.
|
|
4
4
|
|
|
5
|
-
If you want to contribute but don’t know what to do, take a look at these two labels: [help wanted](https://github.com/seanjameshan/starknet/issues?q=is%3Aissue+is%3Aopen+label%3A%22help+wanted%22) and [good first issue](https://github.com/seanjameshan/starknet/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22).
|
|
5
|
+
If you want to contribute but don’t know what to do, take a look at these two labels: [help wanted](https://github.com/seanjameshan/starknet.js/issues?q=is%3Aissue+is%3Aopen+label%3A%22help+wanted%22) and [good first issue](https://github.com/seanjameshan/starknet.js/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22).
|
|
6
6
|
|
|
7
7
|
_[Use GitHub interface](https://blog.sapegin.me/all/open-source-for-everyone/) for simple documentation changes, otherwise follow the steps below._
|
|
8
8
|
|
package/README.md
CHANGED
|
@@ -40,6 +40,8 @@ Install starknet with `npm`
|
|
|
40
40
|
|
|
41
41
|
```bash
|
|
42
42
|
$ npm install starknet
|
|
43
|
+
# or
|
|
44
|
+
$ npm install starknet@next
|
|
43
45
|
```
|
|
44
46
|
|
|
45
47
|
Import `starknet` and use the [API](https://www.starknetjs.com/modules.html)
|
|
@@ -54,16 +56,18 @@ const CONTRACT_ADDRESS =
|
|
|
54
56
|
"0x03e19baa6cb2078631bcdb34844f3f7879449a544c9ce722681a54af08cff4b9";
|
|
55
57
|
|
|
56
58
|
/**
|
|
57
|
-
*
|
|
59
|
+
* invokeFunction() example
|
|
58
60
|
**/
|
|
59
61
|
|
|
60
62
|
/** Reset the liquidity pool **/
|
|
61
|
-
const addTokenResponse = await
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
}
|
|
63
|
+
const addTokenResponse = await defaultProvider.invokeFunction(
|
|
64
|
+
{
|
|
65
|
+
contractAddress: CONTRACT_ADDRESS,
|
|
66
|
+
entrypoint: "init_pool",
|
|
67
|
+
calldata: ["1000000", "1000000"],
|
|
68
|
+
},
|
|
69
|
+
abi // for displaying purposes only (default implementation has no display/ui)
|
|
70
|
+
);
|
|
67
71
|
console.log(addTokenResponse);
|
|
68
72
|
|
|
69
73
|
/**
|
|
@@ -71,26 +75,24 @@ console.log(addTokenResponse);
|
|
|
71
75
|
**/
|
|
72
76
|
|
|
73
77
|
/** Get the balance of the liquidity pool of token A **/
|
|
74
|
-
const poolBalanceTokenA = await callContract({
|
|
75
|
-
|
|
76
|
-
|
|
78
|
+
const poolBalanceTokenA = await defaultProvider.callContract({
|
|
79
|
+
contractAddress: CONTRACT_ADDRESS,
|
|
80
|
+
entrypoint: "get_pool_token_balance",
|
|
77
81
|
calldata: ["1"],
|
|
78
82
|
});
|
|
79
83
|
const balanceA = poolBalanceTokenA.result[0];
|
|
80
84
|
console.log('token a liquidity pool balance: ', parseInt(balanceA, 16));
|
|
81
85
|
|
|
82
86
|
/** Get the balance of the liquidity pool of token B **/
|
|
83
|
-
const poolBalanceTokenB = await callContract({
|
|
84
|
-
|
|
85
|
-
|
|
87
|
+
const poolBalanceTokenB = await defaultProvider.callContract({
|
|
88
|
+
contractAddress: CONTRACT_ADDRESS,
|
|
89
|
+
entrypoint: "get_pool_token_balance",
|
|
86
90
|
calldata: ["2"],
|
|
87
91
|
});
|
|
88
92
|
const balanceB = poolBalanceTokenB.result[0];
|
|
89
93
|
console.log('token b liquidity pool balance: ', parseInt(balanceB, 16));
|
|
90
94
|
```
|
|
91
95
|
|
|
92
|
-
For more information about **signing transactions**, please take a look at this [pull request](https://github.com/seanjameshan/starknet.js/pull/51)
|
|
93
|
-
|
|
94
96
|
## 🌐 API
|
|
95
97
|
|
|
96
98
|
[Click Here](https://www.starknetjs.com/modules.html)
|
|
@@ -113,6 +115,6 @@ This library would not be possible without these rockstars.
|
|
|
113
115
|
|
|
114
116
|
## 📜 License
|
|
115
117
|
|
|
116
|
-
Copyright (c)
|
|
118
|
+
Copyright (c) 2022 0xs34n
|
|
117
119
|
|
|
118
120
|
Licensed under the [MIT license](https://github.com/seanjameshan/starknet.js/blob/main/LICENSE).
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"types": {
|
|
3
|
+
"StarkNetDomain": [
|
|
4
|
+
{ "name": "name", "type": "felt" },
|
|
5
|
+
{ "name": "version", "type": "felt" },
|
|
6
|
+
{ "name": "chainId", "type": "felt" }
|
|
7
|
+
],
|
|
8
|
+
"Person": [
|
|
9
|
+
{ "name": "name", "type": "felt" },
|
|
10
|
+
{ "name": "wallet", "type": "felt" }
|
|
11
|
+
],
|
|
12
|
+
"Mail": [
|
|
13
|
+
{ "name": "from", "type": "Person" },
|
|
14
|
+
{ "name": "to", "type": "Person" },
|
|
15
|
+
{ "name": "contents", "type": "felt" }
|
|
16
|
+
]
|
|
17
|
+
},
|
|
18
|
+
"primaryType": "Mail",
|
|
19
|
+
"domain": {
|
|
20
|
+
"name": "StarkNet Mail",
|
|
21
|
+
"version": "1",
|
|
22
|
+
"chainId": 1
|
|
23
|
+
},
|
|
24
|
+
"message": {
|
|
25
|
+
"from": {
|
|
26
|
+
"name": "Cow",
|
|
27
|
+
"wallet": "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826"
|
|
28
|
+
},
|
|
29
|
+
"to": {
|
|
30
|
+
"name": "Bob",
|
|
31
|
+
"wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB"
|
|
32
|
+
},
|
|
33
|
+
"contents": "Hello, Bob!"
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -1,36 +1,18 @@
|
|
|
1
1
|
import fs from 'fs';
|
|
2
2
|
|
|
3
|
+
import typedDataExample from '../__mocks__/typedDataExample.json';
|
|
3
4
|
import {
|
|
5
|
+
Account,
|
|
4
6
|
CompiledContract,
|
|
5
7
|
Contract,
|
|
6
8
|
compileCalldata,
|
|
7
9
|
defaultProvider,
|
|
8
10
|
ec,
|
|
9
|
-
encode,
|
|
10
|
-
hash,
|
|
11
11
|
json,
|
|
12
12
|
number,
|
|
13
13
|
stark,
|
|
14
14
|
} from '../src';
|
|
15
|
-
|
|
16
|
-
describe('getStarkAccountFromPrivateKey()', () => {
|
|
17
|
-
test('it works with valid privateKey', () => {
|
|
18
|
-
const privateKey = '0xb696427c0d79c5d28a1fa6f748bae1b98b3f4b86bd1a2505bab144673c856fa9';
|
|
19
|
-
|
|
20
|
-
const starkKeyPair = ec.getKeyPair(privateKey);
|
|
21
|
-
const starkKey = ec.getStarkKey(starkKeyPair);
|
|
22
|
-
|
|
23
|
-
expect(starkKey).toBe('0x060d46f8d7ef3d83ed05f3ed9beb91e22f9529289b9d863683fd71eafaf28035');
|
|
24
|
-
});
|
|
25
|
-
test('it works with valid privateKey', () => {
|
|
26
|
-
const privateKey = '0x5f65099e269b080000000000000000000000000000000000000000000000000';
|
|
27
|
-
|
|
28
|
-
const starkKeyPair = ec.getKeyPair(privateKey);
|
|
29
|
-
const starkKey = ec.getStarkKey(starkKeyPair);
|
|
30
|
-
|
|
31
|
-
expect(starkKey).toBe('0xf321e59b257a577836d8313150aabd21f412491358c329966218df76bab591');
|
|
32
|
-
});
|
|
33
|
-
});
|
|
15
|
+
import { toBN } from '../src/utils/number';
|
|
34
16
|
|
|
35
17
|
const compiledArgentAccount: CompiledContract = json.parse(
|
|
36
18
|
fs.readFileSync('./__mocks__/ArgentAccount.json').toString('ascii')
|
|
@@ -44,31 +26,30 @@ describe('deploy and test Wallet', () => {
|
|
|
44
26
|
|
|
45
27
|
const starkKeyPair = ec.getKeyPair(privateKey);
|
|
46
28
|
const starkKeyPub = ec.getStarkKey(starkKeyPair);
|
|
47
|
-
let wallet: Contract;
|
|
48
29
|
let walletAddress: string;
|
|
49
30
|
let erc20: Contract;
|
|
50
31
|
let erc20Address: string;
|
|
32
|
+
let account: Account;
|
|
33
|
+
|
|
51
34
|
beforeAll(async () => {
|
|
52
|
-
const { code: codeErc20, address: erc20AddressLocal } = await defaultProvider.deployContract(
|
|
53
|
-
compiledErc20,
|
|
54
|
-
|
|
55
|
-
);
|
|
35
|
+
const { code: codeErc20, address: erc20AddressLocal } = await defaultProvider.deployContract({
|
|
36
|
+
contract: compiledErc20,
|
|
37
|
+
});
|
|
56
38
|
erc20Address = erc20AddressLocal;
|
|
57
39
|
erc20 = new Contract(compiledErc20.abi, erc20Address);
|
|
58
40
|
|
|
59
41
|
expect(codeErc20).toBe('TRANSACTION_RECEIVED');
|
|
60
42
|
|
|
61
|
-
const { code, address: walletAddressLocal } = await defaultProvider.deployContract(
|
|
62
|
-
compiledArgentAccount,
|
|
63
|
-
compileCalldata({
|
|
43
|
+
const { code, address: walletAddressLocal } = await defaultProvider.deployContract({
|
|
44
|
+
contract: compiledArgentAccount,
|
|
45
|
+
constructorCalldata: compileCalldata({
|
|
64
46
|
signer: starkKeyPub,
|
|
65
47
|
guardian: '0',
|
|
66
48
|
L1_address: '0',
|
|
67
49
|
}),
|
|
68
|
-
starkKeyPub
|
|
69
|
-
);
|
|
50
|
+
addressSalt: starkKeyPub,
|
|
51
|
+
});
|
|
70
52
|
walletAddress = walletAddressLocal;
|
|
71
|
-
wallet = new Contract(compiledArgentAccount.abi, walletAddress);
|
|
72
53
|
expect(code).toBe('TRANSACTION_RECEIVED');
|
|
73
54
|
|
|
74
55
|
const { code: codeErc20Mint, transaction_hash: txErc20Mint } = await erc20.invoke('mint', {
|
|
@@ -78,12 +59,21 @@ describe('deploy and test Wallet', () => {
|
|
|
78
59
|
|
|
79
60
|
expect(codeErc20Mint).toBe('TRANSACTION_RECEIVED');
|
|
80
61
|
|
|
62
|
+
account = new Account(defaultProvider, walletAddressLocal, starkKeyPair);
|
|
63
|
+
|
|
81
64
|
await defaultProvider.waitForTx(txErc20Mint);
|
|
82
65
|
});
|
|
66
|
+
test('same wallet address', () => {
|
|
67
|
+
expect(walletAddress).toBe(account.address);
|
|
68
|
+
});
|
|
83
69
|
test('read nonce', async () => {
|
|
84
|
-
const {
|
|
70
|
+
const { result } = await account.callContract({
|
|
71
|
+
contractAddress: account.address,
|
|
72
|
+
entrypoint: 'get_nonce',
|
|
73
|
+
});
|
|
74
|
+
const nonce = result[0];
|
|
85
75
|
|
|
86
|
-
expect(number.toBN(nonce
|
|
76
|
+
expect(number.toBN(nonce).toString()).toStrictEqual(number.toBN(0).toString());
|
|
87
77
|
});
|
|
88
78
|
test('read balance of wallet', async () => {
|
|
89
79
|
const { res } = await erc20.call('balance_of', {
|
|
@@ -93,31 +83,13 @@ describe('deploy and test Wallet', () => {
|
|
|
93
83
|
expect(number.toBN(res as string).toString()).toStrictEqual(number.toBN(1000).toString());
|
|
94
84
|
});
|
|
95
85
|
test('execute by wallet owner', async () => {
|
|
96
|
-
const {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
stark.getSelectorFromName('transfer'),
|
|
102
|
-
[erc20Address, '10'],
|
|
103
|
-
nonce.toString()
|
|
104
|
-
)
|
|
105
|
-
);
|
|
106
|
-
|
|
107
|
-
const signature = ec.sign(starkKeyPair, msgHash);
|
|
108
|
-
const { code, transaction_hash } = await wallet.invoke(
|
|
109
|
-
'execute',
|
|
110
|
-
{
|
|
111
|
-
to: erc20Address,
|
|
112
|
-
selector: stark.getSelectorFromName('transfer'),
|
|
113
|
-
calldata: [erc20Address, '10'],
|
|
114
|
-
nonce: nonce.toString(),
|
|
115
|
-
},
|
|
116
|
-
signature
|
|
117
|
-
);
|
|
86
|
+
const { code, transaction_hash } = await account.execute({
|
|
87
|
+
contractAddress: erc20Address,
|
|
88
|
+
entrypoint: 'transfer',
|
|
89
|
+
calldata: [toBN(erc20Address).toString(), '10'],
|
|
90
|
+
});
|
|
118
91
|
|
|
119
92
|
expect(code).toBe('TRANSACTION_RECEIVED');
|
|
120
|
-
|
|
121
93
|
await defaultProvider.waitForTx(transaction_hash);
|
|
122
94
|
});
|
|
123
95
|
test('read balance of wallet after transfer', async () => {
|
|
@@ -127,35 +99,28 @@ describe('deploy and test Wallet', () => {
|
|
|
127
99
|
|
|
128
100
|
expect(number.toBN(res as string).toString()).toStrictEqual(number.toBN(990).toString());
|
|
129
101
|
});
|
|
130
|
-
|
|
102
|
+
test('execute with custom nonce', async () => {
|
|
103
|
+
const { result } = await account.callContract({
|
|
104
|
+
contractAddress: account.address,
|
|
105
|
+
entrypoint: 'get_nonce',
|
|
106
|
+
});
|
|
107
|
+
const nonce = toBN(result[0]).toNumber();
|
|
108
|
+
const { code, transaction_hash } = await account.execute(
|
|
109
|
+
{
|
|
110
|
+
contractAddress: erc20Address,
|
|
111
|
+
entrypoint: 'transfer',
|
|
112
|
+
calldata: [toBN(erc20Address).toString(), '10'],
|
|
113
|
+
},
|
|
114
|
+
undefined,
|
|
115
|
+
{ nonce }
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
expect(code).toBe('TRANSACTION_RECEIVED');
|
|
119
|
+
await defaultProvider.waitForTx(transaction_hash);
|
|
120
|
+
});
|
|
121
|
+
test('sign and verify offchain message', async () => {
|
|
122
|
+
const signature = await account.signMessage(typedDataExample);
|
|
131
123
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
const keyPair = ec.getKeyPair(privateKey);
|
|
135
|
-
const address = ec.getStarkKey(keyPair);
|
|
136
|
-
|
|
137
|
-
expect(address).toBe('0x04024999b9574cb7623679ce049a609db62a95098982c5b28ac61abdebd1c82b');
|
|
138
|
-
|
|
139
|
-
const selector = stark.getSelectorFromName('transfer');
|
|
140
|
-
|
|
141
|
-
expect(selector).toBe(
|
|
142
|
-
number.toHex(
|
|
143
|
-
number.toBN('232670485425082704932579856502088130646006032362877466777181098476241604910')
|
|
144
|
-
)
|
|
145
|
-
);
|
|
146
|
-
|
|
147
|
-
const msgHash = hash.hashMessage(address, '1', selector, ['6', '7'], '0');
|
|
148
|
-
expect(number.toBN(msgHash).toString()).toStrictEqual(
|
|
149
|
-
number
|
|
150
|
-
.toBN('2154230509011102177917341711834485670139815171447919056633262592518907948680')
|
|
151
|
-
.toString()
|
|
152
|
-
);
|
|
153
|
-
|
|
154
|
-
const [r, s] = ec.sign(keyPair, msgHash);
|
|
155
|
-
expect(r.toString()).toBe(
|
|
156
|
-
'706800951915233622090196542158919402159816118214143837213294331713137614072'
|
|
157
|
-
);
|
|
158
|
-
expect(s.toString()).toBe(
|
|
159
|
-
'1857147121895075123389037565321926580259282654271568123966453051614350474888'
|
|
160
|
-
);
|
|
124
|
+
expect(await account.verifyMessage(typedDataExample, signature)).toBe(true);
|
|
125
|
+
});
|
|
161
126
|
});
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
CompiledContract,
|
|
5
|
+
Contract,
|
|
6
|
+
compileCalldata,
|
|
7
|
+
defaultProvider,
|
|
8
|
+
ec,
|
|
9
|
+
encode,
|
|
10
|
+
hash,
|
|
11
|
+
json,
|
|
12
|
+
number,
|
|
13
|
+
stark,
|
|
14
|
+
} from '../src';
|
|
15
|
+
|
|
16
|
+
describe('getStarkAccountFromPrivateKey()', () => {
|
|
17
|
+
test('it works with valid privateKey', () => {
|
|
18
|
+
const privateKey = '0xb696427c0d79c5d28a1fa6f748bae1b98b3f4b86bd1a2505bab144673c856fa9';
|
|
19
|
+
|
|
20
|
+
const starkKeyPair = ec.getKeyPair(privateKey);
|
|
21
|
+
const starkKey = ec.getStarkKey(starkKeyPair);
|
|
22
|
+
|
|
23
|
+
expect(starkKey).toBe('0x060d46f8d7ef3d83ed05f3ed9beb91e22f9529289b9d863683fd71eafaf28035');
|
|
24
|
+
});
|
|
25
|
+
test('it works with valid privateKey', () => {
|
|
26
|
+
const privateKey = '0x5f65099e269b080000000000000000000000000000000000000000000000000';
|
|
27
|
+
|
|
28
|
+
const starkKeyPair = ec.getKeyPair(privateKey);
|
|
29
|
+
const starkKey = ec.getStarkKey(starkKeyPair);
|
|
30
|
+
|
|
31
|
+
expect(starkKey).toBe('0xf321e59b257a577836d8313150aabd21f412491358c329966218df76bab591');
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
const compiledArgentAccount: CompiledContract = json.parse(
|
|
36
|
+
fs.readFileSync('./__mocks__/ArgentAccount.json').toString('ascii')
|
|
37
|
+
);
|
|
38
|
+
const compiledErc20: CompiledContract = json.parse(
|
|
39
|
+
fs.readFileSync('./__mocks__/ERC20.json').toString('ascii')
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
describe('deploy and test Wallet', () => {
|
|
43
|
+
const privateKey = stark.randomAddress();
|
|
44
|
+
|
|
45
|
+
const starkKeyPair = ec.getKeyPair(privateKey);
|
|
46
|
+
const starkKeyPub = ec.getStarkKey(starkKeyPair);
|
|
47
|
+
let wallet: Contract;
|
|
48
|
+
let walletAddress: string;
|
|
49
|
+
let erc20: Contract;
|
|
50
|
+
let erc20Address: string;
|
|
51
|
+
beforeAll(async () => {
|
|
52
|
+
const { code: codeErc20, address: erc20AddressLocal } = await defaultProvider.deployContract({
|
|
53
|
+
contract: compiledErc20,
|
|
54
|
+
});
|
|
55
|
+
erc20Address = erc20AddressLocal;
|
|
56
|
+
erc20 = new Contract(compiledErc20.abi, erc20Address);
|
|
57
|
+
|
|
58
|
+
expect(codeErc20).toBe('TRANSACTION_RECEIVED');
|
|
59
|
+
|
|
60
|
+
const { code, address: walletAddressLocal } = await defaultProvider.deployContract({
|
|
61
|
+
contract: compiledArgentAccount,
|
|
62
|
+
constructorCalldata: compileCalldata({
|
|
63
|
+
signer: starkKeyPub,
|
|
64
|
+
guardian: '0',
|
|
65
|
+
L1_address: '0',
|
|
66
|
+
}),
|
|
67
|
+
addressSalt: starkKeyPub,
|
|
68
|
+
});
|
|
69
|
+
walletAddress = walletAddressLocal;
|
|
70
|
+
wallet = new Contract(compiledArgentAccount.abi, walletAddress);
|
|
71
|
+
expect(code).toBe('TRANSACTION_RECEIVED');
|
|
72
|
+
|
|
73
|
+
const { code: codeErc20Mint, transaction_hash: txErc20Mint } = await erc20.invoke('mint', {
|
|
74
|
+
recipient: walletAddress,
|
|
75
|
+
amount: '1000',
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
expect(codeErc20Mint).toBe('TRANSACTION_RECEIVED');
|
|
79
|
+
|
|
80
|
+
await defaultProvider.waitForTx(txErc20Mint);
|
|
81
|
+
});
|
|
82
|
+
test('read nonce', async () => {
|
|
83
|
+
const { nonce } = await wallet.call('get_nonce');
|
|
84
|
+
|
|
85
|
+
expect(number.toBN(nonce as string).toString()).toStrictEqual(number.toBN(0).toString());
|
|
86
|
+
});
|
|
87
|
+
test('read balance of wallet', async () => {
|
|
88
|
+
const { res } = await erc20.call('balance_of', {
|
|
89
|
+
user: walletAddress,
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
expect(number.toBN(res as string).toString()).toStrictEqual(number.toBN(1000).toString());
|
|
93
|
+
});
|
|
94
|
+
test('execute by wallet owner', async () => {
|
|
95
|
+
const { nonce } = await wallet.call('get_nonce');
|
|
96
|
+
const msgHash = encode.addHexPrefix(
|
|
97
|
+
hash.hashMessage(
|
|
98
|
+
wallet.connectedTo,
|
|
99
|
+
erc20Address,
|
|
100
|
+
stark.getSelectorFromName('transfer'),
|
|
101
|
+
[erc20Address, '10'],
|
|
102
|
+
nonce.toString()
|
|
103
|
+
)
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
const signature = ec.sign(starkKeyPair, msgHash);
|
|
107
|
+
const { code, transaction_hash } = await wallet.invoke(
|
|
108
|
+
'execute',
|
|
109
|
+
{
|
|
110
|
+
to: erc20Address,
|
|
111
|
+
selector: stark.getSelectorFromName('transfer'),
|
|
112
|
+
calldata: [erc20Address, '10'],
|
|
113
|
+
nonce: nonce.toString(),
|
|
114
|
+
},
|
|
115
|
+
signature
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
expect(code).toBe('TRANSACTION_RECEIVED');
|
|
119
|
+
|
|
120
|
+
await defaultProvider.waitForTx(transaction_hash);
|
|
121
|
+
});
|
|
122
|
+
test('read balance of wallet after transfer', async () => {
|
|
123
|
+
const { res } = await erc20.call('balance_of', {
|
|
124
|
+
user: walletAddress,
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
expect(number.toBN(res as string).toString()).toStrictEqual(number.toBN(990).toString());
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
test('build tx', async () => {
|
|
132
|
+
const privateKey = '0x1B69B4BE052FAB1';
|
|
133
|
+
const keyPair = ec.getKeyPair(privateKey);
|
|
134
|
+
const address = ec.getStarkKey(keyPair);
|
|
135
|
+
|
|
136
|
+
expect(address).toBe('0x04024999b9574cb7623679ce049a609db62a95098982c5b28ac61abdebd1c82b');
|
|
137
|
+
|
|
138
|
+
const selector = stark.getSelectorFromName('transfer');
|
|
139
|
+
|
|
140
|
+
expect(selector).toBe(
|
|
141
|
+
number.toHex(
|
|
142
|
+
number.toBN('232670485425082704932579856502088130646006032362877466777181098476241604910')
|
|
143
|
+
)
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
const msgHash = hash.hashMessage(address, '1', selector, ['6', '7'], '0');
|
|
147
|
+
expect(number.toBN(msgHash).toString()).toStrictEqual(
|
|
148
|
+
number
|
|
149
|
+
.toBN('2154230509011102177917341711834485670139815171447919056633262592518907948680')
|
|
150
|
+
.toString()
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
const [r, s] = ec.sign(keyPair, msgHash);
|
|
154
|
+
expect(r.toString()).toBe(
|
|
155
|
+
'706800951915233622090196542158919402159816118214143837213294331713137614072'
|
|
156
|
+
);
|
|
157
|
+
expect(s.toString()).toBe(
|
|
158
|
+
'1857147121895075123389037565321926580259282654271568123966453051614350474888'
|
|
159
|
+
);
|
|
160
|
+
});
|
|
@@ -14,7 +14,9 @@ describe('class Contract {}', () => {
|
|
|
14
14
|
code,
|
|
15
15
|
transaction_hash,
|
|
16
16
|
address: erc20address,
|
|
17
|
-
} = await defaultProvider.deployContract(
|
|
17
|
+
} = await defaultProvider.deployContract({
|
|
18
|
+
contract: compiledERC20,
|
|
19
|
+
});
|
|
18
20
|
|
|
19
21
|
contract = new Contract(compiledERC20.abi, erc20address);
|
|
20
22
|
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
import * as AxiosLogger from 'axios-logger';
|
|
3
|
+
|
|
4
|
+
jest.setTimeout(50 * 60 * 1000);
|
|
5
|
+
|
|
6
|
+
if (process.env.DEBUG === 'true') {
|
|
7
|
+
axios.interceptors.request.use(AxiosLogger.requestLogger, AxiosLogger.errorLogger);
|
|
8
|
+
axios.interceptors.response.use(AxiosLogger.responseLogger, AxiosLogger.errorLogger);
|
|
9
|
+
}
|
|
@@ -21,7 +21,7 @@ describe('defaultProvider', () => {
|
|
|
21
21
|
).resolves.not.toThrow();
|
|
22
22
|
});
|
|
23
23
|
test('getBlock(blockHash=undefined, blockNumber=36657)', () => {
|
|
24
|
-
return expect(defaultProvider.getBlock(
|
|
24
|
+
return expect(defaultProvider.getBlock(36657)).resolves.not.toThrow();
|
|
25
25
|
});
|
|
26
26
|
test('getBlock(blockHash=undefined, blockNumber=null)', () => {
|
|
27
27
|
return expect(defaultProvider.getBlock()).resolves.not.toThrow();
|
|
@@ -34,7 +34,6 @@ describe('defaultProvider', () => {
|
|
|
34
34
|
return expect(
|
|
35
35
|
defaultProvider.getCode(
|
|
36
36
|
'0x01d1f307c073bb786a66e6e042ec2a9bdc385a3373bb3738d95b966d5ce56166',
|
|
37
|
-
undefined,
|
|
38
37
|
36663
|
|
39
38
|
)
|
|
40
39
|
).resolves.not.toThrow();
|
|
@@ -51,7 +50,6 @@ describe('defaultProvider', () => {
|
|
|
51
50
|
defaultProvider.getStorageAt(
|
|
52
51
|
'0x01d1f307c073bb786a66e6e042ec2a9bdc385a3373bb3738d95b966d5ce56166',
|
|
53
52
|
0,
|
|
54
|
-
undefined,
|
|
55
53
|
36663
|
|
56
54
|
)
|
|
57
55
|
).resolves.not.toThrow();
|
|
@@ -78,11 +76,20 @@ describe('defaultProvider', () => {
|
|
|
78
76
|
)
|
|
79
77
|
).resolves.not.toThrow();
|
|
80
78
|
});
|
|
79
|
+
|
|
80
|
+
test('getTransactionReceipt', async () => {
|
|
81
|
+
return expect(
|
|
82
|
+
defaultProvider.getTransactionReceipt({
|
|
83
|
+
txHash: '0x37013e1cb9c133e6fe51b4b371b76b317a480f56d80576730754c1662582348',
|
|
84
|
+
})
|
|
85
|
+
).resolves.not.toThrow();
|
|
86
|
+
});
|
|
87
|
+
|
|
81
88
|
test('callContract()', () => {
|
|
82
89
|
return expect(
|
|
83
90
|
defaultProvider.callContract({
|
|
84
|
-
|
|
85
|
-
|
|
91
|
+
contractAddress: '0x9ff64f4ab0e1fe88df4465ade98d1ea99d5732761c39279b8e1374fa943e9b',
|
|
92
|
+
entrypoint: 'balance_of',
|
|
86
93
|
calldata: compileCalldata({
|
|
87
94
|
user: '0x9ff64f4ab0e1fe88df4465ade98d1ea99d5732761c39279b8e1374fa943e9b',
|
|
88
95
|
}),
|
|
@@ -92,45 +99,21 @@ describe('defaultProvider', () => {
|
|
|
92
99
|
});
|
|
93
100
|
|
|
94
101
|
describe('addTransaction()', () => {
|
|
95
|
-
test('
|
|
102
|
+
test('deployContract()', async () => {
|
|
96
103
|
const inputContract = compiledArgentAccount as unknown as CompiledContract;
|
|
97
104
|
|
|
98
|
-
const
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
const response = await defaultProvider.addTransaction({
|
|
104
|
-
type: 'DEPLOY',
|
|
105
|
-
contract_address_salt: stark.randomAddress(),
|
|
106
|
-
constructor_calldata: compileCalldata({
|
|
105
|
+
const response = await defaultProvider.deployContract({
|
|
106
|
+
contract: inputContract,
|
|
107
|
+
constructorCalldata: compileCalldata({
|
|
107
108
|
signer: stark.randomAddress(),
|
|
108
109
|
guardian: '0',
|
|
109
110
|
L1_address: '0',
|
|
110
111
|
}),
|
|
111
|
-
contract_definition: contractDefinition,
|
|
112
112
|
});
|
|
113
113
|
|
|
114
114
|
expect(response.code).toBe('TRANSACTION_RECEIVED');
|
|
115
115
|
expect(response.transaction_hash).toBeDefined();
|
|
116
116
|
expect(response.address).toBeDefined();
|
|
117
117
|
});
|
|
118
|
-
|
|
119
|
-
test('deployContract()', async () => {
|
|
120
|
-
const inputContract = compiledArgentAccount as unknown as CompiledContract;
|
|
121
|
-
|
|
122
|
-
const response = await defaultProvider.deployContract(
|
|
123
|
-
inputContract,
|
|
124
|
-
compileCalldata({
|
|
125
|
-
signer: stark.randomAddress(),
|
|
126
|
-
guardian: '0',
|
|
127
|
-
L1_address: '0',
|
|
128
|
-
})
|
|
129
|
-
);
|
|
130
|
-
|
|
131
|
-
expect(response.code).toBe('TRANSACTION_RECEIVED');
|
|
132
|
-
expect(response.transaction_hash).toBeDefined();
|
|
133
|
-
expect(response.address).toBeDefined();
|
|
134
|
-
});
|
|
135
118
|
});
|
|
136
119
|
});
|