57 lines
2.0 KiB
TypeScript
57 lines
2.0 KiB
TypeScript
import { ipcMain } from 'electron'
|
|
import { Wallet } from 'ethers'
|
|
import fs from 'fs'
|
|
import path from 'path'
|
|
|
|
ipcMain.handle('wallet:createKeystore', async (_event, seed, password) => {
|
|
try {
|
|
const wallet = Wallet.fromPhrase(seed)
|
|
const keystore = await wallet.encrypt(password)
|
|
|
|
const savePath = path.join(process.cwd(), 'wallets')
|
|
fs.mkdirSync(savePath, { recursive: true })
|
|
|
|
const filePath = path.join(savePath, `${wallet.address}.json`)
|
|
fs.writeFileSync(filePath, keystore)
|
|
|
|
return { address: wallet.address, filePath, error: null }
|
|
} catch (error) {
|
|
console.error('Error creating keystore:', error)
|
|
return { address: null, filePath: null, error: String(error) }
|
|
}
|
|
})
|
|
|
|
ipcMain.handle('wallet:decryptKeystore', async (_event, filePath, password) => {
|
|
try {
|
|
const json = fs.readFileSync(filePath, 'utf-8')
|
|
const wallet = await Wallet.fromEncryptedJson(json, password)
|
|
|
|
let phrase: string | undefined
|
|
if ('mnemonic' in wallet && wallet.mnemonic) {
|
|
phrase = wallet.mnemonic.phrase
|
|
}
|
|
|
|
return { address: wallet.address, phrase, error: null }
|
|
} catch (error) {
|
|
console.error('Error decrypting keystore ipc:', error)
|
|
return { address: null, phrase: null, error: String(error) }
|
|
}
|
|
})
|
|
|
|
ipcMain.handle('wallet:checkKeystore', async () => {
|
|
try {
|
|
const walletDir = path.join(process.cwd(), 'wallets')
|
|
if (!fs.existsSync(walletDir))
|
|
return { exists: false, filePath: null, error: 'Wallet directory not found' }
|
|
|
|
const file = fs.readdirSync(walletDir).find((f) => f.endsWith('.json'))
|
|
if (!file) return { exists: false, filePath: null, error: 'Keystore file not found' }
|
|
|
|
const filePath = path.join(walletDir, file)
|
|
return { exists: true, filePath, error: null }
|
|
} catch (error) {
|
|
console.error('Error checking keystore:', error)
|
|
return { exists: false, filePath: null, error: String(error) }
|
|
}
|
|
})
|