Skip to content

Commit

Permalink
return pubkey in b58
Browse files Browse the repository at this point in the history
  • Loading branch information
himanshu committed Mar 19, 2024
1 parent 6950486 commit 855aca0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
1 change: 0 additions & 1 deletion src/helpers/metadataUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
7 changes: 5 additions & 2 deletions src/helpers/nodeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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];
}
Expand Down Expand Up @@ -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}`);
Expand Down
20 changes: 14 additions & 6 deletions src/torus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 855aca0

Please sign in to comment.