separate getStringContent and getHumanReadable

This commit is contained in:
Manuel Araoz 2014-03-07 18:04:23 -03:00
parent dc56cb8d45
commit 1d72154c54
4 changed files with 86 additions and 47 deletions

118
Script.js
View File

@ -38,7 +38,7 @@ function spec(b) {
} }
this.chunks = []; this.chunks = [];
this.parse(); this.parse();
}; }
this.class = Script; this.class = Script;
Script.TX_UNKNOWN = TX_UNKNOWN; Script.TX_UNKNOWN = TX_UNKNOWN;
@ -265,47 +265,6 @@ function spec(b) {
return this.buffer; return this.buffer;
}; };
Script.fromStringContent = function(s) {
var chunks = [];
var split = s.split(' ');
for (var i = 0; i < split.length; i++) {
var word = split[i];
if (word === '') continue;
if (word.length > 2 && word.substring(0, 2) === '0x') {
// raw hex value
//console.log('hex value');
chunks.push(new Buffer(word.substring(2, word.length), 'hex'));
} else {
var opcode = Opcode.map['OP_' + word];
if (typeof opcode !== 'undefined') {
// op code in string form
//console.log('opcode');
chunks.push(opcode);
} else {
var integer = parseInt(word);
if (!isNaN(integer)) {
// integer
//console.log('integer');
var data = util.intToBuffer(integer);
chunks.push(data);
} else if (word[0] === '\'' && word[word.length-1] === '\'') {
// string
//console.log('string');
word = word.substring(1,word.length-1);
var hex = '';
for(var c=0;c<word.length;c++) {
hex += ''+word.charCodeAt(c).toString(16);
}
chunks.push(new Buffer(hex,'hex'));
} else {
throw new Error('Could not parse word "' +word+'" from script "'+s+'"');
}
}
}
}
return Script.fromChunks(chunks);
};
Script.prototype.getStringContent = function(truncate, maxEl) { Script.prototype.getStringContent = function(truncate, maxEl) {
if (truncate === null) { if (truncate === null) {
truncate = true; truncate = true;
@ -325,7 +284,7 @@ function spec(b) {
if (Buffer.isBuffer(chunk)) { if (Buffer.isBuffer(chunk)) {
if (chunk.length === 0) { if (chunk.length === 0) {
s += '\'\'' s += '\'\'';
} else { } else {
s += '0x' + util.formatBuffer(chunk, truncate ? null : 0); s += '0x' + util.formatBuffer(chunk, truncate ? null : 0);
} }
@ -504,6 +463,79 @@ function spec(b) {
script.updateBuffer(); script.updateBuffer();
return script; return script;
}; };
Script.fromHumanReadable = function(s) {
return new Script(Script.stringToBuffer(s));
};
Script.prototype.toHumanReadable = function() {
var s = '';
for (var i = 0, l = this.chunks.length; i < l; i++) {
var chunk = this.chunks[i];
if (i > 0) {
s += ' ';
}
if (Buffer.isBuffer(chunk)) {
if (chunk.length === 0) {
s += '0';
} else {
s += '0x' + util.formatBuffer(encodeLen(chunk.length), 0) + ' ';
s += '0x' + util.formatBuffer(chunk, 0);
}
} else {
var opcode = Opcode.reverseMap[chunk];
if (typeof opcode === 'undefined') {
opcode = '0x'+chunk.toString(16);
}
s += opcode;
}
}
return s;
};
Script.stringToBuffer = function(s) {
var buf = new Put();
var split = s.split(' ');
for (var i = 0; i < split.length; i++) {
var word = split[i];
if (word === '') continue;
if (word.length > 2 && word.substring(0, 2) === '0x') {
// raw hex value
//console.log('hex value');
buf.put(new Buffer(word.substring(2, word.length), 'hex'));
} else {
var opcode = Opcode.map['OP_' + word];
if (typeof opcode !== 'undefined') {
// op code in string form
//console.log('opcode');
buf.word8(opcode);
} else {
var integer = parseInt(word);
if (!isNaN(integer)) {
// integer
//console.log('integer');
var data = util.intToBuffer(integer);
buf.put(Script.chunksToBuffer([data]));
} else if (word[0] === '\'' && word[word.length-1] === '\'') {
// string
//console.log('string');
word = word.substring(1,word.length-1);
var hexString = '';
for(var c=0;c<word.length;c++) {
hexString += ''+word.charCodeAt(c).toString(16);
}
buf.put(Script.chunksToBuffer([new Buffer(word)]));
} else {
throw new Error('Could not parse word "' +word+'" from script "'+s+'"');
}
}
}
}
return buf.buffer();
};
Script.chunksToBuffer = function(chunks) { Script.chunksToBuffer = function(chunks) {
var buf = new Put(); var buf = new Put();

View File

@ -27,6 +27,8 @@ function spec(b) {
throw new Error("ScriptInterpreter.eval() requires a callback"); throw new Error("ScriptInterpreter.eval() requires a callback");
} }
console.log(script);
var pc = 0; var pc = 0;
var execStack = []; var execStack = [];

View File

@ -88,8 +88,13 @@ describe('Script', function() {
if (datum.length < 2) throw new Error('Invalid test data'); if (datum.length < 2) throw new Error('Invalid test data');
var human = datum[0] + ' ' + datum[1]; var human = datum[0] + ' ' + datum[1];
it('should parse script from human readable ' + human, function() { it('should parse script from human readable ' + human, function() {
var h2 = Script.fromStringContent(human).getStringContent(false, null); //console.log('********');
Script.fromStringContent(h2).getStringContent(false, null).should.equal(h2); //console.log(human);
var script = Script.fromHumanReadable(human);
//console.log(script);
var h2 = script.toHumanReadable();
//console.log(h2);
Script.fromHumanReadable(h2).toHumanReadable().should.equal(h2);
}); });
}); });

View File

@ -31,8 +31,8 @@ describe('ScriptInterpreter', function() {
it('should validate script ' + human, function(done) { it('should validate script ' + human, function(done) {
i++; i++;
console.log(i + ' ' + human); console.log(i + ' ' + human);
ScriptInterpreter.verify(Script.fromStringContent(scriptSig), ScriptInterpreter.verify(Script.fromHumanReadable(scriptSig),
Script.fromStringContent(scriptPubKey), Script.fromHumanReadable(scriptPubKey),
null, 0, 0, // tx, output index, and hashtype null, 0, 0, // tx, output index, and hashtype
function (err, result) { function (err, result) {
should.not.exist(err); should.not.exist(err);