Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wallet creation #15

Merged
merged 2 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/components/wallet/verticalNavbar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default defineComponent({
methods: {
...mapActions(['addAccount', 'selectAccount', 'initializeAccountsFromLocalStorage']),
addAccountToStore() {
const { newWallet, mnemonic, privateKey } = generateWallet(this.walletCounter)
const { newWallet, mnemonic, privateKey } = generateWallet(this.walletCounter, this.password)
const newAccount: Account = {
name: `Account ${this.walletCounter + 1}`,
address: newWallet.account.address,
Expand All @@ -61,7 +61,9 @@ export default defineComponent({
}
this.mnemonic = mnemonic
this.addAccount(newAccount)
this.isMnemonicVisible = true
if (this.walletCounter === 1) {
this.isMnemonicVisible = true
}
localStorage.setItem(
`privateKeyAccount${this.walletCounter}`,
encryption(privateKey, this.password)
Expand All @@ -80,7 +82,7 @@ export default defineComponent({
closeImportPopUp(mnemonicWords: string[]) {
if (mnemonicWords && mnemonicWords.every((word) => word !== '')) {
console.log('Importing wallet...')
const { newWallet, privateKey } = generateWalletFromMnemonic(mnemonicWords)
const { newWallet, privateKey } = generateWalletFromMnemonic(mnemonicWords, this.password)
if (newWallet === null) {
this.isImportVisible = false
return
Expand Down
19 changes: 16 additions & 3 deletions src/utils/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { mnemonicToSeedSync } from '@scure/bip39'
import { createWalletClient, http, type WalletClient } from 'viem'
import { privateKeyToAccount, generateMnemonic, english } from 'viem/accounts'
import { mainnet } from 'viem/chains'
import { decryption, encryption } from '@/utils/crypto'

function uint8ToHexString(uint8Array: Uint8Array | null): `0x${string}` {
if (uint8Array === null) return `0x` as `0x${string}`
Expand All @@ -12,12 +13,20 @@ function uint8ToHexString(uint8Array: Uint8Array | null): `0x${string}` {
return `0x${hexString}` as `0x${string}`
}

export function generateWallet(walletNumber: number): {
export function generateWallet(walletNumber: number, password: string): {
newWallet: WalletClient
mnemonic: string
privateKey: string
} {
const mnemonic = generateMnemonic(english)
const storedMnemonic = localStorage.getItem('storedMnemonic')
let mnemonic: string
if (!storedMnemonic) {
const mnemonic = generateMnemonic(english)
const encryptedMnemonic = encryption(mnemonic, password)
localStorage.setItem('storedMnemonic', encryptedMnemonic)
} else {
mnemonic = decryption(storedMnemonic, password)
}
const seed = mnemonicToSeedSync(mnemonic)
const masterKey = HDKey.fromMasterSeed(seed)
const privateKeyUint8 = masterKey.derive(`m/44'/60'/0'/0/${walletNumber}`).privateKey
Expand All @@ -41,11 +50,15 @@ export function generateWalletFromPrivateKey(privateKey: `0x${string}`): WalletC
})
}

export function generateWalletFromMnemonic(mnemonic: string[]): {
export function generateWalletFromMnemonic(mnemonic: string[], password: string): {
newWallet: WalletClient | null
privateKey: string | null
} {
try {
const checkStoredMnemonic = localStorage.getItem('storedMnemonic')
if (!checkStoredMnemonic) {
localStorage.setItem('storedMnemonic', encryption(mnemonic.join(' '), password))
}
const seed = mnemonicToSeedSync(mnemonic.join(' '))
const masterKey = HDKey.fromMasterSeed(seed)
const privateKeyUint8 = masterKey.derive(`m/44'/60'/0'/0/0`).privateKey
Expand Down
Loading