Skip to content

Commit

Permalink
feat: alpha 4
Browse files Browse the repository at this point in the history
BREAKING CHANGE: implements alpha 4
  • Loading branch information
janek26 committed Nov 18, 2021
1 parent 9260d6d commit f12db5a
Show file tree
Hide file tree
Showing 15 changed files with 46,162 additions and 52,367 deletions.
91,446 changes: 42,401 additions & 49,045 deletions __mocks__/ArgentAccount.json

Large diffs are not rendered by default.

6,995 changes: 3,721 additions & 3,274 deletions __mocks__/ERC20.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions __mocks__/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Sources can be found here:
https://github.com/argentlabs/argent-contracts-starknet
12 changes: 6 additions & 6 deletions __tests__/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ describe('deploy and test Wallet', () => {
await defaultProvider.waitForTx(txErc20Mint);
});
test('read nonce', async () => {
const { nonce } = await wallet.call('get_current_nonce');
const { nonce } = await wallet.call('get_nonce');

expect(number.toBN(nonce as string).toString()).toStrictEqual(number.toBN(0).toString());
});
Expand All @@ -93,10 +93,10 @@ describe('deploy and test Wallet', () => {
expect(number.toBN(res as string).toString()).toStrictEqual(number.toBN(1000).toString());
});
test('execute by wallet owner', async () => {
const { nonce } = await wallet.call('get_current_nonce');
const { nonce } = await wallet.call('get_nonce');
const msgHash = encode.addHexPrefix(
hash.hashMessage(
'0', // needs to be walletAddress once it's possible to retrieve address(self) in cairo
wallet.connectedTo,
erc20Address,
stark.getSelectorFromName('transfer'),
[erc20Address, '10'],
Expand Down Expand Up @@ -147,15 +147,15 @@ test('build tx', async () => {
const msgHash = hash.hashMessage(address, '1', selector, ['6', '7'], '0');
expect(number.toBN(msgHash).toString()).toStrictEqual(
number
.toBN('2221651675559331189881349481637314109810712322791057846116415219218634672652')
.toBN('2154230509011102177917341711834485670139815171447919056633262592518907948680')
.toString()
);

const { r, s } = ec.sign(keyPair, msgHash);
expect(r.toString()).toBe(
'2220702546012141050051149396887481489960265709213083422658245644097500180866'
'706800951915233622090196542158919402159816118214143837213294331713137614072'
);
expect(s.toString()).toBe(
'1542316446019190634489932498001415389924394685441251344076931639569381539117'
'1857147121895075123389037565321926580259282654271568123966453051614350474888'
);
});
8 changes: 4 additions & 4 deletions __tests__/provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,24 @@ describe('defaultProvider', () => {
test('getTransactionStatus()', () => {
return expect(
defaultProvider.getTransactionStatus(
'0x2086ff26645fb0e31a3e252302f3cb1e7612c60389102e5473dfc89758a3aa9'
'0x774f7856b1ce6d5ce023a18cd5a06ab67e3a6d81c7bfcd01f99f32243c2d2ef'
)
).resolves.not.toThrow();
});
test('getTransaction()', async () => {
return expect(
defaultProvider.getTransaction(
'0x2086ff26645fb0e31a3e252302f3cb1e7612c60389102e5473dfc89758a3aa9'
'0x774f7856b1ce6d5ce023a18cd5a06ab67e3a6d81c7bfcd01f99f32243c2d2ef'
)
).resolves.not.toThrow();
});
test('callContract()', () => {
return expect(
defaultProvider.callContract({
contract_address: '0x58bceda58a83a5a100117ddc893234bad9c84a6833c2008f0f1ca90150149af',
contract_address: '0x9ff64f4ab0e1fe88df4465ade98d1ea99d5732761c39279b8e1374fa943e9b',
entry_point_selector: stark.getSelectorFromName('balance_of'),
calldata: compileCalldata({
user: '0x58bceda58a83a5a100117ddc893234bad9c84a6833c2008f0f1ca90150149af',
user: '0x9ff64f4ab0e1fe88df4465ade98d1ea99d5732761c39279b8e1374fa943e9b',
}),
})
).resolves.not.toThrow();
Expand Down
2 changes: 1 addition & 1 deletion __tests__/signer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe('deploy and test Wallet', () => {
test('read nonce', async () => {
const { result } = await signer.callContract({
contract_address: signer.address,
entry_point_selector: stark.getSelectorFromName('get_current_nonce'),
entry_point_selector: stark.getSelectorFromName('get_nonce'),
});
const nonce = result[0];

Expand Down
4 changes: 2 additions & 2 deletions __tests__/utils/__snapshots__/utils.browser.test.ts.snap

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions __tests__/utils/__snapshots__/utils.test.ts.snap

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions __tests__/utils/ellipticalCurve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ test('pedersen()', () => {
test('hashCalldata()', () => {
const array = ['1', '2', '3', '4'];
expect(hashCalldata(array)).toBe(
'0x1439c58e1c389a2ac51f8462ecc0a4ec7f812be1c04e3b82ce2af1c2cf959ef'
'0x66bd4335902683054d08a0572747ea78ebd9e531536fb43125424ca9f902084'
);
expect(array).toStrictEqual(['1', '2', '3', '4']);

expect(hashCalldata(['1', '2'])).toBe(
'0x2ab889bd35e684623df9b4ea4a4a1f6d9e0ef39b67c1293b8a89dd17e351235'
'0x501a3a8e6cd4f5241c639c74052aaa34557aafa84dd4ba983d6443c590ab7df'
);
});

Expand All @@ -38,13 +38,13 @@ test('hashMessage()', () => {
['1', '2'],
'2'
);
expect(hashMsg).toBe('0xf7ec4a68876819eed838be83b5d5dc337081f4a5fb8e421f3d9bdef7c69e9b');
expect(hashMsg).toBe('0x7f15c38ea577a26f4f553282fcfe4f1feeb8ecfaad8f221ae41abf8224cbddd');
const keyPair = getKeyPair(privateKey);
const { r, s } = sign(keyPair, removeHexPrefix(hashMsg));
expect(r.toString()).toStrictEqual(
toBN('2699852629692218907583414128365108566181098618321049245303767746418549764831').toString()
toBN('2458502865976494910213617956670505342647705497324144349552978333078363662855').toString()
);
expect(s.toString()).toStrictEqual(
toBN('2362979021721299440845279407227912881357338080403308888611869245024056250189').toString()
toBN('3439514492576562277095748549117516048613512930236865921315982886313695689433').toString()
);
});
6 changes: 3 additions & 3 deletions __tests__/utils/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'fs';

import { constants, json, number, stark } from '../../src';
import { constants, hash, json, number, stark } from '../../src';

const { IS_BROWSER } = constants;

Expand Down Expand Up @@ -61,13 +61,13 @@ describe('getSelectorFromName()', () => {
});
describe('computeHashOnElements()', () => {
test('should return valid hash for empty array', () => {
const res = stark.computeHashOnElements([]);
const res = hash.computeHashOnElements([]);
expect(res).toMatchInlineSnapshot(
`"0x49ee3eba8c1600700ee1b87eb599f16716b0b1022947733551fde4050ca6804"`
);
});
test('should return valid hash for valid array', () => {
const res = stark.computeHashOnElements([
const res = hash.computeHashOnElements([
number.toBN(123782376),
number.toBN(213984),
number.toBN(128763521321),
Expand Down
8 changes: 5 additions & 3 deletions src/provider/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class Provider implements ProviderInterface {
switch (name) {
case 'alpha':
default:
return 'https://alpha3.starknet.io';
return 'https://alpha4.starknet.io';
}
}

Expand Down Expand Up @@ -257,8 +257,9 @@ export class Provider implements ProviderInterface {
});
}

public async waitForTx(txHash: BigNumberish, retryInterval: number = 2000) {
public async waitForTx(txHash: BigNumberish, retryInterval: number = 5000) {
let onchain = false;
let firstRun = true;
while (!onchain) {
// eslint-disable-next-line no-await-in-loop
await wait(retryInterval);
Expand All @@ -269,9 +270,10 @@ export class Provider implements ProviderInterface {
onchain = true;
} else if (res.tx_status === 'REJECTED') {
throw Error('REJECTED');
} else if (res.tx_status === 'NOT_RECEIVED') {
} else if (res.tx_status === 'NOT_RECEIVED' && !firstRun) {
throw Error('NOT_RECEIVED');
}
firstRun = false;
}
}
}
4 changes: 2 additions & 2 deletions src/signer/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ export class Signer extends Provider implements SignerInterface {

const { result } = await this.callContract({
contract_address: this.address,
entry_point_selector: getSelectorFromName('get_current_nonce'),
entry_point_selector: getSelectorFromName('get_nonce'),
});
const nonceBn = toBN(result[0]);
const calldataDecimal = (tx.calldata || []).map((x) => toBN(x).toString());

const msgHash = addHexPrefix(
hashMessage(
'0', // needs to be walletAddress once it's possible to retrieve address(self) in cairo
this.address,
tx.contract_address,
tx.entry_point_selector,
calldataDecimal,
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,5 @@ export type GetTransactionResponse = {
export type AddTransactionResponse = {
code: TxStatus;
transaction_hash: string;
address: string;
address?: string;
};
20 changes: 6 additions & 14 deletions src/utils/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,12 @@ export function pedersen(input: [BigNumberish, BigNumberish]) {
return addHexPrefix(point.getX().toString(16));
}

export function computeHashOnElements(data: BigNumberish[]) {
return [...data, data.length].reduce((x, y) => pedersen([x, y]), 0).toString();
}

export function hashCalldata(calldata: string[]): string {
const calldataCopy = [...calldata];
if (calldataCopy.length === 0) {
return '0';
}
if (calldataCopy.length === 1) {
return calldataCopy[0];
}
// calldata element will always be there as it was checked by an if statement before (!)
const calldataEl = calldataCopy.shift()!;
return pedersen([hashCalldata(calldataCopy), calldataEl]);
return computeHashOnElements(calldata);
}

export function hashMessage(
Expand All @@ -63,9 +58,6 @@ export function hashMessage(
calldata: string[],
nonce: string
) {
const hash0 = pedersen([account, to]);
const hash1 = pedersen([hash0, selector]);
const calldataHash = hashCalldata(calldata);
const hash2 = pedersen([hash1, calldataHash]);
return pedersen([hash2, nonce]);
return computeHashOnElements([account, to, selector, calldataHash, nonce]);
}
6 changes: 1 addition & 5 deletions src/utils/stark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { gzip } from 'pako';
import { CompressedProgram, Program } from '../types';
import { genKeyPair, getStarkKey } from './ellipticCurve';
import { addHexPrefix, btoaUniversal } from './encode';
import { pedersen, starknetKeccak } from './hash';
import { starknetKeccak } from './hash';
import { stringify } from './json';
import { BigNumberish, toBN, toHex } from './number';

Expand Down Expand Up @@ -49,7 +49,3 @@ export function formatSignature(sig?: [BigNumberish, BigNumberish]): [string, st
return [];
}
}

export function computeHashOnElements(data: BigNumberish[]) {
return [...data, data.length].reduce((x, y) => pedersen([x, y]), 0);
}

0 comments on commit f12db5a

Please sign in to comment.