Skip to content

Commit

Permalink
fix: add backcompatible tests
Browse files Browse the repository at this point in the history
reset on start of Login test
  • Loading branch information
ieow committed Feb 27, 2024
1 parent 17a9d92 commit 1fa38b9
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 10 deletions.
130 changes: 130 additions & 0 deletions tests/backwardCompatible.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/* eslint-disable mocha/handle-done-callback */
import assert from "node:assert";
import test from "node:test";

import { UX_MODE_TYPE } from "@toruslabs/customauth";
import { keccak256 } from "@toruslabs/metadata-helpers";
import * as TssLib from "@toruslabs/tss-lib-node";
import BN from "bn.js";
import { ec as EC } from "elliptic";

import { BrowserStorage, COREKIT_STATUS, WEB3AUTH_NETWORK, WEB3AUTH_NETWORK_TYPE, Web3AuthMPCCoreKit } from "../src";
import { mockLogin } from "./setup";

type TestVariable = {
web3AuthNetwork: WEB3AUTH_NETWORK_TYPE;
uxMode: UX_MODE_TYPE | "nodejs";
manualSync?: boolean;

email: string;
};

const defaultTestEmail = "backwardcompatible";
const variable: TestVariable[] = [
{ web3AuthNetwork: WEB3AUTH_NETWORK.DEVNET, uxMode: "nodejs", email: defaultTestEmail },
// { web3AuthNetwork: WEB3AUTH_NETWORK.MAINNET, uxMode: UX_MODE.REDIRECT, email: defaultTestEmail },

{ web3AuthNetwork: WEB3AUTH_NETWORK.DEVNET, uxMode: "nodejs", manualSync: true, email: defaultTestEmail },
// { web3AuthNetwork: WEB3AUTH_NETWORK.MAINNET, uxMode: UX_MODE.REDIRECT, manualSync: true, email: defaultTestEmail },
];

const checkLogin = async (coreKitInstance: Web3AuthMPCCoreKit) => {
const keyDetails = coreKitInstance.getKeyDetails();
assert.strictEqual(coreKitInstance.status, COREKIT_STATUS.LOGGED_IN);
assert.strictEqual(keyDetails.requiredFactors, 0);
const factorkey = coreKitInstance.getCurrentFactorKey();
await coreKitInstance.tKey.getTSSShare(new BN(factorkey.factorKey, "hex"));
};

variable.forEach((testVariable) => {
const { web3AuthNetwork, uxMode, manualSync, email } = testVariable;
const newCoreKitInstance = () =>
new Web3AuthMPCCoreKit({
web3AuthClientId: "torus-key-test",
web3AuthNetwork,
baseUrl: "http://localhost:3000",
uxMode,
tssLib: TssLib,
storageKey: "memory",
manualSync,
});

const coreKitInstance = newCoreKitInstance();

const testNameSuffix = JSON.stringify(testVariable);
test(`#Login Test with JWT + logout : ${testNameSuffix}`, async (t) => {
t.after(async function () {
// after all test tear down
});

await t.test("#Login ", async function () {
// mocklogin
const { idToken, parsedToken } = await mockLogin(email);
await coreKitInstance.init({ handleRedirectResult: false });
await coreKitInstance.loginWithJWT({
verifier: "torus-test-health",
verifierId: parsedToken.email,
idToken,
});

// get key details
await checkLogin(coreKitInstance);

const tssPublicPoint = coreKitInstance.getTssPublicKey();
const { metadataPubKey, tssPubKey } = coreKitInstance.getKeyDetails();
assert.strictEqual(tssPublicPoint.x.toString("hex"), "d2869f27c3e226d90b275b008f7dc67b8f4b208900a7b98ecc4e5266807d382c");
assert.strictEqual(tssPublicPoint.y.toString("hex"), "15860fd569413eb7f177e655c4bf855f37920b800235de344fdd518196becfe0");

assert.strictEqual(tssPubKey.x.toString("hex"), "d2869f27c3e226d90b275b008f7dc67b8f4b208900a7b98ecc4e5266807d382c");
assert.strictEqual(tssPubKey.y.toString("hex"), "15860fd569413eb7f177e655c4bf855f37920b800235de344fdd518196becfe0");

assert.strictEqual(metadataPubKey.x.toString("hex"), "b3951a441f87ecea4672edc82894ac023316723cf164a93adec72b58a27a1f06");
assert.strictEqual(metadataPubKey.y.toString("hex"), "3be6c118d94242a650e8aebbefcd37ebeceeb927d0ed51f3d2ba723b8fd2740b");
});

await t.test("#relogin ", async function () {
// reload without rehydrate
// await coreKitInstance.init({ rehydrate: false });

// rehydrate
await coreKitInstance.init({ handleRedirectResult: false });
await checkLogin(coreKitInstance);

// logout
await coreKitInstance.logout();

BrowserStorage.getInstance("memory").resetStore();
// rehydrate should fail
await coreKitInstance.init();
assert.strictEqual(coreKitInstance.status, COREKIT_STATUS.INITIALIZED);
try {
coreKitInstance.getCurrentFactorKey();
throw new Error("should not reach here");
} catch (error) {}

// relogin
const { idToken, parsedToken } = await mockLogin(email);
await coreKitInstance.loginWithJWT({
verifier: "torus-test-health",
verifierId: parsedToken.email,
idToken,
});

// get key details
await checkLogin(coreKitInstance);
});

await t.test("#able to sign", async function () {
const msg = "hello world";
const msgBuffer = Buffer.from(msg);
const msgHash = keccak256(msgBuffer);
const signature = await coreKitInstance.sign(msgHash);

const secp256k1 = new EC("secp256k1");
const pubkey = secp256k1.recoverPubKey(msgHash, signature, signature.v - 27);
const publicKeyPoint = coreKitInstance.getTssPublicKey();
assert.strictEqual(pubkey.x.toString("hex"), publicKeyPoint.x.toString("hex"));
assert.strictEqual(pubkey.y.toString("hex"), publicKeyPoint.y.toString("hex"));
});
});
});
41 changes: 31 additions & 10 deletions tests/login.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,36 @@ variable.forEach((testVariable) => {
});

const testNameSuffix = JSON.stringify(testVariable);

let checkPubKey;
let checkTssShare;

test(`#Login Test with JWT + logout : ${testNameSuffix}`, async (t) => {
t.before(async function () {
if (coreKitInstance.status === COREKIT_STATUS.INITIALIZED) await criticalResetAccount(coreKitInstance);
const resetInstance = new Web3AuthMPCCoreKit({
web3AuthClientId: "torus-key-test",
web3AuthNetwork,
baseUrl: "http://localhost:3000",
uxMode,
tssLib: TssLib,
storageKey: "memory",
manualSync,
});
const { idToken, parsedToken } = await mockLogin(email);
await resetInstance.init({ handleRedirectResult: false });
await resetInstance.loginWithJWT({
verifier: "torus-test-health",
verifierId: parsedToken.email,
idToken,
});
if (resetInstance.status === COREKIT_STATUS.INITIALIZED) await criticalResetAccount(resetInstance);
BrowserStorage.getInstance("memory").resetStore();
});

t.after(async function () {
// after all test tear down
});

// t.skip("#Login with Oauth", async function () {
// // popup
// // redirect flow
// // not testable
// });
await t.test("#Login ", async function () {
// mocklogin
const { idToken, parsedToken } = await mockLogin(email);
Expand All @@ -79,15 +94,13 @@ variable.forEach((testVariable) => {

// get key details
await checkLogin(coreKitInstance);
const pubKey = coreKitInstance.getTssPublicKey();
checkPubKey = coreKitInstance.getTssPublicKey();
const factorkey = coreKitInstance.getCurrentFactorKey();
const { tssShare } = await coreKitInstance.tKey.getTSSShare(new BN(factorkey.factorKey, "hex"), {
threshold: 0,
});
checkTssShare = tssShare;
// check whether the public key and tss share is same as old sdks
strictEqual(pubKey.x.toString("hex"), "1f85b9d4fc945dc93739323d65a4dc89060faece4cb90c147a28bb93b01c0cac");
strictEqual(pubKey.y.toString("hex"), "f286a5912766de74d48cfa38bb1e593053b2cb471504ace3ef6186af584502f8");
strictEqual(tssShare.toString("hex"), "6252d281ae566243c1c00e8d89414527c1d6634faf489164efa28c5d6adc450a");
});

await t.test("#relogin ", async function () {
Expand Down Expand Up @@ -119,6 +132,14 @@ variable.forEach((testVariable) => {

// get key details
await checkLogin(coreKitInstance);
const newPubKey = coreKitInstance.getTssPublicKey();
const factorkey = coreKitInstance.getCurrentFactorKey();
const { tssShare: newTssShare } = await coreKitInstance.tKey.getTSSShare(new BN(factorkey.factorKey, "hex"), {
threshold: 0,
});
strictEqual(checkPubKey.x.toString("hex"), newPubKey.x.toString("hex"));
strictEqual(checkPubKey.y.toString("hex"), newPubKey.y.toString("hex"));
strictEqual(checkTssShare.toString("hex"), newTssShare.toString("hex"));
});

await t.test("#able to sign", async function () {
Expand Down

0 comments on commit 1fa38b9

Please sign in to comment.