Fixes to allow bcoin to work with Transaction controller.

This commit is contained in:
Chris Kleeschulte 2017-08-10 18:56:13 -04:00
parent 5a49f5c6c2
commit b63a2208fc
2 changed files with 48 additions and 33 deletions

View File

@ -7,6 +7,8 @@ var Common = require('./common');
function AddressController(node) { function AddressController(node) {
this.node = node; this.node = node;
this._address = this.node.services.address;
this._block = this.node.services.block;
this.txController = new TxController(node); this.txController = new TxController(node);
this.common = new Common({log: this.node.log}); this.common = new Common({log: this.node.log});
} }
@ -110,7 +112,7 @@ AddressController.prototype.check = function(req, res, next, addresses) {
for(var i = 0; i < addresses.length; i++) { for(var i = 0; i < addresses.length; i++) {
try { try {
var a = new bitcore.Address(addresses[i]); new bitcore.Address(addresses[i]);
} catch(e) { } catch(e) {
return self.common.handleErrors({ return self.common.handleErrors({
message: 'Invalid address: ' + e.message, message: 'Invalid address: ' + e.message,
@ -159,7 +161,8 @@ AddressController.prototype.transformUtxo = function(utxoArg) {
}; };
if (utxoArg.height && utxoArg.height > 0) { if (utxoArg.height && utxoArg.height > 0) {
utxo.height = utxoArg.height; utxo.height = utxoArg.height;
utxo.confirmations = this.node.services.bitcoind.height - utxoArg.height + 1; var height = this._block.getTip().height;
utxo.confirmations = height - utxoArg.height + 1;
} else { } else {
utxo.confirmations = 0; utxo.confirmations = 0;
} }
@ -177,7 +180,7 @@ AddressController.prototype._getTransformOptions = function(req) {
}; };
}; };
AddressController.prototype.multitxs = function(req, res, next) { AddressController.prototype.multitxs = function(req, res) {
var self = this; var self = this;
var options = { var options = {

View File

@ -2,6 +2,7 @@
var bitcore = require('bitcore-lib'); var bitcore = require('bitcore-lib');
var _ = bitcore.deps._; var _ = bitcore.deps._;
var bcoin = require('bcoin');
var $ = bitcore.util.preconditions; var $ = bitcore.util.preconditions;
var Common = require('./common'); var Common = require('./common');
var async = require('async'); var async = require('async');
@ -11,6 +12,7 @@ var MAXINT = 0xffffffff; // Math.pow(2, 32) - 1;
function TxController(node) { function TxController(node) {
this.node = node; this.node = node;
this.common = new Common({log: this.node.log}); this.common = new Common({log: this.node.log});
this._block = this.node.services.block;
} }
TxController.prototype.show = function(req, res) { TxController.prototype.show = function(req, res) {
@ -27,12 +29,14 @@ TxController.prototype.transaction = function(req, res, next) {
var txid = req.params.txid; var txid = req.params.txid;
this.node.getDetailedTransaction(txid, function(err, transaction) { this.node.getDetailedTransaction(txid, function(err, transaction) {
if (err && err.code === -5) { if (err) {
return self.common.handleErrors(null, res);
} else if(err) {
return self.common.handleErrors(err, res); return self.common.handleErrors(err, res);
} }
if (!transaction) {
return self.common.handleErrors(null, res);
}
self.transformTransaction(transaction, function(err, transformedTransaction) { self.transformTransaction(transaction, function(err, transformedTransaction) {
if (err) { if (err) {
return self.common.handleErrors(err, res); return self.common.handleErrors(err, res);
@ -50,49 +54,50 @@ TxController.prototype.transformTransaction = function(transaction, options, cal
options = {}; options = {};
} }
$.checkArgument(_.isFunction(callback)); $.checkArgument(_.isFunction(callback));
var confirmations = 0; var confirmations = 0;
if(transaction.height >= 0) { if(transaction.__height >= 0) {
confirmations = this.node.services.bitcoind.height - transaction.height + 1; var height = this._block.height;
confirmations = height - transaction.__height + 1;
} }
var transformed = { var transformed = {
txid: transaction.hash, txid: transaction.txid(),
version: transaction.version, version: transaction.version,
locktime: transaction.locktime locktime: transaction.locktime
}; };
if(transaction.coinbase) { if(transaction.inputs[0].isCoinbase()) {
transformed.vin = [ transformed.vin = [
{ {
coinbase: transaction.inputs[0].script, coinbase: transaction.inputs[0].script.toJSON(),
sequence: transaction.inputs[0].sequence, sequence: transaction.inputs[0].sequence,
n: 0 n: 0
} }
]; ];
} else { } else {
options.inputiValues = transaction.__inputValues;
transformed.vin = transaction.inputs.map(this.transformInput.bind(this, options)); transformed.vin = transaction.inputs.map(this.transformInput.bind(this, options));
} }
transformed.vout = transaction.outputs.map(this.transformOutput.bind(this, options)); transformed.vout = transaction.outputs.map(this.transformOutput.bind(this, options));
transformed.blockhash = transaction.blockHash; transformed.blockhash = transaction.blockHash;
transformed.blockheight = transaction.height; transformed.blockheight = transaction.__height;
transformed.confirmations = confirmations; transformed.confirmations = transaction.confirmations;
// TODO consider mempool txs with receivedTime? // TODO consider mempool txs with receivedTime?
var time = transaction.blockTimestamp ? transaction.blockTimestamp : Math.round(Date.now() / 1000); var time = transaction.__timestamp ? transaction.__timestamp : Math.round(Date.now() / 1000);
transformed.time = time; transformed.time = time;
if (transformed.confirmations) { if (transformed.confirmations) {
transformed.blocktime = transformed.time; transformed.blocktime = transformed.time;
} }
if(transaction.coinbase) { if(transaction.inputs[0].isCoinbase()) {
transformed.isCoinBase = true; transformed.isCoinBase = true;
} }
transformed.valueOut = transaction.outputSatoshis / 1e8; transformed.valueOut = transaction.outputSatoshis / 1e8;
transformed.size = transaction.hex.length / 2; // in bytes transformed.size = transaction.getSize();
if (!transaction.coinbase) { if (!transaction.inputs[0].isCoinbase()) {
transformed.valueIn = transaction.inputSatoshis / 1e8; transformed.valueIn = transaction.inputSatoshis / 1e8;
transformed.fees = transaction.feeSatoshis / 1e8; transformed.fees = transaction.feeSatoshis / 1e8;
} }
@ -103,24 +108,30 @@ TxController.prototype.transformTransaction = function(transaction, options, cal
TxController.prototype.transformInput = function(options, input, index) { TxController.prototype.transformInput = function(options, input, index) {
// Input scripts are validated and can be assumed to be valid // Input scripts are validated and can be assumed to be valid
var transformed = { var transformed = {
txid: input.prevTxId, txid: input.prevout.txid(),
vout: input.outputIndex, vout: input.prevout.index,
sequence: input.sequence, sequence: input.sequence,
n: index n: index
}; };
if (!options.noScriptSig) { if (!options.noScriptSig) {
transformed.scriptSig = { transformed.scriptSig = {
hex: input.script hex: input.script.toJSON()
}; };
if (!options.noAsm) { if (!options.noAsm) {
transformed.scriptSig.asm = input.scriptAsm; transformed.scriptSig.asm = input.script.toASM();
} }
} }
transformed.addr = input.address; var address = input.getAddress();
transformed.valueSat = input.satoshis; if (address) {
transformed.value = input.satoshis / 1e8; address.network = this.node.network || 'main';
transformed.addr = address.toString();
} else {
transformed.addr = null;
}
transformed.valueSat = options.inputValues[index];
transformed.value = transformed.valueSat / 1e8;
transformed.doubleSpentTxID = null; // TODO transformed.doubleSpentTxID = null; // TODO
//transformed.isConfirmed = null; // TODO //transformed.isConfirmed = null; // TODO
//transformed.confirmations = null; // TODO //transformed.confirmations = null; // TODO
@ -131,27 +142,28 @@ TxController.prototype.transformInput = function(options, input, index) {
TxController.prototype.transformOutput = function(options, output, index) { TxController.prototype.transformOutput = function(options, output, index) {
var transformed = { var transformed = {
value: (output.satoshis / 1e8).toFixed(8), value: (output.value / 1e8).toFixed(8),
n: index, n: index,
scriptPubKey: { scriptPubKey: {
hex: output.script hex: output.script.toJSON()
} }
}; };
if (!options.noAsm) { if (!options.noAsm) {
transformed.scriptPubKey.asm = output.scriptAsm; transformed.scriptPubKey.asm = output.script.toASM();
} }
if (!options.noSpent) { if (!options.noSpent) {
transformed.spentTxId = output.spentTxId || null; transformed.spentTxId = output.spentTxId || null; // we aren't tracking this with the bcoin implementation
transformed.spentIndex = _.isUndefined(output.spentIndex) ? null : output.spentIndex; transformed.spentIndex = _.isUndefined(output.spentIndex) ? null : output.spentIndex;
transformed.spentHeight = output.spentHeight || null; transformed.spentHeight = output.spentHeight || null;
} }
if (output.address) { var address = output.getAddress();
transformed.scriptPubKey.addresses = [output.address]; if (address) {
var address = bitcore.Address(output.address); //TODO return type from bitcore-node address.network = this.node.network || 'main';
transformed.scriptPubKey.type = address.type; transformed.scriptPubKey.addresses = [address.toString()];
transformed.scriptPubKey.type = address.getType();
} }
return transformed; return transformed;
}; };