2018-07-21 14:52:54 +02:00
|
|
|
import { AbstractHDWallet } from './abstract-hd-wallet';
|
2019-01-02 17:15:55 +01:00
|
|
|
import bitcoin from 'bitcoinjs-lib';
|
|
|
|
import bip39 from 'bip39';
|
|
|
|
import BigNumber from 'bignumber.js';
|
|
|
|
import signer from '../models/signer';
|
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
|
|
|
|
*/
|
2018-07-21 14:52:54 +02:00
|
|
|
export class HDLegacyP2PKHWallet extends AbstractHDWallet {
|
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);
|
|
|
|
const root = bitcoin.HDNode.fromSeedBuffer(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);
|
|
|
|
const root = bitcoin.HDNode.fromSeedBuffer(seed);
|
|
|
|
const path = `m/44'/0'/0'/${internal ? 1 : 0}/${index}`;
|
|
|
|
const child = root.derivePath(path);
|
2019-01-02 17:08:54 +01:00
|
|
|
|
2018-07-07 23:15:14 +02:00
|
|
|
return child.keyPair.toWIF();
|
|
|
|
}
|
|
|
|
|
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-01-02 16:57:47 +01:00
|
|
|
const node = bitcoin.HDNode.fromBase58(this.getXpub());
|
|
|
|
const address = node
|
|
|
|
.derive(0)
|
|
|
|
.derive(index)
|
|
|
|
.getAddress();
|
|
|
|
|
|
|
|
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-01-02 16:57:47 +01:00
|
|
|
const node = bitcoin.HDNode.fromBase58(this.getXpub());
|
|
|
|
const address = node
|
|
|
|
.derive(1)
|
|
|
|
.derive(index)
|
|
|
|
.getAddress();
|
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
|
|
|
}
|