neptune-web-wallet/electron/ipcHandlers.ts

25 lines
874 B
TypeScript
Raw Normal View History

import { ipcMain } from 'electron';
import type { HDNodeWallet } from 'ethers';
import { Wallet } from 'ethers';
import fs from 'fs';
import path from 'path';
ipcMain.handle('wallet:createKeystore', async (_event, seed, password) => {
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 };
});
ipcMain.handle('wallet:decryptKeystore', async (_event, filePath, password) => {
const json = fs.readFileSync(filePath, 'utf-8');
const wallet = await Wallet.fromEncryptedJson(json, password) as HDNodeWallet;
return { address: wallet.address, phrase: wallet.mnemonic };
});