2020-03-08 21:46:43 +00:00
|
|
|
import { AbstractHDElectrumWallet } from './abstract-hd-electrum-wallet';
|
2022-01-17 15:22:15 +00:00
|
|
|
import BIP32Factory from 'bip32';
|
|
|
|
import * as ecc from 'tiny-secp256k1';
|
|
|
|
const bip32 = BIP32Factory(ecc);
|
2020-07-01 12:56:52 +01:00
|
|
|
const BlueElectrum = require('../../blue_modules/BlueElectrum');
|
2018-07-07 22:15:14 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* HD Wallet (BIP39).
|
|
|
|
* In particular, BIP44 (P2PKH legacy addressess)
|
|
|
|
* @see https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki
|
|
|
|
*/
|
2020-03-08 21:46:43 +00:00
|
|
|
export class HDLegacyP2PKHWallet extends AbstractHDElectrumWallet {
|
2018-12-28 16:52:06 +01:00
|
|
|
static type = 'HDlegacyP2PKH';
|
|
|
|
static typeReadable = 'HD Legacy (BIP44 P2PKH)';
|
2021-03-23 18:58:13 +03:00
|
|
|
static derivationPath = "m/44'/0'/0'";
|
2018-07-07 22:15:14 +01:00
|
|
|
|
2018-12-19 12:22:57 +01:00
|
|
|
allowSend() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-02-18 16:37:43 +03:00
|
|
|
allowCosignPsbt() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-09 14:26:56 +03:00
|
|
|
allowSignVerifyMessage() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-23 18:58:13 +03:00
|
|
|
allowMasterFingerprint() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-15 20:52:48 +03:00
|
|
|
allowXpub() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-07 22:15:14 +01:00
|
|
|
getXpub() {
|
2018-07-22 15:49:59 +01:00
|
|
|
if (this._xpub) {
|
|
|
|
return this._xpub; // cache hit
|
|
|
|
}
|
2021-04-15 20:52:48 +03:00
|
|
|
const seed = this._getSeed();
|
2022-01-17 15:22:15 +00:00
|
|
|
const root = bip32.fromSeed(seed);
|
2018-07-07 22:15:14 +01:00
|
|
|
|
2021-09-23 16:05:10 +03:00
|
|
|
const path = this.getDerivationPath();
|
2019-01-02 17:15:55 +01:00
|
|
|
const child = root.derivePath(path).neutered();
|
2018-07-22 15:49:59 +01:00
|
|
|
this._xpub = child.toBase58();
|
2019-01-02 17:15:55 +01:00
|
|
|
|
2018-07-22 15:49:59 +01:00
|
|
|
return this._xpub;
|
2018-07-07 22:15:14 +01:00
|
|
|
}
|
|
|
|
|
2020-06-23 17:52:25 +01:00
|
|
|
_getNodeAddressByIndex(node, index) {
|
2018-07-07 22:15:14 +01:00
|
|
|
index = index * 1; // cast to int
|
2020-06-23 17:52:25 +01:00
|
|
|
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();
|
2022-01-17 15:22:15 +00:00
|
|
|
const hdNode = bip32.fromBase58(xpub);
|
2020-06-23 17:52:25 +01:00
|
|
|
this._node0 = hdNode.derive(node);
|
|
|
|
}
|
2018-07-07 22:15:14 +01:00
|
|
|
|
2020-06-23 17:52:25 +01:00
|
|
|
if (node === 1 && !this._node1) {
|
|
|
|
const xpub = this.getXpub();
|
2022-01-17 15:22:15 +00:00
|
|
|
const hdNode = bip32.fromBase58(xpub);
|
2020-06-23 17:52:25 +01:00
|
|
|
this._node1 = hdNode.derive(node);
|
|
|
|
}
|
2018-07-07 22:15:14 +01:00
|
|
|
|
2020-06-23 17:52:25 +01:00
|
|
|
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);
|
|
|
|
}
|
2018-07-07 22:15:14 +01:00
|
|
|
}
|
2018-12-19 12:22:57 +01:00
|
|
|
|
2020-04-22 16:13:18 +01:00
|
|
|
async fetchUtxo() {
|
|
|
|
await super.fetchUtxo();
|
|
|
|
// now we need to fetch txhash for each input as required by PSBT
|
2020-06-01 15:54:23 +03:00
|
|
|
const txhexes = await BlueElectrum.multiGetTransactionByTxid(
|
|
|
|
this.getUtxo().map(x => x.txid),
|
2020-04-22 16:13:18 +01:00
|
|
|
50,
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
|
2020-06-01 15:54:23 +03:00
|
|
|
const newUtxos = [];
|
|
|
|
for (const u of this.getUtxo()) {
|
2020-04-22 16:13:18 +01:00
|
|
|
if (txhexes[u.txid]) u.txhex = txhexes[u.txid];
|
|
|
|
newUtxos.push(u);
|
2018-12-19 12:22:57 +01:00
|
|
|
}
|
|
|
|
|
2020-04-22 16:13:18 +01:00
|
|
|
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;
|
|
|
|
}
|
2018-07-07 22:15:14 +01:00
|
|
|
}
|