From 7723dd302a81bda30fe12f7e5a681292428ff099 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Mon, 18 May 2015 10:35:31 -0400 Subject: [PATCH] Added test case for an empty OP_RETURN for script.getData() Fixes #1237 --- lib/script/script.js | 6 +++++- test/script/script.js | 9 +++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/script/script.js b/lib/script/script.js index bd0172b..7b680a6 100644 --- a/lib/script/script.js +++ b/lib/script/script.js @@ -365,7 +365,11 @@ Script.prototype.isDataOut = function() { */ Script.prototype.getData = function() { if (this.isDataOut() || this.isScriptHashOut()) { - return new Buffer(this.chunks[1].buf); + if (_.isUndefined(this.chunks[1])) { + return new Buffer(0); + } else { + return new Buffer(this.chunks[1].buf); + } } if (this.isPublicKeyHashOut()) { return new Buffer(this.chunks[2].buf); diff --git a/test/script/script.js b/test/script/script.js index 5c65275..21a23b5 100644 --- a/test/script/script.js +++ b/test/script/script.js @@ -658,6 +658,15 @@ describe('Script', function() { }); describe('getData returns associated data', function() { + it('works with this testnet transaction', function() { + // testnet block: 00000000a36400fc06440512354515964bc36ecb0020bd0b0fd48ae201965f54 + // txhash: e362e21ff1d2ef78379d401d89b42ce3e0ce3e245f74b1f4cb624a8baa5d53ad (output 0); + var script = Script.fromBuffer(new Buffer('6a', 'hex')); + var dataout = script.isDataOut(); + dataout.should.equal(true); + var data = script.getData(); + data.should.deep.equal(new Buffer(0)); + }); it('for a P2PKH address', function() { var address = Address.fromString('1NaTVwXDDUJaXDQajoa9MqHhz4uTxtgK14'); var script = Script.buildPublicKeyHashOut(address);