import bip39 from 'bip39'; import { AbstractHDElectrumWallet } from './abstract-hd-electrum-wallet'; const bitcoin = require('bitcoinjs-lib'); const HDNode = require('bip32'); const BlueElectrum = require('../../blue_modules/BlueElectrum'); /** * HD Wallet (BIP39). * In particular, BIP44 (P2PKH legacy addressess) * @see https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki */ export class HDLegacyP2PKHWallet extends AbstractHDElectrumWallet { static type = 'HDlegacyP2PKH'; static typeReadable = 'HD Legacy (BIP44 P2PKH)'; allowSend() { return true; } allowSendMax() { return true; } allowCosignPsbt() { return true; } allowSignVerifyMessage() { return true; } getXpub() { if (this._xpub) { return this._xpub; // cache hit } const mnemonic = this.secret; const seed = bip39.mnemonicToSeed(mnemonic); const root = bitcoin.bip32.fromSeed(seed); const path = "m/44'/0'/0'"; const child = root.derivePath(path).neutered(); this._xpub = child.toBase58(); return this._xpub; } _getExternalWIFByIndex(index) { return this._getWIFByIndex(false, index); } _getInternalWIFByIndex(index) { return this._getWIFByIndex(true, index); } /** * Get internal/external WIF by wallet index * @param {Boolean} internal * @param {Number} index * @returns {*} * @private */ _getWIFByIndex(internal, index) { if (!this.secret) return false; const mnemonic = this.secret; const seed = bip39.mnemonicToSeed(mnemonic); const root = HDNode.fromSeed(seed); const path = `m/44'/0'/0'/${internal ? 1 : 0}/${index}`; const child = root.derivePath(path); return child.toWIF(); } _getExternalAddressByIndex(index) { return this._getNodeAddressByIndex(0, index); } _getInternalAddressByIndex(index) { return this._getNodeAddressByIndex(1, index); } _getNodeAddressByIndex(node, index) { index = index * 1; // cast to int if (node === 0) { if (this.external_addresses_cache[index]) return this.external_addresses_cache[index]; // cache hit } if (node === 1) { if (this.internal_addresses_cache[index]) return this.internal_addresses_cache[index]; // cache hit } if (node === 0 && !this._node0) { const xpub = this.getXpub(); const hdNode = HDNode.fromBase58(xpub); this._node0 = hdNode.derive(node); } if (node === 1 && !this._node1) { const xpub = this.getXpub(); const hdNode = HDNode.fromBase58(xpub); this._node1 = hdNode.derive(node); } let address; if (node === 0) { address = this.constructor._nodeToLegacyAddress(this._node0.derive(index)); } if (node === 1) { address = this.constructor._nodeToLegacyAddress(this._node1.derive(index)); } if (node === 0) { return (this.external_addresses_cache[index] = address); } if (node === 1) { return (this.internal_addresses_cache[index] = address); } } async fetchUtxo() { await super.fetchUtxo(); // now we need to fetch txhash for each input as required by PSBT const txhexes = await BlueElectrum.multiGetTransactionByTxid( this.getUtxo().map(x => x.txid), 50, false, ); const newUtxos = []; for (const u of this.getUtxo()) { if (txhexes[u.txid]) u.txhex = txhexes[u.txid]; newUtxos.push(u); } return newUtxos; } _addPsbtInput(psbt, input, sequence, masterFingerprintBuffer) { const pubkey = this._getPubkeyByAddress(input.address); const path = this._getDerivationPathByAddress(input.address, 44); if (!input.txhex) throw new Error('UTXO is missing txhex of the input, which is required by PSBT for non-segwit input'); psbt.addInput({ hash: input.txid, index: input.vout, sequence, bip32Derivation: [ { masterFingerprint: masterFingerprintBuffer, path, pubkey, }, ], // non-segwit inputs now require passing the whole previous tx as Buffer nonWitnessUtxo: Buffer.from(input.txhex, 'hex'), }); return psbt; } }