Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Spends from a 2 of 3 multisig to a pubkeyhash with Bitcoin Cash (UNTESTED)
/* npm install -g https://github.com/afk11/bitcoinjs-lib/tarball/opt-in-bitcoincash-sighash */
/*
This script spends from a 2 of 3 multisig to a pubkeyhash using Bitcoin Cash
*/
var bitcoin = require('bitcoinjs-lib')
var network = bitcoin.networks['bitcoin']
var outputValue = 4500000 // How much to send (remainder is the miner fee)
var inputValue = 4600000 // utxo value is required for bitcoin cash signature
var txid = '6c2b36c213b844c8a522f02592ea754bc8dedcf9380b71ef4992440439a157c6' // for input
var outputAddress = '1GX28yLjVWux7ws4UQ9FB4MnLH4UKTPK2z'
var vout = 0 // vout of input
var keyPairs = [
'5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAnchuDf', // 1EHNa6Q4Jz2uvNExL497mE43ikXhwF6kZm
'5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAvUcVfH', // 1LagHJk2FyCV2VzrNHVqg3gYG4TSYwDV4m
'5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreB1FQ8BZ' // 1NZUP3JAc9JkmbvmoTv7nVgZGtyJjirKV1
].map(function (wif) { return bitcoin.ECPair.fromWIF(wif) })
var pubKeys = keyPairs.map(function (x) { return x.getPublicKeyBuffer() })
var redeemScript = bitcoin.script.multisig.output.encode(2, pubKeys) // 2 of 3
var txb = new bitcoin.TransactionBuilder(network)
txb.addInput(txid, vout, bitcoin.Transaction.DEFAULT_SEQUENCE, redeemScript)
txb.addOutput(outputAddress, outputValue)
txb.enableBitcoinCash(true)
var hashType = bitcoin.Transaction.SIGHASH_ALL | bitcoin.Transaction.SIGHASH_BITCOINCASHBIP143
txb.sign(0, keyPairs[0], null, hashType, inputValue)
txb.sign(0, keyPairs[2], null, hashType, inputValue)
var tx = txb.build()
console.log(tx.toHex())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment