2019-01-02 17:15:55 +01:00
|
|
|
import b58 from 'bs58check';
|
2019-12-23 23:11:00 +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);
|
2019-05-22 00:00:03 +01:00
|
|
|
const bitcoin = require('bitcoinjs-lib');
|
2019-01-02 17:15:55 +01:00
|
|
|
|
2018-06-24 23:19:27 +01:00
|
|
|
/**
|
|
|
|
* HD Wallet (BIP39).
|
2018-07-07 22:15:14 +01:00
|
|
|
* 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 {
|
2018-12-28 16:52:06 +01:00
|
|
|
static type = 'HDsegwitP2SH';
|
|
|
|
static typeReadable = 'HD SegWit (BIP49 P2SH)';
|
2021-03-11 13:45:49 +03:00
|
|
|
static segwitType = 'p2sh(p2wpkh)';
|
2021-03-23 18:58:13 +03:00
|
|
|
static derivationPath = "m/49'/0'/0'";
|
2018-06-24 23:19:27 +01:00
|
|
|
|
2018-09-01 00:28:19 +01:00
|
|
|
allowSend() {
|
2018-10-06 01:45:24 +01:00
|
|
|
return true;
|
2018-09-01 00:28:19 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
allowHodlHodlTrading() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
allowMasterFingerprint() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-15 20:52:48 +03:00
|
|
|
allowXpub() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-09-23 16:05:10 +03:00
|
|
|
_getNodeAddressByIndex(node, index) {
|
2018-06-24 23:19:27 +01:00
|
|
|
index = index * 1; // cast to int
|
2021-09-23 16:05:10 +03: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
|
|
|
|
}
|
2019-01-02 16:57:47 +01:00
|
|
|
|
2021-09-23 16:05:10 +03:00
|
|
|
if (node === 0 && !this._node0) {
|
2019-12-23 23:11:00 +00:00
|
|
|
const xpub = this.constructor._ypubToXpub(this.getXpub());
|
2022-01-17 15:22:15 +00:00
|
|
|
const hdNode = bip32.fromBase58(xpub);
|
2019-05-20 23:57:46 +01:00
|
|
|
this._node0 = hdNode.derive(0);
|
|
|
|
}
|
2018-06-24 23:19:27 +01:00
|
|
|
|
2021-09-23 16:05:10 +03:00
|
|
|
if (node === 1 && !this._node1) {
|
2019-12-23 23:11:00 +00:00
|
|
|
const xpub = this.constructor._ypubToXpub(this.getXpub());
|
2022-01-17 15:22:15 +00:00
|
|
|
const hdNode = bip32.fromBase58(xpub);
|
2019-05-20 23:57:46 +01:00
|
|
|
this._node1 = hdNode.derive(1);
|
|
|
|
}
|
2019-01-02 16:57:47 +01:00
|
|
|
|
2021-09-23 16:05:10 +03:00
|
|
|
let address;
|
|
|
|
if (node === 0) {
|
|
|
|
address = this.constructor._nodeToP2shSegwitAddress(this._node0.derive(index));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node === 1) {
|
|
|
|
address = this.constructor._nodeToP2shSegwitAddress(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-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
|
|
|
|
*/
|
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
|
|
|
|
}
|
2018-07-08 15:32:38 +01:00
|
|
|
// first, getting xpub
|
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();
|
|
|
|
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]);
|
2018-07-22 15:49:59 +01:00
|
|
|
this._xpub = b58.encode(data);
|
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-04-22 16:13:18 +01:00
|
|
|
_addPsbtInput(psbt, input, sequence, masterFingerprintBuffer) {
|
|
|
|
const pubkey = this._getPubkeyByAddress(input.address);
|
2021-09-23 16:05:10 +03:00
|
|
|
const path = this._getDerivationPathByAddress(input.address);
|
2020-04-22 16:13:18 +01:00
|
|
|
const p2wpkh = bitcoin.payments.p2wpkh({ pubkey });
|
2020-06-01 15:54:23 +03:00
|
|
|
const p2sh = bitcoin.payments.p2sh({ redeem: p2wpkh });
|
2020-04-22 16:13:18 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
2020-04-22 16:13:18 +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;
|
|
|
|
}
|
2021-07-06 21:41:00 +01:00
|
|
|
|
2021-09-09 12:00:11 +01:00
|
|
|
isSegwit() {
|
|
|
|
return true;
|
|
|
|
}
|
2018-06-24 23:19:27 +01:00
|
|
|
}
|