deth
Version:
Ethereum node focused on Developer Experience
59 lines (58 loc) • 2.16 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const ethers_1 = require("ethers");
const primitives_1 = require("./primitives");
const debug_1 = __importDefault(require("debug"));
const d = debug_1.default('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
*/
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 ethers_1.Wallet(privateKey);
this.addWallet(wallet);
}
addFromMnemonic(mnemonic, dp) {
const wallet = ethers_1.Wallet.fromMnemonic(mnemonic, dp);
this.addWallet(wallet);
}
addWallet(wallet) {
const address = primitives_1.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 = ethers_1.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 = ethers_1.Wallet.createRandom();
return this.defaultProvider ? wallet.connect(this.defaultProvider) : wallet;
}
getWallets() {
return Array.from(this.wallets.values());
}
}
exports.WalletManager = WalletManager;