Skip to content

Commit

Permalink
feat: implements import of private key or mnemonic for eip155
Browse files Browse the repository at this point in the history
  • Loading branch information
ganchoradkov committed Jan 31, 2025
1 parent ad4e4ec commit 045b1bf
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
14 changes: 11 additions & 3 deletions advanced/wallets/react-wallet-v2/src/lib/EIP155Lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { providers, Wallet } from 'ethers'
*/
interface IInitArgs {
mnemonic?: string
privateKey?: string
}
export interface EIP155Wallet {
getMnemonic(): string
Expand All @@ -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() {
Expand Down
18 changes: 15 additions & 3 deletions advanced/wallets/react-wallet-v2/src/pages/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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 (
<Fragment>
<PageHeader title="Settings" />
Expand Down Expand Up @@ -191,8 +197,14 @@ export default function SettingsPage() {
be used elsewhere!
</Text>

<Text h4 css={{ marginTop: '$5', marginBottom: '$5' }}>
<Text h4 css={{ marginTop: '$5', marginBottom: '$5', display: 'flex' }}>
EIP155 Mnemonic
<Button
style={{ marginLeft: 10 }}
onClick={() => onImportEip155Mnemonic(prompt('Enter mnemonic or private key'))}
>
Import
</Button>
</Text>
<Card bordered borderWeight="light" css={{ minHeight: '100px' }}>
<Text css={{ fontFamily: '$mono' }}>{eip155Wallets[eip155Address].getMnemonic()}</Text>
Expand Down
22 changes: 21 additions & 1 deletion advanced/wallets/react-wallet-v2/src/utils/EIP155WalletUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({})
Expand All @@ -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
*/
Expand Down

0 comments on commit 045b1bf

Please sign in to comment.