BlueWallet/class/wallets/hd-segwit-p2sh-wallet.js

141 lines
3.8 KiB
JavaScript
Raw Normal View History

import bip39 from 'bip39';
import b58 from 'bs58check';
2019-12-23 23:11:00 +00:00
import { AbstractHDElectrumWallet } from './abstract-hd-electrum-wallet';
const bitcoin = require('bitcoinjs-lib');
const HDNode = require('bip32');
2018-06-24 23:19:27 +01:00
/**
* HD Wallet (BIP39).
* In particular, BIP49 (P2SH Segwit)
* @see https://github.com/bitcoin/bips/blob/master/bip-0049.mediawiki
2018-06-24 23:19:27 +01:00
*/
2019-12-23 23:11:00 +00:00
export class HDSegwitP2SHWallet extends AbstractHDElectrumWallet {
static type = 'HDsegwitP2SH';
static typeReadable = 'HD SegWit (BIP49 P2SH)';
2018-06-24 23:19:27 +01:00
allowSend() {
2018-10-06 01:45:24 +01:00
return true;
}
2019-08-04 20:33:15 +01:00
allowSendMax(): boolean {
return true;
}
2019-01-02 17:08:54 +01:00
/**
* Get internal/external WIF by wallet index
* @param {Boolean} internal
* @param {Number} index
* @returns {*}
* @private
*/
_getWIFByIndex(internal, index) {
2020-10-05 12:12:54 +01:00
if (!this.secret) return false;
const mnemonic = this.secret;
const seed = bip39.mnemonicToSeed(mnemonic);
2019-09-14 07:15:59 +09:00
const root = bitcoin.bip32.fromSeed(seed);
const path = `m/49'/0'/0'/${internal ? 1 : 0}/${index}`;
const child = root.derivePath(path);
2019-01-02 17:08:54 +01:00
2019-09-14 07:15:59 +09:00
return bitcoin.ECPair.fromPrivateKey(child.privateKey).toWIF();
2018-06-24 23:19:27 +01:00
}
_getExternalAddressByIndex(index) {
2018-06-24 23:19:27 +01:00
index = index * 1; // cast to int
if (this.external_addresses_cache[index]) return this.external_addresses_cache[index]; // cache hit
if (!this._node0) {
2019-12-23 23:11:00 +00:00
const xpub = this.constructor._ypubToXpub(this.getXpub());
const hdNode = HDNode.fromBase58(xpub);
this._node0 = hdNode.derive(0);
}
2019-12-23 23:11:00 +00:00
const address = this.constructor._nodeToP2shSegwitAddress(this._node0.derive(index));
return (this.external_addresses_cache[index] = address);
2018-06-24 23:19:27 +01:00
}
_getInternalAddressByIndex(index) {
2018-06-24 23:19:27 +01:00
index = index * 1; // cast to int
if (this.internal_addresses_cache[index]) return this.internal_addresses_cache[index]; // cache hit
if (!this._node1) {
2019-12-23 23:11:00 +00:00
const xpub = this.constructor._ypubToXpub(this.getXpub());
const hdNode = HDNode.fromBase58(xpub);
this._node1 = hdNode.derive(1);
}
2019-12-23 23:11:00 +00:00
const address = this.constructor._nodeToP2shSegwitAddress(this._node1.derive(index));
return (this.internal_addresses_cache[index] = address);
2018-06-24 23:19:27 +01:00
}
2018-07-08 15:32:38 +01:00
/**
* Returning ypub actually, not xpub. Keeping same method name
* for compatibility.
*
* @return {String} ypub
*/
getXpub() {
if (this._xpub) {
return this._xpub; // cache hit
}
2018-07-08 15:32:38 +01:00
// first, getting xpub
const mnemonic = this.secret;
const seed = bip39.mnemonicToSeed(mnemonic);
const root = HDNode.fromSeed(seed);
const path = "m/49'/0'/0'";
const child = root.derivePath(path).neutered();
const xpub = child.toBase58();
2018-07-08 15:32:38 +01:00
// bitcoinjs does not support ypub yet, so we just convert it from xpub
let data = b58.decode(xpub);
data = data.slice(4);
data = Buffer.concat([Buffer.from('049d7cb2', 'hex'), data]);
this._xpub = b58.encode(data);
return this._xpub;
}
_addPsbtInput(psbt, input, sequence, masterFingerprintBuffer) {
const pubkey = this._getPubkeyByAddress(input.address);
const path = this._getDerivationPathByAddress(input.address, 49);
const p2wpkh = bitcoin.payments.p2wpkh({ pubkey });
const p2sh = bitcoin.payments.p2sh({ redeem: p2wpkh });
psbt.addInput({
hash: input.txid,
index: input.vout,
sequence,
bip32Derivation: [
{
masterFingerprint: masterFingerprintBuffer,
path,
pubkey,
},
],
witnessUtxo: {
script: p2sh.output,
value: input.amount || input.value,
},
redeemScript: p2wpkh.output,
});
2019-08-04 20:33:15 +01:00
return psbt;
2018-08-08 01:05:34 +01:00
}
2019-12-23 23:11:00 +00:00
/**
* Creates Segwit P2SH Bitcoin address
* @param hdNode
* @returns {String}
*/
static _nodeToP2shSegwitAddress(hdNode) {
const { address } = bitcoin.payments.p2sh({
redeem: bitcoin.payments.p2wpkh({ pubkey: hdNode.publicKey }),
});
return address;
}
allowHodlHodlTrading() {
return true;
}
2018-06-24 23:19:27 +01:00
}