diff --git a/lib/bcoin/block.js b/lib/bcoin/block.js index cd00c346..727ac7bc 100644 --- a/lib/bcoin/block.js +++ b/lib/bcoin/block.js @@ -215,7 +215,7 @@ Block.prototype.inspect = function inspect() { hash: utils.revHex(this.hash('hex')), reward: utils.btc(this.getReward()), fee: utils.btc(this.getFee()), - date: new Date((this.ts || 0) * 1000).toISOString(), + date: new Date(this.ts * 1000).toISOString(), version: this.version, prevBlock: utils.revHex(this.prevBlock), merkleRoot: utils.revHex(this.merkleRoot), diff --git a/lib/bcoin/input.js b/lib/bcoin/input.js index f7cfb10b..f1c52aa3 100644 --- a/lib/bcoin/input.js +++ b/lib/bcoin/input.js @@ -15,193 +15,33 @@ var constants = bcoin.protocol.constants; */ function Input(options, tx) { - var prevout; - if (!(this instanceof Input)) return new Input(options); assert(typeof options.script !== 'string'); - prevout = options.prevout; - - this.prevout = { - hash: prevout.hash, - index: prevout.index - }; + this.prevout = options.prevout; + this.script = options.script || []; + this.sequence = options.sequence == null ? 0xffffffff : options.sequence; + this._size = options._size || 0; + this._offset = options._offset || 0; + this._mutable = !tx || (tx instanceof bcoin.mtx); if (options.output) this.output = bcoin.coin(options.output); if (Buffer.isBuffer(this.prevout.hash)) this.prevout.hash = utils.toHex(this.prevout.hash); - - this.script = options.script || []; - this.sequence = options.sequence == null ? 0xffffffff : options.sequence; - this._size = options._size || 0; - this._offset = options._offset || 0; - this._mutable = !tx || (tx instanceof bcoin.mtx); } -Input.prototype.__defineGetter__('data', function() { - var data; - - if (this._data) - return this._data; - - data = this.getData(); - - if (!this._mutable) - utils.hidden(this, '_data', data); - - return data; -}); - Input.prototype.__defineGetter__('type', function() { - return this.data.type; -}); - -Input.prototype.__defineGetter__('subtype', function() { - return this.data.subtype; -}); - -Input.prototype.__defineGetter__('signature', function() { - return this.signatures[0]; -}); - -Input.prototype.__defineGetter__('key', function() { - return this.keys[0]; -}); - -Input.prototype.__defineGetter__('hash160', function() { - return this.data.scriptHash || this.hashes[0]; -}); - -Input.prototype.__defineGetter__('id', function() { - return this.address || this.getID(); + return this.getType(); }); Input.prototype.__defineGetter__('address', function() { - return this.data.address; + return this.getAddress(); }); -Input.prototype.__defineGetter__('signatures', function() { - return this.data.signatures || []; -}); - -Input.prototype.__defineGetter__('keys', function() { - return this.data.keys || []; -}); - -Input.prototype.__defineGetter__('hashes', function() { - return this.data.hashes || []; -}); - -Input.prototype.__defineGetter__('addresses', function() { - return this.data.addresses || []; -}); - -Input.prototype.__defineGetter__('redeem', function() { - return this.data.redeem; -}); - -Input.prototype.__defineGetter__('scriptHash', function() { - return this.data.scriptHash; -}); - -Input.prototype.__defineGetter__('scriptAddress', function() { - return this.data.scriptAddress; -}); - -Input.prototype.__defineGetter__('m', function() { - return this.data.m || 1; -}); - -Input.prototype.__defineGetter__('n', function() { - return this.data.n || this.m; -}); - -Input.prototype.__defineGetter__('locktime', function() { - if (!this.output) - return 0; - return this.output.locktime; -}); - -Input.prototype.__defineGetter__('flags', function() { - return this.data.flags; -}); - -Input.prototype.__defineGetter__('text', function() { - return this.data.text; -}); - -Input.prototype.__defineGetter__('value', function() { - if (!this.output) - return; - return this.output.value; -}); - -// Schema and defaults for data object: -// { -// type: String, -// subtype: String, -// side: 'input', -// signatures: Array, -// keys: Array, -// hashes: Array, -// addresses: Array, -// redeem: Array, -// scriptHash: Array, -// scriptAddress: String, -// m: Number, -// n: Number, -// height: Number, -// flags: Array, -// text: String, -// locktime: Number, -// value: bn, -// script: Array, -// sequence: Number, -// prev: String, -// index: Number, -// none: Boolean -// } - -Input.getData = function getData(input) { - var def, data; - - assert(input instanceof Input); - - def = { - side: 'input', - value: new bn(0), - script: input.script, - sequence: input.sequence - }; - - def.prev = input.prevout.hash; - def.index = input.prevout.index; - - if (input.isCoinbase()) { - data = bcoin.script.getCoinbaseData(input.script); - return utils.merge(def, data, { - type: 'coinbase', - none: true - }); - } - - if (input.output) { - data = bcoin.script.getInputData(input.script, input.output.script); - data.value = input.output.value; - return utils.merge(def, data); - } - - return utils.merge(def, bcoin.script.getInputData(input.script)); -}; - -Input.prototype.getData = function getData() { - return Input.getData(this); -}; - Input.prototype.getType = function getType() { var type; @@ -250,12 +90,6 @@ Input.prototype.isFinal = function isFinal() { return this.sequence === 0xffffffff; }; -Input.prototype.getID = function getID() { - var data = bcoin.script.encode(this.script); - var hash = utils.toHex(utils.ripesha(data)); - return '[' + this.type + ':' + hash.slice(0, 7) + ']'; -}; - Input.prototype.getLocktime = function getLocktime() { var output, redeem, lock, type; @@ -277,38 +111,6 @@ Input.prototype.isCoinbase = function isCoinbase() { return +this.prevout.hash === 0; }; -Input.prototype.testScript = function testScript(key, redeem, type) { - // if (!type || type === 'pubkey') { - // if (key) { - // if (bcoin.script.isPubkeyInput(this.script, key, tx, i)) - // return true; - // } - // } - - if (!type || type === 'pubkeyhash') { - if (key) { - if (bcoin.script.isPubkeyhashInput(this.script, key)) - return true; - } - } - - // if (!type || type === 'multisig') { - // if (keys) { - // if (bcoin.script.isMultisigInput(input.script, keys, tx, i)) - // return true; - // } - // } - - if (!type || type === 'scripthash') { - if (redeem) { - if (bcoin.script.isScripthashInput(this.script, redeem)) - return true; - } - } - - return false; -}; - Input.prototype.test = function test(addressTable) { var address = this.getAddress(); @@ -338,7 +140,46 @@ Input.prototype.getSigops = function getSigops(scriptHash, accurate) { return n; }; +Input.prototype.getID = function getID() { + var data = bcoin.script.encode(this.script); + var hash = utils.toHex(utils.ripesha(data)); + return '[' + this.type + ':' + hash.slice(0, 7) + ']'; +}; + +Input.prototype.getData = function getData() { + var def, data; + + assert(this instanceof Input); + + def = { + side: 'input', + value: new bn(0), + script: this.script, + sequence: this.sequence + }; + + def.prev = this.prevout.hash; + def.index = this.prevout.index; + + if (this.isCoinbase()) { + data = bcoin.script.getCoinbaseData(this.script); + return utils.merge(def, data, { + type: 'coinbase', + none: true + }); + } + + if (this.output) { + data = bcoin.script.getInputData(this.script, this.output.script); + data.value = this.output.value; + return utils.merge(def, data); + } + + return utils.merge(def, bcoin.script.getInputData(this.script)); +}; + Input.prototype.inspect = function inspect() { + var data = this.getData(); var output = this.output ? this.output.inspect() : { type: 'unknown', value: '0.0' }; @@ -347,20 +188,20 @@ Input.prototype.inspect = function inspect() { output.index = this.prevout.index; return { - type: this.type, - subtype: this.subtype, - address: this.address, - keys: this.keys.map(utils.toHex), - hashes: this.hashes.map(utils.toHex), - addresses: this.addresses, - scriptAddress: this.scriptAddress, - signatures: this.signatures.map(utils.toHex), - text: this.text, - locktime: this.locktime, + type: this.getType(), + subtype: data.subtype, + address: this.getAddress(), + keys: data.keys.map(utils.toHex), + hashes: data.hashes.map(utils.toHex), + addresses: data.addresses, + scriptAddress: data.scriptAddress, + signatures: data.signatures.map(utils.toHex), + text: data.text, + locktime: data.locktime, value: utils.btc(output.value), - script: bcoin.script.format(this.script), - redeem: this.redeem ? bcoin.script.format(this.redeem) : null, - sequence: this.sequence, + script: bcoin.script.format(data.script), + redeem: data.redeem ? bcoin.script.format(data.redeem) : null, + sequence: data.sequence, output: output }; }; diff --git a/lib/bcoin/output.js b/lib/bcoin/output.js index a62c5c95..1037c742 100644 --- a/lib/bcoin/output.js +++ b/lib/bcoin/output.js @@ -43,132 +43,14 @@ function Output(options, tx) { assert(!(this.value.toArray('be', 8)[0] & 0x80)); } -Output.prototype.__defineGetter__('data', function() { - var data; - - if (this._data) - return this._data; - - data = this.getData(); - - if (!this._mutable) - utils.hidden(this, '_data', data); - - return data; -}); - Output.prototype.__defineGetter__('type', function() { - return this.data.type; -}); - -Output.prototype.__defineGetter__('signature', function() { - return this.signatures[0]; -}); - -Output.prototype.__defineGetter__('key', function() { - return this.keys[0]; -}); - -Output.prototype.__defineGetter__('hash160', function() { - return this.data.scriptHash || this.hashes[0]; -}); - -Output.prototype.__defineGetter__('id', function() { - return this.address || this.getID(); + return this.getType(); }); Output.prototype.__defineGetter__('address', function() { - return this.data.address; + return this.getAddress(); }); -Output.prototype.__defineGetter__('signatures', function() { - return this.data.signatures || []; -}); - -Output.prototype.__defineGetter__('keys', function() { - return this.data.keys || []; -}); - -Output.prototype.__defineGetter__('hashes', function() { - return this.data.hashes || []; -}); - -Output.prototype.__defineGetter__('addresses', function() { - return this.data.addresses || []; -}); - -Output.prototype.__defineGetter__('scriptHash', function() { - return this.data.scriptHash; -}); - -Output.prototype.__defineGetter__('scriptAddress', function() { - return this.data.scriptAddress; -}); - -Output.prototype.__defineGetter__('m', function() { - return this.data.m || 1; -}); - -Output.prototype.__defineGetter__('n', function() { - return this.data.n || this.m; -}); - -Output.prototype.__defineGetter__('locktime', function() { - return bcoin.script.getLocktime(this.script); -}); - -Output.prototype.__defineGetter__('flags', function() { - return this.data.flags; -}); - -Output.prototype.__defineGetter__('text', function() { - return this.data.text; -}); - -// Schema and defaults for data object: -// { -// type: String, -// subtype: String, -// side: 'output', -// signatures: Array, -// keys: Array, -// hashes: Array, -// addresses: Array, -// redeem: Array, -// scriptHash: Array, -// scriptAddress: String, -// m: Number, -// n: Number, -// height: Number, -// flags: Array, -// text: String, -// locktime: Number, -// value: bn, -// script: Array, -// seq: Number, -// prev: String, -// index: Number, -// none: Boolean -// } - -Output.getData = function getData(output) { - var def; - - assert(output instanceof Output); - - def = { - side: 'output', - value: output.value, - script: output.script - }; - - return utils.merge(def, bcoin.script.getOutputData(output.script)); -}; - -Output.prototype.getData = function getData() { - return Output.getData(this); -}; - Output.prototype.getType = function getType() { var type; @@ -197,12 +79,6 @@ Output.prototype.getAddress = function getAddress() { return address; }; -Output.prototype.getID = function getID() { - var data = bcoin.script.encode(this.script); - var hash = utils.toHex(utils.ripesha(data)); - return '[' + this.type + ':' + hash.slice(0, 7) + ']'; -}; - Output.prototype.test = function test(addressTable) { var address = this.getAddress(); @@ -229,23 +105,42 @@ Output.prototype.getSigops = function getSigops(accurate) { return bcoin.script.getSigops(this.script, accurate); }; +Output.prototype.getID = function getID() { + var data = bcoin.script.encode(this.script); + var hash = utils.toHex(utils.ripesha(data)); + return '[' + this.type + ':' + hash.slice(0, 7) + ']'; +}; + +Output.prototype.getData = function getData() { + var def; + + def = { + side: 'output', + value: this.value, + script: this.script + }; + + return utils.merge(def, bcoin.script.getOutputData(this.script)); +}; + Output.prototype.inspect = function inspect() { + var data = this.getData(); return { - type: this.type, - address: this.address, - keys: this.keys.map(utils.toHex), - hashes: this.hashes.map(utils.toHex), - addresses: this.addresses, - scriptAddress: this.scriptAddress, - m: this.m, - n: this.n, - text: this.text, - locktime: this.locktime, - hash: this.hash, + type: this.getType(), + address: this.getAddress(), + keys: data.keys.map(utils.toHex), + hashes: data.hashes.map(utils.toHex), + addresses: data.addresses, + scriptAddress: data.scriptAddress, + m: data.m, + n: data.n, + text: data.text, + locktime: data.locktime, + hash: this.hash ? utils.revHex(this.hash) : undefined, index: this.index, - height: this.height, - value: utils.btc(this.value), - script: bcoin.script.format(this.script) + height: data.height, + value: utils.btc(data.value), + script: bcoin.script.format(data.script) }; }; diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index 6d38da13..0b553561 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -750,7 +750,7 @@ TX.prototype.inspect = function inspect() { fee: utils.btc(this.getFee()), confirmations: this.getConfirmations(), priority: this.getPriority().toString(10), - date: new Date((this.ts || 0) * 1000).toISOString(), + date: new Date(this.ts * 1000).toISOString(), block: this.block ? utils.revHex(this.block) : null, ts: this.ts, version: this.version,