2019-01-02 17:15:55 +01:00
|
|
|
import bip39 from 'bip39';
|
|
|
|
import BigNumber from 'bignumber.js';
|
|
|
|
import signer from '../models/signer';
|
2020-03-08 22:46:43 +01:00
|
|
|
import { AbstractHDElectrumWallet } from './abstract-hd-electrum-wallet';
|
2019-09-14 02:48:37 +02:00
|
|
|
const bitcoin = require('bitcoinjs-lib');
|
2019-09-22 14:20:03 +02:00
|
|
|
const HDNode = require('bip32');
|
2018-07-07 23:15:14 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* HD Wallet (BIP39).
|
|
|
|
* In particular, BIP44 (P2PKH legacy addressess)
|
|
|
|
* @see https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki
|
|
|
|
*/
|
2020-03-08 22:46:43 +01:00
|
|
|
export class HDLegacyP2PKHWallet extends AbstractHDElectrumWallet {
|
2018-12-28 16:52:06 +01:00
|
|
|
static type = 'HDlegacyP2PKH';
|
|
|
|
static typeReadable = 'HD Legacy (BIP44 P2PKH)';
|
2018-07-07 23:15:14 +02:00
|
|
|
|
2018-12-19 12:22:57 +01:00
|
|
|
allowSend() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-07 23:15:14 +02:00
|
|
|
getXpub() {
|
2018-07-22 16:49:59 +02:00
|
|
|
if (this._xpub) {
|
|
|
|
return this._xpub; // cache hit
|
|
|
|
}
|
2019-01-02 17:15:55 +01:00
|
|
|
const mnemonic = this.secret;
|
|
|
|
const seed = bip39.mnemonicToSeed(mnemonic);
|
2019-09-14 00:15:59 +02:00
|
|
|
const root = bitcoin.bip32.fromSeed(seed);
|
2018-07-07 23:15:14 +02:00
|
|
|
|
2019-01-02 17:15:55 +01:00
|
|
|
const path = "m/44'/0'/0'";
|
|
|
|
const child = root.derivePath(path).neutered();
|
2018-07-22 16:49:59 +02:00
|
|
|
this._xpub = child.toBase58();
|
2019-01-02 17:15:55 +01:00
|
|
|
|
2018-07-22 16:49:59 +02:00
|
|
|
return this._xpub;
|
2018-07-07 23:15:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_getExternalWIFByIndex(index) {
|
2019-01-02 17:08:54 +01:00
|
|
|
return this._getWIFByIndex(false, index);
|
2018-07-07 23:15:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_getInternalWIFByIndex(index) {
|
2019-01-02 17:08:54 +01:00
|
|
|
return this._getWIFByIndex(true, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get internal/external WIF by wallet index
|
|
|
|
* @param {Boolean} internal
|
|
|
|
* @param {Number} index
|
|
|
|
* @returns {*}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_getWIFByIndex(internal, index) {
|
2019-01-02 17:15:55 +01:00
|
|
|
const mnemonic = this.secret;
|
|
|
|
const seed = bip39.mnemonicToSeed(mnemonic);
|
2019-09-22 14:20:03 +02:00
|
|
|
|
|
|
|
const root = HDNode.fromSeed(seed);
|
2019-01-02 17:15:55 +01:00
|
|
|
const path = `m/44'/0'/0'/${internal ? 1 : 0}/${index}`;
|
|
|
|
const child = root.derivePath(path);
|
2019-01-02 17:08:54 +01:00
|
|
|
|
2019-09-22 14:20:03 +02:00
|
|
|
return child.toWIF();
|
2018-07-07 23:15:14 +02:00
|
|
|
}
|
|
|
|
|
2019-01-02 17:15:55 +01:00
|
|
|
_getExternalAddressByIndex(index) {
|
2018-07-07 23:15:14 +02:00
|
|
|
index = index * 1; // cast to int
|
2018-07-22 16:49:59 +02:00
|
|
|
if (this.external_addresses_cache[index]) return this.external_addresses_cache[index]; // cache hit
|
2018-07-07 23:15:14 +02:00
|
|
|
|
2019-09-14 00:15:59 +02:00
|
|
|
const node = bitcoin.bip32.fromBase58(this.getXpub());
|
|
|
|
const address = bitcoin.payments.p2pkh({
|
|
|
|
pubkey: node.derive(0).derive(index).publicKey,
|
|
|
|
}).address;
|
2019-01-02 16:57:47 +01:00
|
|
|
|
|
|
|
return (this.external_addresses_cache[index] = address);
|
2018-07-07 23:15:14 +02:00
|
|
|
}
|
|
|
|
|
2019-01-02 17:15:55 +01:00
|
|
|
_getInternalAddressByIndex(index) {
|
2018-07-07 23:15:14 +02:00
|
|
|
index = index * 1; // cast to int
|
2018-07-22 16:49:59 +02:00
|
|
|
if (this.internal_addresses_cache[index]) return this.internal_addresses_cache[index]; // cache hit
|
2018-07-07 23:15:14 +02:00
|
|
|
|
2019-09-14 00:15:59 +02:00
|
|
|
const node = bitcoin.bip32.fromBase58(this.getXpub());
|
|
|
|
const address = bitcoin.payments.p2pkh({
|
|
|
|
pubkey: node.derive(1).derive(index).publicKey,
|
|
|
|
}).address;
|
2018-07-07 23:15:14 +02:00
|
|
|
|
2019-01-02 16:57:47 +01:00
|
|
|
return (this.internal_addresses_cache[index] = address);
|
2018-07-07 23:15:14 +02:00
|
|
|
}
|
2018-12-19 12:22:57 +01:00
|
|
|
|
|
|
|
createTx(utxos, amount, fee, address) {
|
|
|
|
for (let utxo of utxos) {
|
|
|
|
utxo.wif = this._getWifForAddress(utxo.address);
|
|
|
|
}
|
|
|
|
|
|
|
|
let amountPlusFee = parseFloat(new BigNumber(amount).plus(fee).toString(10));
|
|
|
|
return signer.createHDTransaction(
|
|
|
|
utxos,
|
|
|
|
address,
|
|
|
|
amountPlusFee,
|
|
|
|
fee,
|
|
|
|
this._getInternalAddressByIndex(this.next_free_change_address_index),
|
|
|
|
);
|
|
|
|
}
|
2018-07-07 23:15:14 +02:00
|
|
|
}
|