Added encoding type for buildDataOut

This commit is contained in:
Braydon Fuller 2015-07-06 19:14:48 -04:00
parent c47adf1c04
commit 3ad484fff0
2 changed files with 12 additions and 4 deletions

View File

@ -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);

View File

@ -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() {