If output.script is InvalidBuffer, return null instead of throwing error

This commit is contained in:
Patrick Nagurny 2015-05-12 16:55:31 -04:00
parent a02258dfba
commit de078a40d3
2 changed files with 30 additions and 1 deletions

View File

@ -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;
}

View File

@ -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');
});
});