Skip to content

Commit

Permalink
fix nonce threshold check for new keys
Browse files Browse the repository at this point in the history
  • Loading branch information
himanshu committed Jan 23, 2024
1 parent 360cbe8 commit 2c0d782
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
30 changes: 19 additions & 11 deletions src/helpers/nodeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export const GetPubKeyOrKeyAssign = async (params: {
}
return false;
});

const errorResult = thresholdSame(
lookupPubKeys.map((x2) => x2 && x2.error),
~~(endpoints.length / 2) + 1
Expand All @@ -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) {
Expand All @@ -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;
}
}
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion test/sapphire_mainnet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const HashEnabledVerifier = "torus-test-verifierid-hash";
const TORUS_EXTENDED_VERIFIER_EMAIL = "[email protected]";
const TORUS_IMPORT_EMAIL = "[email protected]";

describe("torus utils sapphire mainnet", function () {
describe.only("torus utils sapphire mainnet", function () {
let torus: TorusUtils;
let TORUS_NODE_MANAGER: NodeManager;

Expand Down

0 comments on commit 2c0d782

Please sign in to comment.