From 2c0d78231427f06a97a5905e5827c5e36c08ed54 Mon Sep 17 00:00:00 2001 From: himanshu Date: Tue, 23 Jan 2024 10:36:25 +0530 Subject: [PATCH] fix nonce threshold check for new keys --- src/helpers/nodeUtils.ts | 30 +++++++++++++++++++----------- test/sapphire_mainnet.test.ts | 2 +- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/helpers/nodeUtils.ts b/src/helpers/nodeUtils.ts index 330257b..3bc410b 100644 --- a/src/helpers/nodeUtils.ts +++ b/src/helpers/nodeUtils.ts @@ -64,6 +64,7 @@ export const GetPubKeyOrKeyAssign = async (params: { } return false; }); + const errorResult = thresholdSame( lookupPubKeys.map((x2) => x2 && x2.error), ~~(endpoints.length / 2) + 1 @@ -74,7 +75,24 @@ export const GetPubKeyOrKeyAssign = async (params: { ~~(endpoints.length / 2) + 1 ); - if (keyResult || errorResult) { + // check for nonce result in response if not a extendedVerifierId and not a legacy network + if (keyResult && !nonceResult && !extendedVerifierId && !LEGACY_NETWORKS_ROUTE_MAP[network as TORUS_LEGACY_NETWORK_TYPE]) { + for (let i = 0; i < lookupResults.length; i++) { + const x1 = lookupResults[i]; + if (x1 && !x1.error) { + const currentNodePubKey = x1.result.keys[0].pub_key_X.toLowerCase(); + const thresholdPubKey = keyResult.keys[0].pub_key_X.toLowerCase(); + const pubNonceX = (x1.result?.keys[0].nonce_data as v2NonceResultType)?.pubNonce?.x; + if (pubNonceX && currentNodePubKey === thresholdPubKey) { + nonceResult = x1.result.keys[0].nonce_data; + break; + } + } + } + } + + // nonceResult must exist except for extendedVerifierId and legacy networks along with keyResult + if ((keyResult && (nonceResult || extendedVerifierId || LEGACY_NETWORKS_ROUTE_MAP[network as TORUS_LEGACY_NETWORK_TYPE])) || errorResult) { if (keyResult) { lookupResults.forEach((x1) => { if (x1 && x1.result) { @@ -86,16 +104,6 @@ export const GetPubKeyOrKeyAssign = async (params: { const nodeIndex = parseInt(x1.result.node_index); if (nodeIndex) nodeIndexes.push(nodeIndex); } - // check for nonce result in response if not a extendedVerifierId and not a legacy network - if (!extendedVerifierId && !LEGACY_NETWORKS_ROUTE_MAP[network as TORUS_LEGACY_NETWORK_TYPE] && !nonceResult) { - // currently only one node returns metadata nonce - // other nodes returns empty object - // pubNonce must be available to derive the public key - const pubNonceX = (x1.result?.keys[0].nonce_data as v2NonceResultType)?.pubNonce?.x; - if (pubNonceX && currentNodePubKey === thresholdPubKey) { - nonceResult = x1.result.keys[0].nonce_data; - } - } } }); } diff --git a/test/sapphire_mainnet.test.ts b/test/sapphire_mainnet.test.ts index 6410d65..812d07b 100644 --- a/test/sapphire_mainnet.test.ts +++ b/test/sapphire_mainnet.test.ts @@ -16,7 +16,7 @@ const HashEnabledVerifier = "torus-test-verifierid-hash"; const TORUS_EXTENDED_VERIFIER_EMAIL = "testextenderverifierid@example.com"; const TORUS_IMPORT_EMAIL = "importeduser5@tor.us"; -describe("torus utils sapphire mainnet", function () { +describe.only("torus utils sapphire mainnet", function () { let torus: TorusUtils; let TORUS_NODE_MANAGER: NodeManager;