From 7f70b573c1a1936057fb7c1a10be6bbc71df9b6c Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 17 Dec 2015 17:56:46 -0800 Subject: [PATCH] enforce checklocktimeverify for nLockTime when adding input. tx.prevOut method. --- lib/bcoin/tx.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index 3f4ee741..45458bd9 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -97,6 +97,20 @@ TX.prototype._input = function _input(i, index) { seq: i.seq === undefined ? 0xffffffff : i.seq }; + if (input.out.tx) { + var prev = input.out.tx.outputs[input.out.index].script; + var lock = bcoin.script.lockTime(prev); + if (lock) { + this.lock = Math.max(lock.toNumber(), this.lock); + // if (this.lock === 0) + // this.lock = lock.toNumber(); + // if (!bcoin.script.spendable(this, this.lock)) + // throw new Error('Cannot spend ' + utils.revHex(input.out.hash)); + if (input.seq === 0xffffffff) + input.seq = 0; + } + } + // Try modifying existing input first var index = this._inputIndex(hash, index); if (index !== -1) { @@ -125,6 +139,18 @@ TX.prototype._inputIndex = function _inputIndex(hash, index) { return -1; }; +TX.prototype.prevOut = function prevOut(i, def) { + if (typeof i === 'object') + i = this.inputs.indexOf(i); + + var input = this.inputs[i]; + + if (!input || !input.out.tx || input.out.index == null) + return def; + + return input.out.tx.outputs[input.out.index] || def; +}; + TX.prototype.signatureHash = function signatureHash(i, type) { if (typeof i === 'object') i = this.inputs.indexOf(i);