@@ -12,21 +12,23 @@ import {
1212 parseToken ,
1313 factorKeyCurve ,
1414 makeEthereumSigner ,
15- SigType ,
15+ SIG_TYPE ,
1616} from "@web3auth/mpc-core-kit" ;
17- import Web3 from "web3" ;
17+ import Web3 , { core } from "web3" ;
1818import { CHAIN_NAMESPACES , CustomChainConfig , IProvider } from "@web3auth/base" ;
1919import { EthereumSigningProvider } from "@web3auth/ethereum-mpc-provider" ;
2020import { BN } from "bn.js" ;
2121import { KeyType , Point } from "@tkey/common-types" ;
22- // import { tssLib } from "@toruslabs/tss-dkls-lib";
23- // import{ tssLib } from "@toruslabs/tss-frost-lib";
24- import { tssLib } from "@toruslabs/tss-frost-lib-bip340" ;
22+ import { tssLib as tssLibDkls } from "@toruslabs/tss-dkls-lib" ;
23+ import { tssLib as tssLibFrost } from "@toruslabs/tss-frost-lib" ;
24+ import { tssLib as tssLibFrostBip340 } from "@toruslabs/tss-frost-lib-bip340" ;
2525
2626import "./App.css" ;
2727import jwt , { Algorithm } from "jsonwebtoken" ;
2828import { flow } from "./flow" ;
2929
30+ type TssLib = typeof tssLibDkls | typeof tssLibFrost | typeof tssLibFrostBip340 ;
31+
3032const uiConsole = ( ...args : any [ ] ) : void => {
3133 const el = document . querySelector ( "#console>p" ) ;
3234 if ( el ) {
@@ -48,16 +50,6 @@ const DEFAULT_CHAIN_CONFIG: CustomChainConfig = {
4850 decimals : 18 ,
4951} ;
5052
51- const coreKitInstance = new Web3AuthMPCCoreKit ( {
52- web3AuthClientId : "BPi5PB_UiIZ-cPz1GtV5i1I2iOSOHuimiXBI0e-Oe_u6X3oVAbCiAZOTEBtTXw4tsluTITPqA8zMsfxIKMjiqNQ" ,
53- web3AuthNetwork : selectedNetwork ,
54- uxMode : "redirect" ,
55- manualSync : true ,
56- storage : window . localStorage ,
57- // sessionTime: 3600, // <== can provide variable session time based on user subscribed plan
58- tssLib,
59- useDKG : false ,
60- } ) ;
6153
6254const privateKey = "MEECAQAwEwYHKoZIzj0CAQYIKoZIzj0DAQcEJzAlAgEBBCCD7oLrcKae+jVZPGx52Cb/lKhdKxpXjl9eGNa1MlY57A==" ;
6355const jwtPrivateKey = `-----BEGIN PRIVATE KEY-----\n${ privateKey } \n-----END PRIVATE KEY-----` ;
@@ -99,7 +91,18 @@ function App() {
9991 const [ question , setQuestion ] = useState < string | undefined > ( undefined ) ;
10092 const [ newQuestion , setNewQuestion ] = useState < string | undefined > ( undefined ) ;
10193 const securityQuestion = useMemo ( ( ) => new TssSecurityQuestion ( ) , [ ] ) ;
102-
94+ const [ selectedTssLib , setSelectedTssLib ] = useState < TssLib > ( tssLibDkls ) ;
95+ const [ coreKitInstance , setCoreKitInstance ] = useState < Web3AuthMPCCoreKit > (
96+ new Web3AuthMPCCoreKit ( {
97+ web3AuthClientId : "BPi5PB_UiIZ-cPz1GtV5i1I2iOSOHuimiXBI0e-Oe_u6X3oVAbCiAZOTEBtTXw4tsluTITPqA8zMsfxIKMjiqNQ" ,
98+ web3AuthNetwork : selectedNetwork ,
99+ uxMode : "redirect" ,
100+ manualSync : true ,
101+ storage : window . localStorage ,
102+ tssLib : selectedTssLib ,
103+ useDKG : false ,
104+ } )
105+ ) ;
103106 async function setupProvider ( chainConfig ?: CustomChainConfig ) {
104107 if ( coreKitInstance . keyType !== KeyType . secp256k1 ) {
105108 console . warn ( `Ethereum requires keytype ${ KeyType . secp256k1 } , skipping provider setup` ) ;
@@ -112,44 +115,58 @@ function App() {
112115
113116 // decide whether to rehydrate session
114117 const rehydrate = true ;
115- const initialized = useRef ( false ) ;
116- useEffect ( ( ) => {
117- const init = async ( ) => {
118- // Example config to handle redirect result manually
119- if ( coreKitInstance . status === COREKIT_STATUS . NOT_INITIALIZED ) {
120- await coreKitInstance . init ( { handleRedirectResult : false , rehydrate } ) ;
121- if ( window . location . hash . includes ( "#state" ) ) {
122- await coreKitInstance . handleRedirectResult ( ) ;
123- }
124- }
125- if ( coreKitInstance . status === COREKIT_STATUS . LOGGED_IN ) {
126- await setupProvider ( ) ;
127- }
128118
129- if ( coreKitInstance . status === COREKIT_STATUS . REQUIRED_SHARE ) {
130- uiConsole (
131- "required more shares, please enter your backup/ device factor key, or reset account unrecoverable once reset, please use it with caution]"
132- ) ;
119+
120+ const init = async ( newCoreKitInstance : Web3AuthMPCCoreKit ) => {
121+ // Example config to handle redirect result manually
122+ if ( newCoreKitInstance . status === COREKIT_STATUS . NOT_INITIALIZED ) {
123+ await newCoreKitInstance . init ( { handleRedirectResult : false , rehydrate } ) ;
124+ if ( window . location . hash . includes ( "#state" ) ) {
125+ await newCoreKitInstance . handleRedirectResult ( ) ;
133126 }
127+ }
128+ if ( newCoreKitInstance . status === COREKIT_STATUS . LOGGED_IN ) {
129+ await setupProvider ( ) ;
130+ }
134131
135- console . log ( "coreKitInstance.status" , coreKitInstance . status ) ;
136- setCoreKitStatus ( coreKitInstance . status ) ;
132+ if ( newCoreKitInstance . status === COREKIT_STATUS . REQUIRED_SHARE ) {
133+ uiConsole (
134+ "required more shares, please enter your backup/ device factor key, or reset account unrecoverable once reset, please use it with caution]"
135+ ) ;
136+ }
137137
138- try {
139- let result = securityQuestion . getQuestion ( coreKitInstance ! ) ;
140- setQuestion ( result ) ;
141- uiConsole ( "security question set" ) ;
142- } catch ( e ) {
143- uiConsole ( "security question not set" ) ;
144- }
145- } ;
146- if ( ! initialized . current )
138+ console . log ( "newCoreKitInstance.status" , newCoreKitInstance . status ) ;
139+ setCoreKitStatus ( newCoreKitInstance . status ) ;
140+
141+ try {
142+ let result = securityQuestion . getQuestion ( newCoreKitInstance ! ) ;
143+ setQuestion ( result ) ;
144+ uiConsole ( "security question set" ) ;
145+ } catch ( e ) {
146+ uiConsole ( "security question not set" ) ;
147+ }
148+ } ;
149+
150+
151+ useEffect ( ( ) => {
152+ const instance = new Web3AuthMPCCoreKit ( {
153+ web3AuthClientId : "BPi5PB_UiIZ-cPz1GtV5i1I2iOSOHuimiXBI0e-Oe_u6X3oVAbCiAZOTEBtTXw4tsluTITPqA8zMsfxIKMjiqNQ" ,
154+ web3AuthNetwork : selectedNetwork ,
155+ uxMode : "redirect" ,
156+ manualSync : true ,
157+ storage : window . localStorage ,
158+ tssLib : selectedTssLib ,
159+ useDKG : false ,
160+ } )
161+ setCoreKitInstance (
162+ instance
163+ )
164+ if ( instance . status === COREKIT_STATUS . NOT_INITIALIZED )
147165 {
148- init ( ) ;
149- initialized . current = true ;
166+ init ( instance ) ;
150167 }
151168
152- } , [ ] ) ;
169+ } , [ selectedTssLib ] ) ;
153170
154171 useEffect ( ( ) => {
155172 if ( provider ) {
@@ -367,19 +384,18 @@ function App() {
367384 } ;
368385
369386 const signMessage = async ( ) : Promise < any > => {
370- if ( coreKitInstance . sigType === SigType . ecdsa_secp256k1 ) {
387+ if ( coreKitInstance . sigType === SIG_TYPE . ECDSA_SECP256K1 ) {
371388 if ( ! web3 ) {
372389 uiConsole ( "web3 not initialized yet" ) ;
373390 return ;
374391 }
375392 const fromAddress = ( await web3 . eth . getAccounts ( ) ) [ 0 ] ;
376-
377393 const message = "hello" ;
378394 const signedMessage = await web3 . eth . personal . sign ( message , fromAddress , "" ) ;
379395
380396
381397 uiConsole ( signedMessage ) ;
382- } else if ( coreKitInstance . sigType === SigType . ed25519 || coreKitInstance . sigType === SigType . bip340 ) {
398+ } else if ( coreKitInstance . sigType === SIG_TYPE . ED25519 || coreKitInstance . sigType === SIG_TYPE . BIP340 ) {
383399 const msg = Buffer . from ( "hello signer!" ) ;
384400 const sig = await coreKitInstance . sign ( msg ) ;
385401 uiConsole ( sig . toString ( "hex" ) ) ;
@@ -575,6 +591,32 @@ function App() {
575591 await coreKitInstance . commitChanges ( ) ;
576592 } ;
577593
594+ const tssLibSelector = (
595+ < div className = "flex-container" >
596+ < label > TSS Library:</ label >
597+ < select
598+ value = { selectedTssLib === tssLibDkls ? "dkls" : selectedTssLib === tssLibFrost ? "frost" : "frostBip340" }
599+ onChange = { ( e ) => {
600+ switch ( e . target . value ) {
601+ case "dkls" :
602+ setSelectedTssLib ( tssLibDkls ) ;
603+ break ;
604+ case "frost" :
605+ setSelectedTssLib ( tssLibFrost ) ;
606+ break ;
607+ case "frostBip340" :
608+ setSelectedTssLib ( tssLibFrostBip340 ) ;
609+ break ;
610+ }
611+ } }
612+ >
613+ < option value = "dkls" > DKLS</ option >
614+ < option value = "frost" > FROST</ option >
615+ < option value = "frostBip340" > FROST BIP340</ option >
616+ </ select >
617+ </ div >
618+ ) ;
619+
578620 const loggedInView = (
579621 < >
580622 < h2 className = "subtitle" > Account Details</ h2 >
@@ -733,6 +775,7 @@ function App() {
733775
734776 const unloggedInView = (
735777 < >
778+ { tssLibSelector }
736779 < input value = { mockEmail } onChange = { ( e ) => setMockEmail ( e . target . value ) } > </ input >
737780 < button onClick = { ( ) => loginWithMock ( ) } className = "card" >
738781 MockLogin
0 commit comments