From 4563a8ffaadb18cbd489f90f79ccd6d2fc96a631 Mon Sep 17 00:00:00 2001 From: Chris Kleeschulte Date: Tue, 29 Aug 2017 16:09:31 -0400 Subject: [PATCH] Fixed network for bcoin. --- .travis.yml | 3 +- lib/status.js | 1 + lib/transactions.js | 7 ++- regtest/address.js | 123 +++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 129 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index aa9deeb..e75ffc1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,5 @@ language: node_js node_js: - - 'v0.12.7' - - 'v4' + - 'v8' install: - npm install diff --git a/lib/status.js b/lib/status.js index e088b3a..d7929d9 100644 --- a/lib/status.js +++ b/lib/status.js @@ -7,6 +7,7 @@ function StatusController(node) { this.common = new Common({log: this.node.log}); this._block = this.node.services.block; this._header = this.node.services.header; + this._block = this.node.services.block; } StatusController.prototype.show = function(req, res) { diff --git a/lib/transactions.js b/lib/transactions.js index 1391eed..0f5ff2a 100644 --- a/lib/transactions.js +++ b/lib/transactions.js @@ -14,6 +14,9 @@ function TxController(node) { this._block = this.node.services.block; this._transaction = this.node.services.transaction; this._address = this.node.services.address; + if (this.node.network === 'livenet') { + this._network = 'main'; + } } TxController.prototype.show = function(req, res) { @@ -125,7 +128,7 @@ TxController.prototype.transformInput = function(options, input, index) { var address = input.getAddress(); if (address) { - address.network = this.node.network || 'main'; + address.network = this._network || 'main'; transformed.addr = address.toString(); } else { transformed.addr = null; @@ -162,7 +165,7 @@ TxController.prototype.transformOutput = function(options, output, index) { var address = output.getAddress(); if (address) { - address.network = this.node.network || 'main'; + address.network = this._network || 'main'; transformed.scriptPubKey.addresses = [address.toString()]; transformed.scriptPubKey.type = address.getType(); } diff --git a/regtest/address.js b/regtest/address.js index f19298b..d49215b 100644 --- a/regtest/address.js +++ b/regtest/address.js @@ -8,6 +8,9 @@ var fs = require('fs'); var async = require('async'); var RPC = require('bitcoind-rpc'); var http = require('http'); +var bitcore = require('bitcore-lib'); +var PrivateKey = bitcore.PrivateKey; +var Transaction = bitcore.Transaction; var rpc1Address; var rpc2Address; @@ -308,7 +311,7 @@ describe('Address', function() { function(next) { setTimeout(function() { startBitcore(next); - }, 6000); + }, 8000); } ], function(err) { if (err) { @@ -325,6 +328,8 @@ describe('Address', function() { }); }); + + it('should get address info correctly: /addr/:addr', function(done) { @@ -761,6 +766,122 @@ describe('Address', function() { }); + it('should index addresses correctly', function(done) { + // if we send a tx that has an address in both the input and the output, does it index correctly? + var txid; + var pk1; + var tx; + async.waterfall([ + function(next) { + rpc1.listUnspent(function(err, res) { + + if (err) { + return next(err); + } + + next(null, res.result[0]); + + }); + }, + function(utxo, next) { + rpc1.dumpPrivKey(utxo.address, function(err, res) { + if (err) { + return next(err); + } + var pk = new PrivateKey(res.result); + pk1 = new PrivateKey('testnet'); + var change = new PrivateKey('testnet'); + var changeAddress = change.toAddress(); + var from = { + txId: utxo.txid, + address: utxo.address, + script: utxo.scriptPubKey, + satoshis: utxo.amount * 1e8, + outputIndex: utxo.vout + }; + tx = new Transaction().from(from).to(pk1.toAddress(), 2500000000).change(changeAddress).sign(pk); + + rpc2.sendRawTransaction(tx.serialize(), function(err, res) { + if (err) { + return next(err); + } + txid = res.result; + console.log(txid); + next(); + }); + }); + }, + function(next) { + rpc2.generate(1, function() { + setTimeout(next, 2000); + }); + }, + function(next) { + var tx2 = new Transaction().from({ + txId: txid, + satoshis: 2500000000, + outputIndex: 0, + script: tx.outputs[0].script.toHex(), + address: pk1.toAddress() + }).to(pk1.toAddress(), 2500000000 - 1000).sign(pk1); + rpc2.sendRawTransaction(tx2.serialize(), function(err, res) { + if (err) { + return next(err); + } + txid = res.result; + console.log(txid); + next(); + + }); + }, + function(next) { + rpc2.generate(1, function() { + setTimeout(next, 2000); + }); + }, + ], function(err) { + if (err) { + return done(err); + } + + var request = http.request('http://localhost:53001/api/addr/' + pk1.toAddress(), function(res) { + + var error; + if (res.statusCode !== 200 && res.statusCode !== 201) { + if (error) { + return; + } + return done('Error from bitcore-node webserver: ' + res.statusCode); + } + + var resError; + var resData = ''; + + res.on('error', function(e) { + resError = e; + }); + + res.on('data', function(data) { + resData += data; + }); + + res.on('end', function() { + if (error) { + return; + } + var data = JSON.parse(resData); + console.log(data); + expect(data.transactions.length).to.equal(2); + done(); + }); + + }); + request.write(''); + request.end(); + + }); + + }); });