Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add script methods fromASM() and toASM() #1295

Merged
merged 2 commits into from
Jul 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 64 additions & 4 deletions lib/script/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,48 @@ Script.prototype.toBuffer = function() {
return bw.concat();
};

Script.fromASM = function(str) {
var script = new Script();
script.chunks = [];

var tokens = str.split(' ');
var i = 0;
while (i < tokens.length) {
var token = tokens[i];
var opcode = Opcode(token);
var opcodenum = opcode.toNumber();

if (_.isUndefined(opcodenum)) {
var buf = new Buffer(tokens[i], 'hex');
script.chunks.push({
buf: buf,
len: buf.length,
opcodenum: buf.length
});
i = i + 1;
} else if (opcodenum === Opcode.OP_PUSHDATA1 ||
opcodenum === Opcode.OP_PUSHDATA2 ||
opcodenum === Opcode.OP_PUSHDATA4) {
script.chunks.push({
buf: new Buffer(tokens[i + 2], 'hex'),
len: parseInt(tokens[i + 1]),
opcodenum: opcodenum
});
i = i + 3;
} else {
script.chunks.push({
opcodenum: opcodenum
});
i = i + 1;
}
}
return script;
};

Script.fromHex = function(str) {
return new Script(new buffer.Buffer(str, 'hex'));
};

Script.fromString = function(str) {
if (JSUtil.isHexa(str) || str.length === 0) {
return new Script(new buffer.Buffer(str, 'hex'));
Expand Down Expand Up @@ -182,8 +224,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
Expand All @@ -194,7 +237,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
Expand All @@ -203,14 +250,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++) {
Expand Down
22 changes: 20 additions & 2 deletions test/script/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,19 @@ describe('Script', function() {

});

describe('#fromASM', function() {
it('should parse this known script in ASM', function() {
var asm = 'OP_DUP OP_HASH160 f4c03610e60ad15100929cc23da2f3a799af1725 OP_EQUALVERIFY OP_CHECKSIG';
var script = Script.fromASM(asm);
script.chunks[0].opcodenum.should.equal(Opcode.OP_DUP);
script.chunks[1].opcodenum.should.equal(Opcode.OP_HASH160);
script.chunks[2].opcodenum.should.equal(20);
script.chunks[2].buf.toString('hex').should.equal('f4c03610e60ad15100929cc23da2f3a799af1725');
script.chunks[3].opcodenum.should.equal(Opcode.OP_EQUALVERIFY);
script.chunks[4].opcodenum.should.equal(Opcode.OP_CHECKSIG);
});
});

describe('#fromString', function() {

it('should parse these known scripts', function() {
Expand Down Expand Up @@ -191,6 +204,11 @@ describe('Script', function() {
script.toString().toString('hex').should.equal('OP_0 OP_PUSHDATA4 3 0x010203 OP_0');
});

it('should output this known script as ASM', function() {
var script = Script.fromHex('76a914f4c03610e60ad15100929cc23da2f3a799af172588ac');
script.toASM().should.equal('OP_DUP OP_HASH160 f4c03610e60ad15100929cc23da2f3a799af1725 OP_EQUALVERIFY OP_CHECKSIG');
});

});

describe('toHex', function() {
Expand Down Expand Up @@ -439,7 +457,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({
Expand Down Expand Up @@ -558,7 +576,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() {
Expand Down