From de078a40d38cdc7e5ea2a5166566bbd6cb123eca Mon Sep 17 00:00:00 2001 From: Patrick Nagurny Date: Tue, 12 May 2015 16:55:31 -0400 Subject: [PATCH] If output.script is InvalidBuffer, return null instead of throwing error --- lib/transaction/output.js | 11 ++++++++++- test/transaction/output.js | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/transaction/output.js b/lib/transaction/output.js index 558f38e..e5fde4c 100644 --- a/lib/transaction/output.js +++ b/lib/transaction/output.js @@ -8,6 +8,7 @@ var JSUtil = require('../util/js'); var BufferWriter = require('../encoding/bufferwriter'); var Script = require('../script'); var $ = require('../util/preconditions'); +var errors = require('../errors'); var MAX_SAFE_INTEGER = 0x1fffffffffffff; @@ -29,7 +30,15 @@ Object.defineProperty(Output.prototype, 'script', { enumerable: true, get: function() { if (!this._script) { - this._script = new Script(this._scriptBuffer); + try { + this._script = Script.fromBuffer(this._scriptBuffer); + } catch(e) { + if(e instanceof errors.Script.InvalidBuffer) { + this._script = null; + } else { + throw e; + } + } } return this._script; } diff --git a/test/transaction/output.js b/test/transaction/output.js index 0ded9d5..c6ca965 100644 --- a/test/transaction/output.js +++ b/test/transaction/output.js @@ -132,4 +132,24 @@ describe('Output', function() { out.setScript.bind(out, 45).should.throw('Invalid argument type: script'); }); + it('sets script to null if it is an InvalidBuffer', function() { + var output = new Output({ + satoshis: 1000 + }); + output._scriptBuffer = new Buffer('4c', 'hex'); + + var result = output.script; + should.equal(result, null); + }); + + it('should throw an error if Script throws an error that is not InvalidBuffer', function() { + var output = new Output({ + satoshis: 1000 + }); + output._scriptBuffer = 'bad'; + + (function() { + var result = output.script; + }).should.throw('Invalid hex string'); + }); });