Skip to content

Commit

Permalink
Merge pull request #1293 from braydonf/script-data-encoding
Browse files Browse the repository at this point in the history
Added encoding type for buildDataOut
  • Loading branch information
pnagurny committed Jul 30, 2015
2 parents f77da04 + 3ad484f commit 6ac7ad9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/script/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions test/script/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 6ac7ad9

Please sign in to comment.