Skip to content

Commit

Permalink
feat: use ec internally
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvkelawala committed Jan 23, 2023
1 parent 649e0aa commit 3392d03
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 39 deletions.
12 changes: 5 additions & 7 deletions __tests__/account.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { getStarkKey, pedersen, sign } from '@noble/curves/stark';

import typedDataExample from '../__mocks__/typedDataExample.json';
import { Account, Contract, Provider, number, stark } from '../src';
import { Account, Contract, Provider, ec, number, stark } from '../src';
import { parseUDCEvent } from '../src/utils/events';
import { calculateContractAddressFromHash, feeTransactionVersion } from '../src/utils/hash';
import { cleanHex, hexToDecimalString, toBigInt, toHex } from '../src/utils/number';
Expand Down Expand Up @@ -241,11 +239,11 @@ describe('deploy and test Wallet', () => {
'1893860513534673656759973582609638731665558071107553163765293299136715951024';
const whitelistingPrivateKey =
'301579081698031303837612923223391524790804435085778862878979120159194507372';
const hashed = pedersen(
pedersen(toBigInt('18925'), toBigInt('1922775124')),
const hashed = ec.starkCurve.pedersen(
ec.starkCurve.pedersen(toBigInt('18925'), toBigInt('1922775124')),
toBigInt(account.address)
);
const signed = sign(hashed, toHex(whitelistingPrivateKey));
const signed = ec.starkCurve.sign(hashed, toHex(whitelistingPrivateKey));

const { transaction_hash } = await account.execute([
{
Expand Down Expand Up @@ -408,7 +406,7 @@ describe('deploy and test Wallet', () => {
await provider.waitForTransaction(declareAccount.transaction_hash);

const privateKey = stark.randomAddress();
starkKeyPub = getStarkKey(privateKey);
starkKeyPub = privateKey;
precalculatedAddress = calculateContractAddressFromHash(
starkKeyPub,
accountClassHash,
Expand Down
19 changes: 9 additions & 10 deletions __tests__/utils/ellipticalCurve.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { getPublicKey, getStarkKey, pedersen, sign, verify } from '@noble/curves/stark';

import { ec } from '../../src';
import { StarknetChainId } from '../../src/constants';
import {
calculateTransactionHash,
Expand All @@ -10,18 +9,18 @@ import { fromCallsToExecuteCalldataWithNonce } from '../../src/utils/transaction

test('getKeyPair()', () => {
const privateKey = '0x019800ea6a9a73f94aee6a3d2edf018fc770443e90c7ba121e8303ec6b349279';
const starkKey = getStarkKey(privateKey);
const starkKey = ec.starkCurve.getStarkKey(privateKey);
// somehow needed, returns error else
expect(starkKey).toBe('0x33f45f07e1bd1a51b45fc24ec8c8c9908db9e42191be9e169bfcac0c0d99745');
});

test('pedersen()', () => {
const own = pedersen('0x12773', '0x872362');
const own = ec.starkCurve.pedersen('0x12773', '0x872362');
expect(own).toMatchSnapshot();
});

test('pedersen() with 0', () => {
const own = pedersen('0x12773', '0x0');
const own = ec.starkCurve.pedersen('0x12773', '0x0');
expect(own).toMatchSnapshot();
});

Expand Down Expand Up @@ -65,7 +64,7 @@ test('hashMessage()', () => {
`"0x6d1706bd3d1ba7c517be2a2a335996f63d4738e2f182144d078a1dd9997062e"`
);

const { r, s } = sign(hashMsg, privateKey);
const { r, s } = ec.starkCurve.sign(hashMsg, privateKey);

expect(r.toString()).toMatchInlineSnapshot(
`"1427981024487605678086498726488552139932400435436186597196374630267616399345"`
Expand All @@ -79,8 +78,8 @@ test('verify signed message()', () => {
const pk = '0x019800ea6a9a73f94aee6a3d2edf018fc770443e90c7ba121e8303ec6b349279';
const account = '0x33f45f07e1bd1a51b45fc24ec8c8c9908db9e42191be9e169bfcac0c0d99745';
const price = '1';
const hashMsg = pedersen(account, price);
const signature = sign(hashMsg, pk);
const pubKey = getPublicKey(pk);
expect(verify(signature.toDERHex(), hashMsg, pubKey)).toBe(true);
const hashMsg = ec.starkCurve.pedersen(account, price);
const signature = ec.starkCurve.sign(hashMsg, pk);
const pubKey = ec.starkCurve.getPublicKey(pk);
expect(ec.starkCurve.verify(signature.toDERHex(), hashMsg, pubKey)).toBe(true);
});
6 changes: 2 additions & 4 deletions __tests__/utils/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import fs from 'fs';

import { pedersen } from '@noble/curves/stark';

import { constants, hash, json, number, stark } from '../../src';
import { constants, ec, hash, json, number, stark } from '../../src';
import { Block } from '../../src/provider/utils';

const { IS_BROWSER } = constants;
Expand Down Expand Up @@ -110,7 +108,7 @@ describe('calculateContractAddressFromHash()', () => {
const classHash = '0x55187E68C60664A947048E0C9E5322F9BF55F7D435ECDCF17ED75724E77368F';

// Any type of salt can be used. It depends on the dApp what kind of salt it wants to use.
const salt = pedersen(ethAddress, daiAddress);
const salt = ec.starkCurve.pedersen(ethAddress, daiAddress);

const res = hash.calculateContractAddressFromHash(
salt,
Expand Down
5 changes: 2 additions & 3 deletions src/account/default.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { pedersen } from '@noble/curves/stark';

import { UDC, ZERO } from '../constants';
import { ProviderInterface, ProviderOptions } from '../provider';
import { Provider } from '../provider/default';
Expand Down Expand Up @@ -31,6 +29,7 @@ import {
UniversalDeployerContractPayload,
} from '../types';
import { EstimateFeeBulk, TransactionSimulation } from '../types/account';
import { starkCurve } from '../utils/ec';
import { parseUDCEvent } from '../utils/events';
import {
calculateContractAddressFromHash,
Expand Down Expand Up @@ -409,7 +408,7 @@ export class Account extends Provider implements AccountInterface {
],
},
address: calculateContractAddressFromHash(
unique ? pedersen(this.address, deploySalt) : deploySalt,
unique ? starkCurve.pedersen(this.address, deploySalt) : deploySalt,
classHash,
compiledConstructorCallData,
unique ? UDC.ADDRESS : 0
Expand Down
15 changes: 7 additions & 8 deletions src/signer/default.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { getStarkKey, sign, utils } from '@noble/curves/stark';

import { Abi, Call, DeclareSignerDetails, InvocationsSignerDetails, Signature } from '../types';
import { DeployAccountSignerDetails } from '../types/signer';
import { starkCurve } from '../utils/ec';
import { buf2hex } from '../utils/encode';
import {
calculateDeclareTransactionHash,
Expand All @@ -16,17 +15,17 @@ import { SignerInterface } from './interface';
export class Signer implements SignerInterface {
protected pk: Uint8Array | string;

constructor(pk: Uint8Array | string = utils.randomPrivateKey()) {
constructor(pk: Uint8Array | string = starkCurve.utils.randomPrivateKey()) {
this.pk = pk instanceof Uint8Array ? buf2hex(pk) : toHex(pk);
}

public async getPubKey(): Promise<string> {
return getStarkKey(this.pk);
return starkCurve.getStarkKey(this.pk);
}

public async signMessage(typedData: TypedData, accountAddress: string): Promise<Signature> {
const msgHash = getMessageHash(typedData, accountAddress);
return sign(msgHash, this.pk);
return starkCurve.sign(msgHash, this.pk);
}

public async signTransaction(
Expand All @@ -50,7 +49,7 @@ export class Signer implements SignerInterface {
transactionsDetail.nonce
);

return sign(msgHash, this.pk);
return starkCurve.sign(msgHash, this.pk);
}

public async signDeployAccountTransaction({
Expand All @@ -74,7 +73,7 @@ export class Signer implements SignerInterface {
nonce
);

return sign(msgHash, this.pk);
return starkCurve.sign(msgHash, this.pk);
}

public async signDeclareTransaction(
Expand All @@ -90,6 +89,6 @@ export class Signer implements SignerInterface {
nonce
);

return sign(msgHash, this.pk);
return starkCurve.sign(msgHash, this.pk);
}
}
2 changes: 1 addition & 1 deletion src/utils/ec.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * as stark from '@noble/curves/stark';
export * as starkCurve from '@noble/curves/stark';
export * as weierstrass from '@noble/curves/abstract/weierstrass';
6 changes: 3 additions & 3 deletions src/utils/hash.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { keccak, pedersen } from '@noble/curves/stark';
/* eslint-disable no-param-reassign */
/* eslint-disable import/extensions */
import { keccak256 } from 'ethereum-cryptography/keccak.js';
import { hexToBytes } from 'ethereum-cryptography/utils.js';

import { API_VERSION, MASK_250, StarknetChainId, TransactionHashPrefix } from '../constants';
import { CompiledContract, RawCalldata } from '../types/lib';
import { starkCurve } from './ec';
import { addHexPrefix, buf2hex, removeHexPrefix, utf8ToArray } from './encode';
import { parse, stringify } from './json';
import {
Expand Down Expand Up @@ -74,7 +74,7 @@ export function getSelector(value: string) {

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

Expand Down Expand Up @@ -246,7 +246,7 @@ export default function computeHintedClassHash(compiledContract: CompiledContrac
[false, '']
)[1];

return addHexPrefix(keccak(utf8ToArray(serialisedJson)).toString(16));
return addHexPrefix(starkCurve.keccak(utf8ToArray(serialisedJson)).toString(16));
}

// Computes the class hash of a given contract class
Expand Down
5 changes: 2 additions & 3 deletions src/utils/merkle.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { pedersen } from '@noble/curves/stark';

import { starkCurve } from './ec';
import { toBigInt } from './number';

export class MerkleTree {
Expand Down Expand Up @@ -34,7 +33,7 @@ export class MerkleTree {

static hash(a: string, b: string) {
const [aSorted, bSorted] = [toBigInt(a), toBigInt(b)].sort((x, y) => (x >= y ? 1 : -1));
return pedersen(aSorted, bSorted);
return starkCurve.pedersen(aSorted, bSorted);
}

public getProof(leaf: string, branch = this.leaves, hashPath: string[] = []): string[] {
Expand Down

0 comments on commit 3392d03

Please sign in to comment.