From bd868cda7aca1c2a98c186afc1cbbb7988b0d01f Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sat, 27 Feb 2016 05:51:48 -0800 Subject: [PATCH] segwit things --- lib/bcoin/block.js | 16 +++++++++++++++- lib/bcoin/chain.js | 10 ++++++++-- lib/bcoin/protocol/framer.js | 2 +- lib/bcoin/script.js | 4 ++++ lib/bcoin/tx.js | 4 ++++ lib/bcoin/wallet.js | 2 +- 6 files changed, 33 insertions(+), 5 deletions(-) diff --git a/lib/bcoin/block.js b/lib/bcoin/block.js index 7c010f61..6edf09f9 100644 --- a/lib/bcoin/block.js +++ b/lib/bcoin/block.js @@ -49,6 +49,9 @@ Block.prototype.render = function render() { return bcoin.protocol.framer.block(this); } + if (this.hasWitness()) + return bcoin.protocol.framer.block(this); + this._raw = bcoin.protocol.framer.block(this); this._size = this._raw.length; this._witness = false; @@ -63,6 +66,9 @@ Block.prototype.renderWitness = function renderWitness() { return bcoin.protocol.framer.witnessBlock(this); } + if (!this.hasWitness()) + return bcoin.protocol.framer.witnessBlock(this); + this._raw = bcoin.protocol.framer.witnessBlock(this); this._size = this._raw.length; this._witness = true; @@ -80,6 +86,14 @@ Block.prototype.getCost = function getCost() { return this._cost; }; +Block.prototype.hasWitness = function hasWitness() { + for (var i = 0; i < this.txs.length; i++) { + if (this.txs[i].hasWitness()) + return true; + } + return false; +}; + Block.prototype.getSigopsCost = function getSigopCost(scriptHash) { var cost = 0; var i; @@ -128,7 +142,7 @@ Block.prototype.getCommitmentHash = function getCommitmentHash() { }; Block.prototype.__defineGetter__('commitmentHash', function() { - var coinbase, i, commitment, witnessNonce, commitmentHash; + var coinbase, i, commitment, commitmentHash; if (this._commitmentHash) return this._commitmentHash; diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index 7e6d2f71..d27c1372 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -348,7 +348,7 @@ Chain.prototype._preload = function _preload(callback) { blocks.forEach(function(data) { var entry = bcoin.chainblock.fromRaw(self, height, data); - var block = bcoin.block(entry, 'headers'); + var block = bcoin.headers(entry); var start; // Do some paranoid checks. @@ -364,7 +364,7 @@ Chain.prototype._preload = function _preload(callback) { // Verify the block headers. We don't want to // trust an external centralized source completely. - if (!block.verify()) { + if (!block.verifyHeaders()) { start = Math.max(0, height - 2); stream.destroy(); self.resetHeightAsync(start, function(e) { @@ -655,6 +655,12 @@ Chain.prototype._checkInputs = function _checkInputs(block, prev, flags, callbac var i, j, input, hash; var sigops = 0; + utils.print(height); + utils.print(block.commitmentHash ? utils.revHex(block.commitmentHash) : null); + utils.print(utils.revHex(block.getCommitmentHash() || '00')); + utils.print(block.txs[0]); + utils.print(block.txs[1]); + if (err) return callback(err); diff --git a/lib/bcoin/protocol/framer.js b/lib/bcoin/protocol/framer.js index 71699f5d..c84ade76 100644 --- a/lib/bcoin/protocol/framer.js +++ b/lib/bcoin/protocol/framer.js @@ -586,7 +586,7 @@ Framer._block = function _block(block, witness) { var i, tx, p; for (i = 0; i < block.txs.length; i++) { - tx = witness + tx = witness && block.txs[i].hasWitness() ? Framer.witnessTX(block.txs[i]) : Framer.tx(block.txs[i]); txs.push(tx); diff --git a/lib/bcoin/script.js b/lib/bcoin/script.js index a4a9441b..eb7bbab9 100644 --- a/lib/bcoin/script.js +++ b/lib/bcoin/script.js @@ -461,8 +461,12 @@ script.verifyProgram = function verifyProgram(witness, output, tx, i, flags) { } } + utils.print(script.format(stack)); + res = script.execute(redeem, stack, tx, i, flags); + utils.print(script.format(redeem)); + utils.print(script.format(stack)); // Verify the script did not fail as well as the stack values if (!res || stack.length === 0 || !script.bool(stack.pop())) { throw new Error('script failed'); diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index f060899d..9e24aa48 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -120,6 +120,10 @@ TX.prototype.witnessHash = function witnessHash(enc) { : new Buffer(constants.zeroHash); } + // if (!this._witness) + if (!this.hasWitness()) + return this.hash(enc); + if (!this._whash) this._whash = utils.dsha256(this.renderWitness()); diff --git a/lib/bcoin/wallet.js b/lib/bcoin/wallet.js index 3820a1d2..d4d8e72f 100644 --- a/lib/bcoin/wallet.js +++ b/lib/bcoin/wallet.js @@ -44,7 +44,7 @@ function Wallet(options) { options.master = bcoin.hd.fromSeed(); this.options = options; - this.db = options.db || new bcoin.walletdb({ type: 'file' }); + this.db = options.db || new bcoin.walletdb(); this.addresses = []; this.master = options.master || null; this.addressMap = options.addressMap || {};