From fe4332bfbdc84e00968085da2a650910df6f02db Mon Sep 17 00:00:00 2001 From: sidazhang Date: Sat, 26 Apr 2014 16:13:03 -0700 Subject: [PATCH] integration tests for scripthash transcations --- package.json | 3 +- test/integration.js | 79 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 test/integration.js diff --git a/package.json b/package.json index 1734e24..cb157e3 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,8 @@ "node-browserify": "https://github.com/substack/node-browserify/tarball/master", "sinon": "1.9.0", "coveralls": "~2.10.0", - "mocha-lcov-reporter": "0.0.1" + "mocha-lcov-reporter": "0.0.1", + "helloblock-js": "^0.2.1" }, "testling": { "browsers": [ diff --git a/test/integration.js b/test/integration.js new file mode 100644 index 0000000..ee35261 --- /dev/null +++ b/test/integration.js @@ -0,0 +1,79 @@ +var assert = require('assert'); + +var Address = require('../src/address'); +var ECKey = require('../src/eckey').ECKey; +var T = require('../src/transaction'); +var Transaction = T.Transaction; +var Script = require('../src/script'); +var network = require('../src/network'); +var crypto = require('../src/crypto'); + +var helloblock = require('helloblock-js')({ + network: 'testnet' +}); + +describe('integration', function() { + this.timeout(10000); + + it('scripthash transactions', function(done) { + // 2-of-2 scripthash + 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, network.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, network.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() + }) + }) + }) + }) + }) + }) +})