From 0b9aff186b11297ea8dfb5df295a70082d1b5be6 Mon Sep 17 00:00:00 2001 From: himanshu Date: Thu, 21 Dec 2023 15:58:18 +0800 Subject: [PATCH] fix review comments --- src/Point.ts | 6 +++++- src/Share.ts | 4 ++-- src/helpers/common.ts | 3 ++- src/torus.ts | 10 +++++----- test/sapphire_devnet.test.ts | 3 +-- test/sapphire_devnet_ed25519.test.ts | 1 - 6 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/Point.ts b/src/Point.ts index 8f8d91b..1c7f51f 100644 --- a/src/Point.ts +++ b/src/Point.ts @@ -19,7 +19,11 @@ class Point { encode(enc: string): Buffer { switch (enc) { case "arr": - return Buffer.concat([Buffer.from("04", "hex"), Buffer.from(this.x.toString("hex"), "hex"), Buffer.from(this.y.toString("hex"), "hex")]); + return Buffer.concat([ + Buffer.from("04", "hex"), + Buffer.from(this.x.toString("hex", 64), "hex"), + Buffer.from(this.y.toString("hex", 64), "hex"), + ]); case "elliptic-compressed": { const key = this.ecCurve.keyFromPublic({ x: this.x.toString("hex", 64), y: this.y.toString("hex", 64) }, "hex"); return Buffer.from(key.getPublic(true, "hex")); diff --git a/src/Share.ts b/src/Share.ts index 86df11f..a048949 100644 --- a/src/Share.ts +++ b/src/Share.ts @@ -19,8 +19,8 @@ class Share { toJSON(): StringifiedType { return { - share: this.share.toString("hex"), - shareIndex: this.shareIndex.toString("hex"), + share: this.share.toString("hex", 64), + shareIndex: this.shareIndex.toString("hex", 64), }; } } diff --git a/src/helpers/common.ts b/src/helpers/common.ts index 1674dd5..8571538 100644 --- a/src/helpers/common.ts +++ b/src/helpers/common.ts @@ -1,4 +1,5 @@ import { Ecies } from "@toruslabs/eccrypto"; +import { BN } from "bn.js"; import JsonStringify from "json-stable-stringify"; import { EciesHex, VerifierLookupResponse } from "../interfaces"; @@ -89,6 +90,6 @@ export function encParamsHexToBuf(eciesData: Omit): Omit export function getProxyCoordinatorEndpointIndex(endpoints: string[], verifier: string, verifierId: string) { const verifierIdStr = `${verifier}${verifierId}`; const hashedVerifierId = keccak256(Buffer.from(verifierIdStr, "utf8")); - const proxyEndpointNum = parseInt(hashedVerifierId, 16) % endpoints.length; + const proxyEndpointNum = new BN(hashedVerifierId, "hex").mod(new BN(endpoints.length)).toNumber(); return proxyEndpointNum; } diff --git a/src/torus.ts b/src/torus.ts index 59ba7c6..f3a8d84 100644 --- a/src/torus.ts +++ b/src/torus.ts @@ -256,7 +256,7 @@ class Torus { // generate temporary private and public key that is used to secure receive shares const tmpKey = generatePrivate(); - const pubKey = getPublic(tmpKey).toString("hex"); + const pubKey = getPublic(tmpKey).toString("hex", 64); const pubKeyX = pubKey.slice(2, 66); const pubKeyY = pubKey.slice(66); const tokenCommitment = keccak256(Buffer.from(idToken, "utf8")); @@ -431,8 +431,8 @@ class Torus { if (!oAuthKey) throw new Error("Invalid private key returned"); const oAuthPubKey = derivePubKey(this.ec, oAuthKey); - const oAuthKeyX = oAuthPubKey.getX().toString("hex"); - const oAuthKeyY = oAuthPubKey.getY().toString("hex"); + const oAuthKeyX = oAuthPubKey.getX().toString("hex", 64); + const oAuthKeyY = oAuthPubKey.getY().toString("hex", 64); let metadataNonce: BN; let finalPubKey: curve.base.BasePoint; @@ -456,13 +456,13 @@ class Torus { // for imported keys in legacy networks metadataNonce = await getMetadata(this.legacyMetadataHost, { pub_key_X: oAuthKeyX, pub_key_Y: oAuthKeyY }); const privateKeyWithNonce = oAuthKey.add(metadataNonce).umod(this.ec.curve.n); - finalPubKey = this.ec.keyFromPrivate(privateKeyWithNonce.toString("hex"), "hex").getPublic(); + finalPubKey = this.ec.keyFromPrivate(privateKeyWithNonce.toString("hex", 64), "hex").getPublic(); } } else { // for imported keys in legacy networks metadataNonce = await getMetadata(this.legacyMetadataHost, { pub_key_X: oAuthKeyX, pub_key_Y: oAuthKeyY }); const privateKeyWithNonce = oAuthKey.add(metadataNonce).umod(this.ec.curve.n); - finalPubKey = this.ec.keyFromPrivate(privateKeyWithNonce.toString("hex"), "hex").getPublic(); + finalPubKey = this.ec.keyFromPrivate(privateKeyWithNonce.toString("hex", 64), "hex").getPublic(); } const oAuthKeyAddress = generateAddressFromPrivKey(this.ec, oAuthKey); diff --git a/test/sapphire_devnet.test.ts b/test/sapphire_devnet.test.ts index e663875..53fd9f1 100644 --- a/test/sapphire_devnet.test.ts +++ b/test/sapphire_devnet.test.ts @@ -21,7 +21,7 @@ const TORUS_TEST_VERIFIER = "torus-test-health"; const TORUS_TEST_AGGREGATE_VERIFIER = "torus-test-health-aggregate"; const HashEnabledVerifier = "torus-test-verifierid-hash"; -describe.only("torus utils sapphire devnet", function () { +describe("torus utils sapphire devnet", function () { let torus: TorusUtils; let TORUS_NODE_MANAGER: NodeManager; @@ -226,7 +226,6 @@ describe.only("torus utils sapphire devnet", function () { nodesData: result.nodesData, }); }); - // we are working on a new implementation for import sss keys, so skipping it for now. it("should fetch public address of imported user", async function () { const verifierDetails = { verifier: TORUS_TEST_VERIFIER, verifierId: TORUS_IMPORT_EMAIL }; const nodeDetails = await TORUS_NODE_MANAGER.getNodeDetails(verifierDetails); diff --git a/test/sapphire_devnet_ed25519.test.ts b/test/sapphire_devnet_ed25519.test.ts index 902b80a..a2188ec 100644 --- a/test/sapphire_devnet_ed25519.test.ts +++ b/test/sapphire_devnet_ed25519.test.ts @@ -97,7 +97,6 @@ describe.skip("torus utils ed25519 sapphire devnet", function () { nodesData: result.nodesData, }); }); - // we are working on a new implementation for import sss keys, so skipping it for now. it("should fetch public address of imported user", async function () { const verifierDetails = { verifier: TORUS_TEST_VERIFIER, verifierId: TORUS_IMPORT_EMAIL }; const nodeDetails = await TORUS_NODE_MANAGER.getNodeDetails(verifierDetails);