Browse Source

Merge pull request #1225 from pnagurny/feature/nullScript

If output.script is InvalidBuffer, return null instead of throwing error. Closes #880. Closes #1216.
patch-2
Braydon Fuller 10 years ago
parent
commit
bb843ee6a5
  1. 11
      lib/transaction/output.js
  2. 20
      test/transaction/output.js

11
lib/transaction/output.js

@ -8,6 +8,7 @@ var JSUtil = require('../util/js');
var BufferWriter = require('../encoding/bufferwriter'); var BufferWriter = require('../encoding/bufferwriter');
var Script = require('../script'); var Script = require('../script');
var $ = require('../util/preconditions'); var $ = require('../util/preconditions');
var errors = require('../errors');
var MAX_SAFE_INTEGER = 0x1fffffffffffff; var MAX_SAFE_INTEGER = 0x1fffffffffffff;
@ -29,7 +30,15 @@ Object.defineProperty(Output.prototype, 'script', {
enumerable: true, enumerable: true,
get: function() { get: function() {
if (!this._script) { 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; return this._script;
} }

20
test/transaction/output.js

@ -132,4 +132,24 @@ describe('Output', function() {
out.setScript.bind(out, 45).should.throw('Invalid argument type: script'); 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…
Cancel
Save