Skip to content

Commit 39f6540

Browse files
committed
feat: default server time offset
1 parent de06da0 commit 39f6540

File tree

4 files changed

+39
-11
lines changed

4 files changed

+39
-11
lines changed

src/helpers/common.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import { EciesHex, VerifierLookupResponse } from "../interfaces";
88
// like created_at field might vary and nonce_data might not be returned by all nodes because
99
// of the metadata implementation in sapphire.
1010
export const normalizeKeysResult = (result: VerifierLookupResponse) => {
11-
const finalResult: Pick<VerifierLookupResponse, "keys" | "is_new_key"> = {
11+
const finalResult: Pick<VerifierLookupResponse, "keys" | "is_new_key" | "server_time_offset"> = {
1212
keys: [],
1313
is_new_key: result.is_new_key,
14+
server_time_offset: result.server_time_offset,
1415
};
1516
if (result && result.keys && result.keys.length > 0) {
1617
const finalKey = result.keys[0];

src/helpers/nodeUtils.ts

+26-5
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ export async function retrieveOrImportShare(params: {
139139
}): Promise<TorusKey> {
140140
const {
141141
legacyMetadataHost,
142-
serverTimeOffset,
143142
enableOneKey,
144143
ecCurve,
145144
allowHost,
@@ -317,7 +316,14 @@ export async function retrieveOrImportShare(params: {
317316
let thresholdNonceData: GetOrSetNonceResult;
318317
return Some<
319318
void | JRPCResponse<ShareRequestResult>,
320-
| { privateKey: BN; sessionTokenData: SessionToken[]; thresholdNonceData: GetOrSetNonceResult; nodeIndexes: BN[]; isNewKey: boolean }
319+
| {
320+
privateKey: BN;
321+
sessionTokenData: SessionToken[];
322+
thresholdNonceData: GetOrSetNonceResult;
323+
nodeIndexes: BN[];
324+
isNewKey: boolean;
325+
serverTimeOffsetResponse?: number;
326+
}
321327
| undefined
322328
>(promiseArrRequest, async (shareResponses, sharedState) => {
323329
// check if threshold number of nodes have returned the same user public key
@@ -377,6 +383,7 @@ export async function retrieveOrImportShare(params: {
377383
const nodeIndexes: BN[] = [];
378384
const sessionTokenData: SessionToken[] = [];
379385
const isNewKeyResponses: string[] = [];
386+
const serverTimeOffsetResponses: string[] = [];
380387

381388
for (let i = 0; i < completedRequests.length; i += 1) {
382389
const currentShareResponse = completedRequests[i] as JRPCResponse<ShareRequestResult>;
@@ -387,9 +394,11 @@ export async function retrieveOrImportShare(params: {
387394
session_token_sig_metadata: sessionTokenSigMetadata,
388395
keys,
389396
is_new_key: isNewKey,
397+
server_time_offset: serverTimeOffset,
390398
} = currentShareResponse.result;
391399

392400
isNewKeyResponses.push(isNewKey);
401+
serverTimeOffsetResponses.push(serverTimeOffset);
393402

394403
if (sessionTokenSigs?.length > 0) {
395404
// decrypt sessionSig if enc metadata is sent
@@ -513,13 +522,24 @@ export async function retrieveOrImportShare(params: {
513522
}
514523
const thresholdIsNewKey = thresholdSame(isNewKeyResponses, ~~(endpoints.length / 2) + 1);
515524

516-
return { privateKey, sessionTokenData, thresholdNonceData, nodeIndexes, isNewKey: thresholdIsNewKey === "true" };
525+
// Convert each string timestamp to a number
526+
const epochTimes = serverTimeOffsetResponses.map((timestamp) => parseInt(timestamp, 10));
527+
528+
// console.log(epochTimes); // Output: 1709299134
529+
return {
530+
privateKey,
531+
sessionTokenData,
532+
thresholdNonceData,
533+
nodeIndexes,
534+
isNewKey: thresholdIsNewKey === "true",
535+
serverTimeOffsetResponse: Math.max(...epochTimes),
536+
};
517537
}
518538
throw new Error("Invalid");
519539
});
520540
})
521541
.then(async (res) => {
522-
const { privateKey, sessionTokenData, thresholdNonceData, nodeIndexes, isNewKey } = res;
542+
const { privateKey, sessionTokenData, thresholdNonceData, nodeIndexes, isNewKey, serverTimeOffsetResponse } = res;
523543
let nonceResult = thresholdNonceData;
524544
if (!privateKey) throw new Error("Invalid private key returned");
525545
const oAuthKey = privateKey;
@@ -538,7 +558,8 @@ export async function retrieveOrImportShare(params: {
538558
finalPubKey = ecCurve.keyFromPublic({ x: oAuthPubkeyX, y: oAuthPubkeyY }).getPublic();
539559
} else if (LEGACY_NETWORKS_ROUTE_MAP[network as TORUS_LEGACY_NETWORK_TYPE]) {
540560
if (enableOneKey) {
541-
nonceResult = await getOrSetNonce(legacyMetadataHost, ecCurve, serverTimeOffset, oAuthPubkeyX, oAuthPubkeyY, oAuthKey, !isNewKey);
561+
const serverTimeOffsetValue = this.serverTimeOffset || serverTimeOffsetResponse;
562+
nonceResult = await getOrSetNonce(legacyMetadataHost, ecCurve, serverTimeOffsetValue, oAuthPubkeyX, oAuthPubkeyY, oAuthKey, !isNewKey);
542563
metadataNonce = new BN(nonceResult.nonce || "0", 16);
543564
typeOfUser = nonceResult.typeOfUser;
544565
if (typeOfUser === "v2") {

src/interfaces.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export interface TorusCtorOptions {
3939

4040
export interface LegacyVerifierLookupResponse {
4141
keys: { pub_key_X: string; pub_key_Y: string; address: string }[];
42+
server_time_offset?: string;
4243
}
4344

4445
export interface VerifierLookupResponse {
@@ -51,6 +52,7 @@ export interface VerifierLookupResponse {
5152
}[];
5253
is_new_key: boolean;
5354
node_index: string;
55+
server_time_offset?: string;
5456
}
5557

5658
export interface CommitmentRequestResult {
@@ -73,12 +75,12 @@ export interface JRPCResponse<T> {
7375
}
7476

7577
export interface LegacyKeyLookupResult {
76-
keyResult: Pick<LegacyVerifierLookupResponse, "keys">;
78+
keyResult: Pick<LegacyVerifierLookupResponse, "keys" | "server_time_offset">;
7779
errorResult: JRPCResponse<LegacyVerifierLookupResponse>["error"];
7880
}
7981

8082
export interface KeyLookupResult {
81-
keyResult: Pick<VerifierLookupResponse, "keys" | "is_new_key">;
83+
keyResult: Pick<VerifierLookupResponse, "keys" | "is_new_key" | "server_time_offset">;
8284
nodeIndexes: number[];
8385
errorResult: JRPCResponse<VerifierLookupResponse>["error"];
8486
nonceResult?: GetOrSetNonceResult;
@@ -147,6 +149,7 @@ export interface ShareRequestResult {
147149
node_pubx: string;
148150
node_puby: string;
149151
is_new_key: string;
152+
server_time_offset?: string;
150153
}
151154

152155
export interface ImportedShare {

src/torus.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -582,12 +582,12 @@ class Torus {
582582
} else {
583583
throw new Error(`node results do not match at first lookup ${JSON.stringify(keyResult || {})}, ${JSON.stringify(errorResult || {})}`);
584584
}
585-
586585
if (finalKeyResult) {
587586
return this.formatLegacyPublicKeyData({
588587
finalKeyResult,
589588
isNewKey,
590589
enableOneKey,
590+
serverTimeOffset: keyResult.server_time_offset,
591591
});
592592
}
593593
throw new Error(`node results do not match at final lookup ${JSON.stringify(keyResult || {})}, ${JSON.stringify(errorResult || {})}`);
@@ -659,6 +659,7 @@ class Torus {
659659
finalKeyResult: {
660660
keys: keyResult.keys,
661661
},
662+
serverTimeOffset: keyResult.server_time_offset,
662663
});
663664
} else {
664665
const v2NonceResult = nonceResult as v2NonceResultType;
@@ -711,8 +712,9 @@ class Torus {
711712
finalKeyResult: LegacyVerifierLookupResponse;
712713
enableOneKey: boolean;
713714
isNewKey: boolean;
715+
serverTimeOffset: string;
714716
}): Promise<TorusPublicKey> {
715-
const { finalKeyResult, enableOneKey, isNewKey } = params;
717+
const { finalKeyResult, enableOneKey, isNewKey, serverTimeOffset } = params;
716718
const { pub_key_X: X, pub_key_Y: Y } = finalKeyResult.keys[0];
717719
let nonceResult: GetOrSetNonceResult;
718720
let nonce: BN;
@@ -722,9 +724,10 @@ class Torus {
722724

723725
const oAuthPubKey = this.ec.keyFromPublic({ x: X, y: Y }).getPublic();
724726

727+
const serverTimeOffsetValue = this.serverTimeOffset || parseInt(serverTimeOffset);
725728
if (enableOneKey) {
726729
try {
727-
nonceResult = await getOrSetNonce(this.legacyMetadataHost, this.ec, this.serverTimeOffset, X, Y, undefined, !isNewKey);
730+
nonceResult = await getOrSetNonce(this.legacyMetadataHost, this.ec, serverTimeOffsetValue, X, Y, undefined, !isNewKey);
728731
nonce = new BN(nonceResult.nonce || "0", 16);
729732
typeOfUser = nonceResult.typeOfUser;
730733
} catch {

0 commit comments

Comments
 (0)