script: do not use util.reverse.

This commit is contained in:
Christopher Jeffrey 2017-10-25 23:55:38 -07:00
parent 636d66a5c7
commit 1a5782fa06
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
4 changed files with 172 additions and 48 deletions

View File

@ -815,7 +815,15 @@ MTX.prototype.signVector = function signVector(prev, vector, sig, ring) {
// Find the key index so we can place
// the signature in the same index.
let keyIndex = util.indexOf(keys, ring.publicKey);
let keyIndex = -1;
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
if (key.equals(ring.publicKey)) {
keyIndex = i;
break;
}
}
// Our public key is not in the prev_out
// script. We tried to sign a transaction

View File

@ -12,7 +12,6 @@
*/
const assert = require('assert');
const util = require('../utils/util');
const secp256k1 = require('bcrypto/lib/secp256k1');
const ScriptNum = require('./scriptnum');
@ -163,7 +162,141 @@ exports.opcodes = {
* @const {RevMap}
*/
exports.opcodesByVal = util.reverse(exports.opcodes);
exports.opcodesByVal = {
// Push
0x00: 'OP_0',
0x4c: 'OP_PUSHDATA1',
0x4d: 'OP_PUSHDATA2',
0x4e: 'OP_PUSHDATA4',
0x4f: 'OP_1NEGATE',
0x50: 'OP_RESERVED',
0x51: 'OP_1',
0x52: 'OP_2',
0x53: 'OP_3',
0x54: 'OP_4',
0x55: 'OP_5',
0x56: 'OP_6',
0x57: 'OP_7',
0x58: 'OP_8',
0x59: 'OP_9',
0x5a: 'OP_10',
0x5b: 'OP_11',
0x5c: 'OP_12',
0x5d: 'OP_13',
0x5e: 'OP_14',
0x5f: 'OP_15',
0x60: 'OP_16',
// Control
0x61: 'OP_NOP',
0x62: 'OP_VER',
0x63: 'OP_IF',
0x64: 'OP_NOTIF',
0x65: 'OP_VERIF',
0x66: 'OP_VERNOTIF',
0x67: 'OP_ELSE',
0x68: 'OP_ENDIF',
0x69: 'OP_VERIFY',
0x6a: 'OP_RETURN',
// Stack
0x6b: 'OP_TOALTSTACK',
0x6c: 'OP_FROMALTSTACK',
0x6d: 'OP_2DROP',
0x6e: 'OP_2DUP',
0x6f: 'OP_3DUP',
0x70: 'OP_2OVER',
0x71: 'OP_2ROT',
0x72: 'OP_2SWAP',
0x73: 'OP_IFDUP',
0x74: 'OP_DEPTH',
0x75: 'OP_DROP',
0x76: 'OP_DUP',
0x77: 'OP_NIP',
0x78: 'OP_OVER',
0x79: 'OP_PICK',
0x7a: 'OP_ROLL',
0x7b: 'OP_ROT',
0x7c: 'OP_SWAP',
0x7d: 'OP_TUCK',
// Splice
0x7e: 'OP_CAT',
0x7f: 'OP_SUBSTR',
0x80: 'OP_LEFT',
0x81: 'OP_RIGHT',
0x82: 'OP_SIZE',
// Bit
0x83: 'OP_INVERT',
0x84: 'OP_AND',
0x85: 'OP_OR',
0x86: 'OP_XOR',
0x87: 'OP_EQUAL',
0x88: 'OP_EQUALVERIFY',
0x89: 'OP_RESERVED1',
0x8a: 'OP_RESERVED2',
// Numeric
0x8b: 'OP_1ADD',
0x8c: 'OP_1SUB',
0x8d: 'OP_2MUL',
0x8e: 'OP_2DIV',
0x8f: 'OP_NEGATE',
0x90: 'OP_ABS',
0x91: 'OP_NOT',
0x92: 'OP_0NOTEQUAL',
0x93: 'OP_ADD',
0x94: 'OP_SUB',
0x95: 'OP_MUL',
0x96: 'OP_DIV',
0x97: 'OP_MOD',
0x98: 'OP_LSHIFT',
0x99: 'OP_RSHIFT',
0x9a: 'OP_BOOLAND',
0x9b: 'OP_BOOLOR',
0x9c: 'OP_NUMEQUAL',
0x9d: 'OP_NUMEQUALVERIFY',
0x9e: 'OP_NUMNOTEQUAL',
0x9f: 'OP_LESSTHAN',
0xa0: 'OP_GREATERTHAN',
0xa1: 'OP_LESSTHANOREQUAL',
0xa2: 'OP_GREATERTHANOREQUAL',
0xa3: 'OP_MIN',
0xa4: 'OP_MAX',
0xa5: 'OP_WITHIN',
// Crypto
0xa6: 'OP_RIPEMD160',
0xa7: 'OP_SHA1',
0xa8: 'OP_SHA256',
0xa9: 'OP_HASH160',
0xaa: 'OP_HASH256',
0xab: 'OP_CODESEPARATOR',
0xac: 'OP_CHECKSIG',
0xad: 'OP_CHECKSIGVERIFY',
0xae: 'OP_CHECKMULTISIG',
0xaf: 'OP_CHECKMULTISIGVERIFY',
// Expansion
0xb0: 'OP_NOP1',
0xb1: 'OP_CHECKLOCKTIMEVERIFY',
0xb2: 'OP_CHECKSEQUENCEVERIFY',
0xb3: 'OP_NOP4',
0xb4: 'OP_NOP5',
0xb5: 'OP_NOP6',
0xb6: 'OP_NOP7',
0xb7: 'OP_NOP8',
0xb8: 'OP_NOP9',
0xb9: 'OP_NOP10',
// Custom
0xff: 'OP_INVALIDOPCODE'
};
/**
* Small ints (1 indexed, 1==0).
@ -294,7 +427,12 @@ exports.hashType = {
* @const {RevMap}
*/
exports.hashTypeByVal = util.reverse(exports.hashType);
exports.hashTypeByVal = {
1: 'ALL',
2: 'NONE',
3: 'SINGLE',
0x80: 'ANYONECANPAY'
};
/**
* Output script types.
@ -308,10 +446,10 @@ exports.types = {
SCRIPTHASH: 3,
MULTISIG: 4,
NULLDATA: 5,
WITNESSMALFORMED: 0x80 | 0,
WITNESSSCRIPTHASH: 0x80 | 1,
WITNESSPUBKEYHASH: 0x80 | 2,
WITNESSMASTHASH: 0x80 | 3
WITNESSMALFORMED: 0x80,
WITNESSSCRIPTHASH: 0x81,
WITNESSPUBKEYHASH: 0x82,
WITNESSMASTHASH: 0x83
};
/**
@ -319,7 +457,18 @@ exports.types = {
* @const {RevMap}
*/
exports.typesByVal = util.reverse(exports.types);
exports.typesByVal = {
0: 'NONSTANDARD',
1: 'PUBKEY',
2: 'PUBKEYHASH',
3: 'SCRIPTHASH',
4: 'MULTISIG',
5: 'NULLDATA',
0x80: 'WITNESSMALFORMED',
0x81: 'WITNESSSCRIPTHASH',
0x82: 'WITNESSPUBKEYHASH',
0x83: 'WITNESSMASTHASH'
};
/**
* Test a signature to see whether it contains a valid sighash type.

View File

@ -362,7 +362,12 @@ Witness.prototype.getRedeem = function getRedeem() {
*/
Witness.prototype.indexOf = function indexOf(data) {
return util.indexOf(this.items, data);
for (let i = 0; i < this.items.length; i++) {
const item = this.items[i];
if (item.equals(data))
return i;
}
return -1;
};
/**

View File

@ -536,29 +536,6 @@ util.mb = function mb(size) {
return Math.floor(size / 1024 / 1024);
};
/**
* Find index of a buffer in an array of buffers.
* @param {Buffer[]} items
* @param {Buffer} data - Target buffer to find.
* @returns {Number} Index (-1 if not found).
*/
util.indexOf = function indexOf(items, data) {
assert(Array.isArray(items));
assert(Buffer.isBuffer(data));
for (let i = 0; i < items.length; i++) {
const item = items[i];
assert(Buffer.isBuffer(item));
if (item.equals(data))
return i;
}
return -1;
};
/**
* Convert a number to a padded uint8
* string (3 digits in decimal).
@ -701,21 +678,6 @@ util.revHex = function revHex(data) {
return out;
};
/**
* Reverse an object's keys and values.
* @param {Object} obj
* @returns {Object} Reversed object.
*/
util.reverse = function reverse(obj) {
const reversed = {};
for (const key of Object.keys(obj))
reversed[obj[key]] = key;
return reversed;
};
/**
* Perform a binary search on a sorted array.
* @param {Array} items