From 7bb20437bc4ba67b1e4cbfe44294c06be71a628c Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sat, 6 Feb 2016 17:12:16 -0800 Subject: [PATCH] refactor tx instantiation. --- lib/bcoin/block.js | 18 ++++++++---------- lib/bcoin/tx.js | 11 +++++++++-- lib/bcoin/wallet.js | 41 +++++++++-------------------------------- 3 files changed, 26 insertions(+), 44 deletions(-) diff --git a/lib/bcoin/block.js b/lib/bcoin/block.js index 3dc0a3a6..2dc99b0c 100644 --- a/lib/bcoin/block.js +++ b/lib/bcoin/block.js @@ -7,6 +7,7 @@ var bcoin = require('../bcoin'); var bn = require('bn.js'); var utils = bcoin.utils; +var assert = utils.assert; var constants = bcoin.protocol.constants; var network = bcoin.protocol.network; @@ -67,13 +68,9 @@ function Block(data, subtype) { } if (this.subtype === 'block') { - this.txs = this.txs.map(function(tx) { - tx.network = self.network; - tx.relayedBy = self.relayedBy; - tx = bcoin.tx(tx); - tx.block = self.hash('hex'); - tx.ts = tx.ts || self.ts; - return tx; + this.txs = this.txs.map(function(data) { + assert(!(data instanceof bcoin.tx)); + return bcoin.tx(data, self); }); } @@ -136,6 +133,7 @@ Block.prototype.getSize = function getSize() { Block.prototype.size = Block.prototype.getSize; Block.prototype.hasTX = function hasTX(hash) { + assert(this.subtype === 'merkleblock'); return this.tx.indexOf(hash) !== -1; }; @@ -328,7 +326,7 @@ Block.prototype.verifyContext = function verifyContext() { } // Ensure the miner's target is equal to what we expect - if (this.bits !== this.chain.target(prev, this)) { + if (this.bits !== this.chain.getTarget(prev, this)) { utils.debug('Block is using wrong target: %s', this.rhash); return false; } @@ -659,8 +657,8 @@ Block.prototype.toJSON = function toJSON() { Block.fromJSON = function fromJSON(json) { var raw, parser, data, block; - utils.assert.equal(json.v, 1); - utils.assert.equal(json.type, 'block'); + assert.equal(json.v, 1); + assert.equal(json.type, 'block'); raw = utils.toArray(json.block, 'hex'); diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index ead12590..e7cb93e8 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -57,8 +57,15 @@ function TX(data, block) { }, this); } - if (block && block.subtype === 'merkleblock') { - if (!data.ts && block && block.hasTX(this.hash('hex'))) { + if (block && !data.ts) { + this.network = true; + this.relayedBy = block.relayedBy; + if (block.subtype === 'merkleblock') { + if (block.hasTX(this.hash('hex'))) { + this.ts = block.ts; + this.block = block.hash('hex'); + } + } else { this.ts = block.ts; this.block = block.hash('hex'); } diff --git a/lib/bcoin/wallet.js b/lib/bcoin/wallet.js index ba0e2649..0456ba9d 100644 --- a/lib/bcoin/wallet.js +++ b/lib/bcoin/wallet.js @@ -794,13 +794,11 @@ Wallet.prototype.setChangeDepth = function setChangeDepth(depth) { return true; }; -Wallet.prototype.getKeyHash = -Wallet.prototype.getKeyhash = function getKeyhash() { +Wallet.prototype.getKeyHash = function getKeyHash() { return this.currentAddress.getKeyHash(); }; -Wallet.prototype.getKeyAddress = -Wallet.prototype.getKeyaddress = function getKeyaddress() { +Wallet.prototype.getKeyAddress = function getKeyAddress() { return this.currentAddress.getKeyAddress(); }; @@ -813,39 +811,18 @@ Wallet.prototype.getAddress = function getAddress() { }; Wallet.prototype.ownInput = function ownInput(tx, index) { - if (tx instanceof bcoin.input) { - var input = tx; - var scriptAddress = input.getScriptAddress(); - if (this._addressTable[scriptAddress] != null) - return true; - var addresses = input.getAddresses(); - var address; - for (var i = 0; i < addresses.length; i++) { - address = addresses[i]; - if (this._addressTable[address] != null) - return true; - } - return false; - } + if (tx instanceof bcoin.input) + return tx.test(this._addressTable) ? [tx] : false; + this.fillPrevout(tx); + return tx.testInputs(this._addressTable, index, true); }; Wallet.prototype.ownOutput = function ownOutput(tx, index) { - if ((tx instanceof bcoin.output) || (tx instanceof bcoin.coin)) { - var output = tx; - var scriptAddress = output.getScriptAddress(); - if (this._addressTable[scriptAddress] != null) - return true; - var addresses = output.getAddresses(); - var address; - for (var i = 0; i < addresses.length; i++) { - address = addresses[i]; - if (this._addressTable[address] != null) - return true; - } - return false; - } + if ((tx instanceof bcoin.output) || (tx instanceof bcoin.coin)) + return tx.test(this._addressTable) ? [tx] : false; + return tx.testOutputs(this._addressTable, index, true); };