diff --git a/lib/bcoin/block.js b/lib/bcoin/block.js index ae6dfcfe..46311e57 100644 --- a/lib/bcoin/block.js +++ b/lib/bcoin/block.js @@ -14,6 +14,7 @@ var constants = bcoin.protocol.constants; function Block(data, subtype) { var self = this; + var tx; if (!(this instanceof Block)) return new Block(data, subtype); @@ -57,6 +58,13 @@ function Block(data, subtype) { tx.ts = tx.ts || self.ts; return tx; }); + + if (this.version >= 2) { + tx = this.txs[0]; + if (tx && tx.inputs[0] && +tx.inputs[0].out.hash === 0) + this._height = bcoin.script.coinbase(tx.inputs[0].script); + } + this.invalid = !this._checkBlock(); } diff --git a/lib/bcoin/script.js b/lib/bcoin/script.js index 7e9bf29d..0c9e7751 100644 --- a/lib/bcoin/script.js +++ b/lib/bcoin/script.js @@ -1138,6 +1138,20 @@ script.isScripthashInput = function isScripthashInput(s, redeem) { return keys; }; +script.coinbase = function coinbase(s) { + var base = s._raw || script.encode(s); + var len = Math.min(base[0], base.length); + var height = 0; + var i, b; + + for (i = 0; i < len; i++) { + b = base[i + 1] & 0xff; + height |= b << (8 * i); + } + + return height; +}; + // https://github.com/bitcoin/bips/blob/master/bip-0066.mediawiki /** * A canonical signature exists of: <30> <02> <02>