Skip to content

Commit

Permalink
fix mainnnet import key tests
Browse files Browse the repository at this point in the history
  • Loading branch information
himanshu committed Feb 4, 2024
2 parents 6b9b45b + 271733d commit 38dc335
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 35 deletions.
35 changes: 29 additions & 6 deletions package-lock.json

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

7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@toruslabs/torus.js",
"version": "12.1.0",
"version": "12.1.1",
"description": "Handle communication with torus nodes",
"main": "dist/torusUtils.cjs.js",
"module": "dist/torusUtils.esm.js",
Expand All @@ -17,16 +17,15 @@
"prepare": "husky install"
},
"files": [
"dist",
"src"
"dist"
],
"peerDependencies": {
"@babel/runtime": "7.x"
},
"dependencies": {
"@toruslabs/constants": "^13.1.0",
"@toruslabs/eccrypto": "^4.0.0",
"@toruslabs/http-helpers": "^5.0.0",
"@toruslabs/http-helpers": "^6.0.0",
"bn.js": "^5.2.1",
"elliptic": "^6.5.4",
"ethereum-cryptography": "^2.1.2",
Expand Down
16 changes: 14 additions & 2 deletions src/torus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ import { Some } from "./some";
// Implement threshold logic wrappers around public APIs
// of Torus nodes to handle malicious node responses
class Torus {
private static sessionTime: number = 86400; // 86400 = 24 hour

public allowHost: string;

public serverTimeOffset: number;
Expand Down Expand Up @@ -113,6 +115,10 @@ class Torus {
setEmbedHost(embedHost);
}

static setSessionTime(sessionTime: number): void {
Torus.sessionTime = sessionTime;
}

static isGetOrSetNonceError(err: unknown): boolean {
return err instanceof GetOrSetNonceError;
}
Expand Down Expand Up @@ -166,7 +172,10 @@ class Torus {
newImportedShares: [],
overrideExistingKey: false,
nodePubkeys,
extraParams,
extraParams: {
...extraParams,
session_token_exp_second: Torus.sessionTime,
},
});
}

Expand Down Expand Up @@ -213,7 +222,10 @@ class Torus {
overrideExistingKey: true,
newImportedShares: sharesData,
nodePubkeys,
extraParams,
extraParams: {
...extraParams,
session_token_exp_second: Torus.sessionTime,
},
});
}

Expand Down
28 changes: 28 additions & 0 deletions test/sapphire_devnet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,34 @@ describe("torus utils sapphire devnet", function () {
nodesData: result.nodesData,
});
});
it("should be able to update the `sessionTime` of the token signature data", async function () {
const token = generateIdToken(TORUS_TEST_EMAIL, "ES256");

const nodeDetails = await TORUS_NODE_MANAGER.getNodeDetails({ verifier: TORUS_TEST_VERIFIER, verifierId: TORUS_TEST_EMAIL });
const torusNodeEndpoints = nodeDetails.torusNodeSSSEndpoints;
torusNodeEndpoints[1] = "https://example.com";

const customSessionTime = 3600;
TorusUtils.setSessionTime(customSessionTime); // 1hr

const result = await torus.retrieveShares(
torusNodeEndpoints,
nodeDetails.torusIndexes,
TORUS_TEST_VERIFIER,
{ verifier_id: TORUS_TEST_EMAIL },
token,
nodeDetails.torusNodePub
);

const signatures = result.sessionData.sessionTokenData.map((s) => ({ data: s.token, sig: s.signature }));

const parsedSigsData = signatures.map((s) => JSON.parse(atob(s.data)));
parsedSigsData.forEach((ps) => {
const sessionTime = ps.exp - Math.floor(Date.now() / 1000);
expect(sessionTime).eql(customSessionTime);
});
});

it("should be able to import a key for a new user", async function () {
const email = faker.internet.email();
const token = generateIdToken(email, "ES256");
Expand Down
78 changes: 55 additions & 23 deletions test/sapphire_mainnet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ const TORUS_TEST_VERIFIER = "torus-test-health";
const TORUS_TEST_AGGREGATE_VERIFIER = "torus-aggregate-sapphire-mainnet";
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 Expand Up @@ -50,15 +49,15 @@ describe("torus utils sapphire mainnet", function () {
X: "56e803db7710adbfe0ecca35bc6a3ad27e966df142e157e76e492773c88e8433",
Y: "f4168594c1126ca731756dd480f992ee73b0834ba4b787dd892a9211165f50a3",
},
nonce: new BN("f3ba568eeeaca5c285b25878a067fd85b1720b94f9099591d4274dc0a8cada9b", "hex"),
nonce: new BN("0", "hex"),
upgraded: false,
typeOfUser: "v2",
},
nodesData: result.nodesData,
});
});

it.skip("should be able to import a key for a new user", async function () {
it("should be able to import a key for a new user", async function () {
const email = faker.internet.email();
const token = generateIdToken(email, "ES256");
const ec = new EC("secp256k1");
Expand All @@ -77,29 +76,33 @@ describe("torus utils sapphire mainnet", function () {
privHex
);
expect(result.finalKeyData.privKey).to.be.equal(privHex);
});
it.skip("should be able to import a key for a existing user", async function () {
let verifierDetails = { verifier: TORUS_TEST_VERIFIER, verifierId: TORUS_IMPORT_EMAIL };
const nodeDetails = await TORUS_NODE_MANAGER.getNodeDetails(verifierDetails);
const torusNodeEndpoints = nodeDetails.torusNodeSSSEndpoints;
const token = generateIdToken(TORUS_IMPORT_EMAIL, "ES256");
const ec = new EC("secp256k1");

const privKeyBuffer = generatePrivateKey(ec, Buffer);
const privHex = privKeyBuffer.toString("hex");
const result1 = await torus.importPrivateKey(
const { finalKeyData } = await torus.getPublicAddress(torusNodeEndpoints, nodeDetails.torusNodePub, {
verifier: TORUS_TEST_VERIFIER,
verifierId: email,
});
expect(result.finalKeyData.evmAddress).to.be.equal(finalKeyData.evmAddress);

const privKeyBufferNew = generatePrivateKey(ec, Buffer);
const privHexNew = privKeyBufferNew.toString("hex");
const tokenNew = generateIdToken(email, "ES256");

const resultNew = await torus.importPrivateKey(
torusNodeEndpoints,
nodeDetails.torusIndexes,
nodeDetails.torusNodePub,
TORUS_TEST_VERIFIER,
{ verifier_id: TORUS_IMPORT_EMAIL },
token,
privHex
{ verifier_id: email },
tokenNew,
privHexNew
);
expect(result1.finalKeyData.privKey).to.be.equal(privHex);
verifierDetails = { verifier: TORUS_TEST_VERIFIER, verifierId: TORUS_IMPORT_EMAIL };
const result2 = await torus.getPublicAddress(torusNodeEndpoints, nodeDetails.torusNodePub, verifierDetails);
expect(result1.finalKeyData.evmAddress).to.be.equal(result2.finalKeyData.evmAddress);
expect(resultNew.finalKeyData.privKey).to.be.equal(privHexNew);

const { finalKeyData: finalKeyDataNew } = await torus.getPublicAddress(torusNodeEndpoints, nodeDetails.torusNodePub, {
verifier: TORUS_TEST_VERIFIER,
verifierId: email,
});
expect(resultNew.finalKeyData.evmAddress).to.be.equal(finalKeyDataNew.evmAddress);
});

it("should be able to key assign", async function () {
Expand Down Expand Up @@ -208,7 +211,7 @@ describe("torus utils sapphire mainnet", function () {
X: "498ed301af25a3b7136f478fa58677c79a6d6fe965bc13002a6f459b896313bd",
Y: "d6feb9a1e0d6d0627fbb1ce75682bc09ab4cf0e2da4f0f7fcac0ba9d07596c8f",
},
nonce: new BN("3c2b6ba5b54ca0ba4ae978eb48429a84c47b7b3e526b35e7d46dd716887f52bf", "hex"),
nonce: new BN("0", "hex"),
upgraded: false,
typeOfUser: "v2",
},
Expand Down Expand Up @@ -238,7 +241,7 @@ describe("torus utils sapphire mainnet", function () {
X: "498ed301af25a3b7136f478fa58677c79a6d6fe965bc13002a6f459b896313bd",
Y: "d6feb9a1e0d6d0627fbb1ce75682bc09ab4cf0e2da4f0f7fcac0ba9d07596c8f",
},
nonce: new BN("3c2b6ba5b54ca0ba4ae978eb48429a84c47b7b3e526b35e7d46dd716887f52bf", "hex"),
nonce: new BN("0", "hex"),
upgraded: false,
typeOfUser: "v2",
},
Expand Down Expand Up @@ -357,4 +360,33 @@ describe("torus utils sapphire mainnet", function () {
expect(result.metadata.nonce).to.not.equal(null);
expect(result.metadata.upgraded).to.equal(false);
});

it("should be able to update the `sessionTime` of the token signature data", async function () {
const email = faker.internet.email();
const token = generateIdToken(TORUS_TEST_EMAIL, "ES256");

const nodeDetails = await TORUS_NODE_MANAGER.getNodeDetails({ verifier: TORUS_TEST_AGGREGATE_VERIFIER, verifierId: email });
const torusNodeEndpoints = nodeDetails.torusNodeSSSEndpoints;
torusNodeEndpoints[1] = "https://example.com";

const customSessionTime = 3600;
TorusUtils.setSessionTime(customSessionTime); // 1hr

const result = await torus.retrieveShares(
torusNodeEndpoints,
nodeDetails.torusIndexes,
TORUS_TEST_VERIFIER,
{ verifier_id: TORUS_TEST_EMAIL },
token,
nodeDetails.torusNodePub
);

const signatures = result.sessionData.sessionTokenData.map((s) => ({ data: s.token, sig: s.signature }));

const parsedSigsData = signatures.map((s) => JSON.parse(atob(s.data)));
parsedSigsData.forEach((ps) => {
const sessionTime = ps.exp - Math.floor(Date.now() / 1000);
expect(sessionTime).eql(customSessionTime);
});
});
});

0 comments on commit 38dc335

Please sign in to comment.