2014-05-13 09:55:53 +02:00
|
|
|
var assert = require('assert')
|
2014-04-27 01:13:03 +02:00
|
|
|
|
2014-05-13 09:55:53 +02:00
|
|
|
var bitcoin = require('../../')
|
|
|
|
var networks = bitcoin.networks
|
2014-06-13 01:58:52 +02:00
|
|
|
var scripts = bitcoin.scripts
|
2014-05-13 09:55:53 +02:00
|
|
|
|
|
|
|
var Address = bitcoin.Address
|
|
|
|
var ECKey = bitcoin.ECKey
|
2014-07-25 08:18:13 +02:00
|
|
|
var TransactionBuilder = bitcoin.TransactionBuilder
|
2014-04-27 01:13:03 +02:00
|
|
|
|
|
|
|
var helloblock = require('helloblock-js')({
|
|
|
|
network: 'testnet'
|
2014-05-27 17:52:35 +02:00
|
|
|
})
|
2014-04-27 01:13:03 +02:00
|
|
|
|
2014-05-27 17:52:35 +02:00
|
|
|
describe('Bitcoin-js', function() {
|
|
|
|
this.timeout(10000)
|
2014-04-27 01:13:03 +02:00
|
|
|
|
2014-05-27 17:52:35 +02:00
|
|
|
it('can spend from a 2-of-2 address', function(done) {
|
2014-04-27 01:13:03 +02:00
|
|
|
var privKeys = [
|
|
|
|
'91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgwmaKkrx',
|
|
|
|
'91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgww7vXtT'
|
|
|
|
].map(function(wif) {
|
|
|
|
return ECKey.fromWIF(wif)
|
|
|
|
})
|
|
|
|
|
2014-05-27 17:52:35 +02:00
|
|
|
var coldAmount = 2e4
|
|
|
|
var outputAmount = 1e4
|
|
|
|
|
|
|
|
var pubKeys = privKeys.map(function(eck) { return eck.pub })
|
2014-06-13 01:58:52 +02:00
|
|
|
var redeemScript = scripts.multisigOutput(2, pubKeys)
|
|
|
|
var scriptPubKey = scripts.scriptHashOutput(redeemScript.getHash())
|
2014-05-27 17:52:35 +02:00
|
|
|
|
2014-06-13 03:30:07 +02:00
|
|
|
var multisigAddress = Address.fromOutputScript(scriptPubKey, networks.testnet).toString()
|
2014-06-04 16:06:28 +02:00
|
|
|
|
|
|
|
// Attempt to send funds to the source address, providing some unspents for later
|
2014-05-27 17:52:35 +02:00
|
|
|
helloblock.faucet.withdraw(multisigAddress, coldAmount, function(err) {
|
|
|
|
if (err) return done(err)
|
|
|
|
})
|
|
|
|
|
|
|
|
// make a random private key
|
2014-06-04 16:06:07 +02:00
|
|
|
var targetAddress = ECKey.makeRandom().pub.getAddress(networks.testnet).toString()
|
2014-05-27 17:52:35 +02:00
|
|
|
|
|
|
|
// get latest unspents from the multisigAddress
|
2014-07-25 08:18:13 +02:00
|
|
|
helloblock.addresses.getUnspents(multisigAddress, function(err, res, unspents) {
|
2014-05-27 17:52:35 +02:00
|
|
|
if (err) return done(err)
|
|
|
|
|
|
|
|
// use the oldest unspent
|
2014-07-25 08:18:13 +02:00
|
|
|
var unspent = unspents[unspents.length - 1]
|
2014-05-27 17:52:35 +02:00
|
|
|
var spendAmount = Math.min(unspent.value, outputAmount)
|
|
|
|
|
2014-07-25 08:18:13 +02:00
|
|
|
var txb = new TransactionBuilder()
|
|
|
|
txb.addInput(unspent.txHash, unspent.index)
|
|
|
|
txb.addOutput(targetAddress, spendAmount)
|
2014-05-27 17:52:35 +02:00
|
|
|
|
2014-07-25 08:18:13 +02:00
|
|
|
privKeys.forEach(function(privKey) {
|
|
|
|
txb.sign(0, privKey, redeemScript)
|
2014-05-27 17:52:35 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
// broadcast our transaction
|
2014-07-25 08:18:13 +02:00
|
|
|
helloblock.transactions.propagate(txb.build().toHex(), function(err, res) {
|
2014-05-27 17:52:35 +02:00
|
|
|
// no err means that the transaction has been successfully propagated
|
|
|
|
if (err) return done(err)
|
|
|
|
|
|
|
|
// Check that the funds (spendAmount Satoshis) indeed arrived at the intended address
|
2014-07-25 08:18:13 +02:00
|
|
|
helloblock.addresses.get(targetAddress, function(err, res, addrInfo) {
|
2014-05-27 17:52:35 +02:00
|
|
|
if (err) return done(err)
|
|
|
|
|
2014-07-25 08:18:13 +02:00
|
|
|
assert.equal(addrInfo.balance, spendAmount)
|
2014-05-27 17:52:35 +02:00
|
|
|
done()
|
2014-04-27 01:13:03 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|