Browse Source

Added script.toASM method

patch-2
Braydon Fuller 10 years ago
parent
commit
b81a64e8cf
  1. 26
      lib/script/script.js
  2. 4
      test/script/script.js

26
lib/script/script.js

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

4
test/script/script.js

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

Loading…
Cancel
Save