From 855aca0d96f07c138ea99f07bee914788dee0c4e Mon Sep 17 00:00:00 2001 From: himanshu Date: Wed, 20 Mar 2024 00:42:37 +0530 Subject: [PATCH] return pubkey in b58 --- src/helpers/metadataUtils.ts | 1 - src/helpers/nodeUtils.ts | 7 +++++-- src/torus.ts | 20 ++++++++++++++------ 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/helpers/metadataUtils.ts b/src/helpers/metadataUtils.ts index a9f24b0..5fc6ef8 100644 --- a/src/helpers/metadataUtils.ts +++ b/src/helpers/metadataUtils.ts @@ -131,7 +131,6 @@ export const decryptSeedData = async (seedBase64: string, finalUserKey: BN) => { const decryptionKey = getSecpKeyFromEd25519(finalUserKey); const seedUtf8 = Buffer.from(seedBase64, "base64").toString("utf-8"); const seedJson = JSON.parse(seedUtf8) as EncryptedSeed; - const bufferMetadata = { ephemPublicKey: Buffer.from(seedJson.metadata.ephemPublicKey, "hex"), iv: Buffer.from(seedJson.metadata.iv, "hex"), diff --git a/src/helpers/nodeUtils.ts b/src/helpers/nodeUtils.ts index c650bbd..2ba9f18 100644 --- a/src/helpers/nodeUtils.ts +++ b/src/helpers/nodeUtils.ts @@ -2,6 +2,7 @@ import { INodePub, LEGACY_NETWORKS_ROUTE_MAP, TORUS_LEGACY_NETWORK_TYPE, TORUS_N import { generatePrivate, getPublic } from "@toruslabs/eccrypto"; import { generateJsonRPCObject, get, post } from "@toruslabs/http-helpers"; import BN from "bn.js"; +import base58 from "bs58"; import { curve, ec } from "elliptic"; import { config } from "../config"; @@ -214,7 +215,8 @@ export async function retrieveOrImportShare(params: { } finalImportedShares = newImportedShares; } else if (!useDkg) { - const importedKey = new BN(generatePrivateKey(ecCurve, Buffer)); + const bufferKey = generatePrivateKey(ecCurve, Buffer); + const importedKey = new BN(bufferKey); const generatedShares = await generateShares(ecCurve, keyType, serverTimeOffset, indexes, nodePubkeys, importedKey); finalImportedShares = [...finalImportedShares, ...generatedShares]; } @@ -755,9 +757,10 @@ export async function retrieveOrImportShare(params: { if (keyWithNonce && !nonceResult.seed) { throw new Error("Invalid data, seed data is missing for ed25519 key, Please report this bug"); } else if (keyWithNonce && nonceResult.seed) { + // console.log("nonceResult.seed", nonceResult.seed, keyWithNonce); const decryptedSeed = await decryptSeedData(nonceResult.seed, new BN(keyWithNonce, "hex")); const totalLength = decryptedSeed.length + encodedPubKey.length; - finalPrivKey = Buffer.concat([decryptedSeed, encodedPubKey], totalLength).toString("hex"); + finalPrivKey = base58.encode(Buffer.concat([decryptedSeed, encodedPubKey], totalLength)); } } else { throw new Error(`Invalid keyType: ${keyType}`); diff --git a/src/torus.ts b/src/torus.ts index 1175a88..4cfd587 100644 --- a/src/torus.ts +++ b/src/torus.ts @@ -6,10 +6,12 @@ import { SIGNER_MAP, TORUS_LEGACY_NETWORK_TYPE, TORUS_NETWORK_TYPE, + TORUS_SAPPHIRE_NETWORK, } from "@toruslabs/constants"; import { decrypt, generatePrivate, getPublic } from "@toruslabs/eccrypto"; import { generateJsonRPCObject, get, post, setAPIKey, setEmbedHost } from "@toruslabs/http-helpers"; import BN from "bn.js"; +import base58 from "bs58"; import { curve, ec as EC } from "elliptic"; import { config } from "./config"; @@ -84,7 +86,7 @@ class Torus { }: TorusCtorOptions) { if (!clientId) throw new Error("Please provide a valid clientId in constructor"); if (!network) throw new Error("Please provide a valid network in constructor"); - if (keyType === "ed25519" && LEGACY_NETWORKS_ROUTE_MAP[network as TORUS_LEGACY_NETWORK_TYPE]) { + if (keyType === "ed25519" && network !== TORUS_SAPPHIRE_NETWORK.SAPPHIRE_DEVNET) { throw new Error(`keyType: ${keyType} is not supported by ${network} network`); } this.keyType = keyType; @@ -220,13 +222,19 @@ class Torus { throw new Error(`length of endpoints array must be same as length of nodeIndexes array`); } - const privKeyBuffer = Buffer.from(newPrivateKey.padStart(64, "0"), "hex"); + let privKeyBuffer; - if (this.keyType === "secp256k1" && privKeyBuffer.length !== 32) { - throw new Error("Invalid private key length for give secp256k1 key"); + if (this.keyType === "secp256k1") { + privKeyBuffer = Buffer.from(newPrivateKey.padStart(64, "0"), "hex"); + if (privKeyBuffer.length !== 32) { + throw new Error("Invalid private key length for given secp256k1 key"); + } } - if (this.keyType === "ed25519" && privKeyBuffer.length !== 64) { - throw new Error("Invalid private key length for give secp256k1 key"); + if (this.keyType === "ed25519") { + privKeyBuffer = Buffer.from(base58.decode(newPrivateKey)); + if (privKeyBuffer.length !== 64) { + throw new Error("Invalid private key length for given ed25519 key"); + } } const finalPrivKey = this.keyType === "secp256k1" ? privKeyBuffer : privKeyBuffer.slice(0, 32);