|
|
@ -131,6 +131,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')); |
|
|
@ -179,8 +221,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
|
|
|
@ -191,8 +234,12 @@ Script.prototype._chunkToString = function(chunk) { |
|
|
|
if (numstr.length % 2 !== 0) { |
|
|
|
numstr = '0' + numstr; |
|
|
|
} |
|
|
|
if (asm) { |
|
|
|
str = str + ' ' + numstr; |
|
|
|
} else { |
|
|
|
str = str + ' ' + '0x' + numstr; |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
// data chunk
|
|
|
|
if (opcodenum === Opcode.OP_PUSHDATA1 || |
|
|
@ -200,14 +247,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++) { |
|
|
|