2015-02-23 00:36:57 +01:00
|
|
|
/* global describe, it */
|
|
|
|
|
2014-11-24 11:15:28 +01:00
|
|
|
var assert = require('assert')
|
|
|
|
var bitcoin = require('../../')
|
2014-12-09 01:54:36 +01:00
|
|
|
var blockchain = new (require('cb-helloblock'))('testnet')
|
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'
|
|
|
|
].map(bitcoin.ECPubKey.fromHex)
|
|
|
|
|
|
|
|
var redeemScript = bitcoin.scripts.multisigOutput(2, pubKeys) // 2 of 3
|
|
|
|
var scriptPubKey = bitcoin.scripts.scriptHashOutput(redeemScript.getHash())
|
|
|
|
var address = bitcoin.Address.fromOutputScript(scriptPubKey).toString()
|
|
|
|
|
|
|
|
assert.equal(address, '36NUkt6FWUi3LAWBqWRdDmdTWbt91Yvfu7')
|
|
|
|
})
|
|
|
|
|
2015-03-05 09:16:22 +01:00
|
|
|
it('can spend from a 2-of-4 multsig P2SH address', function (done) {
|
2014-11-25 04:24:05 +01:00
|
|
|
this.timeout(20000)
|
|
|
|
|
2014-11-24 11:15:28 +01:00
|
|
|
var privKeys = [
|
|
|
|
'91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgwmaKkrx',
|
2015-03-03 11:22:42 +01:00
|
|
|
'91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgww7vXtT',
|
2015-03-05 09:16:22 +01:00
|
|
|
'91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgx3cTMqe',
|
|
|
|
'91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgx9rcrL7'
|
2014-11-24 11:15:28 +01:00
|
|
|
].map(bitcoin.ECKey.fromWIF)
|
2015-02-23 00:36:57 +01:00
|
|
|
var pubKeys = privKeys.map(function (x) {
|
|
|
|
return x.pub
|
|
|
|
})
|
2014-11-24 11:15:28 +01:00
|
|
|
|
2015-03-05 09:16:22 +01:00
|
|
|
var redeemScript = bitcoin.scripts.multisigOutput(2, pubKeys) // 2 of 4
|
2014-11-24 11:15:28 +01:00
|
|
|
var scriptPubKey = bitcoin.scripts.scriptHashOutput(redeemScript.getHash())
|
2014-11-25 04:21:10 +01:00
|
|
|
var address = bitcoin.Address.fromOutputScript(scriptPubKey, bitcoin.networks.testnet).toString()
|
2014-11-24 11:15:28 +01:00
|
|
|
|
|
|
|
// Attempt to send funds to the source address
|
2015-02-23 00:36:57 +01:00
|
|
|
blockchain.addresses.__faucetWithdraw(address, 2e4, function (err) {
|
2014-11-24 11:15:28 +01:00
|
|
|
if (err) return done(err)
|
|
|
|
|
2015-03-02 03:31:03 +01:00
|
|
|
// get latest unspents from the address
|
2015-02-23 00:36:57 +01:00
|
|
|
blockchain.addresses.unspents(address, function (err, unspents) {
|
2014-11-24 11:15:28 +01:00
|
|
|
if (err) return done(err)
|
|
|
|
|
2015-03-02 03:31:03 +01:00
|
|
|
// filter small unspents
|
2015-02-23 00:36:57 +01:00
|
|
|
unspents = unspents.filter(function (unspent) {
|
|
|
|
return unspent.value > 1e4
|
|
|
|
})
|
2014-11-24 11:15:28 +01:00
|
|
|
|
|
|
|
// use the oldest unspent
|
|
|
|
var unspent = unspents.pop()
|
|
|
|
|
|
|
|
// make a random destination address
|
|
|
|
var targetAddress = bitcoin.ECKey.makeRandom().pub.getAddress(bitcoin.networks.testnet).toString()
|
|
|
|
|
|
|
|
var txb = new bitcoin.TransactionBuilder()
|
2014-12-09 01:54:36 +01:00
|
|
|
txb.addInput(unspent.txId, unspent.vout)
|
2014-11-24 11:15:28 +01:00
|
|
|
txb.addOutput(targetAddress, 1e4)
|
|
|
|
|
2015-03-03 11:22:42 +01:00
|
|
|
// sign with 1st and 3rd key
|
|
|
|
txb.sign(0, privKeys[0], redeemScript)
|
|
|
|
txb.sign(0, privKeys[2], redeemScript)
|
2014-11-24 11:15:28 +01:00
|
|
|
|
|
|
|
// broadcast our transaction
|
2015-02-23 00:36:57 +01:00
|
|
|
blockchain.transactions.propagate(txb.build().toHex(), function (err) {
|
2014-11-24 11:15:28 +01:00
|
|
|
if (err) return done(err)
|
|
|
|
|
2015-03-02 03:31:03 +01:00
|
|
|
// check that the funds (1e4 Satoshis) indeed arrived at the intended address
|
2015-02-23 00:36:57 +01:00
|
|
|
blockchain.addresses.summary(targetAddress, function (err, result) {
|
2014-11-24 11:15:28 +01:00
|
|
|
if (err) return done(err)
|
|
|
|
|
2014-12-09 01:54:36 +01:00
|
|
|
assert.equal(result.balance, 1e4)
|
2014-11-24 11:15:28 +01:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|