Skip to content

Commit

Permalink
Merge pull request #158 from torusresearch/fix/isNewKey-race-condition
Browse files Browse the repository at this point in the history
fix is new key race condition
  • Loading branch information
chaitanyapotti authored Aug 26, 2024
2 parents 55c0c4c + 7dd56fe commit bb07e60
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/helpers/nodeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,10 @@ export async function retrieveOrImportShare(params: {
const sessionTokenPromises: Promise<void | Buffer>[] = [];
const nodeIndexes: BN[] = [];
const sessionTokenData: SessionToken[] = [];
const isNewKeyResponses: string[] = [];
const isNewKeyResponses: {
isNewKey: string;
publicKey: string;
}[] = [];
const serverTimeOffsetResponses: string[] = [];

for (let i = 0; i < completedRequests.length; i += 1) {
Expand All @@ -522,7 +525,10 @@ export async function retrieveOrImportShare(params: {
server_time_offset: serverTimeOffsetResponse,
} = currentShareResponse.result;

isNewKeyResponses.push(isNewKey);
isNewKeyResponses.push({
isNewKey,
publicKey: currentShareResponse.result?.keys[0]?.public_key?.X || "",
});
serverTimeOffsetResponses.push(serverTimeOffsetResponse || "0");

if (sessionTokenSigs?.length > 0) {
Expand Down Expand Up @@ -644,7 +650,12 @@ export async function retrieveOrImportShare(params: {
throw new Error("could not derive private key");
}

const thresholdIsNewKey = thresholdSame(isNewKeyResponses, halfThreshold);
let isNewKey = false;
isNewKeyResponses.forEach((x) => {
if (x.isNewKey === "true" && x.publicKey.toLowerCase() === thresholdPublicKey.X.toLowerCase()) {
isNewKey = true;
}
});

// Convert each string timestamp to a number
const serverOffsetTimes = serverTimeOffsetResponses.map((timestamp) => Number.parseInt(timestamp, 10));
Expand All @@ -655,7 +666,7 @@ export async function retrieveOrImportShare(params: {
thresholdNonceData,
nodeIndexes,
thresholdPubKey: thresholdPublicKey,
isNewKey: thresholdIsNewKey === "true",
isNewKey,
serverTimeOffsetResponse: serverTimeOffset || calculateMedian(serverOffsetTimes),
};
}
Expand Down
6 changes: 6 additions & 0 deletions src/torus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ class Torus {
// for ed25519 keys import keys flows is the default
let shouldUseDkg;
if (typeof useDkg === "boolean") {
if (useDkg === false && LEGACY_NETWORKS_ROUTE_MAP[this.network as TORUS_LEGACY_NETWORK_TYPE]) {
throw new Error(`useDkg cannot be false for legacy network; ${this.network}`);
}
shouldUseDkg = useDkg;
} else if (this.keyType === KEY_TYPE.ED25519) {
shouldUseDkg = false;
Expand Down Expand Up @@ -176,6 +179,9 @@ class Torus {
async importPrivateKey(params: ImportKeyParams): Promise<TorusKey> {
const { nodeIndexes, newPrivateKey, verifier, verifierParams, idToken, nodePubkeys, endpoints, extraParams = {} } = params;

if (LEGACY_NETWORKS_ROUTE_MAP[this.network as TORUS_LEGACY_NETWORK_TYPE]) {
throw new Error(`importPrivateKey is not supported by legacy network; ${this.network}`);
}
if (endpoints.length !== nodeIndexes.length) {
throw new Error(`length of endpoints array must be same as length of nodeIndexes array`);
}
Expand Down

0 comments on commit bb07e60

Please sign in to comment.