Skip to content

Commit

Permalink
feat: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
janek26 committed Oct 29, 2021
1 parent c79bfdc commit 42a8ab1
Show file tree
Hide file tree
Showing 12 changed files with 3,958 additions and 42 deletions.
51 changes: 51 additions & 0 deletions __tests__/ec.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { pedersen as pedersenReal } from '@authereum/starkware-crypto';
import { BigNumber } from '@ethersproject/bignumber';
import { ensure0x, ensureNo0x, hashCalldata, hashMessage } from '../src';
import { getKeyPair, getStarkKey, pedersen, sign } from '../src/ec';

test('does work with package', () => {
const pk = '0x019800ea6a9a73f94aee6a3d2edf018fc770443e90c7ba121e8303ec6b349279';
const pair = getKeyPair(pk);
// somehow needed, returns error else
expect(BigNumber.from(getStarkKey(pair)).toHexString()).toBe(
'0x033f45f07e1bd1a51b45fc24ec8c8c9908db9e42191be9e169bfcac0c0d99745'
);
});

test('pedersen', () => {
const real = ensure0x(pedersenReal(['0x12773', '0x872362']));
const own = pedersen(['0x12773', '0x872362']);
// somehow needed, returns error else
expect(own).toBe(real);
});

test('hashCalldata()', () => {
const array = ['1', '2', '3', '4'];
expect(hashCalldata(array)).toBe(
'0x1439c58e1c389a2ac51f8462ecc0a4ec7f812be1c04e3b82ce2af1c2cf959ef'
);
expect(array).toStrictEqual(['1', '2', '3', '4']);

expect(hashCalldata(['1', '2'])).toBe(
'0x2ab889bd35e684623df9b4ea4a4a1f6d9e0ef39b67c1293b8a89dd17e351235'
);
});
test('hashMessage()', () => {
const pk = '0x019800ea6a9a73f94aee6a3d2edf018fc770443e90c7ba121e8303ec6b349279';
const hashMsg = hashMessage(
'0x33f45f07e1bd1a51b45fc24ec8c8c9908db9e42191be9e169bfcac0c0d99745',
'5',
'6',
['1', '2'],
'2'
);
expect(hashMsg).toBe('0xf7ec4a68876819eed838be83b5d5dc337081f4a5fb8e421f3d9bdef7c69e9b');
const keyPair = getKeyPair(pk);
const { r, s } = sign(keyPair, ensureNo0x(hashMsg));
expect(BigNumber.from(ensure0x(r.toString('hex')))).toStrictEqual(
BigNumber.from('2699852629692218907583414128365108566181098618321049245303767746418549764831')
);
expect(BigNumber.from(ensure0x(s.toString('hex')))).toStrictEqual(
BigNumber.from('2362979021721299440845279407227912881357338080403308888611869245024056250189')
);
});
54 changes: 54 additions & 0 deletions __tests__/math.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { getKeyPair, getStarkKey, getStarkPublicKey } from '@authereum/starkware-crypto';
import { BigNumber } from '@ethersproject/bignumber';
import { ensure0x, ensureNo0x } from '../src';
import { ecAdd, igcdex, privateToStarkKey } from '../src/math';

describe('igcdex()', () => {
test('works with (0, 0)', () => {
const result = igcdex(BigNumber.from(0), BigNumber.from(0));

expect(result).toStrictEqual([BigNumber.from(0), BigNumber.from(1), BigNumber.from(0)]);
});
test('works with (0, 2004)', () => {
const result = igcdex(BigNumber.from(0), BigNumber.from(2004));

expect(result).toStrictEqual([BigNumber.from(0), BigNumber.from(1), BigNumber.from(2004)]);
});
test('works with (100, 0)', () => {
const result = igcdex(BigNumber.from(100), BigNumber.from(0));

expect(result).toStrictEqual([BigNumber.from(1), BigNumber.from(0), BigNumber.from(100)]);
});
test('works with (100, 2004)', () => {
const result = igcdex(BigNumber.from(100), BigNumber.from(2004));

expect(result).toStrictEqual([BigNumber.from(-20), BigNumber.from(1), BigNumber.from(4)]);
});
});
describe('ecAdd()', () => {
test('works with ([10,20], [21,32], 5)', () => {
const result = ecAdd(
[BigNumber.from(10), BigNumber.from(20)],
[BigNumber.from(21), BigNumber.from(32)],
BigNumber.from(5)
);
expect(result).toStrictEqual([BigNumber.from(3), BigNumber.from(4)]);
});
});
describe('privateToStarkKey()', () => {
test('works with valid pk', () => {
const pk = '0x019800ea6a9a73f94aee6a3d2edf018fc770443e90c7ba121e8303ec6b349279';
const pub = privateToStarkKey(pk);

expect(pub).toBe('0x033f45f07e1bd1a51b45fc24ec8c8c9908db9e42191be9e169bfcac0c0d99745');
});
test('doesnt work with package', () => {
const pk = ensureNo0x('0x019800ea6a9a73f94aee6a3d2edf018fc770443e90c7ba121e8303ec6b349279');
const pair = getKeyPair(pk);
// somehow needed, returns error else
getStarkPublicKey(pair);
expect(BigNumber.from(ensure0x(getStarkKey(pair))).toHexString()).toBe(
'0x033f45f07e1bd1a51b45fc24ec8c8c9908db9e42191be9e169bfcac0c0d99745'
);
});
});
169 changes: 169 additions & 0 deletions __tests__/wallet.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
import { BigNumber } from '@ethersproject/bignumber';

import fs from 'fs';
import {
hashMessage,
CompiledContract,
Contract,
deployContract,
JsonParser,
waitForTx,
randomAddress,
getSelectorFromName,
ensure0x,
getStarkKey,
getKeyPair,
sign,
} from '../src';

describe('getStarkAccountFromPk()', () => {
test('it works with valid pk', () => {
const pk = '0xb696427c0d79c5d28a1fa6f748bae1b98b3f4b86bd1a2505bab144673c856fa9';

const starkKeyPair = getKeyPair(pk);
const starkKey = getStarkKey(starkKeyPair);

expect(starkKey).toBe('0x060d46f8d7ef3d83ed05f3ed9beb91e22f9529289b9d863683fd71eafaf28035');
});
test('it works with valid pk', () => {
const pk = '0x5f65099e269b080000000000000000000000000000000000000000000000000';

const starkKeyPair = getKeyPair(pk);
const starkKey = getStarkKey(starkKeyPair);

expect(starkKey).toBe('0xf321e59b257a577836d8313150aabd21f412491358c329966218df76bab591');
});
});

const compiledArgentAccount: CompiledContract = JsonParser.parse(
fs.readFileSync('./__mocks__/ArgentAccount.json').toString('ascii')
);
const compiledErc20: CompiledContract = JsonParser.parse(
fs.readFileSync('./__mocks__/ERC20.json').toString('ascii')
);

describe('deploy and test Wallet', () => {
const pk = randomAddress();

// eslint-disable-next-line no-console
console.log('PK:', pk);

const starkKeyPair = getKeyPair(pk);
const walletAddress = getStarkKey(starkKeyPair);
const wallet = new Contract(compiledArgentAccount.abi, walletAddress);
const erc20Address = getStarkKey(getKeyPair(randomAddress()));
const erc20 = new Contract(compiledErc20.abi, erc20Address);
beforeAll(async () => {
const { code: codeErc20, tx_id: txErc20 } = await deployContract(compiledErc20, erc20Address);
// I want to show the tx number to the tester, so he/she can trace the transaction in the explorer.
// eslint-disable-next-line no-console
console.log('deployed erc20', txErc20);
expect(codeErc20).toBe('TRANSACTION_RECEIVED');

const { code, tx_id } = await deployContract(compiledArgentAccount, walletAddress);
// I want to show the tx number to the tester, so he/she can trace the transaction in the explorer.
// eslint-disable-next-line no-console
console.log('deployed wallet', tx_id);
expect(code).toBe('TRANSACTION_RECEIVED');

const { code: code2, tx_id: txId2 } = await wallet.invoke('initialize', {
signer: walletAddress,
guardian: '0',
L1_address: '0',
self_address: walletAddress,
});

// I want to show the tx number to the tester, so he/she can trace the transaction in the explorer.
// eslint-disable-next-line no-console
console.log('initialized wallet', txId2);
expect(code2).toBe('TRANSACTION_RECEIVED');

const { code: codeErc20Mint, tx_id: txErc20Mint } = await erc20.invoke('mint', {
recipient: walletAddress,
amount: '1000',
});

// I want to show the tx number to the tester, so he/she can trace the transaction in the explorer.
// eslint-disable-next-line no-console
console.log('mint erc20', txErc20Mint);
expect(codeErc20Mint).toBe('TRANSACTION_RECEIVED');

await waitForTx(txErc20Mint);
});
test('read nonce', async () => {
const { nonce } = await wallet.call('get_current_nonce');

expect(BigNumber.from(nonce)).toStrictEqual(BigNumber.from(0));
});
test('read balance of wallet', async () => {
const { res } = await erc20.call('balance_of', {
user: walletAddress,
});

expect(BigNumber.from(res)).toStrictEqual(BigNumber.from(1000));
});
test('execute by wallet owner', async () => {
const { nonce } = await wallet.call('get_current_nonce');
const msgHash = ensure0x(
hashMessage(
walletAddress,
erc20Address,
getSelectorFromName('transfer'),
[erc20Address, '10'],
nonce.toString()
)
);
console.log(msgHash);
const { r, s } = sign(starkKeyPair, msgHash);
const { code, tx_id } = await wallet.invoke('execute', {
to: erc20Address,
selector: getSelectorFromName('transfer'),
calldata: [erc20Address, '10'],
nonce: nonce.toString(),
sig: [ensure0x(r.toString('hex')), ensure0x(s.toString('hex'))],
});

// I want to show the tx number to the tester, so he/she can trace the transaction in the explorer.
// eslint-disable-next-line no-console
console.log('transfer erc20 using wallet execute', tx_id);
expect(code).toBe('TRANSACTION_RECEIVED');

await waitForTx(tx_id);
});
test('read balance of wallet after transfer', async () => {
const { res } = await erc20.call('balance_of', {
user: walletAddress,
});

expect(BigNumber.from(res)).toStrictEqual(BigNumber.from(990));
});
});

test('build tx', async () => {
const pk = '0x1B69B4BE052FAB1';
const keyPair = getKeyPair(pk);
const address = getStarkKey(keyPair);

expect(address).toBe('0x04024999b9574cb7623679ce049a609db62a95098982c5b28ac61abdebd1c82b');

const selector = getSelectorFromName('transfer');

expect(selector).toBe(
BigNumber.from(
'232670485425082704932579856502088130646006032362877466777181098476241604910'
).toHexString()
);

const msgHash = hashMessage(address, '1', selector, ['6', '7'], '0');
expect(BigNumber.from(msgHash)).toStrictEqual(
BigNumber.from('2221651675559331189881349481637314109810712322791057846116415219218634672652')
);

const { r, s } = sign(keyPair, msgHash);
expect(r.toString()).toBe(
'2220702546012141050051149396887481489960265709213083422658245644097500180866'
);
expect(s.toString()).toBe(
'1542316446019190634489932498001415389924394685441251344076931639569381539117'
);
});
Loading

0 comments on commit 42a8ab1

Please sign in to comment.