UNPKG

tnb-hd-wallet

Version:

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

45 lines (44 loc) 2.31 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BtcHdWallet = void 0; const axios_1 = __importDefault(require("axios")); const webHd_1 = require("./webHd"); const bip32_1 = require("bip32"); const bs58check_ts_1 = __importDefault(require("bs58check-ts")); class BtcHdWallet extends webHd_1.WebHdWallet { constructor(seed, configOptions = { addressGapLimit: 20 }) { super(seed, "bitcoin", configOptions); } hasAddressBeenUsed(publicKey) { return __awaiter(this, void 0, void 0, function* () { const data = (yield axios_1.default.get(`https://api.blockchair.com/bitcoin/dashboards/address/${publicKey}?limit=1`)).data.data; const transactionCount = data[publicKey].address.transaction_count; return !!transactionCount; }); } formatAddress(address) { if (address.chainCode) { const privateKey = Buffer.from(address.privateKey, "hex"); const chainCode = Buffer.from(address.chainCode, "hex"); const bip32 = bip32_1.fromPrivateKey(privateKey, chainCode); delete address.chainCode; const identifier = bip32.identifier; const pubKey = bs58check_ts_1.default.encode(Buffer.concat([Buffer.alloc(1), identifier])); return Object.assign(Object.assign({}, address), { publicKey: pubKey, privateKey: bip32.toWIF() }); } return address; } } exports.BtcHdWallet = BtcHdWallet;