@toruslabs/torus.js
Advanced tools
Comparing version
@@ -8,5 +8,5 @@ import stringify from 'json-stable-stringify'; | ||
import { post, generateJsonRPCObject, get, setAPIKey, setEmbedHost } from '@toruslabs/http-helpers'; | ||
import { ec } from 'elliptic'; | ||
import log$1 from 'loglevel'; | ||
import { LEGACY_NETWORKS_ROUTE_MAP, SIGNER_MAP, METADATA_MAP } from '@toruslabs/constants'; | ||
import { ec } from 'elliptic'; | ||
@@ -381,2 +381,3 @@ const JRPC_METHODS = { | ||
const secp256k1Curve = new ec("secp256k1"); | ||
function convertMetadataToNonce(params) { | ||
@@ -448,4 +449,4 @@ if (!params || !params.message) { | ||
} | ||
async function getOrSetSapphireMetadataNonce(X, Y) { | ||
const data = { | ||
async function getOrSetSapphireMetadataNonce(X, Y, serverTimeOffset, privKey) { | ||
let data = { | ||
pub_key_X: X, | ||
@@ -458,2 +459,14 @@ pub_key_Y: Y, | ||
}; | ||
if (privKey) { | ||
const key = secp256k1Curve.keyFromPrivate(privKey.toString("hex", 64)); | ||
const setData = { | ||
operation: "getOrSetNonce", | ||
timestamp: new BN(~~(serverTimeOffset + Date.now() / 1000)).toString(16) | ||
}; | ||
const sig = key.sign(keccak256(Buffer.from(stringify(setData), "utf8")).slice(2)); | ||
data = _objectSpread(_objectSpread({}, data), {}, { | ||
set_data: setData, | ||
signature: Buffer.from(sig.r.toString(16, 64) + sig.s.toString(16, 64) + new BN("").toString(16, 2), "hex").toString("base64") | ||
}); | ||
} | ||
return post(`${SAPPHIRE_METADATA_URL}/get_or_set_nonce`, data, undefined, { | ||
@@ -619,2 +632,5 @@ useAPIKey: true | ||
nonceResult = metadataNonceResult; | ||
if (nonceResult.nonce) { | ||
delete nonceResult.nonce; | ||
} | ||
} | ||
@@ -845,14 +861,2 @@ } | ||
}); | ||
// if both thresholdNonceData and extended_verifier_id are not available | ||
// then we need to throw other wise address would be incorrect. | ||
if (!thresholdNonceData && !verifierParams.extended_verifier_id && !LEGACY_NETWORKS_ROUTE_MAP[network]) { | ||
const metadataNonceResult = await getOrSetSapphireMetadataNonce(thresholdPublicKey.X, thresholdPublicKey.Y); | ||
// rechecking nonceResult to avoid promise race condition. | ||
if (metadataNonceResult && !thresholdNonceData) { | ||
thresholdNonceData = metadataNonceResult; | ||
} else { | ||
throw new Error(`invalid metadata result from nodes, nonce metadata is empty for verifier: ${verifier} and verifierId: ${verifierParams.verifier_id}`); | ||
} | ||
} | ||
const thresholdReqCount = importedShares.length > 0 ? endpoints.length : minThreshold; | ||
@@ -862,3 +866,3 @@ // optimistically run lagrange interpolation once threshold number of shares have been received | ||
// Note: no need of thresholdMetadataNonce for extended_verifier_id key | ||
if (completedRequests.length >= thresholdReqCount && thresholdPublicKey && (thresholdNonceData || verifierParams.extended_verifier_id || LEGACY_NETWORKS_ROUTE_MAP[network])) { | ||
if (completedRequests.length >= thresholdReqCount && thresholdPublicKey) { | ||
const sharePromises = []; | ||
@@ -999,4 +1003,4 @@ const sessionTokenSigPromises = []; | ||
sessionTokenData, | ||
nodeIndexes, | ||
thresholdNonceData, | ||
nodeIndexes, | ||
isNewKey, | ||
@@ -1011,2 +1015,14 @@ serverTimeOffsetResponse | ||
const oAuthPubkeyY = oAuthPubKey.slice(66); | ||
// 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]) { | ||
const metadataNonceResult = await getOrSetSapphireMetadataNonce(oAuthPubkeyX, oAuthPubkeyY, serverTimeOffset, oAuthKey); | ||
// rechecking nonceResult to avoid promise race condition. | ||
if (metadataNonceResult && !thresholdNonceData) { | ||
nonceResult = metadataNonceResult; | ||
} else { | ||
throw new Error(`invalid metadata result from nodes, nonce metadata is empty for verifier: ${verifier} and verifierId: ${verifierParams.verifier_id}`); | ||
} | ||
} | ||
let metadataNonce = new BN((_nonceResult = nonceResult) !== null && _nonceResult !== void 0 && _nonceResult.nonce ? nonceResult.nonce.padStart(64, "0") : "0", "hex"); | ||
@@ -1013,0 +1029,0 @@ let finalPubKey; |
/// <reference types="node" /> | ||
import BN from "bn.js"; | ||
import { ec } from "elliptic"; | ||
import { ec as EC } from "elliptic"; | ||
import { EciesHex, GetOrSetNonceResult, MetadataParams } from "../interfaces"; | ||
@@ -9,6 +9,6 @@ export declare function convertMetadataToNonce(params: { | ||
export declare function decryptNodeData(eciesData: EciesHex, ciphertextHex: string, privKey: Buffer): Promise<Buffer>; | ||
export declare function generateMetadataParams(ecCurve: ec, serverTimeOffset: number, message: string, privateKey: BN): MetadataParams; | ||
export declare function generateMetadataParams(ecCurve: EC, serverTimeOffset: number, message: string, privateKey: BN): MetadataParams; | ||
export declare function getMetadata(legacyMetadataHost: string, data: Omit<MetadataParams, "set_data" | "signature">, options?: RequestInit): Promise<BN>; | ||
export declare function getOrSetNonce(legacyMetadataHost: string, ecCurve: ec, serverTimeOffset: number, X: string, Y: string, privKey?: BN, getOnly?: boolean): Promise<GetOrSetNonceResult>; | ||
export declare function getNonce(legacyMetadataHost: string, ecCurve: ec, serverTimeOffset: number, X: string, Y: string, privKey?: BN): Promise<GetOrSetNonceResult>; | ||
export declare function getOrSetSapphireMetadataNonce(X: string, Y: string): Promise<GetOrSetNonceResult>; | ||
export declare function getOrSetNonce(legacyMetadataHost: string, ecCurve: EC, serverTimeOffset: number, X: string, Y: string, privKey?: BN, getOnly?: boolean): Promise<GetOrSetNonceResult>; | ||
export declare function getNonce(legacyMetadataHost: string, ecCurve: EC, serverTimeOffset: number, X: string, Y: string, privKey?: BN): Promise<GetOrSetNonceResult>; | ||
export declare function getOrSetSapphireMetadataNonce(X: string, Y: string, serverTimeOffset?: number, privKey?: BN): Promise<GetOrSetNonceResult>; |
@@ -193,1 +193,12 @@ import type { TORUS_NETWORK_TYPE } from "@toruslabs/constants"; | ||
} | ||
export interface SapphireMetadataParams { | ||
namespace?: string; | ||
pub_key_X: string; | ||
pub_key_Y: string; | ||
key_type: "secp256k1" | "ed25519"; | ||
set_data: { | ||
operation: "getNonce" | "getOrSetNonce" | string; | ||
timestamp?: string; | ||
}; | ||
signature?: string; | ||
} |
{ | ||
"name": "@toruslabs/torus.js", | ||
"version": "12.3.4", | ||
"version": "12.3.5", | ||
"description": "Handle communication with torus nodes", | ||
@@ -5,0 +5,0 @@ "main": "dist/torusUtils.cjs.js", |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
382107
0.51%3570
1.22%