@@ -4,34 +4,63 @@ import test from "node:test";
44import { tssLib as tssLibDKLS } from "@toruslabs/tss-dkls-lib" ;
55import { tssLib as tssLibFROST } from "@toruslabs/tss-frost-lib" ;
66
7- import { AsyncStorage , MemoryStorage , TssLibType , TssShareType , WEB3AUTH_NETWORK } from "../src" ;
8- import { bufferToElliptic , criticalResetAccount , newCoreKitLogInInstance } from "./setup" ;
7+ import { MemoryStorage , sigToRSV , TssLibType , TssShareType , WEB3AUTH_NETWORK , Web3AuthMPCCoreKit } from "../src" ;
8+ import { bufferToElliptic , criticalResetAccount , mockLogin } from "./setup" ;
9+ import { EllipticPoint , KeyType , secp256k1 } from "@tkey/common-types" ;
10+ import { keccak256 } from "@toruslabs/metadata-helpers" ;
911
1012type ImportKeyTestVariable = {
1113 manualSync ?: boolean ;
1214 email : string ;
1315 importKeyEmail : string ;
1416 tssLib : TssLibType ;
1517} ;
18+ async function signSecp256k1Data ( params : { coreKitInstance : Web3AuthMPCCoreKit , msg : string , } ) {
19+ const { coreKitInstance, msg } = params
20+ const msgBuffer1 = Buffer . from ( msg ) ;
21+ const msgHash = keccak256 ( msgBuffer1 ) ;
1622
23+ const signature = sigToRSV ( await coreKitInstance . sign ( msgHash , true ) ) ;
24+
25+ const pubkey = secp256k1 . recoverPubKey ( msgHash , signature , signature . v ) as EllipticPoint ;
26+ const publicKeyPoint = bufferToElliptic ( coreKitInstance . getPubKey ( ) ) ;
27+ assert ( pubkey . eq ( publicKeyPoint ) ) ;
28+ }
1729const storageInstance = new MemoryStorage ( ) ;
1830export const ImportTest = async ( testVariable : ImportKeyTestVariable ) => {
1931 async function newCoreKitInstance ( email : string , importTssKey ?: string ) {
20- return newCoreKitLogInInstance ( {
21- network : WEB3AUTH_NETWORK . DEVNET ,
22- manualSync : testVariable . manualSync ,
23- email : email ,
24- storageInstance,
25- tssLib : testVariable . tssLib ,
26- importTssKey,
27- } ) ;
32+ const instance = new Web3AuthMPCCoreKit ( {
33+ web3AuthClientId : "torus-key-test" ,
34+ web3AuthNetwork : WEB3AUTH_NETWORK . DEVNET ,
35+ baseUrl : "http://localhost:3000" ,
36+ uxMode : "nodejs" ,
37+ tssLib : testVariable . tssLib ,
38+ storage : storageInstance ,
39+ manualSync : testVariable . manualSync ,
40+ disableSessionManager : true
41+ } ) ;
42+
43+ const { idToken, parsedToken } = await mockLogin ( email ) ;
44+ await instance . init ( { handleRedirectResult : false , rehydrate : false } ) ;
45+ await instance . loginWithJWT ( {
46+ verifier : "torus-test-health" ,
47+ verifierId : parsedToken . email ,
48+ idToken,
49+ importTssKey
50+ } ) ;
51+ return instance ;
52+
2853 }
2954
3055 async function resetAccount ( email : string ) {
3156 const kit = await newCoreKitInstance ( email ) ;
57+ console . log ( 'tss pub key' , kit . state . tssPubKey )
3258 await criticalResetAccount ( kit ) ;
59+ if ( testVariable . manualSync ) {
60+ await kit . commitChanges ( ) ;
61+ }
3362 await kit . logout ( ) ;
34- await new AsyncStorage ( kit . _storageKey , storageInstance ) . resetStore ( ) ;
63+ // await new AsyncStorage(kit._storageKey, storageInstance).resetStore();
3564 }
3665
3766 test ( `import recover tss key : ${ testVariable . manualSync } ` , async function ( t ) {
@@ -50,6 +79,9 @@ export const ImportTest = async (testVariable: ImportKeyTestVariable) => {
5079 shareType : TssShareType . DEVICE ,
5180 } ) ;
5281
82+ if ( testVariable . tssLib . keyType === KeyType . secp256k1 ) {
83+ await signSecp256k1Data ( { coreKitInstance, msg : "hello world" } ) ;
84+ }
5385 const factorKeyRecovery = await coreKitInstance . createFactor ( {
5486 shareType : TssShareType . RECOVERY ,
5587 } ) ;
@@ -62,8 +94,21 @@ export const ImportTest = async (testVariable: ImportKeyTestVariable) => {
6294 const exportedTssKey1 = await coreKitInstance . _UNSAFE_exportTssKey ( ) ;
6395 await coreKitInstance . logout ( ) ;
6496
97+ const instance = new Web3AuthMPCCoreKit ( {
98+ web3AuthClientId : "torus-key-test" ,
99+ web3AuthNetwork : WEB3AUTH_NETWORK . DEVNET ,
100+ baseUrl : "http://localhost:3000" ,
101+ uxMode : "nodejs" ,
102+ tssLib : testVariable . tssLib ,
103+ storage : storageInstance ,
104+ manualSync : testVariable . manualSync ,
105+ disableSessionManager : true
106+ } ) ;
107+
108+ await instance . init ( { rehydrate : false , handleRedirectResult : false } )
109+
65110 // Recover key from any two factors.
66- const recoveredTssKey = await coreKitInstance . _UNSAFE_recoverTssKey ( [ factorKeyDevice , factorKeyRecovery ] ) ;
111+ const recoveredTssKey = await instance . _UNSAFE_recoverTssKey ( [ factorKeyDevice , factorKeyRecovery ] ) ;
67112 assert . strictEqual ( recoveredTssKey , exportedTssKey1 ) ;
68113
69114 // Initialize new instance and import existing key.
@@ -105,9 +150,9 @@ export const ImportTest = async (testVariable: ImportKeyTestVariable) => {
105150} ;
106151
107152const variable : ImportKeyTestVariable [ ] = [
108- { manualSync : false , email : "emailexport" , importKeyEmail : "emailimport" , tssLib : tssLibDKLS } ,
109- { manualSync : true , email : "emailexport" , importKeyEmail : "emailimport" , tssLib : tssLibDKLS } ,
110- { manualSync : false , email : "emailexport_ed25519" , importKeyEmail : "emailimport_ed25519" , tssLib : tssLibFROST } ,
153+ { manualSync : false , email : "emailexport-01 " , importKeyEmail : "emailimport-001 " , tssLib : tssLibDKLS } ,
154+ { manualSync : true , email : "emailexport-01 " , importKeyEmail : "emailimport-001 " , tssLib : tssLibDKLS } ,
155+ // { manualSync: false, email: "emailexport_ed25519", importKeyEmail: "emailimport_ed25519", tssLib: tssLibFROST },
111156] ;
112157
113158variable . forEach ( async ( testVariable ) => {
0 commit comments