diff --git a/lib/script/script.js b/lib/script/script.js index 4893f71f908..41215b0df7f 100644 --- a/lib/script/script.js +++ b/lib/script/script.js @@ -682,12 +682,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 cd94da8bf1e..6d16b77101a 100644 --- a/test/script/script.js +++ b/test/script/script.js @@ -599,6 +599,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() {