Browse Source

fix format in ScriptInterpreter

patch-2
Manuel Araoz 11 years ago
parent
commit
9a64cb0ea1
  1. 126
      ScriptInterpreter.js

126
ScriptInterpreter.js

@ -20,7 +20,7 @@ function spec(b) {
function ScriptInterpreter() { function ScriptInterpreter() {
this.stack = []; this.stack = [];
this.disableUnsafeOpcodes = true; this.disableUnsafeOpcodes = true;
}; }
ScriptInterpreter.prototype.eval = function eval(script, tx, inIndex, hashType, callback) { ScriptInterpreter.prototype.eval = function eval(script, tx, inIndex, hashType, callback) {
if ("function" !== typeof callback) { if ("function" !== typeof callback) {
@ -76,22 +76,22 @@ function spec(b) {
} }
if (this.disableUnsafeOpcodes && if (this.disableUnsafeOpcodes &&
"number" === typeof opcode && "number" === typeof opcode &&
(opcode === OP_CAT || (opcode === OP_CAT ||
opcode === OP_SUBSTR || opcode === OP_SUBSTR ||
opcode === OP_LEFT || opcode === OP_LEFT ||
opcode === OP_RIGHT || opcode === OP_RIGHT ||
opcode === OP_INVERT || opcode === OP_INVERT ||
opcode === OP_AND || opcode === OP_AND ||
opcode === OP_OR || opcode === OP_OR ||
opcode === OP_XOR || opcode === OP_XOR ||
opcode === OP_2MUL || opcode === OP_2MUL ||
opcode === OP_2DIV || opcode === OP_2DIV ||
opcode === OP_MUL || opcode === OP_MUL ||
opcode === OP_DIV || opcode === OP_DIV ||
opcode === OP_MOD || opcode === OP_MOD ||
opcode === OP_LSHIFT || opcode === OP_LSHIFT ||
opcode === OP_RSHIFT)) { opcode === OP_RSHIFT)) {
throw new Error("Encountered a disabled opcode"); throw new Error("Encountered a disabled opcode");
} }
@ -619,15 +619,15 @@ function spec(b) {
// Verify signature // Verify signature
checkSig(sig, pubkey, scriptCode, tx, inIndex, hashType, function(e, result) { checkSig(sig, pubkey, scriptCode, tx, inIndex, hashType, function(e, result) {
try { try {
var success; var success;
if (e) { if (e) {
// We intentionally ignore errors during signature verification and // We intentionally ignore errors during signature verification and
// treat these cases as an invalid signature. // treat these cases as an invalid signature.
success = false; success = false;
} else { } else {
success = result; success = result;
} }
// Update stack // Update stack
@ -635,18 +635,18 @@ function spec(b) {
this.stackPop(); this.stackPop();
this.stack.push(new Buffer([success ? 1 : 0])); this.stack.push(new Buffer([success ? 1 : 0]));
if (opcode === OP_CHECKSIGVERIFY) { if (opcode === OP_CHECKSIGVERIFY) {
if (success) { if (success) {
this.stackPop(); this.stackPop();
} else { } else {
throw new Error("OP_CHECKSIGVERIFY negative"); throw new Error("OP_CHECKSIGVERIFY negative");
} }
} }
// Run next step // Run next step
executeStep.call(this, cb); executeStep.call(this, cb);
} catch (e) { } catch (e) {
cb(e); cb(e);
} }
}.bind(this)); }.bind(this));
// Note that for asynchronous opcodes we have to return here to prevent // Note that for asynchronous opcodes we have to return here to prevent
@ -690,12 +690,12 @@ function spec(b) {
// Drop the signatures, since a signature can't sign itself // Drop the signatures, since a signature can't sign itself
sigs.forEach(function(sig) { sigs.forEach(function(sig) {
scriptCode.findAndDelete(sig); scriptCode.findAndDelete(sig);
}); });
var success = true, var success = true,
isig = 0, isig = 0,
ikey = 0; ikey = 0;
checkMultiSigStep.call(this); checkMultiSigStep.call(this);
function checkMultiSigStep() { function checkMultiSigStep() {
@ -705,26 +705,26 @@ function spec(b) {
var key = keys[ikey]; var key = keys[ikey];
checkSig(sig, key, scriptCode, tx, inIndex, hashType, function(e, result) { checkSig(sig, key, scriptCode, tx, inIndex, hashType, function(e, result) {
try { try {
if (!e && result) { if (!e && result) {
isig++; isig++;
sigsCount--; sigsCount--;
} else { } else {
ikey++; ikey++;
keysCount--; keysCount--;
// If there are more signatures than keys left, then too many // If there are more signatures than keys left, then too many
// signatures have failed // signatures have failed
if (sigsCount > keysCount) { if (sigsCount > keysCount) {
success = false; success = false;
} }
} }
checkMultiSigStep.call(this); checkMultiSigStep.call(this);
} catch (e) { } catch (e) {
cb(e); cb(e);
} }
}.bind(this)); }.bind(this));
} else { } else {
this.stack.push(new Buffer([success ? 1 : 0])); this.stack.push(new Buffer([success ? 1 : 0]));
if (opcode === OP_CHECKMULTISIGVERIFY) { if (opcode === OP_CHECKMULTISIGVERIFY) {
@ -748,7 +748,7 @@ function spec(b) {
return; return;
default: default:
console.log('opcode '+opcode); console.log('opcode ' + opcode);
throw new Error("Unknown opcode encountered"); throw new Error("Unknown opcode encountered");
} }
@ -767,7 +767,7 @@ function spec(b) {
} }
} catch (e) { } catch (e) {
log.debug("Script aborted: " + log.debug("Script aborted: " +
(e.message ? e.message : e)); (e.message ? e.message : e));
cb(e); cb(e);
} }
} }
@ -778,14 +778,14 @@ function spec(b) {
var self = this; var self = this;
self.eval(scriptSig, tx, n, hashType, function(e) { self.eval(scriptSig, tx, n, hashType, function(e) {
if (e) { if (e) {
callback(e) callback(e)
return; return;
} }
self.eval(scriptPubkey, tx, n, hashType, callback); self.eval(scriptPubkey, tx, n, hashType, callback);
}); });
}; };
/** /**
* Get the top element of the stack. * Get the top element of the stack.
@ -825,7 +825,7 @@ function spec(b) {
} }
var s = this.stack, var s = this.stack,
l = s.length; l = s.length;
var tmp = s[l - a]; var tmp = s[l - a];
s[l - a] = s[l - b]; s[l - a] = s[l - b];
@ -840,16 +840,16 @@ function spec(b) {
*/ */
ScriptInterpreter.prototype.getPrimitiveStack = function getPrimitiveStack() { ScriptInterpreter.prototype.getPrimitiveStack = function getPrimitiveStack() {
return this.stack.map(function(entry) { return this.stack.map(function(entry) {
if (entry.length > 2) { if (entry.length > 2) {
return buffertools.toHex(entry.slice(0)); return buffertools.toHex(entry.slice(0));
} }
var num = castBigint(entry); var num = castBigint(entry);
if (num.cmp(-128) >= 0 && num.cmp(127) <= 0) { if (num.cmp(-128) >= 0 && num.cmp(127) <= 0) {
return num.toNumber(); return num.toNumber();
} else { } else {
return buffertools.toHex(entry.slice(0)); return buffertools.toHex(entry.slice(0));
} }
}); });
}; };
var castBool = ScriptInterpreter.castBool = function castBool(v) { var castBool = ScriptInterpreter.castBool = function castBool(v) {

Loading…
Cancel
Save