/** * tx.js - transaction object for bcoin * Copyright (c) 2014-2015, Fedor Indutny (MIT License) * https://github.com/indutny/bcoin */ var bn = require('bn.js'); var bcoin = require('../bcoin'); var utils = require('./utils'); var assert = utils.assert; var constants = bcoin.protocol.constants; var network = bcoin.protocol.network; var Script = bcoin.script; var Stack = bcoin.script.stack; var BufferReader = require('./reader'); var BufferWriter = require('./writer'); /** * TX */ function TX(data, block, index) { if (!(this instanceof TX)) return new TX(data, block, index); if (!data) data = {}; this.type = 'tx'; this.version = data.version || 1; this.flag = data.flag || 1; this.inputs = []; this.outputs = []; this.locktime = data.locktime || 0; this.ts = data.ts || 0; this.block = data.block || null; this.index = data.index != null ? data.index : -1; this.ps = this.ts === 0 ? utils.now() : 0; this._hash = null; this._whash = null; this._raw = data._raw || null; this._size = data._size || 0; this._witnessSize = data._witnessSize || 0; this.height = data.height != null ? data.height : -1; // assert(data.inputs.length !== 0); // assert(data.outputs.length !== 0); data.inputs.forEach(function(input) { this.inputs.push(new bcoin.input(input, this)); }, this); data.outputs.forEach(function(output) { this.outputs.push(new bcoin.output(output, this)); }, this); if (block && this.ts === 0) { if (block.type === 'merkleblock') { if (block.hasTX(this.hash('hex'))) this.setBlock(block, index); } else { this.setBlock(block, index); } } } TX.prototype.clone = function clone() { var copy, i; copy = { version: this.version, inputs: [], outputs: [], locktime: this.locktime, flag: this.flag, ts: this.ts, block: this.block, index: this.index, height: this.height }; for (i = 0; i < this.inputs.length; i++) { copy.inputs.push({ prevout: { hash: this.inputs[i].prevout.hash, index: this.inputs[i].prevout.index }, coin: null, script: { code: this.inputs[i].script.code.slice(), raw: this.inputs[i].script.raw }, witness: { items: this.inputs[i].witness.items.slice() }, sequence: this.inputs[i].sequence }); } for (i = 0; i < this.outputs.length; i++) { copy.outputs.push({ value: this.outputs[i].value.clone(), script: { code: this.inputs[i].script.code.slice(), raw: this.inputs[i].script.raw } }); } return new TX(copy); }; TX.prototype.setBlock = function setBlock(block, index) { this.ts = block.ts; this.block = block.hash('hex'); this.height = block.height; this.index = index; }; TX.prototype.hash = function hash(enc) { if (!this._hash) this._hash = utils.dsha256(this.renderNormal()); return enc === 'hex' ? utils.toHex(this._hash) : this._hash; }; TX.prototype.witnessHash = function witnessHash(enc) { if (this.isCoinbase()) { return enc === 'hex' ? utils.toHex(constants.zeroHash) : utils.slice(constants.zeroHash); } if (!this.hasWitness()) return this.hash(enc); if (!this._whash) this._whash = utils.dsha256(this.renderWitness()); return enc === 'hex' ? utils.toHex(this._whash) : this._whash; }; TX.prototype.render = function render() { return this.getRaw(); }; TX.prototype.renderNormal = function renderNormal() { var raw = this.getRaw(); if (!bcoin.protocol.parser.isWitnessTX(raw)) return raw; return bcoin.protocol.framer.tx(this); }; TX.prototype.renderWitness = function renderWitness() { var raw = this.getRaw(); if (bcoin.protocol.parser.isWitnessTX(raw)) return raw; return bcoin.protocol.framer.witnessTX(this); }; TX.prototype.getRaw = function getRaw() { var raw; if (this._raw) { assert(this._size > 0); assert(this._witnessSize >= 0); return this._raw; } if (this.hasWitness()) raw = bcoin.protocol.framer.witnessTX(this); else raw = bcoin.protocol.framer.tx(this); // this._raw = raw; this._size = raw.length; this._witnessSize = raw._witnessSize; return raw; }; TX.prototype.getVirtualSize = function getVirtualSize() { var size, witnessSize, base; this.getRaw(); size = this._size; witnessSize = this._witnessSize; base = size - witnessSize; return (base * 4 + witnessSize + 3) / 4 | 0; }; TX.prototype.getSize = function getSize() { return this.getRaw().length; }; TX.prototype.hasWitness = function hasWitness() { var i; for (i = 0; i < this.inputs.length; i++) { if (this.inputs[i].witness.items.length > 0) return true; } return false; }; TX.prototype._inputIndex = function _inputIndex(hash, index) { var i, ex; for (i = 0; i < this.inputs.length; i++) { ex = this.inputs[i]; if (ex.prevout.hash === hash && ex.prevout.index === index) return i; } return -1; }; TX.prototype.signatureHash = function signatureHash(index, prev, type, version) { assert(version >= 0 && version <= 1); // Traditional sighashing if (version === 0) return this.signatureHashV0(index, prev, type); // Segwit sighashing if (version === 1) return this.signatureHashV1(index, prev, type); }; TX.prototype.signatureHashV0 = function signatureHashV0(index, prev, type) { var p = new BufferWriter(); var i, copy; if (typeof index !== 'number') index = this.inputs.indexOf(index); if (typeof type === 'string') type = constants.hashType[type]; assert(index >= 0 && index < this.inputs.length); assert(prev instanceof Script); // Clone the transaction. copy = { version: this.version, inputs: [], outputs: [], locktime: this.locktime }; for (i = 0; i < this.inputs.length; i++) { copy.inputs.push({ prevout: this.inputs[i].prevout, script: this.inputs[i].script.clone(), witness: this.inputs[i].witness.clone(), sequence: this.inputs[i].sequence }); } for (i = 0; i < this.outputs.length; i++) { copy.outputs.push({ value: this.outputs[i].value, script: this.outputs[i].script.clone() }); } // Remove all signatures. for (i = 0; i < copy.inputs.length; i++) copy.inputs[i].script = new Script([]); // Set our input to previous output's script copy.inputs[index].script = prev; if ((type & 0x1f) === constants.hashType.none) { // Drop all outputs. We don't want to sign them. copy.outputs.length = 0; // Allow input sequence updates for other inputs. for (i = 0; i < copy.inputs.length; i++) { if (i !== index) copy.inputs[i].sequence = 0; } } else if ((type & 0x1f) === constants.hashType.single) { // Bitcoind used to return 1 as an error code: // it ended up being treated like a hash. if (index >= copy.outputs.length) return utils.slice(constants.oneHash); // Drop all the outputs after the input index. copy.outputs.length = index + 1; // Null outputs that are not the at current input index. for (i = 0; i < copy.outputs.length; i++) { if (i !== index) { copy.outputs[i].script = new Script([]); copy.outputs[i].value = utils.U64; } } // Allow input sequence updates for other inputs. for (i = 0; i < copy.inputs.length; i++) { if (i !== index) copy.inputs[i].sequence = 0; } } // Only sign our input. Allows anyone to add inputs. if (type & constants.hashType.anyonecanpay) { copy.inputs[0] = copy.inputs[index]; copy.inputs.length = 1; } // Render the copy and append the hashtype. bcoin.protocol.framer.tx(copy, p); p.writeU32(type); return utils.dsha256(p.render()); }; TX.prototype.signatureHashV1 = function signatureHashV1(index, prev, type) { var p = new BufferWriter(); var i, prevout, hashPrevouts, hashSequence, hashOutputs; if (typeof index !== 'number') index = this.inputs.indexOf(index); if (typeof type === 'string') type = constants.hashType[type]; assert(index >= 0 && index < this.inputs.length); assert(prev instanceof Script); if (!(type & constants.hashType.anyonecanpay)) { hashPrevouts = new BufferWriter(); for (i = 0; i < this.inputs.length; i++) { prevout = this.inputs[i].prevout; hashPrevouts.writeHash(prevout.hash); hashPrevouts.writeU32(prevout.index); } hashPrevouts = utils.dsha256(hashPrevouts.render()); } else { hashPrevouts = utils.slice(constants.zeroHash); } if (!(type & constants.hashType.anyonecanpay) && (type & 0x1f) !== constants.hashType.single && (type & 0x1f) !== constants.hashType.none) { hashSequence = new BufferWriter(); for (i = 0; i < this.inputs.length; i++) hashSequence.writeU32(this.inputs[i].sequence); hashSequence = utils.dsha256(hashSequence.render()); } else { hashSequence = utils.slice(constants.zeroHash); } if ((type & 0x1f) !== constants.hashType.single && (type & 0x1f) !== constants.hashType.none) { hashOutputs = new BufferWriter(); for (i = 0; i < this.outputs.length; i++) bcoin.protocol.framer.output(this.outputs[i], hashOutputs); hashOutputs = utils.dsha256(hashOutputs.render()); } else if ((type & 0x1f) === constants.hashType.single && index < this.outputs.length) { hashOutputs = bcoin.protocol.framer.output(this.outputs[index]); hashOutputs = utils.dsha256(hashOutputs); } else { hashOutputs = utils.slice(constants.zeroHash); } p.write32(this.version); p.writeBytes(hashPrevouts); p.writeBytes(hashSequence); p.writeHash(this.inputs[index].prevout.hash); p.writeU32(this.inputs[index].prevout.index); p.writeVarBytes(prev.encode()); p.write64(this.inputs[index].coin.value); p.writeU32(this.inputs[index].sequence); p.writeBytes(hashOutputs); p.writeU32(this.locktime); p.writeU32(type); return utils.dsha256(p.render()); }; TX.prototype.verify = function verify(index, force, flags) { // Valid if included in block if (!force && this.ts !== 0) return true; if (this.inputs.length === 0) return false; if (index && typeof index === 'object') index = this.inputs.indexOf(index); if (index != null) assert(this.inputs[index]); if (this.isCoinbase()) return true; return this.inputs.every(function(input, i) { if (index != null && i !== index) return true; if (!input.coin) { utils.debug('Warning: Not all outputs available for tx.verify().'); return false; } return Script.verify( input.script, input.witness, input.coin.script, this, i, flags ); }, this); }; TX.prototype.verifyAsync = function verifyAsync(index, force, flags, callback) { var self = this; utils.nextTick(function() { var res; try { res = self.verify(index, force, flags); } catch (e) { return callback(e); } return callback(null, res); }); }; TX.prototype.isCoinbase = function isCoinbase() { return this.inputs.length === 1 && this.inputs[0].prevout.hash === constants.nullHash; }; TX.prototype.getFee = function getFee() { if (!this.hasCoins()) return new bn(0); return this.getInputValue().sub(this.getOutputValue()); }; TX.prototype.getInputValue = function getInputValue() { var acc = new bn(0); if (this.inputs.length === 0) return acc; if (!this.hasCoins()) return acc; return this.inputs.reduce(function(acc, input) { return acc.iadd(input.coin.value); }, acc); }; TX.prototype.getOutputValue = function getOutputValue() { var acc = new bn(0); if (this.outputs.length === 0) return acc; return this.outputs.reduce(function(acc, output) { return acc.iadd(output.value); }, acc); }; TX.prototype.getFunds = function getFunds(side) { if (side === 'in' || side === 'input') return this.getInputValue(); return this.getOutputValue(); }; TX.prototype.getInputAddresses = function getInputAddresses() { var table = {}; var addresses = []; var i, address; for (i = 0; i < this.inputs.length; i++) { address = this.inputs[i].getAddress(); if (address && !table[address]) { table[address] = true; addresses.push(address); } } addresses.table = table; return addresses; }; TX.prototype.getOutputAddresses = function getOutputAddresses() { var table = {}; var addresses = []; var i, address; for (i = 0; i < this.outputs.length; i++) { address = this.outputs[i].getAddress(); if (address && !table[address]) { table[address] = true; addresses.push(address); } } addresses.table = table; return addresses; }; TX.prototype.getAddresses = function getAddresses() { var input = this.getInputAddresses(); var output = this.getOutputAddresses(); var i; for (i = 0; i < output.length; i++) { if (!input.table[output[i]]) { input.table[output[i]] = true; input.push(output[i]); } } return input; }; TX.prototype.testInputs = function testInputs(addressMap, index) { var i; if (typeof addressMap === 'string') addressMap = [addressMap]; if (Array.isArray(addressMap)) addressMap = utils.toMap(addressMap); if (index && typeof index === 'object') index = this.inputs.indexOf(index); if (index != null) return this.inputs[index].test(addressMap); for (i = 0; i < this.inputs.length; i++) { if (index != null && i !== index) continue; if (this.inputs[i].test(addressMap)) return true; } return false; }; TX.prototype.testOutputs = function testOutputs(addressMap, index) { var i; if (typeof addressMap === 'string') addressMap = [addressMap]; if (Array.isArray(addressMap)) addressMap = utils.toMap(addressMap); if (index && typeof index === 'object') index = this.outputs.indexOf(index); if (index != null) return this.outputs[index].test(addressMap); for (i = 0; i < this.outputs.length; i++) { if (index != null && i !== index) continue; if (this.outputs[i].test(addressMap)) return true; } return false; }; TX.prototype.hasCoins = function hasCoins() { if (this.inputs.length === 0) return false; // if (this.isCoinbase()) // return true; return this.inputs.every(function(input) { return !!input.coin; }); }; TX.prototype.fillCoins = function fillCoins(coins) { var total = 0; var inputs, txs, key, i, input; if (!Array.isArray(coins)) coins = [coins]; if (Array.isArray(coins)) { coins = coins.reduce(function(out, coin) { if (coin instanceof TX) { out[coin.hash('hex')] = coin; } else { assert(typeof coin.hash === 'string'); out[coin.hash + '/' + coin.index] = coin; } return out; }, {}); } for (i = 0; i < this.inputs.length; i++) { input = this.inputs[i]; if (!input.coin) { if (coins[input.prevout.hash]) { input.coin = bcoin.coin(coins[input.prevout.hash], input.prevout.index); } else { key = input.prevout.hash + '/' + input.prevout.index; if (coins[key]) input.coin = coins[key]; } } if (input.coin) total++; } return total === this.inputs.length; }; TX.prototype.isFinal = function isFinal(height, ts) { var threshold = constants.locktimeThreshold; var i; if (this.locktime === 0) return true; if (this.locktime < (this.locktime < threshold ? height : ts)) return true; for (i = 0; i < this.inputs.length; i++) { if (this.inputs[i].sequence !== 0xffffffff) return false; } return true; }; TX.prototype._getSigops = function _getSigops(scriptHash, accurate) { var total = 0; this.inputs.forEach(function(input) { var prev; if (!input.coin) return; prev = input.coin.script; total += input.script.getSigops(accurate); if (scriptHash && !this.isCoinbase()) { if (!prev.isScripthash()) return; if (!input.script.isPushOnly()) return; prev = input.script.getRedeem(); if (!prev) return; total += prev.getSigops(true); } }, this); this.outputs.forEach(function(output) { total += output.script.getSigops(accurate); }, this); return total; }; TX.prototype.getSigops = function getSigops(scriptHash, accurate) { var cost = this._getSigops(scriptHash, accurate) * 4; this.inputs.forEach(function(input) { var prev; if (!input.coin) return; prev = input.coin.script; if (prev.isScripthash()) prev = input.script.getRedeem(); if (prev && prev.isWitnessScripthash()) { prev = input.witness.getRedeem(); cost += prev.getSigops(true); } else { cost += 0; } }, this); this.outputs.forEach(function(output) { if (output.script.isWitnessPubkeyhash()) cost += 1; else cost += 0; }, this); return (cost + 3) / 4 | 0; }; // CheckTransaction TX.prototype.isSane = function isSane(ret) { var uniq = {}; var total = new bn(0); var i, input, output, size; if (!ret) ret = {}; if (this.inputs.length === 0) { ret.reason = 'bad-txns-vin-empty'; ret.score = 100; return false; } if (this.outputs.length === 0) { ret.reason = 'bad-txns-vout-empty'; ret.score = 100; return false; } if (this.getVirtualSize() > constants.block.maxSize) { ret.reason = 'bad-txns-oversize'; ret.score = 100; return false; } for (i = 0; i < this.outputs.length; i++) { output = this.outputs[i]; if (output.value.cmpn(0) < 0) { ret.reason = 'bad-txns-vout-negative'; ret.score = 100; return false; } if (output.value.cmp(constants.maxMoney) > 0) { ret.reason = 'bad-txns-vout-toolarge'; ret.score = 100; return false; } total.iadd(output.value); if (total.cmpn(0) < 0 || total.cmp(constants.maxMoney) > 0) { ret.reason = 'bad-txns-txouttotal-toolarge'; ret.score = 100; return false; } } for (i = 0; i < this.inputs.length; i++) { input = this.inputs[i]; if (uniq[input.prevout.hash]) { ret.reason = 'bad-txns-inputs-duplicate'; ret.score = 100; return false; } uniq[input.prevout.hash] = true; } if (this.isCoinbase()) { size = this.inputs[0].script.getSize(); if (size < 2 || size > 100) { ret.reason = 'bad-cb-length'; ret.score = 100; return false; } } else { for (i = 0; i < this.inputs.length; i++) { input = this.inputs[i]; if (input.prevout.hash === constants.nullHash) { ret.reason = 'bad-txns-prevout-null'; ret.score = 10; return false; } } } return true; }; // IsStandardTx TX.prototype.isStandard = function isStandard(flags, ret) { var i, input, output, type; var nulldata = 0; var maxVersion = constants.tx.version; if (!ret) ret = { reason: null }; if (flags == null) flags = constants.flags.STANDARD_VERIFY_FLAGS; if (this.version < 1 || this.version > constants.tx.version) { ret.reason = 'version'; return false; } if (this.getVirtualSize() > constants.tx.maxSize) { ret.reason = 'tx-size'; return false; } for (i = 0; i < this.inputs.length; i++) { input = this.inputs[i]; if (input.script.getSize() > 1650) { ret.reason = 'scriptsig-size'; return false; } // Not accurate if (this.isCoinbase()) continue; if (flags & constants.flags.VERIFY_SIGPUSHONLY) { if (!input.script.isPushOnly()) { ret.reason = 'scriptsig-not-pushonly'; return false; } } } for (i = 0; i < this.outputs.length; i++) { output = this.outputs[i]; type = output.script.getType(); if (!output.script.isStandard()) { ret.reason = 'scriptpubkey'; return false; } if (type === 'nulldata') { nulldata++; continue; } if (type === 'multisig' && !constants.tx.bareMultisig) { ret.reason = 'bare-multisig'; return false; } if (output.value.cmpn(constants.tx.dustThreshold) < 0) { ret.reason = 'dust'; return false; } } if (nulldata > 1) { ret.reason = 'multi-op-return'; return false; } return true; }; // AreInputsStandard TX.prototype.hasStandardInputs = function hasStandardInputs(flags) { var maxSigops = constants.script.maxScripthashSigops; var i, input, stack, res, redeem; if (flags == null) flags = constants.flags.STANDARD_VERIFY_FLAGS; if (this.isCoinbase()) return true; for (i = 0; i < this.inputs.length; i++) { input = this.inputs[i]; if (!input.coin) return false; if (input.coin.script.isNonstandard()) return false; if ((flags & constants.flags.VERIFY_P2SH) && input.coin.script.isScripthash()) { // Not accurate: // Failsafe to avoid getting dos'd in case we ever // call hasStandardInputs before isStandard. if (!input.script.isPushOnly()) return false; stack = new Stack([]); res = input.script.execute(stack, constants.flags.VERIFY_NONE, this, i, 0); if (!res) return false; if (stack.length === 0) return false; redeem = stack.getRedeem(false); if (!redeem) return false; if (redeem.getSigops(true) > maxSigops) return false; } } return true; }; TX.prototype.maxSize = function maxSize() { return this.getVirtualSize(); }; TX.prototype.getPriority = function getPriority(height, size) { var sum, i, input, age; if (height == null) { height = this.height; if (height === -1) height = network.height + 1; } if (!this.hasCoins()) return new bn(0); if (size == null) size = this.maxSize(); sum = new bn(0); for (i = 0; i < this.inputs.length; i++) { input = this.inputs[i]; age = input.coin.getAge(height); sum.iadd(input.coin.value.muln(age)); } return sum.divn(size); }; TX.prototype.isFree = function isFree(height, size) { var priority; if (!this.hasCoins()) return false; if (height == null) { height = this.height; if (height === -1) height = network.height + 1; } if (size == null) size = this.maxSize(); if (size >= constants.tx.maxFreeSize) return false; priority = this.getPriority(height, size); return priority.cmp(constants.tx.freeThreshold) > 0; }; TX.prototype.getMinFee = function getMinFee(size) { var fee; if (size == null) size = this.maxSize(); fee = new bn(constants.tx.minFee).muln(size).divn(1000); if (fee.cmpn(0) === 0 && constants.tx.minFee > 0) fee = new bn(constants.tx.minFee); return fee; }; TX.prototype.getConfirmations = function getConfirmations(height) { if (height == null) height = network.height; if (this.height === -1) return 0; if (height < this.height) return 1; return height - this.height + 1; }; TX.prototype.getValue = function getValue() { return this.getOutputValue(); }; TX.prototype.hasType = function hasType(type) { var i; for (i = 0; i < this.inputs.length; i++) { if (this.inputs[i].getType() === type) return true; } for (i = 0; i < this.outputs.length; i++) { if (this.outputs[i].getType() === type) return true; } return false; }; TX.prototype.__defineGetter__('rblock', function() { return this.block ? utils.revHex(this.block) : null; }); TX.prototype.__defineGetter__('rhash', function() { return utils.revHex(this.hash('hex')); }); TX.prototype.__defineGetter__('rwhash', function() { return utils.revHex(this.witnessHash('hex')); }); TX.prototype.__defineGetter__('fee', function() { return this.getFee(); }); TX.prototype.__defineGetter__('value', function() { return this.getValue(); }); TX.prototype.__defineGetter__('confirmations', function() { return this.getConfirmations(); }); TX.prototype.__defineGetter__('priority', function() { return this.getPriority(); }); TX.prototype.getPrevout = function getPrevout() { var prevout = {}; if (this.isCoinbase()) return []; this.inputs.forEach(function(input) { prevout[input.prevout.hash] = true; }); return Object.keys(prevout); }; TX.prototype.inspect = function inspect() { return { type: this.type, hash: utils.revHex(this.hash('hex')), witnessHash: utils.revHex(this.witnessHash('hex')), size: this.getSize(), virtualSize: this.getVirtualSize(), height: this.height, value: utils.btc(this.getValue()), fee: utils.btc(this.getFee()), confirmations: this.getConfirmations(), priority: this.getPriority().toString(10), date: new Date(this.ts * 1000).toISOString(), block: this.block ? utils.revHex(this.block) : null, ts: this.ts, index: this.index, version: this.version, inputs: this.inputs, outputs: this.outputs, locktime: this.locktime, hint: this.hint }; }; TX.prototype.toJSON = function toJSON() { return { type: 'tx', hash: utils.revHex(this.hash('hex')), witnessHash: utils.revHex(this.witnessHash('hex')), height: this.height, block: this.block ? utils.revHex(this.block) : null, ts: this.ts, ps: this.ps, index: this.index, changeIndex: this.changeIndex || -1, version: this.version, inputs: this.inputs.map(function(input) { return input.toJSON(); }), outputs: this.outputs.map(function(output) { return output.toJSON(); }), locktime: this.locktime }; }; TX._fromJSON = function fromJSON(json) { assert.equal(json.type, 'tx'); return { block: json.block ? utils.revHex(json.block) : null, height: json.height, ts: json.ts, ps: json.ps, index: json.index, changeIndex: json.changeIndex || -1, version: json.version, inputs: json.inputs.map(function(input) { return bcoin.input._fromJSON(input); }), outputs: json.outputs.map(function(output) { return bcoin.output._fromJSON(output); }), locktime: json.locktime }; }; TX.fromJSON = function fromJSON(json) { assert.equal(json.type, 'tx'); return new TX(TX._fromJSON(json)); }; TX.prototype.toRaw = function toRaw(enc) { var data = this.render(); if (enc === 'hex') data = utils.toHex(data); return data; }; TX._fromRaw = function _fromRaw(data, enc) { if (enc === 'hex') data = new Buffer(data, 'hex'); return bcoin.protocol.parser.parseTX(data); }; TX.fromRaw = function fromRaw(data, enc) { return new bcoin.tx(TX._fromRaw(data, enc)); }; TX.prototype.toExtended = function toExtended(saveCoins) { var height = this.height; var index = this.index; var changeIndex = this.changeIndex != null ? this.changeIndex : -1; var p = new BufferWriter(); if (height === -1) height = 0x7fffffff; if (index === -1) index = 0x7fffffff; if (changeIndex === -1) changeIndex = 0x7fffffff; bcoin.protocol.framer.renderTX(this, true, p); p.writeU32(height); p.writeHash(this.block || constants.zeroHash); p.writeU32(this.index); p.writeU32(this.ts); p.writeU32(this.ps); // p.writeU32(changeIndex); if (saveCoins) { p.writeVarint(this.inputs.length); this.inputs.forEach(function(input) { if (!input.coin) { p.writeVarint(0); return; } p.writeVarBytes(bcoin.protocol.framer.coin(input.coin, false)); }); } return p.render(); }; TX._fromExtended = function _fromExtended(buf, saveCoins) { var p = new BufferReader(buf); var tx, coinCount, coin, i; p.start(); tx = bcoin.protocol.parser.parseTX(p); tx.height = p.readU32(); tx.block = p.readHash('hex'); tx.index = p.readU32(); tx.ts = p.readU32(); tx.ps = p.readU32(); // tx.changeIndex = p.readU32(); if (tx.block === constants.nullHash) tx.block = null; if (tx.height === 0x7fffffff) tx.height = -1; if (tx.index === 0x7fffffff) tx.index = -1; if (tx.changeIndex === 0x7fffffff) tx.changeIndex = -1; if (saveCoins) { coinCount = p.readVarint(); for (i = 0; i < coinCount; i++) { coin = p.readVarBytes(); if (coin.length === 0) continue; coin = bcoin.protocol.parser.parseCoin(coin, false); coin.hash = tx.inputs[i].prevout.hash; coin.index = tx.inputs[i].prevout.index; tx.inputs[i].coin = coin; } } p.end(); return tx; }; TX.fromExtended = function fromExtended(buf, saveCoins) { return new TX(TX._fromExtended(buf, saveCoins)); }; TX.prototype.toCoins = function toCoins() { return bcoin.coins.fromTX(this); }; TX.isTX = function isTX(obj) { return obj && Array.isArray(obj.inputs) && typeof obj.ps === 'number' && typeof obj.getVirtualSize === 'function'; }; /** * Expose */ module.exports = TX;