Skip to content

Commit 38dc335

Browse files
author
himanshu
committed
fix mainnnet import key tests
2 parents 6b9b45b + 271733d commit 38dc335

5 files changed

+129
-35
lines changed

package-lock.json

+29-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@toruslabs/torus.js",
3-
"version": "12.1.0",
3+
"version": "12.1.1",
44
"description": "Handle communication with torus nodes",
55
"main": "dist/torusUtils.cjs.js",
66
"module": "dist/torusUtils.esm.js",
@@ -17,16 +17,15 @@
1717
"prepare": "husky install"
1818
},
1919
"files": [
20-
"dist",
21-
"src"
20+
"dist"
2221
],
2322
"peerDependencies": {
2423
"@babel/runtime": "7.x"
2524
},
2625
"dependencies": {
2726
"@toruslabs/constants": "^13.1.0",
2827
"@toruslabs/eccrypto": "^4.0.0",
29-
"@toruslabs/http-helpers": "^5.0.0",
28+
"@toruslabs/http-helpers": "^6.0.0",
3029
"bn.js": "^5.2.1",
3130
"elliptic": "^6.5.4",
3231
"ethereum-cryptography": "^2.1.2",

src/torus.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ import { Some } from "./some";
5151
// Implement threshold logic wrappers around public APIs
5252
// of Torus nodes to handle malicious node responses
5353
class Torus {
54+
private static sessionTime: number = 86400; // 86400 = 24 hour
55+
5456
public allowHost: string;
5557

5658
public serverTimeOffset: number;
@@ -113,6 +115,10 @@ class Torus {
113115
setEmbedHost(embedHost);
114116
}
115117

118+
static setSessionTime(sessionTime: number): void {
119+
Torus.sessionTime = sessionTime;
120+
}
121+
116122
static isGetOrSetNonceError(err: unknown): boolean {
117123
return err instanceof GetOrSetNonceError;
118124
}
@@ -166,7 +172,10 @@ class Torus {
166172
newImportedShares: [],
167173
overrideExistingKey: false,
168174
nodePubkeys,
169-
extraParams,
175+
extraParams: {
176+
...extraParams,
177+
session_token_exp_second: Torus.sessionTime,
178+
},
170179
});
171180
}
172181

@@ -213,7 +222,10 @@ class Torus {
213222
overrideExistingKey: true,
214223
newImportedShares: sharesData,
215224
nodePubkeys,
216-
extraParams,
225+
extraParams: {
226+
...extraParams,
227+
session_token_exp_second: Torus.sessionTime,
228+
},
217229
});
218230
}
219231

test/sapphire_devnet.test.ts

+28
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,34 @@ describe("torus utils sapphire devnet", function () {
418418
nodesData: result.nodesData,
419419
});
420420
});
421+
it("should be able to update the `sessionTime` of the token signature data", async function () {
422+
const token = generateIdToken(TORUS_TEST_EMAIL, "ES256");
423+
424+
const nodeDetails = await TORUS_NODE_MANAGER.getNodeDetails({ verifier: TORUS_TEST_VERIFIER, verifierId: TORUS_TEST_EMAIL });
425+
const torusNodeEndpoints = nodeDetails.torusNodeSSSEndpoints;
426+
torusNodeEndpoints[1] = "https://example.com";
427+
428+
const customSessionTime = 3600;
429+
TorusUtils.setSessionTime(customSessionTime); // 1hr
430+
431+
const result = await torus.retrieveShares(
432+
torusNodeEndpoints,
433+
nodeDetails.torusIndexes,
434+
TORUS_TEST_VERIFIER,
435+
{ verifier_id: TORUS_TEST_EMAIL },
436+
token,
437+
nodeDetails.torusNodePub
438+
);
439+
440+
const signatures = result.sessionData.sessionTokenData.map((s) => ({ data: s.token, sig: s.signature }));
441+
442+
const parsedSigsData = signatures.map((s) => JSON.parse(atob(s.data)));
443+
parsedSigsData.forEach((ps) => {
444+
const sessionTime = ps.exp - Math.floor(Date.now() / 1000);
445+
expect(sessionTime).eql(customSessionTime);
446+
});
447+
});
448+
421449
it("should be able to import a key for a new user", async function () {
422450
const email = faker.internet.email();
423451
const token = generateIdToken(email, "ES256");

test/sapphire_mainnet.test.ts

+55-23
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ const TORUS_TEST_VERIFIER = "torus-test-health";
1414
const TORUS_TEST_AGGREGATE_VERIFIER = "torus-aggregate-sapphire-mainnet";
1515
const HashEnabledVerifier = "torus-test-verifierid-hash";
1616
const TORUS_EXTENDED_VERIFIER_EMAIL = "[email protected]";
17-
const TORUS_IMPORT_EMAIL = "[email protected]";
1817

19-
describe("torus utils sapphire mainnet", function () {
18+
describe.only("torus utils sapphire mainnet", function () {
2019
let torus: TorusUtils;
2120
let TORUS_NODE_MANAGER: NodeManager;
2221

@@ -50,15 +49,15 @@ describe("torus utils sapphire mainnet", function () {
5049
X: "56e803db7710adbfe0ecca35bc6a3ad27e966df142e157e76e492773c88e8433",
5150
Y: "f4168594c1126ca731756dd480f992ee73b0834ba4b787dd892a9211165f50a3",
5251
},
53-
nonce: new BN("f3ba568eeeaca5c285b25878a067fd85b1720b94f9099591d4274dc0a8cada9b", "hex"),
52+
nonce: new BN("0", "hex"),
5453
upgraded: false,
5554
typeOfUser: "v2",
5655
},
5756
nodesData: result.nodesData,
5857
});
5958
});
6059

61-
it.skip("should be able to import a key for a new user", async function () {
60+
it("should be able to import a key for a new user", async function () {
6261
const email = faker.internet.email();
6362
const token = generateIdToken(email, "ES256");
6463
const ec = new EC("secp256k1");
@@ -77,29 +76,33 @@ describe("torus utils sapphire mainnet", function () {
7776
privHex
7877
);
7978
expect(result.finalKeyData.privKey).to.be.equal(privHex);
80-
});
81-
it.skip("should be able to import a key for a existing user", async function () {
82-
let verifierDetails = { verifier: TORUS_TEST_VERIFIER, verifierId: TORUS_IMPORT_EMAIL };
83-
const nodeDetails = await TORUS_NODE_MANAGER.getNodeDetails(verifierDetails);
84-
const torusNodeEndpoints = nodeDetails.torusNodeSSSEndpoints;
85-
const token = generateIdToken(TORUS_IMPORT_EMAIL, "ES256");
86-
const ec = new EC("secp256k1");
8779

88-
const privKeyBuffer = generatePrivateKey(ec, Buffer);
89-
const privHex = privKeyBuffer.toString("hex");
90-
const result1 = await torus.importPrivateKey(
80+
const { finalKeyData } = await torus.getPublicAddress(torusNodeEndpoints, nodeDetails.torusNodePub, {
81+
verifier: TORUS_TEST_VERIFIER,
82+
verifierId: email,
83+
});
84+
expect(result.finalKeyData.evmAddress).to.be.equal(finalKeyData.evmAddress);
85+
86+
const privKeyBufferNew = generatePrivateKey(ec, Buffer);
87+
const privHexNew = privKeyBufferNew.toString("hex");
88+
const tokenNew = generateIdToken(email, "ES256");
89+
90+
const resultNew = await torus.importPrivateKey(
9191
torusNodeEndpoints,
9292
nodeDetails.torusIndexes,
9393
nodeDetails.torusNodePub,
9494
TORUS_TEST_VERIFIER,
95-
{ verifier_id: TORUS_IMPORT_EMAIL },
96-
token,
97-
privHex
95+
{ verifier_id: email },
96+
tokenNew,
97+
privHexNew
9898
);
99-
expect(result1.finalKeyData.privKey).to.be.equal(privHex);
100-
verifierDetails = { verifier: TORUS_TEST_VERIFIER, verifierId: TORUS_IMPORT_EMAIL };
101-
const result2 = await torus.getPublicAddress(torusNodeEndpoints, nodeDetails.torusNodePub, verifierDetails);
102-
expect(result1.finalKeyData.evmAddress).to.be.equal(result2.finalKeyData.evmAddress);
99+
expect(resultNew.finalKeyData.privKey).to.be.equal(privHexNew);
100+
101+
const { finalKeyData: finalKeyDataNew } = await torus.getPublicAddress(torusNodeEndpoints, nodeDetails.torusNodePub, {
102+
verifier: TORUS_TEST_VERIFIER,
103+
verifierId: email,
104+
});
105+
expect(resultNew.finalKeyData.evmAddress).to.be.equal(finalKeyDataNew.evmAddress);
103106
});
104107

105108
it("should be able to key assign", async function () {
@@ -208,7 +211,7 @@ describe("torus utils sapphire mainnet", function () {
208211
X: "498ed301af25a3b7136f478fa58677c79a6d6fe965bc13002a6f459b896313bd",
209212
Y: "d6feb9a1e0d6d0627fbb1ce75682bc09ab4cf0e2da4f0f7fcac0ba9d07596c8f",
210213
},
211-
nonce: new BN("3c2b6ba5b54ca0ba4ae978eb48429a84c47b7b3e526b35e7d46dd716887f52bf", "hex"),
214+
nonce: new BN("0", "hex"),
212215
upgraded: false,
213216
typeOfUser: "v2",
214217
},
@@ -238,7 +241,7 @@ describe("torus utils sapphire mainnet", function () {
238241
X: "498ed301af25a3b7136f478fa58677c79a6d6fe965bc13002a6f459b896313bd",
239242
Y: "d6feb9a1e0d6d0627fbb1ce75682bc09ab4cf0e2da4f0f7fcac0ba9d07596c8f",
240243
},
241-
nonce: new BN("3c2b6ba5b54ca0ba4ae978eb48429a84c47b7b3e526b35e7d46dd716887f52bf", "hex"),
244+
nonce: new BN("0", "hex"),
242245
upgraded: false,
243246
typeOfUser: "v2",
244247
},
@@ -357,4 +360,33 @@ describe("torus utils sapphire mainnet", function () {
357360
expect(result.metadata.nonce).to.not.equal(null);
358361
expect(result.metadata.upgraded).to.equal(false);
359362
});
363+
364+
it("should be able to update the `sessionTime` of the token signature data", async function () {
365+
const email = faker.internet.email();
366+
const token = generateIdToken(TORUS_TEST_EMAIL, "ES256");
367+
368+
const nodeDetails = await TORUS_NODE_MANAGER.getNodeDetails({ verifier: TORUS_TEST_AGGREGATE_VERIFIER, verifierId: email });
369+
const torusNodeEndpoints = nodeDetails.torusNodeSSSEndpoints;
370+
torusNodeEndpoints[1] = "https://example.com";
371+
372+
const customSessionTime = 3600;
373+
TorusUtils.setSessionTime(customSessionTime); // 1hr
374+
375+
const result = await torus.retrieveShares(
376+
torusNodeEndpoints,
377+
nodeDetails.torusIndexes,
378+
TORUS_TEST_VERIFIER,
379+
{ verifier_id: TORUS_TEST_EMAIL },
380+
token,
381+
nodeDetails.torusNodePub
382+
);
383+
384+
const signatures = result.sessionData.sessionTokenData.map((s) => ({ data: s.token, sig: s.signature }));
385+
386+
const parsedSigsData = signatures.map((s) => JSON.parse(atob(s.data)));
387+
parsedSigsData.forEach((ps) => {
388+
const sessionTime = ps.exp - Math.floor(Date.now() / 1000);
389+
expect(sessionTime).eql(customSessionTime);
390+
});
391+
});
360392
});

0 commit comments

Comments
 (0)