If output.script is InvalidBuffer, return null instead of throwing error
This commit is contained in:
parent
a02258dfba
commit
de078a40d3
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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');
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue