Added script.toASM method

This commit is contained in:
Braydon Fuller 2015-07-06 19:50:10 -04:00
parent c47adf1c04
commit b81a64e8cf
2 changed files with 24 additions and 6 deletions

View File

@ -182,8 +182,9 @@ Script.fromString = function(str) {
return script;
};
Script.prototype._chunkToString = function(chunk) {
Script.prototype._chunkToString = function(chunk, type) {
var opcodenum = chunk.opcodenum;
var asm = (type === 'asm');
var str = '';
if (!chunk.buf) {
// no data chunk
@ -194,7 +195,11 @@ Script.prototype._chunkToString = function(chunk) {
if (numstr.length % 2 !== 0) {
numstr = '0' + numstr;
}
str = str + ' ' + '0x' + numstr;
if (asm) {
str = str + ' ' + numstr;
} else {
str = str + ' ' + '0x' + numstr;
}
}
} else {
// data chunk
@ -203,14 +208,27 @@ Script.prototype._chunkToString = function(chunk) {
opcodenum === Opcode.OP_PUSHDATA4) {
str = str + ' ' + Opcode(opcodenum).toString();
}
str = str + ' ' + chunk.len;
if (chunk.len > 0) {
str = str + ' ' + '0x' + chunk.buf.toString('hex');
if (asm) {
str = str + ' ' + chunk.buf.toString('hex');
} else {
str = str + ' ' + chunk.len + ' ' + '0x' + chunk.buf.toString('hex');
}
}
}
return str;
};
Script.prototype.toASM = function() {
var str = '';
for (var i = 0; i < this.chunks.length; i++) {
var chunk = this.chunks[i];
str += this._chunkToString(chunk, 'asm');
}
return str.substr(1);
};
Script.prototype.toString = function() {
var str = '';
for (var i = 0; i < this.chunks.length; i++) {

View File

@ -439,7 +439,7 @@ describe('Script', function() {
});
it('should work for no data OP_RETURN', function() {
Script().add(Opcode.OP_RETURN).add(new Buffer('')).toString().should.equal('OP_RETURN 0');
Script().add(Opcode.OP_RETURN).add(new Buffer('')).toString().should.equal('OP_RETURN');
});
it('works with objects', function() {
Script().add({
@ -558,7 +558,7 @@ describe('Script', function() {
var data = new Buffer('');
var s = Script.buildDataOut(data);
should.exist(s);
s.toString().should.equal('OP_RETURN 0');
s.toString().should.equal('OP_RETURN');
s.isDataOut().should.equal(true);
});
it('should create script from some data', function() {