Skip to content

Commit b114b3d

Browse files
fix comments
1 parent d23c622 commit b114b3d

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

src/Point.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { ed25519 } from "@noble/curves/ed25519.js";
2-
import { secp256k1 } from "@noble/curves/secp256k1.js";
32
import { concatBytes, hexToBytes, numberToBytesBE } from "@noble/curves/utils.js";
43

5-
import { toBigIntBE } from "./helpers/common";
4+
import { getSecp256k1, toBigIntBE } from "./helpers/common";
65
import { BigIntString, KeyType } from "./interfaces";
76

87
class Point {
@@ -24,7 +23,7 @@ class Point {
2423
return concatBytes(hexToBytes("04"), numberToBytesBE(this.x, 32), numberToBytesBE(this.y, 32));
2524
case "elliptic-compressed": {
2625
if (this.keyType === "secp256k1") {
27-
const point = secp256k1.Point.fromAffine({ x: this.x, y: this.y });
26+
const point = getSecp256k1().Point.fromAffine({ x: this.x, y: this.y });
2827
return point.toBytes();
2928
}
3029
// ed25519: standard compressed encoding (y in LE + x sign bit)

src/helpers/common.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ export const getEd25519 = () => ed25519;
8282

8383
export const getKeyCurve = (keyType: KeyType): Curve => {
8484
if (keyType === KEY_TYPE.SECP256K1) {
85-
return secp256k1;
85+
return getSecp256k1();
8686
} else if (keyType === KEY_TYPE.ED25519) {
87-
return ed25519;
87+
return getEd25519();
8888
}
8989
throw new Error(`Invalid keyType: ${keyType}`);
9090
};

src/helpers/metadataUtils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { mod } from "@noble/curves/abstract/modular.js";
2-
import { secp256k1 } from "@noble/curves/secp256k1.js";
32
import { KEY_TYPE, TORUS_NETWORK_TYPE, TORUS_SAPPHIRE_NETWORK } from "@toruslabs/constants";
43
import { decrypt } from "@toruslabs/eccrypto";
54
import { Data, post } from "@toruslabs/http-helpers";
@@ -28,6 +27,7 @@ import {
2827
Curve,
2928
derivePubKey,
3029
encParamsHexToBuf,
30+
getSecp256k1,
3131
hexToBytes,
3232
keccak256Bytes,
3333
numberToBytesBE,
@@ -42,6 +42,7 @@ export const getSecpKeyFromEd25519 = (
4242
scalar: bigint;
4343
point: Point2D;
4444
} => {
45+
const secp256k1 = getSecp256k1();
4546
const N = secp256k1.Point.CURVE().n;
4647

4748
const keyHash = keccakHash(numberToBytesBE(ed25519Scalar, 32));
@@ -93,6 +94,7 @@ export function generateMetadataParams(ecCurve: Curve, serverTimeOffset: number,
9394
data: message,
9495
timestamp: (~~(serverTimeOffset + Date.now() / 1000)).toString(16),
9596
};
97+
const secp256k1 = getSecp256k1();
9698
const msgHash = keccak256Bytes(utf8ToBytes(stringify(setData)));
9799
// metadata only uses secp for sig validation; prehash: false because msgHash is already hashed
98100
const sig = secp256k1.sign(msgHash, numberToBytesBE(privateKey, 32), { prehash: false });
@@ -146,6 +148,7 @@ export function generateNonceMetadataParams(
146148
setData.seed = ""; // setting it as empty to keep ordering same while serializing the data on backend.
147149
}
148150

151+
const secp256k1 = getSecp256k1();
149152
const msgHash = keccak256Bytes(utf8ToBytes(stringify(setData)));
150153
const sig = secp256k1.sign(msgHash, numberToBytesBE(privateKey, 32), { prehash: false });
151154
const pubKey = derivePubKey(secp256k1, privateKey);
@@ -257,6 +260,7 @@ export async function getOrSetSapphireMetadataNonce(
257260
operation: "getOrSetNonce",
258261
timestamp: (~~(serverTimeOffset + Date.now() / 1000)).toString(16),
259262
};
263+
const secp256k1 = getSecp256k1();
260264
const msgHash = keccak256Bytes(utf8ToBytes(stringify(setData)));
261265
const sig = secp256k1.sign(msgHash, numberToBytesBE(privKey, 32), { prehash: false });
262266
const pubKey = derivePubKey(secp256k1, privKey);

0 commit comments

Comments
 (0)