BlueWallet/class/segwit-p2sh-wallet.js

76 lines
2.8 KiB
JavaScript
Raw Normal View History

2018-03-24 22:24:20 +01:00
import { LegacyWallet } from './legacy-wallet';
const bitcoin = require('bitcoinjs-lib');
const signer = require('../models/signer');
const BigNumber = require('bignumber.js');
2018-03-20 21:41:07 +01:00
export class SegwitP2SHWallet extends LegacyWallet {
static type = 'segwitP2SH';
static typeReadable = 'SegWit (P2SH)';
2018-03-20 21:41:07 +01:00
2018-10-31 21:14:28 +01:00
allowRBF() {
return true;
}
2018-06-24 23:27:34 +02:00
static witnessToAddress(witness) {
const pubKey = Buffer.from(witness, 'hex');
const pubKeyHash = bitcoin.crypto.hash160(pubKey);
2018-07-07 15:04:32 +02:00
const redeemScript = bitcoin.script.witnessPubKeyHash.output.encode(pubKeyHash);
2018-06-24 23:27:34 +02:00
const redeemScriptHash = bitcoin.crypto.hash160(redeemScript);
2018-07-07 15:04:32 +02:00
const scriptPubkey = bitcoin.script.scriptHash.output.encode(redeemScriptHash);
return bitcoin.address.fromOutputScript(scriptPubkey, bitcoin.networks.bitcoin);
2018-06-24 23:27:34 +02:00
}
2018-03-20 21:41:07 +01:00
getAddress() {
if (this._address) return this._address;
let address;
try {
let keyPair = bitcoin.ECPair.fromWIF(this.secret);
let pubKey = keyPair.getPublicKeyBuffer();
if (!keyPair.compressed) {
console.warn('only compressed public keys are good for segwit');
return false;
}
2018-07-07 15:04:32 +02:00
let witnessScript = bitcoin.script.witnessPubKeyHash.output.encode(bitcoin.crypto.hash160(pubKey));
let scriptPubKey = bitcoin.script.scriptHash.output.encode(bitcoin.crypto.hash160(witnessScript));
2018-03-20 21:41:07 +01:00
address = bitcoin.address.fromOutputScript(scriptPubKey);
} catch (err) {
return false;
}
this._address = address;
return this._address;
}
/**
* Takes UTXOs (as presented by blockcypher api), transforms them into
* format expected by signer module, creates tx and returns signed string txhex.
*
* @param utxos Unspent outputs, expects blockcypher format
* @param amount
* @param fee
* @param address
* @param memo
* @param sequence By default zero. Increased with each transaction replace.
* @return string Signed txhex ready for broadcast
*/
2018-03-20 21:41:07 +01:00
createTx(utxos, amount, fee, address, memo, sequence) {
// TODO: memo is not used here, get rid of it
2018-03-20 21:41:07 +01:00
if (sequence === undefined) {
sequence = 0;
}
// transforming UTXOs fields to how module expects it
for (let u of utxos) {
u.confirmations = 6; // hack to make module accept 0 confirmations
u.txid = u.tx_hash;
u.vout = u.tx_output_n;
u.amount = new BigNumber(u.value);
u.amount = u.amount.dividedBy(100000000);
2018-03-20 21:41:07 +01:00
u.amount = u.amount.toString(10);
}
// console.log('creating tx ', amount, ' with fee ', fee, 'secret=', this.getSecret(), 'from address', this.getAddress());
let amountPlusFee = parseFloat(new BigNumber(amount).plus(fee).toString(10));
2018-03-20 21:41:07 +01:00
// to compensate that module substracts fee from amount
return signer.createSegwitTransaction(utxos, address, amountPlusFee, fee, this.getSecret(), this.getAddress(), sequence);
2018-03-20 21:41:07 +01:00
}
}