1
1
import "./globals" ;
2
2
import "@ethersproject/shims" ;
3
3
4
+ import { Point , secp256k1 } from "@tkey/common-types" ;
4
5
import { CHAIN_NAMESPACES } from "@web3auth/base" ;
5
6
import { EthereumSigningProvider } from "@web3auth/ethereum-mpc-provider" ;
6
7
// IMP START - Quick Start
7
8
import {
8
9
Bridge ,
9
10
COREKIT_STATUS ,
11
+ FactorKeyTypeShareDescription ,
10
12
generateFactorKey ,
11
13
keyToMnemonic ,
12
14
makeEthereumSigner ,
@@ -20,14 +22,13 @@ import {
20
22
// IMP END - Quick Start
21
23
import { BN } from "bn.js" ;
22
24
import { ethers } from "ethers" ;
23
- // IMP END - Auth Provider Login
24
25
import * as SecureStore from "expo-secure-store" ;
25
26
import React , { useEffect , useState } from "react" ;
26
27
import { ActivityIndicator , Button , Dimensions , ScrollView , StyleSheet , Text , TextInput , View } from "react-native" ;
27
28
// IMP START - Auth Provider Login
28
29
import { Auth0Provider , useAuth0 } from "react-native-auth0" ;
30
+ // IMP END - Auth Provider Login
29
31
30
- // IMP START - SDK Initialization
31
32
// IMP START - Dashboard Registration
32
33
const web3AuthClientId = "BPi5PB_UiIZ-cPz1GtV5i1I2iOSOHuimiXBI0e-Oe_u6X3oVAbCiAZOTEBtTXw4tsluTITPqA8zMsfxIKMjiqNQ" ; // get from https://dashboard.web3auth.io
33
34
// IMP END - Dashboard Registration
@@ -36,6 +37,7 @@ const web3AuthClientId = "BPi5PB_UiIZ-cPz1GtV5i1I2iOSOHuimiXBI0e-Oe_u6X3oVAbCiAZ
36
37
const verifier = "w3a-auth0-demo" ;
37
38
// IMP END - Verifier Creation
38
39
40
+ // IMP START - Chain Config
39
41
const chainConfig = {
40
42
chainNamespace : CHAIN_NAMESPACES . EIP155 ,
41
43
chainId : "0xaa36a7" , // Please use 0x1 for Mainnet
@@ -46,7 +48,9 @@ const chainConfig = {
46
48
ticker : "ETH" ,
47
49
tickerName : "Ethereum" ,
48
50
} ;
51
+ // IMP END - Chain Config
49
52
53
+ // IMP START - SDK Initialization
50
54
// setup async storage for react native
51
55
const asyncStorageKey = {
52
56
getItem : async ( key : string ) => {
@@ -94,14 +98,13 @@ function Home() {
94
98
} ;
95
99
init ( ) ;
96
100
}
97
- // eslint-disable-next-line react-hooks/exhaustive-deps
98
101
} , [ bridgeReady ] ) ;
99
102
100
103
const { authorize, getCredentials } = useAuth0 ( ) ;
101
104
105
+ // IMP START - Auth Provider Login
102
106
const signInWithAuth0 = async ( ) => {
103
107
try {
104
- // @ts -ignore
105
108
await authorize (
106
109
{
107
110
scope : "openid profile email" ,
@@ -122,6 +125,7 @@ function Home() {
122
125
console . error ( error ) ;
123
126
}
124
127
} ;
128
+ // IMP END - Auth Provider Login
125
129
126
130
const uiConsole = ( ...args : any ) => {
127
131
setConsoleUI ( `${ JSON . stringify ( args || { } , null , 2 ) } \n\n\n\n${ consoleUI } ` ) ;
@@ -140,7 +144,6 @@ function Home() {
140
144
// IMP END - Auth Provider Login
141
145
uiConsole ( "idToken" , idToken ) ;
142
146
143
- // IMP START - Login
144
147
uiConsole ( "idToken" , idToken ) ;
145
148
146
149
if ( ! idToken ) {
@@ -149,6 +152,7 @@ function Home() {
149
152
150
153
const parsedToken = parseToken ( idToken ) ;
151
154
155
+ // IMP START - Login
152
156
const idTokenLoginParams = {
153
157
verifier,
154
158
verifierId : parsedToken . sub ,
@@ -206,8 +210,10 @@ function Home() {
206
210
try {
207
211
setConsoleUI ( "Enabling MFA, please wait" ) ;
208
212
209
- const factorKey = await coreKitInstance . enableMFA ( { } , false ) ;
210
- const factorKeyMnemonic = keyToMnemonic ( factorKey ) ;
213
+ const factorKey = generateFactorKey ( ) ;
214
+
215
+ await coreKitInstance . enableMFA ( { factorKey : factorKey . private , shareDescription : FactorKeyTypeShareDescription . SeedPhrase } ) ;
216
+ const factorKeyMnemonic = keyToMnemonic ( factorKey . private . toString ( "hex" ) ) ;
211
217
212
218
uiConsole ( "MFA enabled, device factor stored in local store, deleted hashed cloud key, your backup factor key: " , factorKeyMnemonic ) ;
213
219
} catch ( error : any ) {
@@ -238,6 +244,7 @@ function Home() {
238
244
}
239
245
} ;
240
246
247
+ // IMP START - Store Device Factor
241
248
const storeDeviceFactor = async ( ) => {
242
249
try {
243
250
if ( ! coreKitInstance ) {
@@ -255,26 +262,30 @@ function Home() {
255
262
uiConsole ( error . message ) ;
256
263
}
257
264
} ;
265
+ // IMP END - Store Device Factor
258
266
259
- const exportMnemonicFactor = async ( ) : Promise < void > => {
267
+ // IMP START - Export Mnemonic Factor
268
+ const createMnemonicFactor = async ( ) : Promise < void > => {
260
269
if ( ! coreKitInstance ) {
261
270
throw new Error ( "coreKitInstance is not set" ) ;
262
271
}
263
272
setLoading ( true ) ;
264
- uiConsole ( "export share type: " , TssShareType . RECOVERY ) ;
273
+ uiConsole ( "share type: " , TssShareType . RECOVERY ) ;
265
274
const factorKey = generateFactorKey ( ) ;
266
275
await coreKitInstance . createFactor ( {
267
276
shareType : TssShareType . RECOVERY ,
268
277
factorKey : factorKey . private ,
278
+ shareDescription : FactorKeyTypeShareDescription . SeedPhrase ,
269
279
} ) ;
270
280
const factorKeyMnemonic = await keyToMnemonic ( factorKey . private . toString ( "hex" ) ) ;
271
281
setLoading ( false ) ;
272
282
273
- uiConsole ( "Export factor key mnemonic: " , factorKeyMnemonic ) ;
283
+ uiConsole ( "New factor key mnemonic: " , factorKeyMnemonic ) ;
274
284
if ( coreKitInstance . status === COREKIT_STATUS . LOGGED_IN ) {
275
285
await coreKitInstance . commitChanges ( ) ;
276
286
}
277
287
} ;
288
+ // IMP END - Export Mnemonic Factor
278
289
279
290
const MnemonicToFactorKeyHex = async ( mnemonic : string ) => {
280
291
if ( ! coreKitInstance ) {
@@ -289,6 +300,29 @@ function Home() {
289
300
}
290
301
} ;
291
302
303
+ // IMP START - Delete Factor
304
+ const deleteFactor = async ( ) => {
305
+ let factorPub : string | undefined ;
306
+ for ( const [ key , value ] of Object . entries ( ( await coreKitInstance . getKeyDetails ( ) ) . shareDescriptions ) ) {
307
+ if ( value . length > 0 ) {
308
+ const parsedData = JSON . parse ( value [ 0 ] ) ;
309
+ if ( parsedData . module === FactorKeyTypeShareDescription . SeedPhrase ) {
310
+ factorPub = key ;
311
+ }
312
+ }
313
+ }
314
+ if ( factorPub ) {
315
+ uiConsole ( "Deleting Mnemonic Factor, please wait..." , "Factor Pub:" , factorPub ) ;
316
+ const pub = Point . fromSEC1 ( secp256k1 , factorPub ) ;
317
+ await coreKitInstance . deleteFactor ( pub ) ;
318
+ await coreKitInstance . commitChanges ( ) ;
319
+ uiConsole ( "Mnemonic Factor deleted" ) ;
320
+ } else {
321
+ uiConsole ( "No Mnemonic factor found to delete" ) ;
322
+ }
323
+ } ;
324
+ // IMP END - Delete Factor
325
+
292
326
const getUserInfo = async ( ) => {
293
327
// IMP START - Get User Information
294
328
const user = coreKitInstance . getUserInfo ( ) ;
@@ -304,11 +338,6 @@ function Home() {
304
338
setCoreKitStatus ( coreKitInstance . status ) ;
305
339
// Log out from Auth0
306
340
setLoading ( false ) ;
307
- try {
308
- uiConsole ( "logged out from auth0" ) ;
309
- } catch ( error : any ) {
310
- uiConsole ( error . message ) ;
311
- }
312
341
uiConsole ( "logged out from web3auth" ) ;
313
342
} ;
314
343
@@ -389,7 +418,6 @@ function Home() {
389
418
setLoading ( false ) ;
390
419
uiConsole ( signedMessage ) ;
391
420
} ;
392
- // IMP END - Blockchain Calls
393
421
394
422
const sendTransaction = async ( ) => {
395
423
if ( ! coreKitInstance ) {
@@ -419,6 +447,7 @@ function Home() {
419
447
setLoading ( false ) ;
420
448
uiConsole ( receipt ) ;
421
449
} ;
450
+ // IMP END - Blockchain Calls
422
451
423
452
const criticalResetAccount = async ( ) : Promise < void > => {
424
453
// This is a critical function that should only be used for testing purposes
@@ -428,10 +457,7 @@ function Home() {
428
457
throw new Error ( "coreKitInstance is not set" ) ;
429
458
}
430
459
setLoading ( true ) ;
431
- // @ts -ignore
432
- // if (selectedNetwork === WEB3AUTH_NETWORK.MAINNET) {
433
- // throw new Error("reset account is not recommended on mainnet");
434
- // }
460
+
435
461
await coreKitInstance . _UNSAFE_resetAccount ( ) ;
436
462
uiConsole ( "reset" ) ;
437
463
if ( coreKitInstance . status === COREKIT_STATUS . LOGGED_IN ) {
@@ -453,9 +479,10 @@ function Home() {
453
479
< Button title = "Send Transaction" onPress = { sendTransaction } />
454
480
< Button title = "Log Out" onPress = { logout } />
455
481
< Button title = "Enable MFA" onPress = { enableMFA } />
456
- < Button title = "Generate Backup (Mnemonic) - CreateFactor" onPress = { exportMnemonicFactor } />
482
+ < Button title = "Generate Backup (Mnemonic) - CreateFactor" onPress = { createMnemonicFactor } />
457
483
< Button title = "Get Device Factor" onPress = { ( ) => getDeviceFactor ( ) } />
458
484
< Button title = "Store Device Factor" onPress = { ( ) => storeDeviceFactor ( ) } />
485
+ < Button title = "Delete Mnemonic Factor" onPress = { ( ) => deleteFactor ( ) } />
459
486
< Button title = "[CRITICAL] Reset Account" onPress = { criticalResetAccount } />
460
487
</ View >
461
488
) ;
0 commit comments