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 crypto = bitcoin.crypto
|
|
|
|
var networks = bitcoin.networks
|
|
|
|
|
|
|
|
var Address = bitcoin.Address
|
|
|
|
var ECKey = bitcoin.ECKey
|
|
|
|
var Transaction = bitcoin.Transaction
|
|
|
|
var Script = bitcoin.Script
|
2014-04-27 01:13:03 +02:00
|
|
|
|
|
|
|
var helloblock = require('helloblock-js')({
|
|
|
|
network: 'testnet'
|
|
|
|
});
|
|
|
|
|
2014-05-04 07:35:11 +02:00
|
|
|
describe('p2sh', function() {
|
2014-04-27 01:13:03 +02:00
|
|
|
this.timeout(10000);
|
|
|
|
|
2014-05-04 07:35:11 +02:00
|
|
|
it('spends 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)
|
|
|
|
})
|
|
|
|
|
|
|
|
var pubKeys = privKeys.map(function(eck) {
|
|
|
|
return eck.pub
|
|
|
|
})
|
2014-05-08 05:55:57 +02:00
|
|
|
var redeemScript = Script.createMultisigScriptPubKey(2, pubKeys)
|
2014-04-27 01:13:03 +02:00
|
|
|
var hash160 = crypto.hash160(redeemScript.buffer)
|
2014-05-05 05:23:22 +02:00
|
|
|
var multisigAddress = new Address(hash160, networks.testnet.scriptHash)
|
2014-04-27 01:13:03 +02:00
|
|
|
|
|
|
|
// Check what our target address's starting value is
|
|
|
|
var targetAddress = 'mrCDrCybB6J1vRfbwM5hemdJz73FwDBC8r';
|
|
|
|
helloblock.addresses.get(targetAddress, function(err, resp, resource) {
|
|
|
|
if (err) done(err);
|
|
|
|
var startingBalance = resource.balance
|
|
|
|
|
|
|
|
// Send some testnet coins to the multisig address so we ensure it has some unspents
|
|
|
|
helloblock.faucet.withdraw(multisigAddress.toString(), 100000, function(err, resp, resource) {
|
|
|
|
if (err) done(err);
|
|
|
|
|
|
|
|
// Get latest unspents from the mutlsigAddress
|
|
|
|
helloblock.addresses.getUnspents(multisigAddress.toString(), function(err, resp, resource) {
|
|
|
|
if (err) done(err);
|
|
|
|
|
|
|
|
var tx = new Transaction()
|
|
|
|
var unspent = resource[0];
|
|
|
|
tx.addInput(unspent.txHash, unspent.index)
|
2014-05-05 05:23:22 +02:00
|
|
|
tx.addOutput(targetAddress, 100000, networks.testnet)
|
2014-04-27 01:13:03 +02:00
|
|
|
|
|
|
|
var signatures = privKeys.map(function(privKey) {
|
|
|
|
return tx.signScriptSig(0, redeemScript, privKey)
|
|
|
|
})
|
|
|
|
|
2014-05-13 16:01:40 +02:00
|
|
|
var redeemScriptSig = Script.createMultisigScriptSig(signatures)
|
|
|
|
var scriptSig = Script.createP2SHScriptSig(redeemScriptSig, redeemScript)
|
2014-04-27 01:13:03 +02:00
|
|
|
tx.setScriptSig(0, scriptSig)
|
|
|
|
|
|
|
|
// Send from mutlsigAddress to targetAddress
|
2014-05-08 02:44:35 +02:00
|
|
|
helloblock.transactions.propagate(tx.toHex(), function(err, resp, resource) {
|
2014-04-27 01:13:03 +02:00
|
|
|
// no err means that transaction has been successfully propagated
|
|
|
|
if (err) done(err);
|
|
|
|
|
|
|
|
// Check that the funds (100000) indeed arrived at the intended target address
|
|
|
|
helloblock.addresses.get(targetAddress, function(err, resp, resource) {
|
|
|
|
if (err) done(err);
|
|
|
|
assert.equal(resource.balance, startingBalance + 100000)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|