From 1fe8282eda1969dd18506b6ca2bd26098867058b Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Sat, 10 May 2014 11:35:21 +1000 Subject: [PATCH 1/2] Script: remove redundant opcode.map access --- src/script.js | 68 +++++++++++++++++++++++++-------------------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/src/script.js b/src/script.js index a5a4562..f72c0cf 100644 --- a/src/script.js +++ b/src/script.js @@ -1,6 +1,6 @@ var assert = require('assert') var crypto = require('./crypto') -var Opcode = require('./opcode') +var opcodes = require('./opcode').map function Script(data) { data = data || [] @@ -63,16 +63,16 @@ Script.prototype.parse = function() { } var len - if (opcode > 0 && opcode < Opcode.map.OP_PUSHDATA1) { + if (opcode > 0 && opcode < opcodes.OP_PUSHDATA1) { // Read some bytes of data, opcode value is the length of data readChunk(opcode) - } else if (opcode == Opcode.map.OP_PUSHDATA1) { + } else if (opcode == opcodes.OP_PUSHDATA1) { len = this.buffer[i++] readChunk(len) - } else if (opcode == Opcode.map.OP_PUSHDATA2) { + } else if (opcode == opcodes.OP_PUSHDATA2) { len = (this.buffer[i++] << 8) | this.buffer[i++] readChunk(len) - } else if (opcode == Opcode.map.OP_PUSHDATA4) { + } else if (opcode == opcodes.OP_PUSHDATA4) { len = (this.buffer[i++] << 24) | (this.buffer[i++] << 16) | (this.buffer[i++] << 8) | @@ -136,23 +136,23 @@ Script.prototype.getOutType = function() { function isPubkeyhash() { return this.chunks.length == 5 && - this.chunks[0] == Opcode.map.OP_DUP && - this.chunks[1] == Opcode.map.OP_HASH160 && + this.chunks[0] == opcodes.OP_DUP && + this.chunks[1] == opcodes.OP_HASH160 && Array.isArray(this.chunks[2]) && this.chunks[2].length === 20 && - this.chunks[3] == Opcode.map.OP_EQUALVERIFY && - this.chunks[4] == Opcode.map.OP_CHECKSIG + this.chunks[3] == opcodes.OP_EQUALVERIFY && + this.chunks[4] == opcodes.OP_CHECKSIG } function isPubkey() { return this.chunks.length === 2 && Array.isArray(this.chunks[0]) && - this.chunks[1] === Opcode.map.OP_CHECKSIG + this.chunks[1] === opcodes.OP_CHECKSIG } function isScripthash() { - return this.chunks[this.chunks.length - 1] == Opcode.map.OP_EQUAL && - this.chunks[0] == Opcode.map.OP_HASH160 && + return this.chunks[this.chunks.length - 1] == opcodes.OP_EQUAL && + this.chunks[0] == opcodes.OP_HASH160 && Array.isArray(this.chunks[1]) && this.chunks[1].length === 20 && this.chunks.length == 3 @@ -167,20 +167,20 @@ function isMultisig() { // n greater or equal to m this.chunks[0] <= this.chunks[this.chunks.length - 2] && // n cannot be 0 - this.chunks[this.chunks.length - 2] !== Opcode.map.OP_0 && + this.chunks[this.chunks.length - 2] !== opcodes.OP_0 && // n is the size of chunk length minus 3 (m, n, OP_CHECKMULTISIG) - this.chunks.length - 3 === this.chunks[this.chunks.length - 2] - Opcode.map.OP_RESERVED && + this.chunks.length - 3 === this.chunks[this.chunks.length - 2] - opcodes.OP_RESERVED && // last chunk is OP_CHECKMULTISIG - this.chunks[this.chunks.length - 1] == Opcode.map.OP_CHECKMULTISIG + this.chunks[this.chunks.length - 1] == opcodes.OP_CHECKMULTISIG } function isNulldata() { - return this.chunks[0] === Opcode.map.OP_RETURN + return this.chunks[0] === opcodes.OP_RETURN } function isSmallIntOp(opcode) { - return ((opcode == Opcode.map.OP_0) || - ((opcode >= Opcode.map.OP_1) && (opcode <= Opcode.map.OP_16))) + return ((opcode == opcodes.OP_0) || + ((opcode >= opcodes.OP_1) && (opcode <= opcodes.OP_16))) } Script.prototype.getHash = function() { @@ -222,7 +222,7 @@ Script.prototype.getInType = function() { Array.isArray(this.chunks[0]) && Array.isArray(this.chunks[1])) { return 'pubkeyhash' - } else if (this.chunks[0] == Opcode.map.OP_0 && + } else if (this.chunks[0] == opcodes.OP_0 && this.chunks.slice(1).reduce(function(t, chunk, i) { return t && Array.isArray(chunk) && (chunk[0] == 48 || i == this.chunks.length - 1) }, true)) { @@ -248,17 +248,17 @@ Script.prototype.writeBytes = function(data) { if (Buffer.isBuffer(data)) data = Array.prototype.slice.call(data); assert(Array.isArray(data), "Expected a byte array. Got " + data) - if (data.length < Opcode.map.OP_PUSHDATA1) { + if (data.length < opcodes.OP_PUSHDATA1) { this.buffer.push(data.length) } else if (data.length <= 0xff) { - this.buffer.push(Opcode.map.OP_PUSHDATA1) + this.buffer.push(opcodes.OP_PUSHDATA1) this.buffer.push(data.length) } else if (data.length <= 0xffff) { - this.buffer.push(Opcode.map.OP_PUSHDATA2) + this.buffer.push(opcodes.OP_PUSHDATA2) this.buffer.push(data.length & 0xff) this.buffer.push((data.length >>> 8) & 0xff) } else { - this.buffer.push(Opcode.map.OP_PUSHDATA4) + this.buffer.push(opcodes.OP_PUSHDATA4) this.buffer.push(data.length & 0xff) this.buffer.push((data.length >>> 8) & 0xff) this.buffer.push((data.length >>> 16) & 0xff) @@ -272,11 +272,11 @@ Script.prototype.writeBytes = function(data) { Script.createPubKeyHashScriptPubKey = function(hash) { var script = new Script() - script.writeOp(Opcode.map.OP_DUP) - script.writeOp(Opcode.map.OP_HASH160) + script.writeOp(opcodes.OP_DUP) + script.writeOp(opcodes.OP_HASH160) script.writeBytes(hash) - script.writeOp(Opcode.map.OP_EQUALVERIFY) - script.writeOp(Opcode.map.OP_CHECKSIG) + script.writeOp(opcodes.OP_EQUALVERIFY) + script.writeOp(opcodes.OP_CHECKSIG) return script } @@ -285,9 +285,9 @@ Script.createPubKeyHashScriptPubKey = function(hash) { Script.createP2SHScriptPubKey = function(hash) { var script = new Script() - script.writeOp(Opcode.map.OP_HASH160) + script.writeOp(opcodes.OP_HASH160) script.writeBytes(hash) - script.writeOp(Opcode.map.OP_EQUAL) + script.writeOp(opcodes.OP_EQUAL) return script } @@ -296,12 +296,12 @@ Script.createP2SHScriptPubKey = function(hash) { Script.createMultisigScriptPubKey = function(m, pubKeys) { var script = new Script() - script.writeOp(Opcode.map.OP_1 + m - 1) + script.writeOp(opcodes.OP_1 + m - 1) for (var i = 0; i < pubKeys.length; ++i) { script.writeBytes(pubKeys[i]) } - script.writeOp(Opcode.map.OP_1 + pubKeys.length - 1) - script.writeOp(Opcode.map.OP_CHECKMULTISIG) + script.writeOp(opcodes.OP_1 + pubKeys.length - 1) + script.writeOp(opcodes.OP_CHECKMULTISIG) return script } @@ -320,13 +320,13 @@ Script.createMultisigScriptSig = function(signatures, scriptPubKey) { assert(isMultisig.call(scriptPubKey)) var m = scriptPubKey.chunks[0] - var k = m - (Opcode.map.OP_1 - 1) + var k = m - (opcodes.OP_1 - 1) assert(k <= signatures.length, 'Not enough signatures provided') } var inScript = new Script() - inScript.writeOp(Opcode.map.OP_0) + inScript.writeOp(opcodes.OP_0) signatures.map(function(sig) { inScript.writeBytes(sig) }) From 554ba250b96550c2341a529c65936ef384e49012 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Sat, 10 May 2014 11:35:53 +1000 Subject: [PATCH 2/2] opcode: less redundant indentation --- src/opcode.js | 267 +++++++++++++++++++++++++------------------------- 1 file changed, 135 insertions(+), 132 deletions(-) diff --git a/src/opcode.js b/src/opcode.js index 69049c7..1352505 100644 --- a/src/opcode.js +++ b/src/opcode.js @@ -1,147 +1,150 @@ -var Opcode = { - map: { - // push value - OP_0 : 0, - OP_FALSE : 0, - OP_PUSHDATA1 : 76, - OP_PUSHDATA2 : 77, - OP_PUSHDATA4 : 78, - OP_1NEGATE : 79, - OP_RESERVED : 80, - OP_1 : 81, - OP_TRUE : 81, - OP_2 : 82, - OP_3 : 83, - OP_4 : 84, - OP_5 : 85, - OP_6 : 86, - OP_7 : 87, - OP_8 : 88, - OP_9 : 89, - OP_10 : 90, - OP_11 : 91, - OP_12 : 92, - OP_13 : 93, - OP_14 : 94, - OP_15 : 95, - OP_16 : 96, +var ops = { + // push value + OP_0 : 0, + OP_FALSE : 0, + OP_PUSHDATA1 : 76, + OP_PUSHDATA2 : 77, + OP_PUSHDATA4 : 78, + OP_1NEGATE : 79, + OP_RESERVED : 80, + OP_1 : 81, + OP_TRUE : 81, + OP_2 : 82, + OP_3 : 83, + OP_4 : 84, + OP_5 : 85, + OP_6 : 86, + OP_7 : 87, + OP_8 : 88, + OP_9 : 89, + OP_10 : 90, + OP_11 : 91, + OP_12 : 92, + OP_13 : 93, + OP_14 : 94, + OP_15 : 95, + OP_16 : 96, - // control - OP_NOP : 97, - OP_VER : 98, - OP_IF : 99, - OP_NOTIF : 100, - OP_VERIF : 101, - OP_VERNOTIF : 102, - OP_ELSE : 103, - OP_ENDIF : 104, - OP_VERIFY : 105, - OP_RETURN : 106, + // control + OP_NOP : 97, + OP_VER : 98, + OP_IF : 99, + OP_NOTIF : 100, + OP_VERIF : 101, + OP_VERNOTIF : 102, + OP_ELSE : 103, + OP_ENDIF : 104, + OP_VERIFY : 105, + OP_RETURN : 106, - // stack ops - OP_TOALTSTACK : 107, - OP_FROMALTSTACK : 108, - OP_2DROP : 109, - OP_2DUP : 110, - OP_3DUP : 111, - OP_2OVER : 112, - OP_2ROT : 113, - OP_2SWAP : 114, - OP_IFDUP : 115, - OP_DEPTH : 116, - OP_DROP : 117, - OP_DUP : 118, - OP_NIP : 119, - OP_OVER : 120, - OP_PICK : 121, - OP_ROLL : 122, - OP_ROT : 123, - OP_SWAP : 124, - OP_TUCK : 125, + // stack ops + OP_TOALTSTACK : 107, + OP_FROMALTSTACK : 108, + OP_2DROP : 109, + OP_2DUP : 110, + OP_3DUP : 111, + OP_2OVER : 112, + OP_2ROT : 113, + OP_2SWAP : 114, + OP_IFDUP : 115, + OP_DEPTH : 116, + OP_DROP : 117, + OP_DUP : 118, + OP_NIP : 119, + OP_OVER : 120, + OP_PICK : 121, + OP_ROLL : 122, + OP_ROT : 123, + OP_SWAP : 124, + OP_TUCK : 125, - // splice ops - OP_CAT : 126, - OP_SUBSTR : 127, - OP_LEFT : 128, - OP_RIGHT : 129, - OP_SIZE : 130, + // splice ops + OP_CAT : 126, + OP_SUBSTR : 127, + OP_LEFT : 128, + OP_RIGHT : 129, + OP_SIZE : 130, - // bit logic - OP_INVERT : 131, - OP_AND : 132, - OP_OR : 133, - OP_XOR : 134, - OP_EQUAL : 135, - OP_EQUALVERIFY : 136, - OP_RESERVED1 : 137, - OP_RESERVED2 : 138, + // bit logic + OP_INVERT : 131, + OP_AND : 132, + OP_OR : 133, + OP_XOR : 134, + OP_EQUAL : 135, + OP_EQUALVERIFY : 136, + OP_RESERVED1 : 137, + OP_RESERVED2 : 138, - // numeric - OP_1ADD : 139, - OP_1SUB : 140, - OP_2MUL : 141, - OP_2DIV : 142, - OP_NEGATE : 143, - OP_ABS : 144, - OP_NOT : 145, - OP_0NOTEQUAL : 146, + // numeric + OP_1ADD : 139, + OP_1SUB : 140, + OP_2MUL : 141, + OP_2DIV : 142, + OP_NEGATE : 143, + OP_ABS : 144, + OP_NOT : 145, + OP_0NOTEQUAL : 146, - OP_ADD : 147, - OP_SUB : 148, - OP_MUL : 149, - OP_DIV : 150, - OP_MOD : 151, - OP_LSHIFT : 152, - OP_RSHIFT : 153, + OP_ADD : 147, + OP_SUB : 148, + OP_MUL : 149, + OP_DIV : 150, + OP_MOD : 151, + OP_LSHIFT : 152, + OP_RSHIFT : 153, - OP_BOOLAND : 154, - OP_BOOLOR : 155, - OP_NUMEQUAL : 156, - OP_NUMEQUALVERIFY : 157, - OP_NUMNOTEQUAL : 158, - OP_LESSTHAN : 159, - OP_GREATERTHAN : 160, - OP_LESSTHANOREQUAL : 161, - OP_GREATERTHANOREQUAL : 162, - OP_MIN : 163, - OP_MAX : 164, + OP_BOOLAND : 154, + OP_BOOLOR : 155, + OP_NUMEQUAL : 156, + OP_NUMEQUALVERIFY : 157, + OP_NUMNOTEQUAL : 158, + OP_LESSTHAN : 159, + OP_GREATERTHAN : 160, + OP_LESSTHANOREQUAL : 161, + OP_GREATERTHANOREQUAL : 162, + OP_MIN : 163, + OP_MAX : 164, - OP_WITHIN : 165, + OP_WITHIN : 165, - // crypto - OP_RIPEMD160 : 166, - OP_SHA1 : 167, - OP_SHA256 : 168, - OP_HASH160 : 169, - OP_HASH256 : 170, - OP_CODESEPARATOR : 171, - OP_CHECKSIG : 172, - OP_CHECKSIGVERIFY : 173, - OP_CHECKMULTISIG : 174, - OP_CHECKMULTISIGVERIFY : 175, + // crypto + OP_RIPEMD160 : 166, + OP_SHA1 : 167, + OP_SHA256 : 168, + OP_HASH160 : 169, + OP_HASH256 : 170, + OP_CODESEPARATOR : 171, + OP_CHECKSIG : 172, + OP_CHECKSIGVERIFY : 173, + OP_CHECKMULTISIG : 174, + OP_CHECKMULTISIGVERIFY : 175, - // expansion - OP_NOP1 : 176, - OP_NOP2 : 177, - OP_NOP3 : 178, - OP_NOP4 : 179, - OP_NOP5 : 180, - OP_NOP6 : 181, - OP_NOP7 : 182, - OP_NOP8 : 183, - OP_NOP9 : 184, - OP_NOP10 : 185, + // expansion + OP_NOP1 : 176, + OP_NOP2 : 177, + OP_NOP3 : 178, + OP_NOP4 : 179, + OP_NOP5 : 180, + OP_NOP6 : 181, + OP_NOP7 : 182, + OP_NOP8 : 183, + OP_NOP9 : 184, + OP_NOP10 : 185, - // template matching params - OP_PUBKEYHASH : 253, - OP_PUBKEY : 254, - OP_INVALIDOPCODE : 255 - }, - reverseMap: [] + // template matching params + OP_PUBKEYHASH : 253, + OP_PUBKEY : 254, + OP_INVALIDOPCODE : 255 } -for(var i in Opcode.map) { - Opcode.reverseMap[Opcode.map[i]] = i +var reverse = [] +for (var op in ops) { + var code = ops[code] + + reverse[code] = op } -module.exports = Opcode +module.exports = { + map: ops, + reverseMap: reverse +}