var assert = require('assert') 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 var helloblock = require('helloblock-js')({ network: 'testnet' }); describe('p2sh', function() { this.timeout(10000); it('spends from a 2-of-2 address', function(done) { var privKeys = [ '91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgwmaKkrx', '91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgww7vXtT' ].map(function(wif) { return ECKey.fromWIF(wif) }) var pubKeys = privKeys.map(function(eck) { return eck.pub }) var redeemScript = Script.createMultisigScriptPubKey(2, pubKeys) var hash160 = crypto.hash160(redeemScript.buffer) var multisigAddress = new Address(hash160, networks.testnet.scriptHash) // 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) tx.addOutput(targetAddress, 100000, networks.testnet) var signatures = privKeys.map(function(privKey) { return tx.signScriptSig(0, redeemScript, privKey) }) var redeemScriptSig = Script.createMultisigScriptSig(signatures) var scriptSig = Script.createP2SHScriptSig(redeemScriptSig, redeemScript) tx.setScriptSig(0, scriptSig) // Send from mutlsigAddress to targetAddress helloblock.transactions.propagate(tx.toHex(), function(err, resp, resource) { // 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() }) }) }) }) }) }) })