Skip to content

Commit

Permalink
fix nonce key padding and devnet metadata url
Browse files Browse the repository at this point in the history
  • Loading branch information
himanshu committed Jul 3, 2024
1 parent 0984e18 commit 65762d7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
18 changes: 15 additions & 3 deletions src/helpers/metadataUtils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
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";
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";
Expand Down Expand Up @@ -92,7 +93,16 @@ export async function getNonce(
): Promise<GetOrSetNonceResult> {
return getOrSetNonce(legacyMetadataHost, ecCurve, serverTimeOffset, X, Y, privKey, true);
}
export async function getOrSetSapphireMetadataNonce(X: string, Y: string, serverTimeOffset?: number, privKey?: BN): Promise<GetOrSetNonceResult> {
export async function getOrSetSapphireMetadataNonce(
network: TORUS_NETWORK_TYPE,
X: string,
Y: string,
serverTimeOffset?: number,
privKey?: BN
): Promise<GetOrSetNonceResult> {
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,
Expand All @@ -114,5 +124,7 @@ export async function getOrSetSapphireMetadataNonce(X: string, Y: string, server
};
}

return post<GetOrSetNonceResult>(`${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<GetOrSetNonceResult>(`${metadataUrl}/get_or_set_nonce`, data, undefined, { useAPIKey: true });
}
20 changes: 16 additions & 4 deletions src/helpers/nodeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -338,6 +338,10 @@ export async function retrieveOrImportShare(params: {
void | JRPCResponse<ShareRequestResult>,
| {
privateKey: BN;
thresholdPublicKey: {
X: string;
Y: string;
};
sessionTokenData: SessionToken[];
thresholdNonceData: GetOrSetNonceResult;
nodeIndexes: BN[];
Expand Down Expand Up @@ -534,6 +538,7 @@ export async function retrieveOrImportShare(params: {

return {
privateKey,
thresholdPublicKey,
sessionTokenData,
thresholdNonceData,
nodeIndexes,
Expand All @@ -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");

Expand All @@ -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;
Expand Down

0 comments on commit 65762d7

Please sign in to comment.