Skip to content

Commit 82aa438

Browse files
committed
fix: cleanup
1 parent 2c9e287 commit 82aa438

24 files changed

+815
-1691
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`pedersen 1`] = `"0x5ed2703dfdb505c587700ce2ebfcab5b3515cd7e6114817e6026ec9d4b364ca"`;

__tests__/contracts.test.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
import { BigNumber } from '@ethersproject/bignumber';
21
import fs from 'fs';
3-
import {
4-
CompiledContract,
5-
Contract,
6-
deployContract,
7-
JsonParser,
8-
randomAddress,
9-
waitForTx,
10-
} from '../src';
112

12-
const compiledERC20: CompiledContract = JsonParser.parse(
3+
import { CompiledContract, Contract, deployContract, utils, waitForTx } from '../src';
4+
5+
const {
6+
json: { parse },
7+
number: { toBN },
8+
starknet: { randomAddress },
9+
} = utils;
10+
11+
const compiledERC20: CompiledContract = parse(
1312
fs.readFileSync('./__mocks__/ERC20.json').toString('ascii')
1413
);
1514

@@ -29,7 +28,7 @@ describe('class Contract {}', () => {
2928
const response = await contract.call('balance_of', {
3029
user: wallet,
3130
});
32-
expect(BigNumber.from(response.res)).toStrictEqual(BigNumber.from(0));
31+
expect(toBN(response.res as string).toString()).toStrictEqual(toBN(0).toString());
3332
});
3433
test('add 10 test ERC20 to account', async () => {
3534
const response = await contract.invoke('mint', {
@@ -48,6 +47,6 @@ describe('class Contract {}', () => {
4847
user: wallet,
4948
});
5049

51-
expect(BigNumber.from(response.res)).toStrictEqual(BigNumber.from(10));
50+
expect(toBN(response.res as string).toString()).toStrictEqual(toBN(10).toString());
5251
});
5352
});

__tests__/ec.test.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
1-
import { pedersen as pedersenReal } from '@authereum/starkware-crypto';
2-
import { BigNumber } from '@ethersproject/bignumber';
3-
import { ensure0x, ensureNo0x, hashCalldata, hashMessage } from '../src';
4-
import { getKeyPair, getStarkKey, pedersen, sign } from '../src/ec';
1+
import { removeHexPrefix } from 'enc-utils';
2+
3+
import { getKeyPair, getStarkKey, hashCalldata, hashMessage, pedersen, sign } from '../src';
4+
import { toBN, toHex } from '../src/utils/number';
55

66
test('does work with package', () => {
77
const pk = '0x019800ea6a9a73f94aee6a3d2edf018fc770443e90c7ba121e8303ec6b349279';
88
const pair = getKeyPair(pk);
99
// somehow needed, returns error else
10-
expect(BigNumber.from(getStarkKey(pair)).toHexString()).toBe(
11-
'0x033f45f07e1bd1a51b45fc24ec8c8c9908db9e42191be9e169bfcac0c0d99745'
10+
expect(toHex(toBN(getStarkKey(pair)))).toBe(
11+
'0x33f45f07e1bd1a51b45fc24ec8c8c9908db9e42191be9e169bfcac0c0d99745'
1212
);
1313
});
1414

1515
test('pedersen', () => {
16-
const real = ensure0x(pedersenReal(['0x12773', '0x872362']));
1716
const own = pedersen(['0x12773', '0x872362']);
18-
// somehow needed, returns error else
19-
expect(own).toBe(real);
17+
expect(own).toMatchSnapshot();
2018
});
2119

2220
test('hashCalldata()', () => {
@@ -41,11 +39,11 @@ test('hashMessage()', () => {
4139
);
4240
expect(hashMsg).toBe('0xf7ec4a68876819eed838be83b5d5dc337081f4a5fb8e421f3d9bdef7c69e9b');
4341
const keyPair = getKeyPair(pk);
44-
const { r, s } = sign(keyPair, ensureNo0x(hashMsg));
45-
expect(BigNumber.from(ensure0x(r.toString('hex')))).toStrictEqual(
46-
BigNumber.from('2699852629692218907583414128365108566181098618321049245303767746418549764831')
42+
const { r, s } = sign(keyPair, removeHexPrefix(hashMsg));
43+
expect(r.toString()).toStrictEqual(
44+
toBN('2699852629692218907583414128365108566181098618321049245303767746418549764831').toString()
4745
);
48-
expect(BigNumber.from(ensure0x(s.toString('hex')))).toStrictEqual(
49-
BigNumber.from('2362979021721299440845279407227912881357338080403308888611869245024056250189')
46+
expect(s.toString()).toStrictEqual(
47+
toBN('2362979021721299440845279407227912881357338080403308888611869245024056250189').toString()
5048
);
5149
});

__tests__/index.test.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
import fs from 'fs';
2+
23
import {
34
CompiledContract,
4-
compressProgram,
5-
randomAddress,
6-
JsonParser,
7-
getContractAddresses,
5+
addTransaction,
6+
deployContract,
87
getBlock,
98
getCode,
9+
getContractAddresses,
1010
getStorageAt,
11-
getTransactionStatus,
1211
getTransaction,
13-
addTransaction,
14-
deployContract,
12+
getTransactionStatus,
13+
utils,
1514
} from '../src';
1615

17-
const compiledArgentAccount = JsonParser.parse(
16+
const {
17+
json: { parse },
18+
starknet: { compressProgram, randomAddress },
19+
} = utils;
20+
21+
const compiledArgentAccount = parse(
1822
fs.readFileSync('./__mocks__/ArgentAccount.json').toString('ascii')
1923
);
2024

__tests__/math.test.ts

Lines changed: 0 additions & 54 deletions
This file was deleted.

__tests__/utils.browser.test.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,21 @@
33
*/
44

55
import fs from 'fs';
6-
import { compressProgram, isBrowser, JsonParser } from '../src';
76

8-
const compiledArgentAccount = JsonParser.parse(
7+
import { constants, utils } from '../src';
8+
9+
const { IS_BROWSER } = constants;
10+
const {
11+
json: { parse, stringify },
12+
starknet: { compressProgram },
13+
} = utils;
14+
15+
const compiledArgentAccount = parse(
916
fs.readFileSync('./__mocks__/ArgentAccount.json').toString('ascii')
1017
);
1118

1219
test('isBrowser', () => {
13-
expect(isBrowser).toBe(true);
20+
expect(IS_BROWSER).toBe(true);
1421
});
1522
describe('compressProgram()', () => {
1623
test('compresses a contract program', () => {
@@ -21,7 +28,7 @@ describe('compressProgram()', () => {
2128
expect(compressed).toMatchSnapshot();
2229
});
2330
test('works with strings', () => {
24-
const inputProgram = JsonParser.stringify(compiledArgentAccount.program);
31+
const inputProgram = stringify(compiledArgentAccount.program);
2532

2633
const compressed = compressProgram(inputProgram);
2734

__tests__/utils.test.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
import fs from 'fs';
2-
import { compressProgram, makeAddress, isBrowser, JsonParser, getSelectorFromName } from '../src';
32

4-
const compiledArgentAccount = JsonParser.parse(
3+
import { constants, utils } from '../src';
4+
5+
const { IS_BROWSER } = constants;
6+
const {
7+
json: { parse, stringify },
8+
starknet: { compressProgram, getSelectorFromName, makeAddress },
9+
enc: { hexToDecimalString },
10+
} = utils;
11+
12+
const compiledArgentAccount = parse(
513
fs.readFileSync('./__mocks__/ArgentAccount.json').toString('ascii')
614
);
715

816
test('isNode', () => {
9-
expect(isBrowser).toBe(false);
17+
expect(IS_BROWSER).toBe(false);
1018
});
1119
describe('compressProgram()', () => {
1220
test('compresses a contract program', () => {
@@ -17,13 +25,19 @@ describe('compressProgram()', () => {
1725
expect(compressed).toMatchSnapshot();
1826
});
1927
test('works with strings', () => {
20-
const inputProgram = JsonParser.stringify(compiledArgentAccount.program);
28+
const inputProgram = stringify(compiledArgentAccount.program);
2129

2230
const compressed = compressProgram(inputProgram);
2331

2432
expect(compressed).toMatchSnapshot();
2533
});
2634
});
35+
describe('hexToDecimalString()', () => {
36+
test('parse 0xa23', () => {
37+
expect(hexToDecimalString('0xa23')).toBe('2595');
38+
});
39+
});
40+
2741
describe('makeAddress()', () => {
2842
test('test on eth address', () => {
2943
const ethAddress = '0xdFD0F27FCe99b50909de0bDD328Aed6eAbe76BC5';
@@ -33,7 +47,7 @@ describe('makeAddress()', () => {
3347
expect(starkAddress).toBe('0xdfd0f27fce99b50909de0bdd328aed6eabe76bc5');
3448
});
3549
});
36-
describe('starknetKeccak()', () => {
50+
describe('getSelectorFromName()', () => {
3751
test('hash works for value="test"', () => {
3852
expect(getSelectorFromName('test')).toBe(
3953
'0x22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658'
@@ -46,7 +60,7 @@ describe('starknetKeccak()', () => {
4660
});
4761
test('hash works for value="mint"', () => {
4862
expect(getSelectorFromName('mint')).toBe(
49-
'0x02f0b3c5710379609eb5495f1ecd348cb28167711b73609fe565a72734550354'
63+
'0x2f0b3c5710379609eb5495f1ecd348cb28167711b73609fe565a72734550354'
5064
);
5165
});
5266
});

__tests__/wallet.test.ts

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
1-
import { BigNumber } from '@ethersproject/bignumber';
2-
31
import fs from 'fs';
2+
3+
import { addHexPrefix } from 'enc-utils';
4+
45
import {
5-
hashMessage,
66
CompiledContract,
77
Contract,
88
deployContract,
9-
JsonParser,
10-
waitForTx,
11-
randomAddress,
12-
getSelectorFromName,
13-
ensure0x,
14-
getStarkKey,
159
getKeyPair,
10+
getStarkKey,
11+
hashMessage,
1612
sign,
13+
utils,
14+
waitForTx,
1715
} from '../src';
16+
import { toBN } from '../src/utils/number';
17+
18+
const {
19+
json: { parse },
20+
starknet: { getSelectorFromName, randomAddress },
21+
number: { toHex },
22+
} = utils;
1823

1924
describe('getStarkAccountFromPk()', () => {
2025
test('it works with valid pk', () => {
@@ -35,10 +40,10 @@ describe('getStarkAccountFromPk()', () => {
3540
});
3641
});
3742

38-
const compiledArgentAccount: CompiledContract = JsonParser.parse(
43+
const compiledArgentAccount: CompiledContract = parse(
3944
fs.readFileSync('./__mocks__/ArgentAccount.json').toString('ascii')
4045
);
41-
const compiledErc20: CompiledContract = JsonParser.parse(
46+
const compiledErc20: CompiledContract = parse(
4247
fs.readFileSync('./__mocks__/ERC20.json').toString('ascii')
4348
);
4449

@@ -93,18 +98,18 @@ describe('deploy and test Wallet', () => {
9398
test('read nonce', async () => {
9499
const { nonce } = await wallet.call('get_current_nonce');
95100

96-
expect(BigNumber.from(nonce)).toStrictEqual(BigNumber.from(0));
101+
expect(toBN(nonce as string).toString()).toStrictEqual(toBN(0).toString());
97102
});
98103
test('read balance of wallet', async () => {
99104
const { res } = await erc20.call('balance_of', {
100105
user: walletAddress,
101106
});
102107

103-
expect(BigNumber.from(res)).toStrictEqual(BigNumber.from(1000));
108+
expect(toBN(res as string).toString()).toStrictEqual(toBN(1000).toString());
104109
});
105110
test('execute by wallet owner', async () => {
106111
const { nonce } = await wallet.call('get_current_nonce');
107-
const msgHash = ensure0x(
112+
const msgHash = addHexPrefix(
108113
hashMessage(
109114
walletAddress,
110115
erc20Address,
@@ -113,14 +118,14 @@ describe('deploy and test Wallet', () => {
113118
nonce.toString()
114119
)
115120
);
116-
console.log(msgHash);
121+
117122
const { r, s } = sign(starkKeyPair, msgHash);
118123
const { code, tx_id } = await wallet.invoke('execute', {
119124
to: erc20Address,
120125
selector: getSelectorFromName('transfer'),
121126
calldata: [erc20Address, '10'],
122127
nonce: nonce.toString(),
123-
sig: [ensure0x(r.toString('hex')), ensure0x(s.toString('hex'))],
128+
sig: [toHex(r), toHex(s)],
124129
});
125130

126131
// I want to show the tx number to the tester, so he/she can trace the transaction in the explorer.
@@ -135,7 +140,7 @@ describe('deploy and test Wallet', () => {
135140
user: walletAddress,
136141
});
137142

138-
expect(BigNumber.from(res)).toStrictEqual(BigNumber.from(990));
143+
expect(toBN(res as string).toString()).toStrictEqual(toBN(990).toString());
139144
});
140145
});
141146

@@ -149,14 +154,12 @@ test('build tx', async () => {
149154
const selector = getSelectorFromName('transfer');
150155

151156
expect(selector).toBe(
152-
BigNumber.from(
153-
'232670485425082704932579856502088130646006032362877466777181098476241604910'
154-
).toHexString()
157+
toHex(toBN('232670485425082704932579856502088130646006032362877466777181098476241604910'))
155158
);
156159

157160
const msgHash = hashMessage(address, '1', selector, ['6', '7'], '0');
158-
expect(BigNumber.from(msgHash)).toStrictEqual(
159-
BigNumber.from('2221651675559331189881349481637314109810712322791057846116415219218634672652')
161+
expect(toBN(msgHash).toString()).toStrictEqual(
162+
toBN('2221651675559331189881349481637314109810712322791057846116415219218634672652').toString()
160163
);
161164

162165
const { r, s } = sign(keyPair, msgHash);

0 commit comments

Comments
 (0)