From 65762d72a22204374a121287d4403edbbe914173 Mon Sep 17 00:00:00 2001 From: himanshu Date: Wed, 3 Jul 2024 11:36:42 +0530 Subject: [PATCH] fix nonce key padding and devnet metadata url --- src/constants.ts | 1 + src/helpers/metadataUtils.ts | 18 +++++++++++++++--- src/helpers/nodeUtils.ts | 20 ++++++++++++++++---- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/constants.ts b/src/constants.ts index a570fe5..51da688 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -6,3 +6,4 @@ export const JRPC_METHODS = { }; export const SAPPHIRE_METADATA_URL = "https://node-1.node.web3auth.io/metadata"; +export const SAPPHIRE_DEVNET_METADATA_URL = "https://node-1.dev-node.web3auth.io/metadata"; diff --git a/src/helpers/metadataUtils.ts b/src/helpers/metadataUtils.ts index 0f5da9b..3ffb9e6 100644 --- a/src/helpers/metadataUtils.ts +++ b/src/helpers/metadataUtils.ts @@ -1,3 +1,4 @@ +import { LEGACY_NETWORKS_ROUTE_MAP, TORUS_LEGACY_NETWORK_TYPE, TORUS_NETWORK_TYPE, TORUS_SAPPHIRE_NETWORK } from "@toruslabs/constants"; import { decrypt } from "@toruslabs/eccrypto"; import { Data, post } from "@toruslabs/http-helpers"; import BN from "bn.js"; @@ -5,7 +6,7 @@ import { ec as EC } from "elliptic"; import stringify from "json-stable-stringify"; import log from "loglevel"; -import { SAPPHIRE_METADATA_URL } from "../constants"; +import { SAPPHIRE_DEVNET_METADATA_URL, SAPPHIRE_METADATA_URL } from "../constants"; import { EciesHex, GetOrSetNonceResult, MetadataParams, SapphireMetadataParams } from "../interfaces"; import { encParamsHexToBuf } from "./common"; import { keccak256 } from "./keyUtils"; @@ -92,7 +93,16 @@ export async function getNonce( ): Promise { return getOrSetNonce(legacyMetadataHost, ecCurve, serverTimeOffset, X, Y, privKey, true); } -export async function getOrSetSapphireMetadataNonce(X: string, Y: string, serverTimeOffset?: number, privKey?: BN): Promise { +export async function getOrSetSapphireMetadataNonce( + network: TORUS_NETWORK_TYPE, + X: string, + Y: string, + serverTimeOffset?: number, + privKey?: BN +): Promise { + if (LEGACY_NETWORKS_ROUTE_MAP[network as TORUS_LEGACY_NETWORK_TYPE]) { + throw new Error("getOrSetSapphireMetadataNonce should only be used for sapphire networks"); + } let data: SapphireMetadataParams = { pub_key_X: X, pub_key_Y: Y, @@ -114,5 +124,7 @@ export async function getOrSetSapphireMetadataNonce(X: string, Y: string, server }; } - return post(`${SAPPHIRE_METADATA_URL}/get_or_set_nonce`, data, undefined, { useAPIKey: true }); + const metadataUrl = network === TORUS_SAPPHIRE_NETWORK.SAPPHIRE_DEVNET ? SAPPHIRE_DEVNET_METADATA_URL : SAPPHIRE_METADATA_URL; + + return post(`${metadataUrl}/get_or_set_nonce`, data, undefined, { useAPIKey: true }); } diff --git a/src/helpers/nodeUtils.ts b/src/helpers/nodeUtils.ts index 7837a46..377c573 100644 --- a/src/helpers/nodeUtils.ts +++ b/src/helpers/nodeUtils.ts @@ -91,7 +91,7 @@ export const GetPubKeyOrKeyAssign = async (params: { // if nonce result is not returned by nodes, fetch directly from metadata if (!nonceResult) { - const metadataNonceResult = await getOrSetSapphireMetadataNonce(keyResult.keys[0].pub_key_X, keyResult.keys[0].pub_key_Y); + const metadataNonceResult = await getOrSetSapphireMetadataNonce(network, keyResult.keys[0].pub_key_X, keyResult.keys[0].pub_key_Y); // rechecking nonceResult to avoid promise race condition. if (!nonceResult && metadataNonceResult) { nonceResult = metadataNonceResult; @@ -143,7 +143,7 @@ export async function retrieveOrImportShare(params: { enableOneKey: boolean; ecCurve: ec; allowHost: string; - network: string; + network: TORUS_NETWORK_TYPE; clientId: string; endpoints: string[]; verifier: string; @@ -338,6 +338,10 @@ export async function retrieveOrImportShare(params: { void | JRPCResponse, | { privateKey: BN; + thresholdPublicKey: { + X: string; + Y: string; + }; sessionTokenData: SessionToken[]; thresholdNonceData: GetOrSetNonceResult; nodeIndexes: BN[]; @@ -534,6 +538,7 @@ export async function retrieveOrImportShare(params: { return { privateKey, + thresholdPublicKey, sessionTokenData, thresholdNonceData, nodeIndexes, @@ -551,7 +556,7 @@ export async function retrieveOrImportShare(params: { }); }) .then(async (res) => { - const { privateKey, sessionTokenData, nodeIndexes, thresholdNonceData, isNewKey, serverTimeOffsetResponse } = res; + const { privateKey, thresholdPublicKey, sessionTokenData, nodeIndexes, thresholdNonceData, isNewKey, serverTimeOffsetResponse } = res; let nonceResult = thresholdNonceData; if (!privateKey) throw new Error("Invalid private key returned"); @@ -563,7 +568,14 @@ export async function retrieveOrImportShare(params: { // if both thresholdNonceData and extended_verifier_id are not available // then we need to throw other wise address would be incorrect. if (!nonceResult && !verifierParams.extended_verifier_id && !LEGACY_NETWORKS_ROUTE_MAP[network as TORUS_LEGACY_NETWORK_TYPE]) { - const metadataNonceResult = await getOrSetSapphireMetadataNonce(oAuthPubkeyX, oAuthPubkeyY, serverTimeOffset, oAuthKey); + // NOTE: dont use padded pub key anywhere in metadata apis, send pub keys as is received from nodes. + const metadataNonceResult = await getOrSetSapphireMetadataNonce( + network, + thresholdPublicKey.X, + thresholdPublicKey.Y, + serverTimeOffset, + oAuthKey + ); // rechecking nonceResult to avoid promise race condition. if (metadataNonceResult && !thresholdNonceData) { nonceResult = metadataNonceResult;