2015-02-23 00:36:57 +01:00
|
|
|
/* global describe, it */
|
|
|
|
|
2016-01-28 11:06:20 +01:00
|
|
|
var async = require('async')
|
2014-11-24 11:15:28 +01:00
|
|
|
var assert = require('assert')
|
|
|
|
var bitcoin = require('../../')
|
2015-08-07 06:47:07 +02:00
|
|
|
var blockchain = require('./_blockchain')
|
2014-11-24 11:15:28 +01:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('bitcoinjs-lib (multisig)', function () {
|
|
|
|
it('can create a 2-of-3 multisig P2SH address', function () {
|
2014-11-24 11:15:28 +01:00
|
|
|
var pubKeys = [
|
|
|
|
'026477115981fe981a6918a6297d9803c4dc04f328f22041bedff886bbc2962e01',
|
|
|
|
'02c96db2302d19b43d4c69368babace7854cc84eb9e061cde51cfa77ca4a22b8b9',
|
|
|
|
'03c6103b3b83e4a24a0e33a4df246ef11772f9992663db0c35759a5e2ebf68d8e9'
|
2015-03-02 06:48:36 +01:00
|
|
|
].map(function (hex) {
|
|
|
|
return new Buffer(hex, 'hex')
|
|
|
|
})
|
2014-11-24 11:15:28 +01:00
|
|
|
|
2015-08-20 05:12:05 +02:00
|
|
|
var redeemScript = bitcoin.script.multisigOutput(2, pubKeys) // 2 of 3
|
|
|
|
var scriptPubKey = bitcoin.script.scriptHashOutput(bitcoin.crypto.hash160(redeemScript))
|
|
|
|
var address = bitcoin.address.fromOutputScript(scriptPubKey)
|
2014-11-24 11:15:28 +01:00
|
|
|
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(address, '36NUkt6FWUi3LAWBqWRdDmdTWbt91Yvfu7')
|
2014-11-24 11:15:28 +01:00
|
|
|
})
|
|
|
|
|
2015-03-05 09:16:22 +01:00
|
|
|
it('can spend from a 2-of-4 multsig P2SH address', function (done) {
|
2016-01-06 03:14:36 +01:00
|
|
|
this.timeout(30000)
|
2014-11-25 04:24:05 +01:00
|
|
|
|
2015-03-02 06:48:36 +01:00
|
|
|
var keyPairs = [
|
2014-11-24 11:15:28 +01:00
|
|
|
'91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgwmaKkrx',
|
2015-03-03 11:22:42 +01:00
|
|
|
'91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgww7vXtT',
|
2015-03-05 09:16:22 +01:00
|
|
|
'91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgx3cTMqe',
|
|
|
|
'91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgx9rcrL7'
|
2015-07-28 09:18:13 +02:00
|
|
|
].map(function (wif) { return bitcoin.ECPair.fromWIF(wif, bitcoin.networks.testnet) })
|
2015-03-02 06:48:36 +01:00
|
|
|
var pubKeys = keyPairs.map(function (x) { return x.getPublicKeyBuffer() })
|
2014-11-24 11:15:28 +01:00
|
|
|
|
2015-08-20 05:12:05 +02:00
|
|
|
var redeemScript = bitcoin.script.multisigOutput(2, pubKeys) // 2 of 4
|
|
|
|
var scriptPubKey = bitcoin.script.scriptHashOutput(bitcoin.crypto.hash160(redeemScript))
|
|
|
|
var address = bitcoin.address.fromOutputScript(scriptPubKey, bitcoin.networks.testnet)
|
2014-11-24 11:15:28 +01:00
|
|
|
|
2015-08-07 06:47:07 +02:00
|
|
|
// attempt to send funds to the source address
|
2016-01-06 02:46:02 +01:00
|
|
|
blockchain.t.faucet(address, 2e4, function (err, unspent) {
|
2014-11-24 11:15:28 +01:00
|
|
|
if (err) return done(err)
|
|
|
|
|
2016-01-28 11:06:20 +01:00
|
|
|
var fee = 1e4
|
|
|
|
var targetValue = unspent.value - fee
|
|
|
|
|
2015-11-06 00:49:06 +01:00
|
|
|
// make a random destination address
|
|
|
|
var targetAddress = bitcoin.ECPair.makeRandom({
|
|
|
|
network: bitcoin.networks.testnet
|
|
|
|
}).getAddress()
|
2015-09-14 07:26:52 +02:00
|
|
|
|
2015-11-06 00:49:06 +01:00
|
|
|
var txb = new bitcoin.TransactionBuilder(bitcoin.networks.testnet)
|
|
|
|
txb.addInput(unspent.txId, unspent.vout)
|
2016-01-28 11:06:20 +01:00
|
|
|
txb.addOutput(targetAddress, targetValue)
|
2014-11-24 11:15:28 +01:00
|
|
|
|
2015-11-06 00:49:06 +01:00
|
|
|
// sign with 1st and 3rd key
|
|
|
|
txb.sign(0, keyPairs[0], redeemScript)
|
|
|
|
txb.sign(0, keyPairs[2], redeemScript)
|
2014-11-24 11:15:28 +01:00
|
|
|
|
2015-11-06 00:49:06 +01:00
|
|
|
// broadcast our transaction
|
|
|
|
var tx = txb.build()
|
|
|
|
var txId = tx.getId()
|
2014-11-24 11:15:28 +01:00
|
|
|
|
2015-11-06 00:49:06 +01:00
|
|
|
blockchain.t.transactions.propagate(tx.toHex(), function (err) {
|
|
|
|
if (err) return done(err)
|
2015-09-14 07:26:52 +02:00
|
|
|
|
2016-01-28 11:06:20 +01:00
|
|
|
// allow for TX to be processed
|
|
|
|
async.retry(5, function (callback) {
|
|
|
|
setTimeout(function () {
|
|
|
|
// check that the above transaction included the intended address
|
|
|
|
blockchain.t.addresses.unspents(targetAddress, function (err, unspents) {
|
|
|
|
if (err) return callback(err)
|
|
|
|
|
|
|
|
var unspentFound = unspents.some(function (unspent) {
|
|
|
|
return unspent.txId === txId && unspent.value === targetValue
|
|
|
|
})
|
|
|
|
|
|
|
|
if (!unspentFound) return callback(new Error('Could not find unspent after propagate'))
|
|
|
|
callback()
|
|
|
|
})
|
|
|
|
}, 600)
|
|
|
|
}, done)
|
2014-11-24 11:15:28 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|