mirror of
https://github.com/BlueWallet/BlueWallet.git
synced 2024-11-20 02:09:10 +01:00
101 lines
2.7 KiB
JavaScript
101 lines
2.7 KiB
JavaScript
import { AbstractHDWallet } from './abstract-hd-wallet';
|
|
import bip39 from 'bip39';
|
|
import BigNumber from 'bignumber.js';
|
|
import signer from '../models/signer';
|
|
const bitcoin = require('bitcoinjs-lib');
|
|
const HDNode = require('bip32');
|
|
|
|
/**
|
|
* HD Wallet (BIP39).
|
|
* In particular, BIP44 (P2PKH legacy addressess)
|
|
* @see https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki
|
|
*/
|
|
export class HDLegacyP2PKHWallet extends AbstractHDWallet {
|
|
static type = 'HDlegacyP2PKH';
|
|
static typeReadable = 'HD Legacy (BIP44 P2PKH)';
|
|
|
|
allowSend() {
|
|
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) {
|
|
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) {
|
|
index = index * 1; // cast to int
|
|
if (this.external_addresses_cache[index]) return this.external_addresses_cache[index]; // cache hit
|
|
|
|
const node = bitcoin.bip32.fromBase58(this.getXpub());
|
|
const address = bitcoin.payments.p2pkh({
|
|
pubkey: node.derive(0).derive(index).publicKey,
|
|
}).address;
|
|
|
|
return (this.external_addresses_cache[index] = address);
|
|
}
|
|
|
|
_getInternalAddressByIndex(index) {
|
|
index = index * 1; // cast to int
|
|
if (this.internal_addresses_cache[index]) return this.internal_addresses_cache[index]; // cache hit
|
|
|
|
const node = bitcoin.bip32.fromBase58(this.getXpub());
|
|
const address = bitcoin.payments.p2pkh({
|
|
pubkey: node.derive(1).derive(index).publicKey,
|
|
}).address;
|
|
|
|
return (this.internal_addresses_cache[index] = address);
|
|
}
|
|
|
|
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),
|
|
);
|
|
}
|
|
}
|