Skip to content

Commit

Permalink
fix: implement Petar requests
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippeR26 committed Aug 22, 2024
1 parent 0d0d2c7 commit 7dde016
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 38 deletions.
10 changes: 0 additions & 10 deletions __tests__/utils/ec.test.ts

This file was deleted.

2 changes: 1 addition & 1 deletion __tests__/utils/num.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ describe('stringToSha256ToArrayBuff4', () => {
});
});

describe.only('isBigNumberish', () => {
describe('isBigNumberish', () => {
test('determine if value is a BigNumberish', () => {
expect(num.isBigNumberish(234)).toBe(true);
expect(num.isBigNumberish(234n)).toBe(true);
Expand Down
9 changes: 9 additions & 0 deletions __tests__/utils/stark.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,12 @@ describe('stark', () => {
expect(stark.v3Details(detailsUndefined)).toEqual(expect.objectContaining(detailsAnything));
});
});

describe('ec full public key', () => {
test('determine if value is a BigNumberish', () => {
const privateKey1 = '0x43b7240d227aa2fb8434350b3321c40ac1b88c7067982549e7609870621b535';
expect(stark.getFullPublicKey(privateKey1)).toBe(
'0x0400b730bd22358612b5a67f8ad52ce80f9e8e893639ade263537e6ef35852e5d3057795f6b090f7c6985ee143f798608a53b3659222c06693c630857a10a92acf'
);
});
});
3 changes: 1 addition & 2 deletions __tests__/utils/typedData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
Account,
BigNumberish,
StarknetDomain,
ec,
num,
stark,
typedData,
Expand Down Expand Up @@ -361,7 +360,7 @@ describe('typedData', () => {
describe('verifyMessage', () => {
const addr = '0x64b48806902a367c8598f4f95c305e8c1a1acba5f082d294a43793113115691';
const privK = '0x71d7bb07b9a64f6f78ac4c816aff4da9';
const fullPubK = ec.getFullPublicKey(privK);
const fullPubK = stark.getFullPublicKey(privK);
const myAccount = new Account({ nodeUrl: 'fake' }, addr, privK);
let signedMessage: Signature;
let hashedMessage: string;
Expand Down
20 changes: 0 additions & 20 deletions src/utils/ec.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,3 @@
import { getPublicKey } from '@scure/starknet';
import type { BigNumberish } from '../types';
import { addHexPrefix, buf2hex } from './encode';

// TODO rename
export * as starkCurve from '@scure/starknet';
export * as weierstrass from '@noble/curves/abstract/weierstrass';

/**
* get the hex string of the full public key related to a Starknet private key.
* @param {BigNumberish} privateKey a 252 bits private key.
* @returns {string} an hex string of a 520 bit number, representing the full public key related to `privateKey`.
* @example
* ```typescript
* const result = ec.getFullPublicKey("0x43b7240d227aa2fb8434350b3321c40ac1b88c7067982549e7609870621b535");
* // result = "0x0400b730bd22358612b5a67f8ad52ce80f9e8e893639ade263537e6ef35852e5d3057795f6b090f7c6985ee143f798608a53b3659222c06693c630857a10a92acf"
* ```
*/
export function getFullPublicKey(privateKey: BigNumberish): string {
const privKey = addHexPrefix(BigInt(privateKey).toString(16));
const fullPrivKey = addHexPrefix(buf2hex(getPublicKey(privKey, false)));
return fullPrivKey;
}
2 changes: 1 addition & 1 deletion src/utils/num.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,6 @@ export function isBigNumberish(input: unknown): input is BigNumberish {
return (
isNumber(input) ||
isBigInt(input) ||
(typeof input === 'string' && (isHex(input) || /^[0-9]*$/i.test(input)))
(typeof input === 'string' && (isHex(input) || isStringWholeNumber(input)))
);
}
20 changes: 18 additions & 2 deletions src/utils/stark.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import type { SPEC } from 'starknet-types-07';
import { getStarkKey, utils } from '@scure/starknet';
import { getPublicKey, getStarkKey, utils } from '@scure/starknet';
import { gzip, ungzip } from 'pako';

import { ZERO, FeeMarginPercentage } from '../constants';
Expand All @@ -14,7 +14,7 @@ import {
} from '../types';
import { EDAMode, EDataAvailabilityMode, ETransactionVersion, ResourceBounds } from '../types/api';
import { FeeEstimate } from '../types/provider';
import { addHexPrefix, arrayBufferToString, atobUniversal, btoaUniversal } from './encode';
import { addHexPrefix, arrayBufferToString, atobUniversal, btoaUniversal, buf2hex } from './encode';
import { parse, stringify } from './json';
import {
addPercent,
Expand Down Expand Up @@ -351,3 +351,19 @@ export function reduceV2(providedVersion: ETransactionVersion): ETransactionVers
if (providedVersion === ETransactionVersion.V2) return ETransactionVersion.V1;
return providedVersion;
}

/**
* get the hex string of the full public key related to a Starknet private key.
* @param {BigNumberish} privateKey a 252 bits private key.
* @returns {string} an hex string of a 520 bit number, representing the full public key related to `privateKey`.
* @example
* ```typescript
* const result = ec.getFullPublicKey("0x43b7240d227aa2fb8434350b3321c40ac1b88c7067982549e7609870621b535");
* // result = "0x0400b730bd22358612b5a67f8ad52ce80f9e8e893639ade263537e6ef35852e5d3057795f6b090f7c6985ee143f798608a53b3659222c06693c630857a10a92acf"
* ```
*/
export function getFullPublicKey(privateKey: BigNumberish): string {
const privKey = toHex(privateKey);
const fullPrivKey = addHexPrefix(buf2hex(getPublicKey(privKey, false)));
return fullPrivKey;
}
17 changes: 17 additions & 0 deletions src/utils/typedData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,23 @@ export function getMessageHash(typedData: TypedData, account: BigNumberish): str
* // result1 = result2 = true
* ```
*/
export function verifyMessage(
message: TypedData,
signature: Signature,
fullPublicKey: BigNumberish,
accountAddress: BigNumberish
): boolean;
export function verifyMessage(
message: BigNumberish,
signature: Signature,
fullPublicKey: BigNumberish
): boolean;
export function verifyMessage(
message: BigNumberish | TypedData,
signature: Signature,
fullPublicKey: BigNumberish,
accountAddress?: BigNumberish
): boolean;
export function verifyMessage(
message: BigNumberish | TypedData,
signature: Signature,
Expand Down
4 changes: 2 additions & 2 deletions www/docs/guides/signature.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { ec, hash, type BigNumberish, type WeierstrassSignatureType } from 'star

const privateKey = '0x1234567890987654321';
const starknetPublicKey = ec.starkCurve.getStarkKey(privateKey);
const fullPublicKey = ec.getFullPublicKey(privateKey);
const fullPublicKey = stark.getFullPublicKey(privateKey);
const message: BigNumberish[] = [1, 128, 18, 14];

const msgHash = hash.computeHashOnElements(message);
Expand Down Expand Up @@ -143,7 +143,7 @@ const myTypedData: TypedData = {
};

const account0 = new Account(myProvider, address, privateKey);
const fullPublicKey = ec.getFullPublicKey(privateKey);
const fullPublicKey = stark.getFullPublicKey(privateKey);

const msgHash = await account0.hashMessage(myTypedData);
const signature: Signature = (await account0.signMessage(myTypedData)) as WeierstrassSignatureType;
Expand Down

0 comments on commit 7dde016

Please sign in to comment.