-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
reset on start of Login test
- Loading branch information
Showing
2 changed files
with
161 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters