Skip to content

Commit

Permalink
fix: offNetwork recovery
Browse files Browse the repository at this point in the history
  • Loading branch information
ieow committed Jan 24, 2025
1 parent 5947731 commit fbe282e
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ScrollView } from "react-native";

import { OffNetworkRecoveryView } from "@/components/mpc/OffNetworkRecoveryView";
import { useMPCCoreKitStore } from "@/hooks/useMPCCoreKit";

export default function Index() {
const { coreKitInstance, coreKitStatus, setCoreKitStatus } = useMPCCoreKitStore();
return (
<ScrollView>
<OffNetworkRecoveryView coreKitInstance={coreKitInstance} coreKitStatus={coreKitStatus} setCoreKitStatus={setCoreKitStatus} />
</ScrollView>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ScrollView } from "react-native";

import { OffNetworkRecoveryView } from "@/components/mpc/OffNetworkRecoveryView";
import { useMPCCoreKitStore } from "@/hooks/useMPCCoreKit";

export default function Index() {
const { coreKitEd25519Instance, coreKitEd25519Status, setCoreKitEd25519Status } = useMPCCoreKitStore();
return (
<ScrollView>
<OffNetworkRecoveryView
coreKitInstance={coreKitEd25519Instance}
coreKitStatus={coreKitEd25519Status}
setCoreKitStatus={setCoreKitEd25519Status}
/>
</ScrollView>
);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { COREKIT_STATUS, JWTLoginParams, makeEthereumSigner, parseToken } from "@web3auth/mpc-core-kit";
import { router } from "expo-router";
import { useState } from "react";
import { Button, Text, TextInput, View } from "react-native";

Expand Down Expand Up @@ -118,6 +119,10 @@ export const LoginView = () => {
</View>
<Button title="Register/Login with Web3Auth" onPress={login} />
<Button title="Register/Login with ED25519 Web3Auth" onPress={loginEd25519} />
<View>
<Button title="Off Network Recovery Secp256k1" onPress={() => router.replace({ pathname: "/(off-network-recovery)/evm" })} />
<Button title="Off Network Recovery ED25519" onPress={() => router.replace({ pathname: "/(off-network-recovery)/solana" })} />
</View>
</View>
<ConsoleUI consoleUI={consoleUI} loading={loading} />
</View>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export default function MPCAccoutFunction(props: {
shareType: TssShareType.RECOVERY,
factorKey: factorKey.private,
});
uiConsole("Export Factor Key: ", factorKey);
const factorKeyMnemonic = await keyToMnemonic(factorKey.private.toString("hex"));
setLoading(false);

Expand All @@ -54,10 +55,17 @@ export default function MPCAccoutFunction(props: {
// }
};

const getCurrentFactorKey = async () => {
const currentFactor = coreKitInstance.getCurrentFactorKey();
uiConsole("current factor: ", currentFactor);
uiConsole("Device mnemonic: ", keyToMnemonic(currentFactor.factorKey.toString("hex")));
};

const getDeviceFactor = async () => {
try {
const factorKey = await coreKitInstance.getDeviceFactor();
uiConsole("Device share: ", factorKey);
uiConsole("Device mnemonic: ", keyToMnemonic(factorKey));
} catch (e) {
console.log("catch error");
uiConsole(e);
Expand Down Expand Up @@ -133,6 +141,7 @@ export default function MPCAccoutFunction(props: {
<Button title="Enable MFA" onPress={enableMFA} />
<Button title="Generate Backup (Mnemonic) - CreateFactor" onPress={exportMnemonicFactor} />
<Button title="Get node Signatures" onPress={() => getNodeSignatures()} />
<Button title="Get Current Factor" onPress={() => getCurrentFactorKey()} />
<Button title="Get Device Factor" onPress={() => getDeviceFactor()} />
<Button title="store Device Factor" onPress={() => storeDeviceFactor()} />
{/* <Button title="Store Device Factor" onPress={() => storeDeviceFactor()} /> */}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { COREKIT_STATUS, mnemonicToKey } from "@web3auth/react-native-mpc-core-kit";
import { Web3AuthMPCCoreKitRN } from "@web3auth/react-native-mpc-core-kit/dist/mpclib";
import BN from "bn.js";
import { useState } from "react";
import { Button, Text, TextInput, View } from "react-native";

import { useConsoleUI } from "@/hooks/useConsoleUI";

import { ConsoleUI } from "./ConsoleUI";
import { mpcViewStyles as styles } from "./styles";

export const OffNetworkRecoveryView = (props: {
coreKitInstance: Web3AuthMPCCoreKitRN;
coreKitStatus: COREKIT_STATUS;
setCoreKitStatus: (status: COREKIT_STATUS) => void;
}) => {
const { consoleUI, loading, setLoading, uiConsole } = useConsoleUI();
const { coreKitInstance, coreKitStatus } = props;

const [deviceFactor, setDeviceFactor] = useState("");
const [recoveryFactor, setRecoveryFactor] = useState("");

const recoverTss = async () => {
uiConsole("Recovering TSS...");
if (!coreKitInstance) {
throw new Error("coreKitInstance is not set");
}
if (!deviceFactor || !recoveryFactor) {
throw new Error("deviceFactor or recoveryFactor is not set");
}
console.log(deviceFactor, recoveryFactor);
try {
const factorKey = mnemonicToKey(deviceFactor.trim());
const factorKey2 = mnemonicToKey(recoveryFactor.trim());
// console.log("factorKey2: ", factorKey2);
setLoading(true);
const result = await coreKitInstance._UNSAFE_recoverTssKey([factorKey, factorKey2]);
uiConsole("Recovery Result: ", result);
} catch (e) {
console.log(e);
console.log("error");
}
setLoading(false);
};

return (
<>
<View>
<View style={styles.section}>
<Text>Key In Device Factor</Text>
<TextInput style={styles.input} onChangeText={setDeviceFactor} value={deviceFactor} />
<Text>Key In Recovery Factor</Text>
<TextInput style={styles.input} onChangeText={setRecoveryFactor} value={recoveryFactor} />
{coreKitStatus !== COREKIT_STATUS.INITIALIZED && <Text>Corekit is not initialized</Text>}
<Button disabled={coreKitStatus !== COREKIT_STATUS.INITIALIZED} title="Recover TSS" onPress={() => recoverTss()} />
</View>
</View>
<ConsoleUI consoleUI={consoleUI} loading={loading} />
</>
);
};

0 comments on commit fbe282e

Please sign in to comment.