From 3ad484fff05d01aa119b92bdc74aa18644db8da9 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Mon, 6 Jul 2015 19:14:48 -0400 Subject: [PATCH] Added encoding type for buildDataOut --- lib/script/script.js | 9 +++++---- test/script/script.js | 7 +++++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/script/script.js b/lib/script/script.js index f93ace2..a37a101 100644 --- a/lib/script/script.js +++ b/lib/script/script.js @@ -655,12 +655,13 @@ Script.buildPublicKeyOut = function(pubkey) { /** * @returns {Script} a new OP_RETURN script with data - * @param {(string|Buffer)} to - the data to embed in the output + * @param {(string|Buffer)} data - the data to embed in the output + * @param {(string)} encoding - the type of encoding of the string */ -Script.buildDataOut = function(data) { +Script.buildDataOut = function(data, encoding) { $.checkArgument(_.isUndefined(data) || _.isString(data) || BufferUtil.isBuffer(data)); - if (typeof data === 'string') { - data = new Buffer(data); + if (_.isString(data)) { + data = new Buffer(data, encoding); } var s = new Script(); s.add(Opcode.OP_RETURN); diff --git a/test/script/script.js b/test/script/script.js index dd138a2..b2f6753 100644 --- a/test/script/script.js +++ b/test/script/script.js @@ -575,6 +575,13 @@ describe('Script', function() { s.toString().should.equal('OP_RETURN 14 0x68656c6c6f20776f726c64212121'); s.isDataOut().should.equal(true); }); + it('should create script from a hex string', function() { + var hexString = 'abcdef0123456789'; + var s = Script.buildDataOut(hexString, 'hex'); + should.exist(s); + s.toString().should.equal('OP_RETURN 8 0xabcdef0123456789'); + s.isDataOut().should.equal(true); + }); }); describe('#buildScriptHashOut', function() { it('should create script from another script', function() {