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; return script;
}; };
Script.prototype._chunkToString = function(chunk) { Script.prototype._chunkToString = function(chunk, type) {
var opcodenum = chunk.opcodenum; var opcodenum = chunk.opcodenum;
var asm = (type === 'asm');
var str = ''; var str = '';
if (!chunk.buf) { if (!chunk.buf) {
// no data chunk // no data chunk
@ -194,8 +195,12 @@ Script.prototype._chunkToString = function(chunk) {
if (numstr.length % 2 !== 0) { if (numstr.length % 2 !== 0) {
numstr = '0' + numstr; numstr = '0' + numstr;
} }
if (asm) {
str = str + ' ' + numstr;
} else {
str = str + ' ' + '0x' + numstr; str = str + ' ' + '0x' + numstr;
} }
}
} else { } else {
// data chunk // data chunk
if (opcodenum === Opcode.OP_PUSHDATA1 || if (opcodenum === Opcode.OP_PUSHDATA1 ||
@ -203,14 +208,27 @@ Script.prototype._chunkToString = function(chunk) {
opcodenum === Opcode.OP_PUSHDATA4) { opcodenum === Opcode.OP_PUSHDATA4) {
str = str + ' ' + Opcode(opcodenum).toString(); str = str + ' ' + Opcode(opcodenum).toString();
} }
str = str + ' ' + chunk.len;
if (chunk.len > 0) { 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; 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() { Script.prototype.toString = function() {
var str = ''; var str = '';
for (var i = 0; i < this.chunks.length; i++) { 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() { 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() { it('works with objects', function() {
Script().add({ Script().add({
@ -558,7 +558,7 @@ describe('Script', function() {
var data = new Buffer(''); var data = new Buffer('');
var s = Script.buildDataOut(data); var s = Script.buildDataOut(data);
should.exist(s); should.exist(s);
s.toString().should.equal('OP_RETURN 0'); s.toString().should.equal('OP_RETURN');
s.isDataOut().should.equal(true); s.isDataOut().should.equal(true);
}); });
it('should create script from some data', function() { it('should create script from some data', function() {