From 33c72e49c401aad7def4a2677ccea0b2619a44db Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Sun, 18 Dec 2016 01:24:00 +1100 Subject: [PATCH] package/*: use bitcoin-ops, pushdata-bitcoin packages --- package.json | 2 + src/bufferutils.js | 78 +------------ src/index.js | 2 +- src/opcodes.json | 130 ---------------------- src/script.js | 20 ++-- src/templates/multisig/input.js | 2 +- src/templates/multisig/output.js | 2 +- src/templates/nulldata.js | 2 +- src/templates/pubkey/output.js | 2 +- src/templates/pubkeyhash/output.js | 2 +- src/templates/scripthash/output.js | 2 +- src/templates/witnesspubkeyhash/output.js | 2 +- src/templates/witnessscripthash/output.js | 2 +- src/transaction.js | 2 +- src/transaction_builder.js | 2 +- test/templates.js | 2 +- test/transaction_builder.js | 2 +- 17 files changed, 26 insertions(+), 230 deletions(-) delete mode 100644 src/opcodes.json diff --git a/package.json b/package.json index 2ee7b68..33121d2 100644 --- a/package.json +++ b/package.json @@ -53,12 +53,14 @@ "dependencies": { "bigi": "^1.4.0", "bip66": "^1.1.0", + "bitcoin-ops": "^1.3.0", "bs58check": "^1.0.5", "buffer-reverse": "^1.0.0", "create-hash": "^1.1.0", "create-hmac": "^1.1.3", "ecurve": "^1.0.0", "merkle-lib": "^1.0.0", + "pushdata-bitcoin": "^1.0.1", "randombytes": "^2.0.1", "typeforce": "^1.8.7", "varuint-bitcoin": "^1.0.4", diff --git a/src/bufferutils.js b/src/bufferutils.js index ca3e439..179e84e 100644 --- a/src/bufferutils.js +++ b/src/bufferutils.js @@ -1,50 +1,6 @@ -var opcodes = require('./opcodes.json') +var pushdata = require('pushdata-bitcoin') var varuint = require('varuint-bitcoin') -function pushDataSize (i) { - return i < opcodes.OP_PUSHDATA1 ? 1 - : i <= 0xff ? 2 - : i <= 0xffff ? 3 - : 5 -} - -function readPushDataInt (buffer, offset) { - var opcode = buffer.readUInt8(offset) - var number, size - - // ~6 bit - if (opcode < opcodes.OP_PUSHDATA1) { - number = opcode - size = 1 - - // 8 bit - } else if (opcode === opcodes.OP_PUSHDATA1) { - if (offset + 2 > buffer.length) return null - number = buffer.readUInt8(offset + 1) - size = 2 - - // 16 bit - } else if (opcode === opcodes.OP_PUSHDATA2) { - if (offset + 3 > buffer.length) return null - number = buffer.readUInt16LE(offset + 1) - size = 3 - - // 32 bit - } else { - if (offset + 5 > buffer.length) return null - if (opcode !== opcodes.OP_PUSHDATA4) throw new Error('Unexpected opcode') - - number = buffer.readUInt32LE(offset + 1) - size = 5 - } - - return { - opcode: opcode, - number: number, - size: size - } -} - // https://github.com/feross/buffer/blob/master/index.js#L1127 function verifuint (value, max) { if (typeof value !== 'number') throw new Error('cannot write a non-number as a number') @@ -63,32 +19,6 @@ function readUInt64LE (buffer, offset) { return b + a } -function writePushDataInt (buffer, number, offset) { - var size = pushDataSize(number) - - // ~6 bit - if (size === 1) { - buffer.writeUInt8(number, offset) - - // 8 bit - } else if (size === 2) { - buffer.writeUInt8(opcodes.OP_PUSHDATA1, offset) - buffer.writeUInt8(number, offset + 1) - - // 16 bit - } else if (size === 3) { - buffer.writeUInt8(opcodes.OP_PUSHDATA2, offset) - buffer.writeUInt16LE(number, offset + 1) - - // 32 bit - } else { - buffer.writeUInt8(opcodes.OP_PUSHDATA4, offset) - buffer.writeUInt32LE(number, offset + 1) - } - - return size -} - function writeUInt64LE (buffer, value, offset) { verifuint(value, 0x001fffffffffffff) @@ -114,13 +44,13 @@ function writeVarInt (buffer, number, offset) { } module.exports = { - pushDataSize: pushDataSize, - readPushDataInt: readPushDataInt, + pushDataSize: pushdata.encodingLength, + readPushDataInt: pushdata.decode, readUInt64LE: readUInt64LE, readVarInt: readVarInt, varIntBuffer: varuint.encode, varIntSize: varuint.encodingLength, - writePushDataInt: writePushDataInt, + writePushDataInt: pushdata.encode, writeUInt64LE: writeUInt64LE, writeVarInt: writeVarInt } diff --git a/src/index.js b/src/index.js index 25b3da5..8ca21f1 100644 --- a/src/index.js +++ b/src/index.js @@ -10,6 +10,6 @@ module.exports = { bufferutils: require('./bufferutils'), crypto: require('./crypto'), networks: require('./networks'), - opcodes: require('./opcodes.json'), + opcodes: require('bitcoin-ops'), script: require('./script') } diff --git a/src/opcodes.json b/src/opcodes.json deleted file mode 100644 index f53ec2f..0000000 --- a/src/opcodes.json +++ /dev/null @@ -1,130 +0,0 @@ -{ - "OP_FALSE": 0, - "OP_0": 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, - - "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, - - "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, - - "OP_CAT": 126, - "OP_SUBSTR": 127, - "OP_LEFT": 128, - "OP_RIGHT": 129, - "OP_SIZE": 130, - - "OP_INVERT": 131, - "OP_AND": 132, - "OP_OR": 133, - "OP_XOR": 134, - "OP_EQUAL": 135, - "OP_EQUALVERIFY": 136, - "OP_RESERVED1": 137, - "OP_RESERVED2": 138, - - "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_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_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, - - "OP_NOP1": 176, - "OP_NOP2": 177, - "OP_CHECKLOCKTIMEVERIFY": 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, - - "OP_PUBKEYHASH": 253, - "OP_PUBKEY": 254, - "OP_INVALIDOPCODE": 255 -} diff --git a/src/script.js b/src/script.js index 4245bf1..9aa4e13 100644 --- a/src/script.js +++ b/src/script.js @@ -1,17 +1,11 @@ var bip66 = require('bip66') -var bufferutils = require('./bufferutils') +var pushdata = require('pushdata-bitcoin') var typeforce = require('typeforce') var types = require('./types') var scriptNumber = require('./script_number') -var OPS = require('./opcodes.json') -var REVERSE_OPS = (function () { - var result = {} - for (var op in OPS) { - var code = OPS[op] - result[code] = op - } - return result -})() + +var OPS = require('bitcoin-ops') +var REVERSE_OPS = require('bitcoin-ops/map') var OP_INT_BASE = OPS.OP_RESERVED // OP_1 - 1 function isOPInt (value) { @@ -43,7 +37,7 @@ function compile (chunks) { return accum + 1 } - return accum + bufferutils.pushDataSize(chunk.length) + chunk.length + return accum + pushdata.encodingLength(chunk.length) + chunk.length } // opcode @@ -70,7 +64,7 @@ function compile (chunks) { return } - offset += bufferutils.writePushDataInt(buffer, chunk.length, offset) + offset += pushdata.encode(buffer, chunk.length, offset) chunk.copy(buffer, offset) offset += chunk.length @@ -100,7 +94,7 @@ function decompile (buffer) { // data chunk if ((opcode > OPS.OP_0) && (opcode <= OPS.OP_PUSHDATA4)) { - var d = bufferutils.readPushDataInt(buffer, i) + var d = pushdata.decode(buffer, i) // did reading a pushDataInt fail? empty script if (d === null) return [] diff --git a/src/templates/multisig/input.js b/src/templates/multisig/input.js index 2350125..aed80af 100644 --- a/src/templates/multisig/input.js +++ b/src/templates/multisig/input.js @@ -2,7 +2,7 @@ var bscript = require('../../script') var typeforce = require('typeforce') -var OPS = require('../../opcodes.json') +var OPS = require('bitcoin-ops') function partialSignature (value) { return value === OPS.OP_0 || bscript.isCanonicalSignature(value) diff --git a/src/templates/multisig/output.js b/src/templates/multisig/output.js index 0f4fe7d..70e8488 100644 --- a/src/templates/multisig/output.js +++ b/src/templates/multisig/output.js @@ -3,7 +3,7 @@ var bscript = require('../../script') var types = require('../../types') var typeforce = require('typeforce') -var OPS = require('../../opcodes.json') +var OPS = require('bitcoin-ops') var OP_INT_BASE = OPS.OP_RESERVED // OP_1 - 1 function check (script, allowIncomplete) { diff --git a/src/templates/nulldata.js b/src/templates/nulldata.js index 163249f..b743789 100644 --- a/src/templates/nulldata.js +++ b/src/templates/nulldata.js @@ -3,7 +3,7 @@ var bscript = require('../script') var types = require('../types') var typeforce = require('typeforce') -var OPS = require('../opcodes.json') +var OPS = require('bitcoin-ops') function check (script) { var buffer = bscript.compile(script) diff --git a/src/templates/pubkey/output.js b/src/templates/pubkey/output.js index 83b5c50..69a747f 100644 --- a/src/templates/pubkey/output.js +++ b/src/templates/pubkey/output.js @@ -2,7 +2,7 @@ var bscript = require('../../script') var typeforce = require('typeforce') -var OPS = require('../../opcodes.json') +var OPS = require('bitcoin-ops') function check (script) { var chunks = bscript.decompile(script) diff --git a/src/templates/pubkeyhash/output.js b/src/templates/pubkeyhash/output.js index 4c11006..2a7c4ab 100644 --- a/src/templates/pubkeyhash/output.js +++ b/src/templates/pubkeyhash/output.js @@ -3,7 +3,7 @@ var bscript = require('../../script') var types = require('../../types') var typeforce = require('typeforce') -var OPS = require('../../opcodes.json') +var OPS = require('bitcoin-ops') function check (script) { var buffer = bscript.compile(script) diff --git a/src/templates/scripthash/output.js b/src/templates/scripthash/output.js index fe3b5ce..83d2501 100644 --- a/src/templates/scripthash/output.js +++ b/src/templates/scripthash/output.js @@ -3,7 +3,7 @@ var bscript = require('../../script') var types = require('../../types') var typeforce = require('typeforce') -var OPS = require('../../opcodes.json') +var OPS = require('bitcoin-ops') function check (script) { var buffer = bscript.compile(script) diff --git a/src/templates/witnesspubkeyhash/output.js b/src/templates/witnesspubkeyhash/output.js index 0eae63e..ebcb185 100644 --- a/src/templates/witnesspubkeyhash/output.js +++ b/src/templates/witnesspubkeyhash/output.js @@ -3,7 +3,7 @@ var bscript = require('../../script') var types = require('../../types') var typeforce = require('typeforce') -var OPS = require('../../opcodes.json') +var OPS = require('bitcoin-ops') function check (script) { var buffer = bscript.compile(script) diff --git a/src/templates/witnessscripthash/output.js b/src/templates/witnessscripthash/output.js index 00fb838..f1656b5 100644 --- a/src/templates/witnessscripthash/output.js +++ b/src/templates/witnessscripthash/output.js @@ -3,7 +3,7 @@ var bscript = require('../../script') var types = require('../../types') var typeforce = require('typeforce') -var OPS = require('../../opcodes.json') +var OPS = require('bitcoin-ops') function check (script) { var buffer = bscript.compile(script) diff --git a/src/transaction.js b/src/transaction.js index 09a416d..4a6aa81 100644 --- a/src/transaction.js +++ b/src/transaction.js @@ -2,7 +2,7 @@ var bcrypto = require('./crypto') var bscript = require('./script') var bufferutils = require('./bufferutils') var bufferReverse = require('buffer-reverse') -var opcodes = require('./opcodes.json') +var opcodes = require('bitcoin-ops') var typeforce = require('typeforce') var types = require('./types') diff --git a/src/transaction_builder.js b/src/transaction_builder.js index 039cf7d..b2b7f94 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -3,7 +3,7 @@ var bcrypto = require('./crypto') var bscript = require('./script') var bufferReverse = require('buffer-reverse') var networks = require('./networks') -var ops = require('./opcodes.json') +var ops = require('bitcoin-ops') var typeforce = require('typeforce') var types = require('./types') var scriptTypes = bscript.types diff --git a/test/templates.js b/test/templates.js index 613d4d7..33e36f3 100644 --- a/test/templates.js +++ b/test/templates.js @@ -3,7 +3,7 @@ var assert = require('assert') var bcrypto = require('../src/crypto') var bscript = require('../src/script') -var ops = require('../src/opcodes') +var ops = require('bitcoin-ops') var fixtures = require('./fixtures/templates.json') diff --git a/test/transaction_builder.js b/test/transaction_builder.js index 2c79d46..0be43b5 100644 --- a/test/transaction_builder.js +++ b/test/transaction_builder.js @@ -3,7 +3,7 @@ var assert = require('assert') var baddress = require('../src/address') var bscript = require('../src/script') -var ops = require('../src/opcodes') +var ops = require('bitcoin-ops') var BigInteger = require('bigi') var ECPair = require('../src/ecpair')