Skip to content

Commit f369a3b

Browse files
address comments
1 parent b114b3d commit f369a3b

File tree

6 files changed

+26
-20
lines changed

6 files changed

+26
-20
lines changed

src/helpers/common.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ import { Ecies } from "@toruslabs/eccrypto";
1616
import { keccak256 as keccakHash } from "ethereum-cryptography/keccak";
1717
import JsonStringify from "json-stable-stringify";
1818

19-
import { CommitmentRequestResult, EciesHex, GetORSetKeyResponse, KeyType, Point2D, VerifierLookupResponse } from "../interfaces";
19+
import { AffinePoint, CommitmentRequestResult, EciesHex, GetORSetKeyResponse, KeyType, VerifierLookupResponse } from "../interfaces";
2020

2121
export type Curve = typeof secp256k1 | typeof ed25519;
2222

23-
export function derivePubKey(ecCurve: Curve, sk: bigint): Point2D {
23+
export function derivePubKey(ecCurve: Curve, sk: bigint): AffinePoint {
2424
return ecCurve.Point.BASE.multiply(sk).toAffine();
2525
}
2626

src/helpers/keyUtils.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { sha512 } from "ethereum-cryptography/sha512";
77
import stringify from "json-stable-stringify";
88
import log from "loglevel";
99

10-
import { EncryptedSeed, GetOrSetNonceResult, ImportedShare, KeyType, Point2D, PrivateKeyData, ShareJSON, v2NonceResultType } from "../interfaces";
10+
import { AffinePoint, EncryptedSeed, GetOrSetNonceResult, ImportedShare, KeyType, PrivateKeyData, ShareJSON, v2NonceResultType } from "../interfaces";
1111
import {
1212
bigintToHex,
1313
bytesToBase64,
@@ -68,7 +68,7 @@ function adjustScalarBytes(bytes: Uint8Array): Uint8Array {
6868
/** Convenience method that creates public key and other stuff. RFC8032 5.1.5 */
6969
export function getEd25519ExtendedPublicKey(keyBytes: Uint8Array): {
7070
scalar: bigint;
71-
point: Point2D;
71+
point: AffinePoint;
7272
} {
7373
const ed25519Curve = getKeyCurve(KEY_TYPE.ED25519);
7474
const len = 32;
@@ -90,7 +90,7 @@ export function getEd25519ExtendedPublicKey(keyBytes: Uint8Array): {
9090
return { scalar, point };
9191
}
9292

93-
export function encodeEd25519Point(point: Point2D): Uint8Array {
93+
export function encodeEd25519Point(point: AffinePoint): Uint8Array {
9494
const ed25519Curve = getKeyCurve(KEY_TYPE.ED25519);
9595
return ed25519Curve.Point.fromAffine(point).toBytes();
9696
}
@@ -152,10 +152,9 @@ export const generateSecp256k1KeyData = async (scalarBytes: Uint8Array): Promise
152152
};
153153
};
154154

155-
function generateAddressFromPoint(keyType: KeyType, point: Point2D): string {
155+
function generateAddressFromPoint(keyType: KeyType, point: AffinePoint): string {
156156
if (keyType === KEY_TYPE.SECP256K1) {
157-
const uncompressed = getSecp256k1().Point.fromAffine(point).toBytes(false);
158-
const publicKey = uncompressed.slice(1); // remove 04 prefix
157+
const publicKey = getSecp256k1PublicKeyFromAffinePoint(point);
159158
const evmAddressLower = `0x${keccak256(publicKey).slice(64 - 38)}`;
160159
return toChecksumAddress(evmAddressLower);
161160
} else if (keyType === KEY_TYPE.ED25519) {
@@ -182,6 +181,12 @@ export function getPostboxKeyFrom1OutOf1(ecCurve: Curve, privKey: string, nonce:
182181
return bigintToHex(mod(privKeyBI - nonceBI, ecCurve.Point.CURVE().n));
183182
}
184183

184+
export function getSecp256k1PublicKeyFromAffinePoint(point: AffinePoint): Uint8Array {
185+
const uncompressed = getSecp256k1().Point.fromAffine(point).toBytes(false);
186+
const publicKey = uncompressed.slice(1); // remove 04 prefix
187+
return publicKey;
188+
}
189+
185190
export const generateShares = async (
186191
ecCurve: Curve,
187192
keyType: KeyType,

src/helpers/metadataUtils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ import log from "loglevel";
88

99
import { SAPPHIRE_DEVNET_METADATA_URL, SAPPHIRE_METADATA_URL } from "../constants";
1010
import {
11+
AffinePoint,
1112
EciesHex,
1213
EncryptedSeed,
1314
GetOrSetNonceResult,
1415
KeyType,
1516
MetadataParams,
1617
NonceMetadataParams,
17-
Point2D,
1818
SapphireMetadataParams,
1919
SetNonceData,
2020
} from "../interfaces";
@@ -40,7 +40,7 @@ export const getSecpKeyFromEd25519 = (
4040
ed25519Scalar: bigint
4141
): {
4242
scalar: bigint;
43-
point: Point2D;
43+
point: AffinePoint;
4444
} => {
4545
const secp256k1 = getSecp256k1();
4646
const N = secp256k1.Point.CURVE().n;

src/helpers/nodeUtils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { generateJsonRPCObject, get, post } from "@toruslabs/http-helpers";
55
import { config } from "../config";
66
import { JRPC_METHODS } from "../constants";
77
import {
8+
AffinePoint,
89
CommitmentRequestResult,
910
ExtendedPublicKey,
1011
GetORSetKeyResponse,
@@ -14,7 +15,6 @@ import {
1415
JRPCResponse,
1516
KeyLookupResult,
1617
KeyType,
17-
Point2D,
1818
SessionToken,
1919
ShareRequestResult,
2020
TorusKey,
@@ -825,7 +825,7 @@ export async function retrieveOrImportShare(params: {
825825
}
826826
}
827827
let metadataNonce = nonceResult?.nonce ? toBigIntBE(nonceResult.nonce) : 0n;
828-
let finalPubKey: Point2D;
828+
let finalPubKey: AffinePoint;
829829
let pubNonce: { X: string; Y: string } | undefined;
830830
let typeOfUser: UserType = "v1";
831831
const N = ecCurve.Point.CURVE().n;

src/interfaces.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import type { AffinePoint as AffinePointCurve } from "@noble/curves/abstract/curve.js";
12
import type { INodePub, TORUS_NETWORK_TYPE } from "@toruslabs/constants";
23
import { Ecies } from "@toruslabs/eccrypto";
34

45
import { TorusUtilsExtraParams } from "./TorusUtilsExtraParams";
56

6-
export type Point2D = { x: bigint; y: bigint };
7+
export type AffinePoint = AffinePointCurve<bigint>;
78

89
export interface KeyIndex {
910
index: string;
@@ -152,7 +153,7 @@ export interface ShareRequestResult {
152153
export interface ImportedShare {
153154
oauth_pub_key_x: string;
154155
oauth_pub_key_y: string;
155-
final_user_point: Point2D;
156+
final_user_point: AffinePoint;
156157
signing_pub_key_x: string;
157158
signing_pub_key_y: string;
158159
encrypted_share: string;
@@ -254,7 +255,7 @@ export interface PrivateKeyData {
254255
SigningPubY: bigint;
255256
metadataNonce: bigint;
256257
metadataSigningKey: bigint;
257-
finalUserPubKeyPoint: Point2D;
258+
finalUserPubKeyPoint: AffinePoint;
258259
encryptedSeed?: string;
259260
}
260261

src/torus.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ import {
2222
toBigIntBE,
2323
} from "./helpers";
2424
import {
25+
AffinePoint,
2526
GetOrSetNonceResult,
2627
ImportKeyParams,
2728
KeyType,
2829
LegacyVerifierLookupResponse,
29-
Point2D,
3030
RetrieveSharesParams,
3131
TorusCtorOptions,
3232
TorusKey,
@@ -310,8 +310,8 @@ class Torus {
310310
const { pub_key_X: X, pub_key_Y: Y } = keyResult.keys[0];
311311
let pubNonce: { X: string; Y: string } | undefined;
312312
const nonce = toBigIntBE(nonceResult?.nonce || "0");
313-
let oAuthPubKey: Point2D;
314-
let finalPubKey: Point2D;
313+
let oAuthPubKey: AffinePoint;
314+
let finalPubKey: AffinePoint;
315315
if (extendedVerifierId) {
316316
// for tss key no need to add pub nonce
317317
finalPubKey = { x: toBigIntBE(X), y: toBigIntBE(Y) };
@@ -388,11 +388,11 @@ class Torus {
388388
const { pub_key_X: X, pub_key_Y: Y } = finalKeyResult.keys[0];
389389
let nonceResult: GetOrSetNonceResult;
390390
let nonce: bigint;
391-
let finalPubKey: Point2D;
391+
let finalPubKey: AffinePoint;
392392
let typeOfUser: GetOrSetNonceResult["typeOfUser"];
393393
let pubNonce: { X: string; Y: string } | undefined;
394394

395-
const oAuthPubKey: Point2D = { x: toBigIntBE(X), y: toBigIntBE(Y) };
395+
const oAuthPubKey: AffinePoint = { x: toBigIntBE(X), y: toBigIntBE(Y) };
396396

397397
const finalServerTimeOffset = this.serverTimeOffset || serverTimeOffset;
398398
if (enableOneKey) {

0 commit comments

Comments
 (0)