starknet 2.9.0 → 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 +24 -0
- package/README.md +16 -14
- 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 +15 -30
- 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/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/contract.d.ts +6 -6
- package/dist/contract.js +9 -12
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/provider/default.d.ts +23 -13
- package/dist/provider/default.js +150 -93
- package/dist/provider/interface.d.ts +22 -22
- package/dist/provider/utils.d.ts +4 -4
- package/dist/provider/utils.js +16 -6
- package/dist/signer/default.d.ts +7 -51
- package/dist/signer/default.js +24 -177
- package/dist/signer/index.d.ts +1 -1
- package/dist/signer/index.js +1 -1
- package/dist/signer/interface.d.ts +16 -37
- 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/number.d.ts +1 -0
- package/dist/utils/number.js +5 -1
- package/index.d.ts +1 -1
- package/index.js +1 -1
- package/package.json +8 -2
- package/provider/default.d.ts +40 -25
- package/provider/default.js +205 -178
- package/provider/interface.d.ts +28 -34
- package/provider/utils.d.ts +4 -4
- package/provider/utils.js +15 -6
- package/signer/default.d.ts +11 -51
- package/signer/default.js +51 -232
- package/signer/index.d.ts +1 -1
- package/signer/index.js +1 -1
- package/signer/interface.d.ts +20 -37
- 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/contract.ts +17 -18
- package/src/index.ts +1 -1
- package/src/provider/default.ts +137 -99
- package/src/provider/interface.ts +28 -34
- package/src/provider/utils.ts +16 -6
- package/src/signer/default.ts +33 -115
- package/src/signer/index.ts +1 -1
- package/src/signer/interface.ts +20 -41
- 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/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/number.d.ts +3 -0
- package/utils/number.js +8 -1
- package/__tests__/signer.test.ts +0 -125
- 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,27 @@
|
|
|
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
|
+
|
|
1
25
|
# [2.9.0](https://github.com/seanjameshan/starknet.js/compare/v2.8.0...v2.9.0) (2022-02-04)
|
|
2
26
|
|
|
3
27
|
### Bug Fixes
|
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 defaultProvider.
|
|
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
|
/**
|
|
@@ -72,8 +76,8 @@ console.log(addTokenResponse);
|
|
|
72
76
|
|
|
73
77
|
/** Get the balance of the liquidity pool of token A **/
|
|
74
78
|
const poolBalanceTokenA = await defaultProvider.callContract({
|
|
75
|
-
|
|
76
|
-
|
|
79
|
+
contractAddress: CONTRACT_ADDRESS,
|
|
80
|
+
entrypoint: "get_pool_token_balance",
|
|
77
81
|
calldata: ["1"],
|
|
78
82
|
});
|
|
79
83
|
const balanceA = poolBalanceTokenA.result[0];
|
|
@@ -81,16 +85,14 @@ 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
87
|
const poolBalanceTokenB = await defaultProvider.callContract({
|
|
84
|
-
|
|
85
|
-
|
|
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).
|
|
@@ -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
|
+
}
|
|
@@ -76,11 +76,20 @@ describe('defaultProvider', () => {
|
|
|
76
76
|
)
|
|
77
77
|
).resolves.not.toThrow();
|
|
78
78
|
});
|
|
79
|
+
|
|
80
|
+
test('getTransactionReceipt', async () => {
|
|
81
|
+
return expect(
|
|
82
|
+
defaultProvider.getTransactionReceipt({
|
|
83
|
+
txHash: '0x37013e1cb9c133e6fe51b4b371b76b317a480f56d80576730754c1662582348',
|
|
84
|
+
})
|
|
85
|
+
).resolves.not.toThrow();
|
|
86
|
+
});
|
|
87
|
+
|
|
79
88
|
test('callContract()', () => {
|
|
80
89
|
return expect(
|
|
81
90
|
defaultProvider.callContract({
|
|
82
|
-
|
|
83
|
-
|
|
91
|
+
contractAddress: '0x9ff64f4ab0e1fe88df4465ade98d1ea99d5732761c39279b8e1374fa943e9b',
|
|
92
|
+
entrypoint: 'balance_of',
|
|
84
93
|
calldata: compileCalldata({
|
|
85
94
|
user: '0x9ff64f4ab0e1fe88df4465ade98d1ea99d5732761c39279b8e1374fa943e9b',
|
|
86
95
|
}),
|
|
@@ -90,45 +99,21 @@ describe('defaultProvider', () => {
|
|
|
90
99
|
});
|
|
91
100
|
|
|
92
101
|
describe('addTransaction()', () => {
|
|
93
|
-
test('
|
|
102
|
+
test('deployContract()', async () => {
|
|
94
103
|
const inputContract = compiledArgentAccount as unknown as CompiledContract;
|
|
95
104
|
|
|
96
|
-
const
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
const response = await defaultProvider.addTransaction({
|
|
102
|
-
type: 'DEPLOY',
|
|
103
|
-
contract_address_salt: stark.randomAddress(),
|
|
104
|
-
constructor_calldata: compileCalldata({
|
|
105
|
+
const response = await defaultProvider.deployContract({
|
|
106
|
+
contract: inputContract,
|
|
107
|
+
constructorCalldata: compileCalldata({
|
|
105
108
|
signer: stark.randomAddress(),
|
|
106
109
|
guardian: '0',
|
|
107
110
|
L1_address: '0',
|
|
108
111
|
}),
|
|
109
|
-
contract_definition: contractDefinition,
|
|
110
112
|
});
|
|
111
113
|
|
|
112
114
|
expect(response.code).toBe('TRANSACTION_RECEIVED');
|
|
113
115
|
expect(response.transaction_hash).toBeDefined();
|
|
114
116
|
expect(response.address).toBeDefined();
|
|
115
117
|
});
|
|
116
|
-
|
|
117
|
-
test('deployContract()', async () => {
|
|
118
|
-
const inputContract = compiledArgentAccount as unknown as CompiledContract;
|
|
119
|
-
|
|
120
|
-
const response = await defaultProvider.deployContract(
|
|
121
|
-
inputContract,
|
|
122
|
-
compileCalldata({
|
|
123
|
-
signer: stark.randomAddress(),
|
|
124
|
-
guardian: '0',
|
|
125
|
-
L1_address: '0',
|
|
126
|
-
})
|
|
127
|
-
);
|
|
128
|
-
|
|
129
|
-
expect(response.code).toBe('TRANSACTION_RECEIVED');
|
|
130
|
-
expect(response.transaction_hash).toBeDefined();
|
|
131
|
-
expect(response.address).toBeDefined();
|
|
132
|
-
});
|
|
133
118
|
});
|
|
134
119
|
});
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { Provider } from '../provider';
|
|
2
|
+
import {
|
|
3
|
+
Abi,
|
|
4
|
+
AddTransactionResponse,
|
|
5
|
+
ExecuteInvocation,
|
|
6
|
+
InvocationsDetails,
|
|
7
|
+
KeyPair,
|
|
8
|
+
Signature,
|
|
9
|
+
} from '../types';
|
|
10
|
+
import { BigNumberish } from '../utils/number';
|
|
11
|
+
import { TypedData } from '../utils/typedData';
|
|
12
|
+
import { AccountInterface } from './interface';
|
|
13
|
+
export declare class Account extends Provider implements AccountInterface {
|
|
14
|
+
address: string;
|
|
15
|
+
private signer;
|
|
16
|
+
constructor(provider: Provider, address: string, keyPair: KeyPair);
|
|
17
|
+
getNonce(): Promise<string>;
|
|
18
|
+
/**
|
|
19
|
+
* Invoke execute function in account contract
|
|
20
|
+
*
|
|
21
|
+
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/gateway/gateway_client.py#L13-L17)
|
|
22
|
+
*
|
|
23
|
+
* @param transaction - transaction to be invoked
|
|
24
|
+
* @returns a confirmation of invoking a function on the starknet contract
|
|
25
|
+
*/
|
|
26
|
+
execute(
|
|
27
|
+
transactions: ExecuteInvocation | ExecuteInvocation[],
|
|
28
|
+
abis?: Abi[],
|
|
29
|
+
transactionsDetail?: InvocationsDetails
|
|
30
|
+
): Promise<AddTransactionResponse>;
|
|
31
|
+
/**
|
|
32
|
+
* Sign an JSON object with the starknet private key and return the signature
|
|
33
|
+
*
|
|
34
|
+
* @param json - JSON object to be signed
|
|
35
|
+
* @returns the signature of the JSON object
|
|
36
|
+
* @throws {Error} if the JSON object is not a valid JSON
|
|
37
|
+
*/
|
|
38
|
+
signMessage(typedData: TypedData): Promise<Signature>;
|
|
39
|
+
/**
|
|
40
|
+
* Hash a JSON object with pederson hash and return the hash
|
|
41
|
+
*
|
|
42
|
+
* @param json - JSON object to be hashed
|
|
43
|
+
* @returns the hash of the JSON object
|
|
44
|
+
* @throws {Error} if the JSON object is not a valid JSON
|
|
45
|
+
*/
|
|
46
|
+
hashMessage(typedData: TypedData): Promise<string>;
|
|
47
|
+
/**
|
|
48
|
+
* Verify a signature of a JSON object
|
|
49
|
+
*
|
|
50
|
+
* @param json - JSON object to be verified
|
|
51
|
+
* @param signature - signature of the JSON object
|
|
52
|
+
* @returns true if the signature is valid, false otherwise
|
|
53
|
+
* @throws {Error} if the JSON object is not a valid JSON or the signature is not a valid signature
|
|
54
|
+
*/
|
|
55
|
+
verifyMessageHash(hash: BigNumberish, signature: Signature): Promise<boolean>;
|
|
56
|
+
/**
|
|
57
|
+
* Verify a signature of a given hash
|
|
58
|
+
* @warning This method is not recommended, use verifyMessage instead
|
|
59
|
+
*
|
|
60
|
+
* @param hash - hash to be verified
|
|
61
|
+
* @param signature - signature of the hash
|
|
62
|
+
* @returns true if the signature is valid, false otherwise
|
|
63
|
+
* @throws {Error} if the signature is not a valid signature
|
|
64
|
+
*/
|
|
65
|
+
verifyMessage(typedData: TypedData, signature: Signature): Promise<boolean>;
|
|
66
|
+
}
|