Skip to content

Commit 9aa5da0

Browse files
committed
Add delete factor in rn expo auth0
1 parent b632606 commit 9aa5da0

File tree

4 files changed

+637
-1558
lines changed

4 files changed

+637
-1558
lines changed

mpc-core-kit-react-native/mpc-core-kit-rn-expo-auth0/App.tsx

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import "./globals";
22
import "@ethersproject/shims";
33

4+
import { Point, secp256k1 } from "@tkey/common-types";
45
import { CHAIN_NAMESPACES } from "@web3auth/base";
56
import { EthereumSigningProvider } from "@web3auth/ethereum-mpc-provider";
67
// IMP START - Quick Start
78
import {
89
Bridge,
910
COREKIT_STATUS,
11+
FactorKeyTypeShareDescription,
1012
generateFactorKey,
1113
keyToMnemonic,
1214
makeEthereumSigner,
@@ -20,14 +22,13 @@ import {
2022
// IMP END - Quick Start
2123
import { BN } from "bn.js";
2224
import { ethers } from "ethers";
23-
// IMP END - Auth Provider Login
2425
import * as SecureStore from "expo-secure-store";
2526
import React, { useEffect, useState } from "react";
2627
import { ActivityIndicator, Button, Dimensions, ScrollView, StyleSheet, Text, TextInput, View } from "react-native";
2728
// IMP START - Auth Provider Login
2829
import { Auth0Provider, useAuth0 } from "react-native-auth0";
30+
// IMP END - Auth Provider Login
2931

30-
// IMP START - SDK Initialization
3132
// IMP START - Dashboard Registration
3233
const web3AuthClientId = "BPi5PB_UiIZ-cPz1GtV5i1I2iOSOHuimiXBI0e-Oe_u6X3oVAbCiAZOTEBtTXw4tsluTITPqA8zMsfxIKMjiqNQ"; // get from https://dashboard.web3auth.io
3334
// IMP END - Dashboard Registration
@@ -36,6 +37,7 @@ const web3AuthClientId = "BPi5PB_UiIZ-cPz1GtV5i1I2iOSOHuimiXBI0e-Oe_u6X3oVAbCiAZ
3637
const verifier = "w3a-auth0-demo";
3738
// IMP END - Verifier Creation
3839

40+
// IMP START - Chain Config
3941
const chainConfig = {
4042
chainNamespace: CHAIN_NAMESPACES.EIP155,
4143
chainId: "0xaa36a7", // Please use 0x1 for Mainnet
@@ -46,7 +48,9 @@ const chainConfig = {
4648
ticker: "ETH",
4749
tickerName: "Ethereum",
4850
};
51+
// IMP END - Chain Config
4952

53+
// IMP START - SDK Initialization
5054
// setup async storage for react native
5155
const asyncStorageKey = {
5256
getItem: async (key: string) => {
@@ -94,14 +98,13 @@ function Home() {
9498
};
9599
init();
96100
}
97-
// eslint-disable-next-line react-hooks/exhaustive-deps
98101
}, [bridgeReady]);
99102

100103
const { authorize, getCredentials } = useAuth0();
101104

105+
// IMP START - Auth Provider Login
102106
const signInWithAuth0 = async () => {
103107
try {
104-
// @ts-ignore
105108
await authorize(
106109
{
107110
scope: "openid profile email",
@@ -122,6 +125,7 @@ function Home() {
122125
console.error(error);
123126
}
124127
};
128+
// IMP END - Auth Provider Login
125129

126130
const uiConsole = (...args: any) => {
127131
setConsoleUI(`${JSON.stringify(args || {}, null, 2)}\n\n\n\n${consoleUI}`);
@@ -140,7 +144,6 @@ function Home() {
140144
// IMP END - Auth Provider Login
141145
uiConsole("idToken", idToken);
142146

143-
// IMP START - Login
144147
uiConsole("idToken", idToken);
145148

146149
if (!idToken) {
@@ -149,6 +152,7 @@ function Home() {
149152

150153
const parsedToken = parseToken(idToken);
151154

155+
// IMP START - Login
152156
const idTokenLoginParams = {
153157
verifier,
154158
verifierId: parsedToken.sub,
@@ -206,8 +210,10 @@ function Home() {
206210
try {
207211
setConsoleUI("Enabling MFA, please wait");
208212

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"));
211217

212218
uiConsole("MFA enabled, device factor stored in local store, deleted hashed cloud key, your backup factor key: ", factorKeyMnemonic);
213219
} catch (error: any) {
@@ -238,6 +244,7 @@ function Home() {
238244
}
239245
};
240246

247+
// IMP START - Store Device Factor
241248
const storeDeviceFactor = async () => {
242249
try {
243250
if (!coreKitInstance) {
@@ -255,26 +262,30 @@ function Home() {
255262
uiConsole(error.message);
256263
}
257264
};
265+
// IMP END - Store Device Factor
258266

259-
const exportMnemonicFactor = async (): Promise<void> => {
267+
// IMP START - Export Mnemonic Factor
268+
const createMnemonicFactor = async (): Promise<void> => {
260269
if (!coreKitInstance) {
261270
throw new Error("coreKitInstance is not set");
262271
}
263272
setLoading(true);
264-
uiConsole("export share type: ", TssShareType.RECOVERY);
273+
uiConsole("share type: ", TssShareType.RECOVERY);
265274
const factorKey = generateFactorKey();
266275
await coreKitInstance.createFactor({
267276
shareType: TssShareType.RECOVERY,
268277
factorKey: factorKey.private,
278+
shareDescription: FactorKeyTypeShareDescription.SeedPhrase,
269279
});
270280
const factorKeyMnemonic = await keyToMnemonic(factorKey.private.toString("hex"));
271281
setLoading(false);
272282

273-
uiConsole("Export factor key mnemonic: ", factorKeyMnemonic);
283+
uiConsole("New factor key mnemonic: ", factorKeyMnemonic);
274284
if (coreKitInstance.status === COREKIT_STATUS.LOGGED_IN) {
275285
await coreKitInstance.commitChanges();
276286
}
277287
};
288+
// IMP END - Export Mnemonic Factor
278289

279290
const MnemonicToFactorKeyHex = async (mnemonic: string) => {
280291
if (!coreKitInstance) {
@@ -289,6 +300,29 @@ function Home() {
289300
}
290301
};
291302

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+
292326
const getUserInfo = async () => {
293327
// IMP START - Get User Information
294328
const user = coreKitInstance.getUserInfo();
@@ -304,11 +338,6 @@ function Home() {
304338
setCoreKitStatus(coreKitInstance.status);
305339
// Log out from Auth0
306340
setLoading(false);
307-
try {
308-
uiConsole("logged out from auth0");
309-
} catch (error: any) {
310-
uiConsole(error.message);
311-
}
312341
uiConsole("logged out from web3auth");
313342
};
314343

@@ -389,7 +418,6 @@ function Home() {
389418
setLoading(false);
390419
uiConsole(signedMessage);
391420
};
392-
// IMP END - Blockchain Calls
393421

394422
const sendTransaction = async () => {
395423
if (!coreKitInstance) {
@@ -419,6 +447,7 @@ function Home() {
419447
setLoading(false);
420448
uiConsole(receipt);
421449
};
450+
// IMP END - Blockchain Calls
422451

423452
const criticalResetAccount = async (): Promise<void> => {
424453
// This is a critical function that should only be used for testing purposes
@@ -428,10 +457,7 @@ function Home() {
428457
throw new Error("coreKitInstance is not set");
429458
}
430459
setLoading(true);
431-
// @ts-ignore
432-
// if (selectedNetwork === WEB3AUTH_NETWORK.MAINNET) {
433-
// throw new Error("reset account is not recommended on mainnet");
434-
// }
460+
435461
await coreKitInstance._UNSAFE_resetAccount();
436462
uiConsole("reset");
437463
if (coreKitInstance.status === COREKIT_STATUS.LOGGED_IN) {
@@ -453,9 +479,10 @@ function Home() {
453479
<Button title="Send Transaction" onPress={sendTransaction} />
454480
<Button title="Log Out" onPress={logout} />
455481
<Button title="Enable MFA" onPress={enableMFA} />
456-
<Button title="Generate Backup (Mnemonic) - CreateFactor" onPress={exportMnemonicFactor} />
482+
<Button title="Generate Backup (Mnemonic) - CreateFactor" onPress={createMnemonicFactor} />
457483
<Button title="Get Device Factor" onPress={() => getDeviceFactor()} />
458484
<Button title="Store Device Factor" onPress={() => storeDeviceFactor()} />
485+
<Button title="Delete Mnemonic Factor" onPress={() => deleteFactor()} />
459486
<Button title="[CRITICAL] Reset Account" onPress={criticalResetAccount} />
460487
</View>
461488
);

0 commit comments

Comments
 (0)