Skip to content

Commit

Permalink
refactoring and test case fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
himanshu committed Mar 5, 2024
1 parent 4e8d654 commit 2407129
Show file tree
Hide file tree
Showing 14 changed files with 372 additions and 72 deletions.
140 changes: 140 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@types/json-stable-stringify": "^1.0.36",
"@types/jsonwebtoken": "^9.0.5",
"@types/mocha": "^10.0.6",
"@types/sinon": "^17.0.3",
"chai": "^4.3.10",
"cross-env": "^7.0.3",
"dotenv": "^16.3.1",
Expand All @@ -56,6 +57,7 @@
"mocha": "^10.2.0",
"prettier": "^3.1.0",
"rimraf": "^5.0.5",
"sinon": "^17.0.1",
"ts-node": "^10.9.1",
"typescript": "^5.3.2"
},
Expand Down
25 changes: 22 additions & 3 deletions src/helpers/common.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,36 @@
import { Ecies } from "@toruslabs/eccrypto";
import JsonStringify from "json-stable-stringify";

import { EciesHex, VerifierLookupResponse } from "../interfaces";
import { EciesHex, LegacyVerifierLookupResponse, VerifierLookupResponse } from "../interfaces";

// this function normalizes the result from nodes before passing the result to threshold check function
// For ex: some fields returns by nodes might be different from each other
// like created_at field might vary and nonce_data might not be returned by all nodes because
// of the metadata implementation in sapphire.
export const normalizeKeysResult = (result: VerifierLookupResponse) => {
const finalResult: Pick<VerifierLookupResponse, "keys" | "is_new_key" | "server_time_offset"> = {
const finalResult: Pick<VerifierLookupResponse, "keys" | "is_new_key"> = {
keys: [],
is_new_key: result.is_new_key,
server_time_offset: result.server_time_offset,
};
if (result && result.keys && result.keys.length > 0) {
const finalKey = result.keys[0];
finalResult.keys = [
{
pub_key_X: finalKey.pub_key_X,
pub_key_Y: finalKey.pub_key_Y,
address: finalKey.address,
},
];
}
return finalResult;
};

// this function normalizes the result from nodes before passing the result to threshold check function
// For ex: some fields returns by nodes might be different from each other
// like key_index which may differ on sapphire_network for each queried node
export const normalizeLegacyKeysResult = (result: LegacyVerifierLookupResponse) => {
const finalResult: Pick<LegacyVerifierLookupResponse, "keys"> = {
keys: [],
};
if (result && result.keys && result.keys.length > 0) {
const finalKey = result.keys[0];
Expand Down
28 changes: 24 additions & 4 deletions src/helpers/nodeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
} from "../interfaces";
import log from "../loglevel";
import { Some } from "../some";
import { kCombinations, normalizeKeysResult, thresholdSame } from "./common";
import { kCombinations, normalizeKeysResult, normalizeLegacyKeysResult, thresholdSame } from "./common";
import { generateAddressFromPrivKey, generateAddressFromPubKey, keccak256 } from "./keyUtils";
import { lagrangeInterpolation } from "./langrangeInterpolatePoly";
import { decryptNodeData, getMetadata, getOrSetNonce } from "./metadataUtils";
Expand Down Expand Up @@ -93,6 +93,7 @@ export const GetPubKeyOrKeyAssign = async (params: {
}
}

const serverTimeOffsets: number[] = [];
// 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) {
Expand All @@ -106,10 +107,14 @@ export const GetPubKeyOrKeyAssign = async (params: {
const nodeIndex = parseInt(x1.result.node_index);
if (nodeIndex) nodeIndexes.push(nodeIndex);
}
const serverTimeOffset = x1.result.server_time_offset ? parseInt(x1.result.server_time_offset, 10) : 0;
serverTimeOffsets.push(serverTimeOffset);
}
});
}
return Promise.resolve({ keyResult, nodeIndexes, errorResult, nonceResult });

const serverTimeOffset = Math.max(...serverTimeOffsets);
return Promise.resolve({ keyResult, serverTimeOffset, nodeIndexes, errorResult, nonceResult });
}
return Promise.reject(
new Error(
Expand Down Expand Up @@ -643,6 +648,7 @@ export async function retrieveOrImportShare(params: {
nonce: metadataNonce,
typeOfUser,
upgraded: isUpgraded,
serverTimeOffset: serverTimeOffsetResponse,
},
nodesData: {
nodeIndexes: nodeIndexes.map((x) => x.toNumber()),
Expand All @@ -669,11 +675,25 @@ export const legacyKeyLookup = async (endpoints: string[], verifier: string, ver
~~(endpoints.length / 2) + 1
);
const keyResult = thresholdSame(
lookupShares.map((x3) => x3 && x3.result),
lookupShares.map((x3) => x3 && normalizeLegacyKeysResult(x3.result)),
~~(endpoints.length / 2) + 1
);

const serverTimeOffsets: number[] = [];
// nonceResult must exist except for extendedVerifierId and legacy networks along with keyResult
if (keyResult) {
lookupResults.forEach((x1) => {
if (x1 && x1.result) {
const timeOffSet = x1.result.server_time_offset;
const serverTimeOffset = timeOffSet ? parseInt(timeOffSet, 10) : 0;
serverTimeOffsets.push(serverTimeOffset);
}
});
}

const serverTimeOffset = Math.max(...serverTimeOffsets);
if (keyResult || errorResult) {
return Promise.resolve({ keyResult, errorResult });
return Promise.resolve({ keyResult, errorResult, serverTimeOffset });
}
return Promise.reject(new Error(`invalid results ${JSON.stringify(lookupResults)}`));
});
Expand Down
6 changes: 5 additions & 1 deletion src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,13 @@ export interface JRPCResponse<T> {
export interface LegacyKeyLookupResult {
keyResult: Pick<LegacyVerifierLookupResponse, "keys" | "server_time_offset">;
errorResult: JRPCResponse<LegacyVerifierLookupResponse>["error"];
serverTimeOffset: number;
}

export interface KeyLookupResult {
keyResult: Pick<VerifierLookupResponse, "keys" | "is_new_key" | "server_time_offset">;
keyResult: Pick<VerifierLookupResponse, "keys" | "is_new_key">;
nodeIndexes: number[];
serverTimeOffset: number;
errorResult: JRPCResponse<VerifierLookupResponse>["error"];
nonceResult?: GetOrSetNonceResult;
}
Expand Down Expand Up @@ -136,6 +138,7 @@ export interface KeyAssignment {

export interface LegacyShareRequestResult {
keys: LegacyKeyAssignment[];
server_time_offset?: string;
}

export interface ShareRequestResult {
Expand Down Expand Up @@ -186,6 +189,7 @@ export interface TorusPublicKey {
nonce?: BN;
typeOfUser: UserType;
upgraded: boolean | null;
serverTimeOffset: number;
};
nodesData: {
nodeIndexes: number[];
Expand Down
Loading

0 comments on commit 2407129

Please sign in to comment.