Skip to content

Commit

Permalink
Merge pull request #15 from PoCInnovation/fix-wallet-creation
Browse files Browse the repository at this point in the history
Fix wallet creation
  • Loading branch information
gregorsternat authored Sep 30, 2024
2 parents 3f3aa65 + 1030505 commit e94ce23
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
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

0 comments on commit e94ce23

Please sign in to comment.