diff --git a/advanced/wallets/react-wallet-v2/src/lib/EIP155Lib.ts b/advanced/wallets/react-wallet-v2/src/lib/EIP155Lib.ts index b4e7488df..09210a05b 100644 --- a/advanced/wallets/react-wallet-v2/src/lib/EIP155Lib.ts +++ b/advanced/wallets/react-wallet-v2/src/lib/EIP155Lib.ts @@ -5,6 +5,7 @@ import { providers, Wallet } from 'ethers' */ interface IInitArgs { mnemonic?: string + privateKey?: string } export interface EIP155Wallet { getMnemonic(): string @@ -26,14 +27,21 @@ export default class EIP155Lib implements EIP155Wallet { this.wallet = wallet } - static init({ mnemonic }: IInitArgs) { - const wallet = mnemonic ? Wallet.fromMnemonic(mnemonic) : Wallet.createRandom() + static init({ mnemonic, privateKey }: IInitArgs) { + let wallet + if (privateKey) { + wallet = new Wallet(privateKey) + } else if (mnemonic) { + wallet = Wallet.fromMnemonic(mnemonic) + } else { + wallet = Wallet.createRandom() + } return new EIP155Lib(wallet) } getMnemonic() { - return this.wallet.mnemonic.phrase + return this.wallet.mnemonic?.phrase || this.getPrivateKey() } getPrivateKey() { diff --git a/advanced/wallets/react-wallet-v2/src/pages/settings.tsx b/advanced/wallets/react-wallet-v2/src/pages/settings.tsx index cbe3efd8d..41416acd5 100644 --- a/advanced/wallets/react-wallet-v2/src/pages/settings.tsx +++ b/advanced/wallets/react-wallet-v2/src/pages/settings.tsx @@ -2,12 +2,12 @@ import PageHeader from '@/components/PageHeader' import RelayRegionPicker from '@/components/RelayRegionPicker' import SettingsStore from '@/store/SettingsStore' import { cosmosWallets } from '@/utils/CosmosWalletUtil' -import { eip155Wallets } from '@/utils/EIP155WalletUtil' +import { eip155Wallets, replaceEip155Mnemonic } from '@/utils/EIP155WalletUtil' import { solanaWallets } from '@/utils/SolanaWalletUtil' import { multiversxWallets } from '@/utils/MultiversxWalletUtil' import { tronWallets } from '@/utils/TronWalletUtil' import { kadenaWallets } from '@/utils/KadenaWalletUtil' -import { Card, Col, Divider, Row, Switch, Text } from '@nextui-org/react' +import { Button, Card, Col, Divider, Row, Switch, Text } from '@nextui-org/react' import { Fragment } from 'react' import { useSnapshot } from 'valtio' import packageJSON from '../../package.json' @@ -32,6 +32,12 @@ export default function SettingsPage() { chainAbstractionEnabled } = useSnapshot(SettingsStore.state) + const onImportEip155Mnemonic = (value: string | null) => { + console.log('Importing EIP155 Mnemonic: ', value) + if (!value) throw new Error('No value provided') + replaceEip155Mnemonic(value.trim()) + } + return ( @@ -191,8 +197,14 @@ export default function SettingsPage() { be used elsewhere! - + EIP155 Mnemonic + {eip155Wallets[eip155Address].getMnemonic()} diff --git a/advanced/wallets/react-wallet-v2/src/utils/EIP155WalletUtil.ts b/advanced/wallets/react-wallet-v2/src/utils/EIP155WalletUtil.ts index 247c0ec84..de475da86 100644 --- a/advanced/wallets/react-wallet-v2/src/utils/EIP155WalletUtil.ts +++ b/advanced/wallets/react-wallet-v2/src/utils/EIP155WalletUtil.ts @@ -18,7 +18,11 @@ export function createOrRestoreEIP155Wallet() { const mnemonic2 = localStorage.getItem('EIP155_MNEMONIC_2') if (mnemonic1 && mnemonic2) { - wallet1 = EIP155Lib.init({ mnemonic: mnemonic1 }) + if (mnemonic1.includes(' ')) { + wallet1 = EIP155Lib.init({ mnemonic: mnemonic1 }) + } else { + wallet1 = EIP155Lib.init({ privateKey: mnemonic1 }) + } wallet2 = EIP155Lib.init({ mnemonic: mnemonic2 }) } else { wallet1 = EIP155Lib.init({}) @@ -44,6 +48,22 @@ export function createOrRestoreEIP155Wallet() { } } +export async function replaceEip155Mnemonic(mnemonicOrPrivateKey: string) { + try { + let wallet + if (mnemonicOrPrivateKey.includes(' ')) { + wallet = EIP155Lib.init({ mnemonic: mnemonicOrPrivateKey }) + } else { + wallet = EIP155Lib.init({ privateKey: mnemonicOrPrivateKey }) + } + localStorage.setItem('EIP155_MNEMONIC_1', wallet.getMnemonic()) + location.reload() + } catch (error) { + console.error('Failed to replace mnemonic: ', error) + throw new Error('Invalid mnemonic or private key') + } +} + /** * Get wallet for the address in params */