UNPKG

tnb-hd-wallet

Version:

A hd wallet that derives public and private keys from a 12 word mnemonic phrase with support

103 lines (102 loc) 4.57 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.WebHdWallet = void 0; const baseHd_1 = require("./baseHd"); class WebHdWallet extends baseHd_1.BaseHdWallet { constructor() { super(...arguments); this.hasAccountBeenCreated = (accountIndex) => __awaiter(this, void 0, void 0, function* () { let addressIndex = 0; let addressGap = 0; while (addressGap < this._addressGapLimit) { const addressPath = this.getPath(accountIndex, addressIndex); const address = this.getAddressFromPath(addressPath); // console.log({address}) const isAddressUsed = yield this.hasAddressBeenUsed(address.publicKey); if (isAddressUsed) return true; addressGap += 1; addressIndex += 1; } return false; }); this.scanAccountForAddresses = (accountIndex) => __awaiter(this, void 0, void 0, function* () { const usedAddresses = []; const freshAddresses = []; let addressIndex = 0; let addressGap = 0; while (addressGap < this._addressGapLimit) { const addressPath = this.getPath(accountIndex, addressIndex); const address = this.getAddressFromPath(addressPath); // console.log(address); const isAddressUsed = yield this.hasAddressBeenUsed(address.publicKey); if (isAddressUsed) { usedAddresses.push(address); addressGap = 0; } else { addressGap += 1; freshAddresses.push(address); } addressIndex += 1; } return { usedAddresses, freshAddresses }; }); this.getAccount = (accountIndex) => __awaiter(this, void 0, void 0, function* () { if (accountIndex < 0) throw new Error("accountIndex has to be greater than or equal to 0"); if (accountIndex > 0) { const isAccountCreated = (yield this.hasAccountBeenCreated(accountIndex - 1)); if (!isAccountCreated) { return { usedAddresses: [], freshAddresses: [] }; } } let { usedAddresses, freshAddresses } = yield this.scanAccountForAddresses(accountIndex); const currAccount = { usedAddresses, freshAddresses }; return currAccount; }); this.createAccount = () => __awaiter(this, void 0, void 0, function* () { let accountIndex = 0; while (true) { const isAccountCreated = (yield this.hasAccountBeenCreated(accountIndex)); // console.log({ isAccountCreated }) if (!isAccountCreated) { // console.log(!isAccountCreated) const newAccount = yield this.getAccount(accountIndex); return newAccount; } accountIndex += 1; } }); } getAddressFromPath(path) { return this.formatAddress(super.getAddressFromPath(path)); } getAccounts(start = 0) { return __awaiter(this, void 0, void 0, function* () { const existingAccounts = []; let accountIndex = start; while (true) { const account = yield this.scanAccountForAddresses(accountIndex); if (account.usedAddresses.length) { existingAccounts.push(account); } else { break; } accountIndex += 1; } return existingAccounts; }); } } exports.WebHdWallet = WebHdWallet;