From 6a482af6d078b34278140082f014edc6e6688850 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 6 May 2016 15:33:19 -0700 Subject: [PATCH] coinbase flags. --- bin/bcoin-cli | 20 ++++++++++++++++++++ lib/bcoin/script.js | 30 +++++++++++++++--------------- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/bin/bcoin-cli b/bin/bcoin-cli index 9abc8fcb..92bb92c6 100755 --- a/bin/bcoin-cli +++ b/bin/bcoin-cli @@ -120,6 +120,12 @@ function getTX(callback) { client.getTX(hash, function(err, tx) { if (err) return callback(err); + + if (!tx) { + utils.print('TX not found.'); + return callback(); + } + utils.print(tx); callback(); }); @@ -134,7 +140,15 @@ function getBlock(callback) { client.getBlock(hash, function(err, block) { if (err) return callback(err); + + if (!block) { + utils.print('Block not found.'); + return callback(); + } + utils.print(block); + utils.print('Coinbase Data:'); + utils.print(block.txs[0].inputs[0].script.getCoinbaseFlags()); callback(); }); } @@ -154,6 +168,12 @@ function getCoin(callback) { client.getCoin(hash, index, function(err, coin) { if (err) return callback(err); + + if (!coin) { + utils.print('Coin not found.'); + return callback(); + } + utils.print(coin); callback(); }); diff --git a/lib/bcoin/script.js b/lib/bcoin/script.js index 1cb887b6..06a9c51c 100644 --- a/lib/bcoin/script.js +++ b/lib/bcoin/script.js @@ -3051,28 +3051,28 @@ Script.getCoinbaseHeight = function getCoinbaseHeight(code) { * `extraNonce`, `flags`, and `text`. */ -Script.prototype.getCoinbaseData = function getCoinbaseData() { +Script.prototype.getCoinbaseFlags = function getCoinbaseFlags() { var coinbase = {}; - var flags; + var index = 0; + var nonce; coinbase.height = this.getCoinbaseHeight(); - if (Buffer.isBuffer(this.code[1])) - coinbase.extraNonce = new bn(this.code[1], 'le'); - else - coinbase.extraNonce = new bn(0); + if (coinbase.height !== -1) + index++; - flags = this.code.slice(2).filter(function(chunk) { - return Buffer.isBuffer(chunk) && chunk.length !== 0; - }); + if (Buffer.isBuffer(this.code[1]) && this.code[1].length <= 6) { + coinbase.extraNonce = new bn(this.code[1], 'le').toNumber(); + } else { + nonce = Script.getSmall(this.code[1]); + coinbase.extraNonce = nonce == null ? -1 : nonce; + } - coinbase.flags = flags; + coinbase.flags = Script.encode(this.code.slice(index)); - flags = flags.map(function(flag) { - return flag.toString('utf8'); - }); - - coinbase.text = flags.join('').replace(/[\u0000-\u0019\u007f-\u00ff]/g, ''); + coinbase.text = coinbase.flags + .toString('utf8') + .replace(/[\u0000-\u0019\u007f-\u00ff]/g, ''); return coinbase; };