UNPKG

deth

Version:

Ethereum node focused on Developer Experience

53 lines (52 loc) 1.83 kB
import { Wallet } from 'ethers'; import { makeAddress } from './primitives'; import debug from 'debug'; const d = debug('deth:WalletManager'); /** * Manager for EthersJS Wallets * Ethersjs wallets can be connect to a provider, then convenience functions like getBalance work * This will auto connect wallets to a provider if it was specified in a constructor */ export class WalletManager { constructor(privateKeys, defaultProvider) { this.defaultProvider = defaultProvider; this.wallets = new Map(); if (privateKeys) { privateKeys.forEach(pk => this.addFromPrivateKey(pk)); } } addFromPrivateKey(privateKey) { const wallet = new Wallet(privateKey); this.addWallet(wallet); } addFromMnemonic(mnemonic, dp) { const wallet = Wallet.fromMnemonic(mnemonic, dp); this.addWallet(wallet); } addWallet(wallet) { const address = makeAddress(wallet.address); if (this.wallets.has(address)) { d(`${address} already added... skipping`); } const finalWallet = this.defaultProvider ? wallet.connect(this.defaultProvider) : wallet; this.wallets.set(address, finalWallet); return finalWallet; } getWalletForAddress(address) { return this.wallets.get(address); } createEmptyWallet() { const wallet = Wallet.createRandom(); return this.addWallet(wallet); } /** * Do not track this wallet in wallets map so it won't be possible find it's private key (sign any message) */ createEmptyUntrackedWallet() { const wallet = Wallet.createRandom(); return this.defaultProvider ? wallet.connect(this.defaultProvider) : wallet; } getWallets() { return Array.from(this.wallets.values()); } }