|
| 1 | +import assert from "node:assert"; |
| 2 | +import test from "node:test"; |
| 3 | + |
| 4 | +import { EllipticPoint } from "@tkey/common-types"; |
| 5 | +import { UX_MODE_TYPE } from "@toruslabs/customauth"; |
| 6 | +import { tssLib } from "@toruslabs/tss-frost-lib-bip340"; |
| 7 | +import BN from "bn.js"; |
| 8 | +import { schnorr as bip340 } from '@noble/curves/secp256k1'; |
| 9 | + |
| 10 | +import { AsyncStorage, COREKIT_STATUS, MemoryStorage, WEB3AUTH_NETWORK, WEB3AUTH_NETWORK_TYPE, Web3AuthMPCCoreKit } from "../src"; |
| 11 | +import { bufferToElliptic, criticalResetAccount, mockLogin, mockLogin2 } from "./setup"; |
| 12 | + |
| 13 | +type TestVariable = { |
| 14 | + web3AuthNetwork: WEB3AUTH_NETWORK_TYPE; |
| 15 | + uxMode: UX_MODE_TYPE | "nodejs"; |
| 16 | + manualSync?: boolean; |
| 17 | + email: string; |
| 18 | +}; |
| 19 | + |
| 20 | +const defaultTestEmail = "testEmailForLoginBip340"; |
| 21 | +const variable: TestVariable[] = [ |
| 22 | + { web3AuthNetwork: WEB3AUTH_NETWORK.DEVNET, uxMode: "nodejs", email: defaultTestEmail }, |
| 23 | + // { web3AuthNetwork: WEB3AUTH_NETWORK.MAINNET, uxMode: UX_MODE.REDIRECT, email: defaultTestEmail }, |
| 24 | + |
| 25 | + { web3AuthNetwork: WEB3AUTH_NETWORK.DEVNET, uxMode: "nodejs", manualSync: true, email: defaultTestEmail }, |
| 26 | + // { web3AuthNetwork: WEB3AUTH_NETWORK.MAINNET, uxMode: UX_MODE.REDIRECT, manualSync: true, email: defaultTestEmail }, |
| 27 | +]; |
| 28 | + |
| 29 | +const checkLogin = async (coreKitInstance: Web3AuthMPCCoreKit, accountIndex = 0) => { |
| 30 | + const keyDetails = coreKitInstance.getKeyDetails(); |
| 31 | + assert.strictEqual(coreKitInstance.status, COREKIT_STATUS.LOGGED_IN); |
| 32 | + assert.strictEqual(keyDetails.requiredFactors, 0); |
| 33 | + const factorkey = coreKitInstance.getCurrentFactorKey(); |
| 34 | + await coreKitInstance.tKey.getTSSShare(new BN(factorkey.factorKey, "hex"), { |
| 35 | + accountIndex, |
| 36 | + }); |
| 37 | +}; |
| 38 | + |
| 39 | +const storageInstance = new MemoryStorage(); |
| 40 | + |
| 41 | +variable.forEach((testVariable) => { |
| 42 | + const { web3AuthNetwork, uxMode, manualSync, email } = testVariable; |
| 43 | + const newCoreKitInstance = () => |
| 44 | + new Web3AuthMPCCoreKit({ |
| 45 | + web3AuthClientId: "torus-key-test", |
| 46 | + web3AuthNetwork, |
| 47 | + baseUrl: "http://localhost:3000", |
| 48 | + uxMode, |
| 49 | + tssLib, |
| 50 | + storage: storageInstance, |
| 51 | + manualSync, |
| 52 | + }); |
| 53 | + |
| 54 | + async function resetAccount() { |
| 55 | + const resetInstance = newCoreKitInstance(); |
| 56 | + const { idToken, parsedToken } = await mockLogin(email); |
| 57 | + await resetInstance.init({ handleRedirectResult: false, rehydrate: false }); |
| 58 | + await resetInstance.loginWithJWT({ |
| 59 | + verifier: "torus-test-health", |
| 60 | + verifierId: parsedToken.email, |
| 61 | + idToken, |
| 62 | + }); |
| 63 | + await criticalResetAccount(resetInstance); |
| 64 | + await new AsyncStorage(resetInstance._storageKey, storageInstance).resetStore(); |
| 65 | + } |
| 66 | + |
| 67 | + const testNameSuffix = JSON.stringify(testVariable); |
| 68 | + |
| 69 | + let checkPubKey: EllipticPoint; |
| 70 | + let checkTssShare: BN; |
| 71 | + |
| 72 | + test(`#Login Test with JWT + logout: ${testNameSuffix}`, async (t) => { |
| 73 | + await resetAccount(); |
| 74 | + await t.test("#Login", async function () { |
| 75 | + const coreKitInstance = newCoreKitInstance(); |
| 76 | + |
| 77 | + // mocklogin |
| 78 | + const { idToken, parsedToken } = await mockLogin(email); |
| 79 | + await coreKitInstance.init({ handleRedirectResult: false }); |
| 80 | + await coreKitInstance.loginWithJWT({ |
| 81 | + verifier: "torus-test-health", |
| 82 | + verifierId: parsedToken.email, |
| 83 | + idToken, |
| 84 | + }); |
| 85 | + // get key details |
| 86 | + await checkLogin(coreKitInstance); |
| 87 | + |
| 88 | + checkPubKey = bufferToElliptic(coreKitInstance.getPubKey(), coreKitInstance.tKey.tssCurve); |
| 89 | + const factorkey = coreKitInstance.getCurrentFactorKey(); |
| 90 | + const { tssShare } = await coreKitInstance.tKey.getTSSShare(new BN(factorkey.factorKey, "hex"), { |
| 91 | + threshold: 0, |
| 92 | + }); |
| 93 | + checkTssShare = tssShare; |
| 94 | + |
| 95 | + if (manualSync) { |
| 96 | + await coreKitInstance.commitChanges(); |
| 97 | + } |
| 98 | + // check whether the public key and tss share is same as old sdks |
| 99 | + }); |
| 100 | + |
| 101 | + await t.test("#relogin ", async function () { |
| 102 | + const coreKitInstance = newCoreKitInstance(); |
| 103 | + // rehydrate |
| 104 | + await coreKitInstance.init({ handleRedirectResult: false }); |
| 105 | + await checkLogin(coreKitInstance); |
| 106 | + |
| 107 | + // logout |
| 108 | + await coreKitInstance.logout(); |
| 109 | + |
| 110 | + // rehydrate should fail |
| 111 | + await coreKitInstance.init({ |
| 112 | + rehydrate: false, |
| 113 | + handleRedirectResult: false, |
| 114 | + }); |
| 115 | + assert.strictEqual(coreKitInstance.status, COREKIT_STATUS.INITIALIZED); |
| 116 | + assert.throws(() => coreKitInstance.getCurrentFactorKey()); |
| 117 | + |
| 118 | + // relogin |
| 119 | + const { idToken, parsedToken } = await mockLogin(email); |
| 120 | + await coreKitInstance.loginWithJWT({ |
| 121 | + verifier: "torus-test-health", |
| 122 | + verifierId: parsedToken.email, |
| 123 | + idToken, |
| 124 | + }); |
| 125 | + |
| 126 | + // get key details |
| 127 | + await checkLogin(coreKitInstance); |
| 128 | + const newPubKey = bufferToElliptic(coreKitInstance.getPubKey(), coreKitInstance.tKey.tssCurve); |
| 129 | + const factorkey = coreKitInstance.getCurrentFactorKey(); |
| 130 | + const { tssShare: newTssShare } = await coreKitInstance.tKey.getTSSShare(new BN(factorkey.factorKey, "hex")); |
| 131 | + assert(checkPubKey.eq(newPubKey)); |
| 132 | + assert(checkTssShare.eq(newTssShare)); |
| 133 | + }); |
| 134 | + |
| 135 | + await t.test("#able to sign", async function () { |
| 136 | + const coreKitInstance = newCoreKitInstance(); |
| 137 | + await coreKitInstance.init({ handleRedirectResult: false, rehydrate: false }); |
| 138 | + const localToken = await mockLogin2(email); |
| 139 | + await coreKitInstance.loginWithJWT({ |
| 140 | + verifier: "torus-test-health", |
| 141 | + verifierId: email, |
| 142 | + idToken: localToken.idToken, |
| 143 | + }); |
| 144 | + const msg = "hello world"; |
| 145 | + const msgBuffer = Buffer.from(msg); |
| 146 | + |
| 147 | + const signature = await coreKitInstance.sign(msgBuffer); |
| 148 | + const pk = coreKitInstance.getPubKeyBip340(); |
| 149 | + const valid = bip340.verify(signature, msgBuffer, pk); |
| 150 | + assert(valid); |
| 151 | + }); |
| 152 | + }); |
| 153 | +}); |
0 commit comments