Skip to content

Commit

Permalink
fix: use function consistently
Browse files Browse the repository at this point in the history
  • Loading branch information
janek26 committed Oct 29, 2021
1 parent e08e4b5 commit 67617d7
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 42 deletions.
2 changes: 1 addition & 1 deletion __tests__/ec.test.ts → __tests__/ellipticalCurve.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getKeyPair, getStarkKey, hashCalldata, hashMessage, pedersen, sign } from '../src';
import { removeHexPrefix } from '../src/utils/enc';
import { removeHexPrefix } from '../src/utils/encode';
import { toBN, toHex } from '../src/utils/number';

test('does work with package', () => {
Expand Down
2 changes: 1 addition & 1 deletion __tests__/wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
utils,
waitForTx,
} from '../src';
import { addHexPrefix } from '../src/utils/enc';
import { addHexPrefix } from '../src/utils/encode';
import { toBN } from '../src/utils/number';

const {
Expand Down
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { toBN } from './utils/number';

export { IS_BROWSER } from './utils/enc';
export { IS_BROWSER } from './utils/encode';

export const ZERO = toBN(0);
export const ONE = toBN(1);
Expand Down
8 changes: 4 additions & 4 deletions src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ import { getSelectorFromName } from './utils/starknet';
type Args = { [inputName: string]: string | string[] };
type Calldata = string[];

const parseFelt = (candidate: string): BN => {
function parseFelt(candidate: string): BN {
try {
return toBN(candidate);
} catch (e) {
throw Error('Couldnt parse felt');
}
};
}

const isFelt = (candidate: string): boolean => {
function isFelt(candidate: string): boolean {
try {
parseFelt(candidate);
return true;
} catch (e) {
return false;
}
};
}

export class Contract {
connectedTo: string | null = null;
Expand Down
10 changes: 5 additions & 5 deletions src/ec.ts → src/ellipticalCurve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import hashJS from 'hash.js';
import assert from 'minimalistic-assert';

import { CONSTANT_POINTS, EC_ORDER, FIELD_PRIME, MAX_ECDSA_VAL, ONE, ZERO } from './constants';
import { addHexPrefix, removeHexPrefix, sanitizeBytes } from './utils/enc';
import { addHexPrefix, removeHexPrefix, sanitizeBytes } from './utils/encode';
import { BigNumberish, assertInRange, toBN, toHex } from './utils/number';

export const ec = new EC(
Expand Down Expand Up @@ -40,17 +40,17 @@ function fixMessage(msg: string) {

export const genKeyPair = ec.genKeyPair.bind(ec);

export const getKeyPair = (pk: BigNumberish): EC.KeyPair => {
export function getKeyPair(pk: BigNumberish): EC.KeyPair {
const pkBn = toBN(pk);
return ec.keyFromPrivate(removeHexPrefix(toHex(pkBn)), 'hex');
};
}

export const getStarkKey = (keyPair: EC.KeyPair): string => {
export function getStarkKey(keyPair: EC.KeyPair): string {
// this method needs to be run to generate the .pub property used below
// the result can be dumped
keyPair.getPublic(true, 'hex');
return addHexPrefix(sanitizeBytes((keyPair as any).pub.getX().toString(16), 2));
};
}

/*
Signs a message using the provided key.
Expand Down
4 changes: 3 additions & 1 deletion src/starknet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,9 @@ export function deployContract(
});
}

const wait = (delay: number) => new Promise((res) => setTimeout(res, delay));
function wait(delay: number) {
return new Promise((res) => setTimeout(res, delay));
}
export async function waitForTx(txId: number, retryInterval: number = 2000) {
let onchain = false;
while (!onchain) {
Expand Down
15 changes: 9 additions & 6 deletions src/utils/enc.ts → src/utils/encode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ export const IS_BROWSER = typeof window !== 'undefined';

const STRING_ZERO = '0';

export const arrayBufferToString = (array: ArrayBuffer): string =>
String.fromCharCode.apply(null, array as unknown as number[]);
export function arrayBufferToString(array: ArrayBuffer): string {
return String.fromCharCode.apply(null, array as unknown as number[]);
}

export const btoaUniversal = (b: ArrayBuffer): string =>
IS_BROWSER ? btoa(arrayBufferToString(b)) : Buffer.from(b).toString('base64');
export function btoaUniversal(b: ArrayBuffer): string {
return IS_BROWSER ? btoa(arrayBufferToString(b)) : Buffer.from(b).toString('base64');
}

export const buf2hex = (buffer: Uint8Array) =>
[...buffer].map((x) => x.toString(16).padStart(2, '0')).join('');
export function buf2hex(buffer: Uint8Array) {
return [...buffer].map((x) => x.toString(16).padStart(2, '0')).join('');
}

/**
* Some function imported from https://github.com/pedrouid/enc-utils/blob/master/src/index.ts
Expand Down
8 changes: 5 additions & 3 deletions src/utils/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { keccak256 } from 'ethereum-cryptography/keccak';
import assert from 'minimalistic-assert';

import { CONSTANT_POINTS, FIELD_PRIME, MASK_250, ONE, ZERO } from '../constants';
import { ec } from '../ec';
import { addHexPrefix, buf2hex, utf8ToArray } from './enc';
import { ec } from '../ellipticalCurve';
import { addHexPrefix, buf2hex, utf8ToArray } from './encode';
import { BigNumberish, toBN } from './number';

const keccakHex = (value: string): string => addHexPrefix(buf2hex(keccak256(utf8ToArray(value))));
function keccakHex(value: string): string {
return addHexPrefix(buf2hex(keccak256(utf8ToArray(value))));
}

/**
* Function to get the starknet keccak hash from a string
Expand Down
2 changes: 1 addition & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * as enc from './enc';
export * as enc from './encode';
export * as hash from './hash';
export * as json from './json';
export * as number from './number';
Expand Down
19 changes: 10 additions & 9 deletions src/utils/number.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import BN from 'bn.js';
import assert from 'minimalistic-assert';

import { addHexPrefix, removeHexPrefix } from './enc';
import { addHexPrefix, removeHexPrefix } from './encode';

export type BigNumberish = string | number | BN;

export const isHex = (hex: string): boolean => {
export function isHex(hex: string): boolean {
return hex.startsWith('0x');
};
}

export const toBN = (number: BigNumberish, base?: number | 'hex') => {
export function toBN(number: BigNumberish, base?: number | 'hex') {
if (typeof number === 'string' && isHex(number) && !base)
return new BN(removeHexPrefix(number), 'hex');
return new BN(number, base);
};
}

export const toHex = (number: BN): string => {
export function toHex(number: BN): string {
return addHexPrefix(number.toString('hex'));
};
}

export const hexToDecimalString = (hex: string): string =>
toBN(`0x${hex.replace(/^0x/, '')}`).toString();
export function hexToDecimalString(hex: string): string {
return toBN(`0x${hex.replace(/^0x/, '')}`).toString();
}

/*
Asserts input is equal to or greater then lowerBound and lower then upperBound.
Expand Down
8 changes: 4 additions & 4 deletions src/utils/starknet.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { gzip } from 'pako';

import { genKeyPair, getStarkKey } from '../ec';
import { genKeyPair, getStarkKey } from '../ellipticalCurve';
import { CompressedProgram, Program } from '../types';
import { addHexPrefix, btoaUniversal } from './enc';
import { addHexPrefix, btoaUniversal } from './encode';
import { starknetKeccak } from './hash';
import { stringify } from './json';
import { toHex } from './number';
Expand Down Expand Up @@ -32,10 +32,10 @@ export function getSelectorFromName(funcName: string) {
return toHex(starknetKeccak(funcName));
}

export const randomAddress = (): string => {
export function randomAddress(): string {
const randomKeyPair = genKeyPair();
return getStarkKey(randomKeyPair);
};
}

export function makeAddress(input: string): string {
return addHexPrefix(input).toLowerCase();
Expand Down
12 changes: 6 additions & 6 deletions src/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { pedersen } from './utils/hash';

export * from './ec';
export * from './ellipticalCurve';

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

export const hashMessage = (
export function hashMessage(
account: string,
to: string,
selector: string,
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]);
};
}

0 comments on commit 67617d7

Please sign in to comment.