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 pubKeyBuffers = pubKeys.map(function(q) {
      return q.toBuffer()
    })
    var redeemScript = Script.createMultisigOutputScript(2, pubKeyBuffers)
    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 scriptSig = Script.createP2SHMultisigScriptSig(signatures, redeemScript)
          tx.setScriptSig(0, scriptSig)

          // Send from mutlsigAddress to targetAddress
          helloblock.transactions.propagate(tx.serializeHex(), 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()
            })
          })
        })
      })
    })
  })
})

@@ -833,20 +833,20 @@
 isig
-Output
 Script
+PubKey
 (2, 
@@ -2008,17 +2008,23 @@
     var 
-s
+redeemS
 criptSig
@@ -2043,12 +2043,8 @@
 eate
-P2SH
 Mult
@@ -2067,16 +2067,86 @@
 gnatures
+)%0A          var scriptSig = Script.createP2SHScriptSig(redeemScriptSig
 , redeem

fix integration test