Browse Source

Merge pull request #177 from dcousens/opclean

OPcodes cleanup
hk-custom-address
Wei Lu 11 years ago
parent
commit
e835a6264d
  1. 267
      src/opcode.js
  2. 68
      src/script.js

267
src/opcode.js

@ -1,147 +1,150 @@
var Opcode = { var ops = {
map: { // push value
// push value OP_0 : 0,
OP_0 : 0, OP_FALSE : 0,
OP_FALSE : 0, OP_PUSHDATA1 : 76,
OP_PUSHDATA1 : 76, OP_PUSHDATA2 : 77,
OP_PUSHDATA2 : 77, OP_PUSHDATA4 : 78,
OP_PUSHDATA4 : 78, OP_1NEGATE : 79,
OP_1NEGATE : 79, OP_RESERVED : 80,
OP_RESERVED : 80, OP_1 : 81,
OP_1 : 81, OP_TRUE : 81,
OP_TRUE : 81, OP_2 : 82,
OP_2 : 82, OP_3 : 83,
OP_3 : 83, OP_4 : 84,
OP_4 : 84, OP_5 : 85,
OP_5 : 85, OP_6 : 86,
OP_6 : 86, OP_7 : 87,
OP_7 : 87, OP_8 : 88,
OP_8 : 88, OP_9 : 89,
OP_9 : 89, OP_10 : 90,
OP_10 : 90, OP_11 : 91,
OP_11 : 91, OP_12 : 92,
OP_12 : 92, OP_13 : 93,
OP_13 : 93, OP_14 : 94,
OP_14 : 94, OP_15 : 95,
OP_15 : 95, OP_16 : 96,
OP_16 : 96,
// control // control
OP_NOP : 97, OP_NOP : 97,
OP_VER : 98, OP_VER : 98,
OP_IF : 99, OP_IF : 99,
OP_NOTIF : 100, OP_NOTIF : 100,
OP_VERIF : 101, OP_VERIF : 101,
OP_VERNOTIF : 102, OP_VERNOTIF : 102,
OP_ELSE : 103, OP_ELSE : 103,
OP_ENDIF : 104, OP_ENDIF : 104,
OP_VERIFY : 105, OP_VERIFY : 105,
OP_RETURN : 106, OP_RETURN : 106,
// stack ops // stack ops
OP_TOALTSTACK : 107, OP_TOALTSTACK : 107,
OP_FROMALTSTACK : 108, OP_FROMALTSTACK : 108,
OP_2DROP : 109, OP_2DROP : 109,
OP_2DUP : 110, OP_2DUP : 110,
OP_3DUP : 111, OP_3DUP : 111,
OP_2OVER : 112, OP_2OVER : 112,
OP_2ROT : 113, OP_2ROT : 113,
OP_2SWAP : 114, OP_2SWAP : 114,
OP_IFDUP : 115, OP_IFDUP : 115,
OP_DEPTH : 116, OP_DEPTH : 116,
OP_DROP : 117, OP_DROP : 117,
OP_DUP : 118, OP_DUP : 118,
OP_NIP : 119, OP_NIP : 119,
OP_OVER : 120, OP_OVER : 120,
OP_PICK : 121, OP_PICK : 121,
OP_ROLL : 122, OP_ROLL : 122,
OP_ROT : 123, OP_ROT : 123,
OP_SWAP : 124, OP_SWAP : 124,
OP_TUCK : 125, OP_TUCK : 125,
// splice ops // splice ops
OP_CAT : 126, OP_CAT : 126,
OP_SUBSTR : 127, OP_SUBSTR : 127,
OP_LEFT : 128, OP_LEFT : 128,
OP_RIGHT : 129, OP_RIGHT : 129,
OP_SIZE : 130, OP_SIZE : 130,
// bit logic // bit logic
OP_INVERT : 131, OP_INVERT : 131,
OP_AND : 132, OP_AND : 132,
OP_OR : 133, OP_OR : 133,
OP_XOR : 134, OP_XOR : 134,
OP_EQUAL : 135, OP_EQUAL : 135,
OP_EQUALVERIFY : 136, OP_EQUALVERIFY : 136,
OP_RESERVED1 : 137, OP_RESERVED1 : 137,
OP_RESERVED2 : 138, OP_RESERVED2 : 138,
// numeric // numeric
OP_1ADD : 139, OP_1ADD : 139,
OP_1SUB : 140, OP_1SUB : 140,
OP_2MUL : 141, OP_2MUL : 141,
OP_2DIV : 142, OP_2DIV : 142,
OP_NEGATE : 143, OP_NEGATE : 143,
OP_ABS : 144, OP_ABS : 144,
OP_NOT : 145, OP_NOT : 145,
OP_0NOTEQUAL : 146, OP_0NOTEQUAL : 146,
OP_ADD : 147, OP_ADD : 147,
OP_SUB : 148, OP_SUB : 148,
OP_MUL : 149, OP_MUL : 149,
OP_DIV : 150, OP_DIV : 150,
OP_MOD : 151, OP_MOD : 151,
OP_LSHIFT : 152, OP_LSHIFT : 152,
OP_RSHIFT : 153, OP_RSHIFT : 153,
OP_BOOLAND : 154, OP_BOOLAND : 154,
OP_BOOLOR : 155, OP_BOOLOR : 155,
OP_NUMEQUAL : 156, OP_NUMEQUAL : 156,
OP_NUMEQUALVERIFY : 157, OP_NUMEQUALVERIFY : 157,
OP_NUMNOTEQUAL : 158, OP_NUMNOTEQUAL : 158,
OP_LESSTHAN : 159, OP_LESSTHAN : 159,
OP_GREATERTHAN : 160, OP_GREATERTHAN : 160,
OP_LESSTHANOREQUAL : 161, OP_LESSTHANOREQUAL : 161,
OP_GREATERTHANOREQUAL : 162, OP_GREATERTHANOREQUAL : 162,
OP_MIN : 163, OP_MIN : 163,
OP_MAX : 164, OP_MAX : 164,
OP_WITHIN : 165, OP_WITHIN : 165,
// crypto // crypto
OP_RIPEMD160 : 166, OP_RIPEMD160 : 166,
OP_SHA1 : 167, OP_SHA1 : 167,
OP_SHA256 : 168, OP_SHA256 : 168,
OP_HASH160 : 169, OP_HASH160 : 169,
OP_HASH256 : 170, OP_HASH256 : 170,
OP_CODESEPARATOR : 171, OP_CODESEPARATOR : 171,
OP_CHECKSIG : 172, OP_CHECKSIG : 172,
OP_CHECKSIGVERIFY : 173, OP_CHECKSIGVERIFY : 173,
OP_CHECKMULTISIG : 174, OP_CHECKMULTISIG : 174,
OP_CHECKMULTISIGVERIFY : 175, OP_CHECKMULTISIGVERIFY : 175,
// expansion // expansion
OP_NOP1 : 176, OP_NOP1 : 176,
OP_NOP2 : 177, OP_NOP2 : 177,
OP_NOP3 : 178, OP_NOP3 : 178,
OP_NOP4 : 179, OP_NOP4 : 179,
OP_NOP5 : 180, OP_NOP5 : 180,
OP_NOP6 : 181, OP_NOP6 : 181,
OP_NOP7 : 182, OP_NOP7 : 182,
OP_NOP8 : 183, OP_NOP8 : 183,
OP_NOP9 : 184, OP_NOP9 : 184,
OP_NOP10 : 185, OP_NOP10 : 185,
// template matching params // template matching params
OP_PUBKEYHASH : 253, OP_PUBKEYHASH : 253,
OP_PUBKEY : 254, OP_PUBKEY : 254,
OP_INVALIDOPCODE : 255 OP_INVALIDOPCODE : 255
},
reverseMap: []
} }
for(var i in Opcode.map) { var reverse = []
Opcode.reverseMap[Opcode.map[i]] = i for (var op in ops) {
var code = ops[code]
reverse[code] = op
} }
module.exports = Opcode module.exports = {
map: ops,
reverseMap: reverse
}

68
src/script.js

@ -1,6 +1,6 @@
var assert = require('assert') var assert = require('assert')
var crypto = require('./crypto') var crypto = require('./crypto')
var Opcode = require('./opcode') var opcodes = require('./opcode').map
function Script(data) { function Script(data) {
data = data || [] data = data || []
@ -63,16 +63,16 @@ Script.prototype.parse = function() {
} }
var len 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 // Read some bytes of data, opcode value is the length of data
readChunk(opcode) readChunk(opcode)
} else if (opcode == Opcode.map.OP_PUSHDATA1) { } else if (opcode == opcodes.OP_PUSHDATA1) {
len = this.buffer[i++] len = this.buffer[i++]
readChunk(len) readChunk(len)
} else if (opcode == Opcode.map.OP_PUSHDATA2) { } else if (opcode == opcodes.OP_PUSHDATA2) {
len = (this.buffer[i++] << 8) | this.buffer[i++] len = (this.buffer[i++] << 8) | this.buffer[i++]
readChunk(len) readChunk(len)
} else if (opcode == Opcode.map.OP_PUSHDATA4) { } else if (opcode == opcodes.OP_PUSHDATA4) {
len = (this.buffer[i++] << 24) | len = (this.buffer[i++] << 24) |
(this.buffer[i++] << 16) | (this.buffer[i++] << 16) |
(this.buffer[i++] << 8) | (this.buffer[i++] << 8) |
@ -136,23 +136,23 @@ Script.prototype.getOutType = function() {
function isPubkeyhash() { function isPubkeyhash() {
return this.chunks.length == 5 && return this.chunks.length == 5 &&
this.chunks[0] == Opcode.map.OP_DUP && this.chunks[0] == opcodes.OP_DUP &&
this.chunks[1] == Opcode.map.OP_HASH160 && this.chunks[1] == opcodes.OP_HASH160 &&
Array.isArray(this.chunks[2]) && Array.isArray(this.chunks[2]) &&
this.chunks[2].length === 20 && this.chunks[2].length === 20 &&
this.chunks[3] == Opcode.map.OP_EQUALVERIFY && this.chunks[3] == opcodes.OP_EQUALVERIFY &&
this.chunks[4] == Opcode.map.OP_CHECKSIG this.chunks[4] == opcodes.OP_CHECKSIG
} }
function isPubkey() { function isPubkey() {
return this.chunks.length === 2 && return this.chunks.length === 2 &&
Array.isArray(this.chunks[0]) && Array.isArray(this.chunks[0]) &&
this.chunks[1] === Opcode.map.OP_CHECKSIG this.chunks[1] === opcodes.OP_CHECKSIG
} }
function isScripthash() { function isScripthash() {
return this.chunks[this.chunks.length - 1] == Opcode.map.OP_EQUAL && return this.chunks[this.chunks.length - 1] == opcodes.OP_EQUAL &&
this.chunks[0] == Opcode.map.OP_HASH160 && this.chunks[0] == opcodes.OP_HASH160 &&
Array.isArray(this.chunks[1]) && Array.isArray(this.chunks[1]) &&
this.chunks[1].length === 20 && this.chunks[1].length === 20 &&
this.chunks.length == 3 this.chunks.length == 3
@ -167,20 +167,20 @@ function isMultisig() {
// n greater or equal to m // n greater or equal to m
this.chunks[0] <= this.chunks[this.chunks.length - 2] && this.chunks[0] <= this.chunks[this.chunks.length - 2] &&
// n cannot be 0 // 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) // 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 // 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() { function isNulldata() {
return this.chunks[0] === Opcode.map.OP_RETURN return this.chunks[0] === opcodes.OP_RETURN
} }
function isSmallIntOp(opcode) { function isSmallIntOp(opcode) {
return ((opcode == Opcode.map.OP_0) || return ((opcode == opcodes.OP_0) ||
((opcode >= Opcode.map.OP_1) && (opcode <= Opcode.map.OP_16))) ((opcode >= opcodes.OP_1) && (opcode <= opcodes.OP_16)))
} }
Script.prototype.getHash = function() { Script.prototype.getHash = function() {
@ -222,7 +222,7 @@ Script.prototype.getInType = function() {
Array.isArray(this.chunks[0]) && Array.isArray(this.chunks[0]) &&
Array.isArray(this.chunks[1])) { Array.isArray(this.chunks[1])) {
return 'pubkeyhash' 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) { this.chunks.slice(1).reduce(function(t, chunk, i) {
return t && Array.isArray(chunk) && (chunk[0] == 48 || i == this.chunks.length - 1) return t && Array.isArray(chunk) && (chunk[0] == 48 || i == this.chunks.length - 1)
}, true)) { }, true)) {
@ -248,17 +248,17 @@ Script.prototype.writeBytes = function(data) {
if (Buffer.isBuffer(data)) data = Array.prototype.slice.call(data); if (Buffer.isBuffer(data)) data = Array.prototype.slice.call(data);
assert(Array.isArray(data), "Expected a byte array. Got " + 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) this.buffer.push(data.length)
} else if (data.length <= 0xff) { } else if (data.length <= 0xff) {
this.buffer.push(Opcode.map.OP_PUSHDATA1) this.buffer.push(opcodes.OP_PUSHDATA1)
this.buffer.push(data.length) this.buffer.push(data.length)
} else if (data.length <= 0xffff) { } 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 & 0xff)
this.buffer.push((data.length >>> 8) & 0xff) this.buffer.push((data.length >>> 8) & 0xff)
} else { } 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 & 0xff)
this.buffer.push((data.length >>> 8) & 0xff) this.buffer.push((data.length >>> 8) & 0xff)
this.buffer.push((data.length >>> 16) & 0xff) this.buffer.push((data.length >>> 16) & 0xff)
@ -272,11 +272,11 @@ Script.prototype.writeBytes = function(data) {
Script.createPubKeyHashScriptPubKey = function(hash) { Script.createPubKeyHashScriptPubKey = function(hash) {
var script = new Script() var script = new Script()
script.writeOp(Opcode.map.OP_DUP) script.writeOp(opcodes.OP_DUP)
script.writeOp(Opcode.map.OP_HASH160) script.writeOp(opcodes.OP_HASH160)
script.writeBytes(hash) script.writeBytes(hash)
script.writeOp(Opcode.map.OP_EQUALVERIFY) script.writeOp(opcodes.OP_EQUALVERIFY)
script.writeOp(Opcode.map.OP_CHECKSIG) script.writeOp(opcodes.OP_CHECKSIG)
return script return script
} }
@ -285,9 +285,9 @@ Script.createPubKeyHashScriptPubKey = function(hash) {
Script.createP2SHScriptPubKey = function(hash) { Script.createP2SHScriptPubKey = function(hash) {
var script = new Script() var script = new Script()
script.writeOp(Opcode.map.OP_HASH160) script.writeOp(opcodes.OP_HASH160)
script.writeBytes(hash) script.writeBytes(hash)
script.writeOp(Opcode.map.OP_EQUAL) script.writeOp(opcodes.OP_EQUAL)
return script return script
} }
@ -296,12 +296,12 @@ Script.createP2SHScriptPubKey = function(hash) {
Script.createMultisigScriptPubKey = function(m, pubKeys) { Script.createMultisigScriptPubKey = function(m, pubKeys) {
var script = new Script() 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) { for (var i = 0; i < pubKeys.length; ++i) {
script.writeBytes(pubKeys[i]) script.writeBytes(pubKeys[i])
} }
script.writeOp(Opcode.map.OP_1 + pubKeys.length - 1) script.writeOp(opcodes.OP_1 + pubKeys.length - 1)
script.writeOp(Opcode.map.OP_CHECKMULTISIG) script.writeOp(opcodes.OP_CHECKMULTISIG)
return script return script
} }
@ -320,13 +320,13 @@ Script.createMultisigScriptSig = function(signatures, scriptPubKey) {
assert(isMultisig.call(scriptPubKey)) assert(isMultisig.call(scriptPubKey))
var m = scriptPubKey.chunks[0] 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') assert(k <= signatures.length, 'Not enough signatures provided')
} }
var inScript = new Script() var inScript = new Script()
inScript.writeOp(Opcode.map.OP_0) inScript.writeOp(opcodes.OP_0)
signatures.map(function(sig) { signatures.map(function(sig) {
inScript.writeBytes(sig) inScript.writeBytes(sig)
}) })

Loading…
Cancel
Save