From 5791672c07aa5711771378867313f0e0fec33c46 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 18 Dec 2015 13:01:23 -0800 Subject: [PATCH] pass _network to tx. script debugging. tx filled total. --- lib/bcoin/block.js | 3 +++ lib/bcoin/script.js | 13 ++++++++++--- lib/bcoin/tx.js | 13 +++++++++++-- lib/bcoin/wallet.js | 2 +- 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/lib/bcoin/block.js b/lib/bcoin/block.js index f2466ada..d7a595bd 100644 --- a/lib/bcoin/block.js +++ b/lib/bcoin/block.js @@ -30,6 +30,7 @@ function Block(data, subtype) { var self = this; this.txs = data.txs || []; this.txs = this.txs.map(function(tx) { + tx._network = self._network; tx = bcoin.tx(tx); tx.block = self.hash('hex'); tx.ts = tx.ts || self.ts; @@ -231,6 +232,8 @@ Block.fromJSON = function fromJSON(json) { parser.parseMerkleBlock(raw) : parser.parseBlock(raw); + data._network = json._network; + var block = new Block(data, json.subtype); block._hash = json.hash; diff --git a/lib/bcoin/script.js b/lib/bcoin/script.js index ff0d6775..99856b04 100644 --- a/lib/bcoin/script.js +++ b/lib/bcoin/script.js @@ -36,6 +36,12 @@ script.decode = function decode(s) { } var opcode = constants.opcodesByVal[b]; + + if (i >= s.length) { + opcodes.push(opcode || b); + continue; + } + if (opcode === 'pushdata1') { var len = s[i]; i += 1; @@ -68,6 +74,8 @@ script.decode = function decode(s) { } } + utils.hidden(opcodes, '_raw', s); + return opcodes; }; @@ -1124,8 +1132,7 @@ script.format = function(input, output) { var scripts = []; if (input) { - var script = input.script; - scripts.push(script); + scripts.push(input.script); if (input.out.tx && input.out.tx.outputs[input.out.index]) { var prev = input.out.tx.outputs[input.out.index].script; scripts.push(prev); @@ -1135,7 +1142,7 @@ script.format = function(input, output) { } } } else if (output) { - scripts.push(output); + scripts.push(output.script); } scripts = scripts.map(function(script) { diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index a09087e2..422e1148 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -98,6 +98,9 @@ TX.prototype._input = function _input(i, index) { seq: i.seq === undefined ? 0xffffffff : i.seq }; + if (i.script && i.script._raw) + utils.hidden(input.script, '_raw', i.script._raw); + if (input.out.tx) { var prev = input.out.tx.outputs[input.out.index].script; var lock = bcoin.script.lockTime(prev); @@ -389,6 +392,9 @@ TX.prototype.output = function output(options, value) { script: options.script ? options.script.slice() : [] }; + if (options.script && options.script._raw) + utils.hidden(output.script, '_raw', options.script._raw); + this.outputs.push(output); this.scriptOutput(output, options); @@ -404,6 +410,9 @@ TX.prototype.scriptOutput = function scriptOutput(output, options) { var script = output.script ? output.script.slice() : []; + if (output.script && output.script._raw) + utils.hidden(script, '_raw', output.script._raw); + if (Array.isArray(options.keys || options.address)) { // Raw multisig transaction // https://github.com/bitcoin/bips/blob/master/bip-0010.mediawiki @@ -745,6 +754,7 @@ TX.prototype.utxos = function utxos(unspent) { // Still failing to get enough funds if (this.funds('in').cmp(total) < 0) { this.inputs = inputs; + this.total = total; this.outputs.pop(); return null; } @@ -754,6 +764,7 @@ TX.prototype.utxos = function utxos(unspent) { // Clear the tx of everything we added. this.inputs = inputs; + this.total = total; this.outputs.pop(); // Return necessary utxos and change. @@ -769,8 +780,6 @@ TX.prototype.utxos = function utxos(unspent) { TX.prototype.fillUnspent = function fillUnspent(unspent, changeAddress) { var result = unspent.utxos ? unspent : this.utxos(unspent); - this.filled = result; - this.changeAddress = changeAddress || this.changeAddress; if (!result) diff --git a/lib/bcoin/wallet.js b/lib/bcoin/wallet.js index 8d5419aa..2b3e6b9c 100644 --- a/lib/bcoin/wallet.js +++ b/lib/bcoin/wallet.js @@ -535,7 +535,7 @@ Wallet.prototype.fill = function fill(tx, cb) { var result = tx.fillUnspent(this.unspent(), this.getAddress()); if (!result) { var err = new Error('Not enough funds'); - err.minBalance = tx.filled.total; + err.minBalance = tx.total; cb(err); return null; }