Script().fromString(str)
...the format of fromString and toString are deliberately not compatible with bitcoind. The format here is supposed to be both human-readable, and byte-for-byte isomorphic to the binary representation. In the future we will need to add support for bitcoind-like strings, both for the test data (e.g., script_invalid.json) or for the bitcoind console style.
This commit is contained in:
parent
6375941ef8
commit
3b2b725070
|
@ -105,6 +105,44 @@ Script.prototype.toBuffer = function() {
|
||||||
return bw.concat();
|
return bw.concat();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Script.prototype.fromString = function(str) {
|
||||||
|
this.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 (typeof opcodenum === 'undefined') {
|
||||||
|
opcodenum = parseInt(token);
|
||||||
|
if (opcodenum > 0 && opcodenum < Opcode.map.OP_PUSHDATA1) {
|
||||||
|
this.chunks.push({
|
||||||
|
buf: new Buffer(tokens[i + 1].slice(2), 'hex'),
|
||||||
|
len: opcodenum,
|
||||||
|
opcodenum: opcodenum
|
||||||
|
});
|
||||||
|
i = i + 2;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
throw new Error('Invalid script');
|
||||||
|
}
|
||||||
|
} else if (opcodenum === Opcode.map.OP_PUSHDATA1 || opcodenum === Opcode.map.OP_PUSHDATA2 || opcodenum === Opcode.map.OP_PUSHDATA4) {
|
||||||
|
this.chunks.push({
|
||||||
|
buf: new Buffer(tokens[i + 2].slice(2), 'hex'),
|
||||||
|
len: parseInt(tokens[i + 1]),
|
||||||
|
opcodenum: opcodenum
|
||||||
|
});
|
||||||
|
i = i + 3;
|
||||||
|
} else {
|
||||||
|
this.chunks.push(opcodenum);
|
||||||
|
i = i + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
Script.prototype.toString = function() {
|
Script.prototype.toString = function() {
|
||||||
var str = "";
|
var str = "";
|
||||||
|
|
||||||
|
@ -115,6 +153,7 @@ Script.prototype.toString = function() {
|
||||||
str = str + Opcode(opcodenum).toString() + " ";
|
str = str + Opcode(opcodenum).toString() + " ";
|
||||||
} else {
|
} else {
|
||||||
var opcodenum = chunk.opcodenum;
|
var opcodenum = chunk.opcodenum;
|
||||||
|
if (opcodenum === Opcode.map.OP_PUSHDATA1 || opcodenum === Opcode.map.OP_PUSHDATA2 || opcodenum === Opcode.map.OP_PUSHDATA4)
|
||||||
str = str + Opcode(opcodenum).toString() + " " ;
|
str = str + Opcode(opcodenum).toString() + " " ;
|
||||||
str = str + chunk.len + " " ;
|
str = str + chunk.len + " " ;
|
||||||
str = str + "0x" + chunk.buf.toString('hex') + " ";
|
str = str + "0x" + chunk.buf.toString('hex') + " ";
|
||||||
|
|
|
@ -151,6 +151,17 @@ describe('Script', function() {
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('#fromString', function() {
|
||||||
|
|
||||||
|
it('should parse these known scripts', function() {
|
||||||
|
Script().fromString('OP_0 OP_PUSHDATA4 3 0x010203 OP_0').toString().should.equal('OP_0 OP_PUSHDATA4 3 0x010203 OP_0');
|
||||||
|
Script().fromString('OP_0 OP_PUSHDATA2 3 0x010203 OP_0').toString().should.equal('OP_0 OP_PUSHDATA2 3 0x010203 OP_0');
|
||||||
|
Script().fromString('OP_0 OP_PUSHDATA1 3 0x010203 OP_0').toString().should.equal('OP_0 OP_PUSHDATA1 3 0x010203 OP_0');
|
||||||
|
Script().fromString('OP_0 3 0x010203 OP_0').toString().should.equal('OP_0 3 0x010203 OP_0');
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
describe('#toString', function() {
|
describe('#toString', function() {
|
||||||
|
|
||||||
it('should output this buffer an OP code, data, and another OP code', function() {
|
it('should output this buffer an OP code, data, and another OP code', function() {
|
||||||
|
|
Loading…
Reference in New Issue