diff --git a/integration/regtest.js b/integration/regtest.js index bc313a3b..19fe67fa 100644 --- a/integration/regtest.js +++ b/integration/regtest.js @@ -21,12 +21,31 @@ var assert = chai.assert; var sinon = require('sinon'); var BitcoinRPC = require('bitcoind-rpc'); var blockHashes = []; +var unspentTransactions = []; +var coinbasePrivateKey; +var privateKey = bitcore.PrivateKey(); +var destKey = bitcore.PrivateKey(); -describe('Basic Functionality', function() { +describe('Daemon Binding Functionality', function() { before(function(done) { this.timeout(30000); + // Add the regtest network + bitcore.Networks.remove(bitcore.Networks.testnet); + bitcore.Networks.add({ + name: 'regtest', + alias: 'regtest', + pubkeyhash: 0x6f, + privatekey: 0xef, + scripthash: 0xc4, + xpubkey: 0x043587cf, + xprivkey: 0x04358394, + networkMagic: 0xfabfb5da, + port: 18444, + dnsSeeds: [ ] + }); + var datadir = __dirname + '/data'; rimraf(datadir + '/regtest', function(err) { @@ -62,18 +81,63 @@ describe('Basic Functionality', function() { console.log('Generating 100 blocks...'); - client.generate(100, function(err, response) { + // Generate enough blocks so that the initial coinbase transactions + // can be spent. + + client.generate(150, function(err, response) { if (err) { throw err; } blockHashes = response.result; - done(); + + // The original coinbase transactions when using regtest spend to + // a non-standard pubkeyout instead of a pubkeyhashout. + // We'll construct a new transaction that will send funds + // to a new address with pubkeyhashout for later testing. + + console.log('Preparing unspent outputs...'); + + client.getBalance(function(err, response) { + if (err) { + throw err; + } + + var amount = response.result; + var fee = 0.0001; + var network = bitcore.Networks.get('regtest'); + var address = privateKey.toAddress(network); + + client.sendToAddress(address, amount - fee, '', '', function(err, response) { + if (err) { + throw err; + } + + var txid = response.result; + client.getTransaction(txid, function(err, response) { + if (err) { + throw err; + } + + var tx = bitcore.Transaction(); + tx.fromString(response.result.hex); + unspentTransactions.push(tx); + + // Include this transaction in a block so that it can + // be spent in tests + client.generate(1, function(err, response) { + if (err) { + throw err; + } + + console.log('Testing setup complete!'); + done(); + }); + }); + }); + }); }); - }); - }); - }); after(function(done) { @@ -169,4 +233,32 @@ describe('Basic Functionality', function() { }); }); + describe('send transaction functionality', function() { + + it('will not error and return the transaction hash', function() { + + var unspentTx = unspentTransactions.shift(); + + var utxo = { + txid: unspentTx.hash, + outputIndex: 1, + script: unspentTx.outputs[1].script, + satoshis: unspentTx.outputs[1].satoshis + }; + + // create and sign the transaction + var tx = bitcore.Transaction(); + tx.from(utxo); + tx.change(privateKey.toAddress()); + tx.to(destKey.toAddress(), 100000); + tx.sign(privateKey); + + // test sending the transaction + var hash = bitcoind.sendTransaction(tx.serialize()); + hash.should.equal(tx.hash); + + }); + + }); + }); diff --git a/lib/daemon.js b/lib/daemon.js index 4e529ebc..e5487e40 100644 --- a/lib/daemon.js +++ b/lib/daemon.js @@ -344,6 +344,10 @@ Daemon.prototype.getChainWork = function(blockHash) { return bitcoindjs.getChainWork(blockHash); }; +Daemon.prototype.sendTransaction = function(transaction, allowAbsurdFees) { + return bitcoindjs.sendTransaction(transaction, allowAbsurdFees); +}; + Daemon.prototype.getTransaction = function(txid, queryMempool, callback) { return bitcoindjs.getTransaction(txid, queryMempool, callback); };