You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
865 lines
35 KiB
865 lines
35 KiB
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<title>JSDoc: Source: script/common.js</title>
|
|
|
|
<script src="scripts/prettify/prettify.js"> </script>
|
|
<script src="scripts/prettify/lang-css.js"> </script>
|
|
<!--[if lt IE 9]>
|
|
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
|
|
<![endif]-->
|
|
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
|
|
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<div id="main">
|
|
|
|
<h1 class="page-title">Source: script/common.js</h1>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<section>
|
|
<article>
|
|
<pre class="prettyprint source linenums"><code>/*!
|
|
* common.js - common script functions for bcoin
|
|
* Copyright (c) 2014-2015, Fedor Indutny (MIT License)
|
|
* Copyright (c) 2014-2017, Christopher Jeffrey (MIT License).
|
|
* https://github.com/bcoin-org/bcoin
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
/**
|
|
* @module script/common
|
|
*/
|
|
|
|
var assert = require('assert');
|
|
var BN = require('bn.js');
|
|
var util = require('../utils/util');
|
|
|
|
/**
|
|
* Script opcodes.
|
|
* @enum {Number}
|
|
* @default
|
|
*/
|
|
|
|
exports.opcodes = {
|
|
OP_FALSE: 0x00,
|
|
OP_0: 0x00,
|
|
|
|
OP_PUSHDATA1: 0x4c,
|
|
OP_PUSHDATA2: 0x4d,
|
|
OP_PUSHDATA4: 0x4e,
|
|
|
|
OP_1NEGATE: 0x4f,
|
|
|
|
OP_RESERVED: 0x50,
|
|
|
|
OP_TRUE: 0x51,
|
|
OP_1: 0x51,
|
|
OP_2: 0x52,
|
|
OP_3: 0x53,
|
|
OP_4: 0x54,
|
|
OP_5: 0x55,
|
|
OP_6: 0x56,
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
OP_PUBKEY: 0xfe,
|
|
OP_INVALIDOPCODE: 0xff
|
|
};
|
|
|
|
/**
|
|
* Opcodes by value.
|
|
* @const {RevMap}
|
|
*/
|
|
|
|
exports.opcodesByVal = util.revMap(exports.opcodes);
|
|
|
|
/**
|
|
* Script and locktime flags. See {@link VerifyFlags}.
|
|
* @enum {Number}
|
|
*/
|
|
|
|
exports.flags = {
|
|
VERIFY_NONE: 0,
|
|
VERIFY_P2SH: 1 << 0,
|
|
VERIFY_STRICTENC: 1 << 1,
|
|
VERIFY_DERSIG: 1 << 2,
|
|
VERIFY_LOW_S: 1 << 3,
|
|
VERIFY_NULLDUMMY: 1 << 4,
|
|
VERIFY_SIGPUSHONLY: 1 << 5,
|
|
VERIFY_MINIMALDATA: 1 << 6,
|
|
VERIFY_DISCOURAGE_UPGRADABLE_NOPS: 1 << 7,
|
|
VERIFY_CLEANSTACK: 1 << 8,
|
|
VERIFY_CHECKLOCKTIMEVERIFY: 1 << 9,
|
|
VERIFY_CHECKSEQUENCEVERIFY: 1 << 10,
|
|
VERIFY_WITNESS: 1 << 11,
|
|
VERIFY_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM: 1 << 12,
|
|
VERIFY_MINIMALIF: 1 << 13,
|
|
VERIFY_NULLFAIL: 1 << 14,
|
|
VERIFY_WITNESS_PUBKEYTYPE: 1 << 15,
|
|
VERIFY_MAST: 1 << 16
|
|
};
|
|
|
|
/**
|
|
* Consensus verify flags (used for block validation).
|
|
* @const {VerifyFlags}
|
|
* @default
|
|
*/
|
|
|
|
exports.flags.MANDATORY_VERIFY_FLAGS = exports.flags.VERIFY_P2SH;
|
|
|
|
/**
|
|
* Standard verify flags (used for mempool validation).
|
|
* @const {VerifyFlags}
|
|
* @default
|
|
*/
|
|
|
|
exports.flags.STANDARD_VERIFY_FLAGS = 0
|
|
| exports.flags.MANDATORY_VERIFY_FLAGS
|
|
| exports.flags.VERIFY_DERSIG
|
|
| exports.flags.VERIFY_STRICTENC
|
|
| exports.flags.VERIFY_MINIMALDATA
|
|
| exports.flags.VERIFY_NULLDUMMY
|
|
| exports.flags.VERIFY_DISCOURAGE_UPGRADABLE_NOPS
|
|
| exports.flags.VERIFY_CLEANSTACK
|
|
| exports.flags.VERIFY_MINIMALIF
|
|
| exports.flags.VERIFY_NULLFAIL
|
|
| exports.flags.VERIFY_CHECKLOCKTIMEVERIFY
|
|
| exports.flags.VERIFY_CHECKSEQUENCEVERIFY
|
|
| exports.flags.VERIFY_LOW_S
|
|
| exports.flags.VERIFY_WITNESS
|
|
| exports.flags.VERIFY_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM
|
|
| exports.flags.VERIFY_WITNESS_PUBKEYTYPE;
|
|
|
|
/**
|
|
* Standard flags without mandatory bits.
|
|
* @const {VerifyFlags}
|
|
* @default
|
|
*/
|
|
|
|
exports.flags.ONLY_STANDARD_VERIFY_FLAGS =
|
|
exports.flags.STANDARD_VERIFY_FLAGS & ~exports.flags.MANDATORY_VERIFY_FLAGS;
|
|
|
|
/**
|
|
* Sighash Types.
|
|
* @enum {SighashType}
|
|
* @default
|
|
*/
|
|
|
|
exports.hashType = {
|
|
/*
|
|
* Sign all outputs.
|
|
*/
|
|
|
|
ALL: 1,
|
|
|
|
/*
|
|
* Do not sign outputs (zero sequences).
|
|
*/
|
|
|
|
NONE: 2,
|
|
|
|
/*
|
|
* Sign output at the same index (zero sequences).
|
|
*/
|
|
|
|
SINGLE: 3,
|
|
|
|
/*
|
|
* Sign only the current input (mask).
|
|
*/
|
|
|
|
ANYONECANPAY: 0x80
|
|
};
|
|
|
|
/**
|
|
* Sighash types by value.
|
|
* @const {RevMap}
|
|
*/
|
|
|
|
exports.hashTypeByVal = util.revMap(exports.hashType);
|
|
|
|
/**
|
|
* Output script types.
|
|
* @enum {Number}
|
|
*/
|
|
|
|
exports.types = {
|
|
NONSTANDARD: 0,
|
|
PUBKEY: 1,
|
|
PUBKEYHASH: 2,
|
|
SCRIPTHASH: 3,
|
|
MULTISIG: 4,
|
|
NULLDATA: 5,
|
|
WITNESSMALFORMED: 0x80 | 0,
|
|
WITNESSSCRIPTHASH: 0x80 | 1,
|
|
WITNESSPUBKEYHASH: 0x80 | 2,
|
|
WITNESSMASTHASH: 0x80 | 3
|
|
};
|
|
|
|
/**
|
|
* Output script types by value.
|
|
* @const {RevMap}
|
|
*/
|
|
|
|
exports.typesByVal = util.revMap(exports.types);
|
|
|
|
/**
|
|
* False stack return value.
|
|
* @const {Buffer}
|
|
*/
|
|
|
|
exports.STACK_FALSE = new Buffer([]);
|
|
|
|
/**
|
|
* True stack return value.
|
|
* @const {Buffer}
|
|
*/
|
|
|
|
exports.STACK_TRUE = new Buffer([0x01]);
|
|
|
|
/**
|
|
* -1 stack return value.
|
|
* @const {Buffer}
|
|
*/
|
|
|
|
exports.STACK_NEGATE = new Buffer([0x81]);
|
|
|
|
/**
|
|
* Test whether the data element is a ripemd160 hash.
|
|
* @param {Buffer?} hash
|
|
* @returns {Boolean}
|
|
*/
|
|
|
|
exports.isHash = function isHash(hash) {
|
|
return Buffer.isBuffer(hash) && hash.length === 20;
|
|
};
|
|
|
|
/**
|
|
* Test whether the data element is a public key. Note that
|
|
* this does not verify the format of the key, only the length.
|
|
* @param {Buffer?} key
|
|
* @returns {Boolean}
|
|
*/
|
|
|
|
exports.isKey = function isKey(key) {
|
|
return Buffer.isBuffer(key) && key.length >= 33 && key.length <= 65;
|
|
};
|
|
|
|
/**
|
|
* Test whether the data element is a signature. Note that
|
|
* this does not verify the format of the signature, only the length.
|
|
* @param {Buffer?} sig
|
|
* @returns {Boolean}
|
|
*/
|
|
|
|
exports.isSignature = function isSignature(sig) {
|
|
return Buffer.isBuffer(sig) && sig.length >= 9 && sig.length <= 73;
|
|
};
|
|
|
|
/**
|
|
* Test whether the data element is a null dummy (a zero-length array).
|
|
* @param {Buffer?} data
|
|
* @returns {Boolean}
|
|
*/
|
|
|
|
exports.isDummy = function isDummy(data) {
|
|
return Buffer.isBuffer(data) && data.length === 0;
|
|
};
|
|
|
|
/**
|
|
* Test whether the data element is a compressed key.
|
|
* @param {Buffer} key
|
|
* @returns {Boolean}
|
|
*/
|
|
|
|
exports.isCompressedEncoding = function isCompressedEncoding(key) {
|
|
assert(Buffer.isBuffer(key));
|
|
|
|
if (key.length !== 33)
|
|
return false;
|
|
|
|
if (key[0] !== 0x02 && key[0] !== 0x03)
|
|
return false;
|
|
|
|
return true;
|
|
};
|
|
|
|
/**
|
|
* Test whether the data element is a valid key.
|
|
* @param {Buffer} key
|
|
* @returns {Boolean}
|
|
*/
|
|
|
|
exports.isKeyEncoding = function isKeyEncoding(key) {
|
|
assert(Buffer.isBuffer(key));
|
|
|
|
if (key.length < 33)
|
|
return false;
|
|
|
|
if (key[0] === 0x04) {
|
|
if (key.length !== 65)
|
|
return false;
|
|
} else if (key[0] === 0x02 || key[0] === 0x03) {
|
|
if (key.length !== 33)
|
|
return false;
|
|
} else {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
};
|
|
|
|
/**
|
|
* Test a signature to see if it abides by BIP66.
|
|
* @see https://github.com/bitcoin/bips/blob/master/bip-0066.mediawiki
|
|
* @param {Buffer} sig
|
|
* @returns {Boolean}
|
|
*/
|
|
|
|
exports.isSignatureEncoding = function isSignatureEncoding(sig) {
|
|
var lenR, lenS;
|
|
|
|
assert(Buffer.isBuffer(sig));
|
|
|
|
// Format: 0x30 [total-length] 0x02 [R-length] [R] 0x02 [S-length] [S] [sighash]
|
|
// * total-length: 1-byte length descriptor of everything that follows,
|
|
// excluding the sighash byte.
|
|
// * R-length: 1-byte length descriptor of the R value that follows.
|
|
// * R: arbitrary-length big-endian encoded R value. It must use the shortest
|
|
// possible encoding for a positive integers (which means no null bytes at
|
|
// the start, except a single one when the next byte has its highest bit set).
|
|
// * S-length: 1-byte length descriptor of the S value that follows.
|
|
// * S: arbitrary-length big-endian encoded S value. The same rules apply.
|
|
// * sighash: 1-byte value indicating what data is hashed (not part of the DER
|
|
// signature)
|
|
|
|
// Minimum and maximum size constraints.
|
|
if (sig.length < 9)
|
|
return false;
|
|
|
|
if (sig.length > 73)
|
|
return false;
|
|
|
|
// A signature is of type 0x30 (compound).
|
|
if (sig[0] !== 0x30)
|
|
return false;
|
|
|
|
// Make sure the length covers the entire signature.
|
|
if (sig[1] !== sig.length - 3)
|
|
return false;
|
|
|
|
// Extract the length of the R element.
|
|
lenR = sig[3];
|
|
|
|
// Make sure the length of the S element is still inside the signature.
|
|
if (5 + lenR >= sig.length)
|
|
return false;
|
|
|
|
// Extract the length of the S element.
|
|
lenS = sig[5 + lenR];
|
|
|
|
// Verify that the length of the signature matches the sum of the length
|
|
// of the elements.
|
|
if (lenR + lenS + 7 !== sig.length)
|
|
return false;
|
|
|
|
// Check whether the R element is an integer.
|
|
if (sig[2] !== 0x02)
|
|
return false;
|
|
|
|
// Zero-length integers are not allowed for R.
|
|
if (lenR === 0)
|
|
return false;
|
|
|
|
// Negative numbers are not allowed for R.
|
|
if (sig[4] & 0x80)
|
|
return false;
|
|
|
|
// Null bytes at the start of R are not allowed, unless R would
|
|
// otherwise be interpreted as a negative number.
|
|
if (lenR > 1 && (sig[4] === 0x00) && !(sig[5] & 0x80))
|
|
return false;
|
|
|
|
// Check whether the S element is an integer.
|
|
if (sig[lenR + 4] !== 0x02)
|
|
return false;
|
|
|
|
// Zero-length integers are not allowed for S.
|
|
if (lenS === 0)
|
|
return false;
|
|
|
|
// Negative numbers are not allowed for S.
|
|
if (sig[lenR + 6] & 0x80)
|
|
return false;
|
|
|
|
// Null bytes at the start of S are not allowed, unless S would otherwise be
|
|
// interpreted as a negative number.
|
|
if (lenS > 1 && (sig[lenR + 6] === 0x00) && !(sig[lenR + 7] & 0x80))
|
|
return false;
|
|
|
|
return true;
|
|
};
|
|
|
|
/**
|
|
* Format script code into a human readable-string.
|
|
* @param {Array} code
|
|
* @returns {String} Human-readable string.
|
|
*/
|
|
|
|
exports.formatStack = function formatStack(items) {
|
|
var out = [];
|
|
var i;
|
|
|
|
for (i = 0; i < items.length; i++)
|
|
out.push(items[i].toString('hex'));
|
|
|
|
return out.join(' ');
|
|
};
|
|
|
|
/**
|
|
* Format script code into a human readable-string.
|
|
* @param {Array} code
|
|
* @returns {String} Human-readable string.
|
|
*/
|
|
|
|
exports.formatCode = function formatCode(code) {
|
|
var out = [];
|
|
var i, op, data, value, size;
|
|
|
|
for (i = 0; i < code.length; i++) {
|
|
op = code[i];
|
|
data = op.data;
|
|
value = op.value;
|
|
|
|
if (data) {
|
|
size = data.length.toString(16);
|
|
|
|
while (size.length % 2 !== 0)
|
|
size = '0' + size;
|
|
|
|
if (!exports.opcodesByVal[value]) {
|
|
value = value.toString(16);
|
|
if (value.length < 2)
|
|
value = '0' + value;
|
|
value = '0x' + value + ' 0x' + data.toString('hex');
|
|
out.push(value);
|
|
continue;
|
|
}
|
|
|
|
value = exports.opcodesByVal[value];
|
|
value = value + ' 0x' + size + ' 0x' + data.toString('hex');
|
|
out.push(value);
|
|
continue;
|
|
}
|
|
|
|
assert(typeof value === 'number');
|
|
|
|
if (exports.opcodesByVal[value]) {
|
|
value = exports.opcodesByVal[value];
|
|
out.push(value);
|
|
continue;
|
|
}
|
|
|
|
if (value === -1) {
|
|
out.push('OP_INVALIDOPCODE');
|
|
break;
|
|
}
|
|
|
|
value = value.toString(16);
|
|
|
|
if (value.length < 2)
|
|
value = '0' + value;
|
|
|
|
value = '0x' + value;
|
|
out.push(value);
|
|
}
|
|
|
|
return out.join(' ');
|
|
};
|
|
|
|
/**
|
|
* Format script code into bitcoind asm format.
|
|
* @param {Array} code
|
|
* @param {Boolean?} decode - Attempt to decode hash types.
|
|
* @returns {String} Human-readable string.
|
|
*/
|
|
|
|
exports.formatItem = function formatItem(data, decode) {
|
|
var symbol, type;
|
|
|
|
if (data.length <= 4) {
|
|
data = exports.num(data, exports.flags.VERIFY_NONE);
|
|
return data.toString(10);
|
|
}
|
|
|
|
if (decode) {
|
|
symbol = '';
|
|
if (exports.isSignatureEncoding(data)) {
|
|
type = data[data.length - 1];
|
|
symbol = exports.hashTypeByVal[type & 0x1f] || '';
|
|
if (symbol) {
|
|
if (type & exports.hashType.ANYONECANPAY)
|
|
symbol += '|ANYONECANPAY';
|
|
symbol = '[' + symbol + ']';
|
|
}
|
|
data = data.slice(0, -1);
|
|
}
|
|
return data.toString('hex') + symbol;
|
|
}
|
|
|
|
return data.toString('hex');
|
|
};
|
|
|
|
/**
|
|
* Format script code into bitcoind asm format.
|
|
* @param {Array} code
|
|
* @param {Boolean?} decode - Attempt to decode hash types.
|
|
* @returns {String} Human-readable string.
|
|
*/
|
|
|
|
exports.formatASM = function formatASM(code, decode) {
|
|
var out = [];
|
|
var i, op, data, value;
|
|
|
|
if (code.length > 0 && code[0].value === exports.opcodes.OP_RETURN)
|
|
decode = false;
|
|
|
|
for (i = 0; i < code.length; i++) {
|
|
op = code[i];
|
|
data = op.data;
|
|
value = op.value;
|
|
|
|
if (value === -1) {
|
|
out.push('[error]');
|
|
break;
|
|
}
|
|
|
|
if (data) {
|
|
data = exports.formatItem(data, decode);
|
|
out.push(data);
|
|
continue;
|
|
}
|
|
|
|
value = exports.opcodesByVal[value] || 'OP_UNKNOWN';
|
|
|
|
out.push(value);
|
|
}
|
|
|
|
return out.join(' ');
|
|
};
|
|
|
|
/**
|
|
* Format script code into bitcoind asm format.
|
|
* @param {Array} code
|
|
* @param {Boolean?} decode - Attempt to decode hash types.
|
|
* @returns {String} Human-readable string.
|
|
*/
|
|
|
|
exports.formatStackASM = function formatStackASM(items, decode) {
|
|
var out = [];
|
|
var i, item, data;
|
|
|
|
for (i = 0; i < items.length; i++) {
|
|
item = items[i];
|
|
data = exports.formatItem(item, decode);
|
|
out.push(data);
|
|
}
|
|
|
|
return out.join(' ');
|
|
};
|
|
|
|
/**
|
|
* Create a CScriptNum.
|
|
* @param {Buffer} value
|
|
* @param {Number?} flags - Script standard flags.
|
|
* @param {Number?} size - Max size in bytes.
|
|
* @returns {BN}
|
|
* @throws {ScriptError}
|
|
*/
|
|
|
|
exports.num = function num(value, flags, size) {
|
|
var result;
|
|
|
|
assert(Buffer.isBuffer(value));
|
|
|
|
if (flags == null)
|
|
flags = exports.flags.STANDARD_VERIFY_FLAGS;
|
|
|
|
if (size == null)
|
|
size = 4;
|
|
|
|
if (value.length > size)
|
|
throw new exports.ScriptError('UNKNOWN_ERROR', 'Script number overflow.');
|
|
|
|
if ((flags & exports.flags.VERIFY_MINIMALDATA) && value.length > 0) {
|
|
// If the low bits on the last byte are unset,
|
|
// fail if the value's second to last byte does
|
|
// not have the high bit set. A number can't
|
|
// justify having the last byte's low bits unset
|
|
// unless they ran out of space for the sign bit
|
|
// in the second to last bit. We also fail on [0]
|
|
// to avoid negative zero (also avoids positive
|
|
// zero).
|
|
if (!(value[value.length - 1] & 0x7f)) {
|
|
if (value.length === 1 || !(value[value.length - 2] & 0x80)) {
|
|
throw new exports.ScriptError(
|
|
'UNKNOWN_ERROR',
|
|
'Non-minimally encoded Script number.');
|
|
}
|
|
}
|
|
}
|
|
|
|
if (value.length === 0)
|
|
return new BN(0);
|
|
|
|
result = new BN(value, 'le');
|
|
|
|
// If the input vector's most significant byte is
|
|
// 0x80, remove it from the result's msb and return
|
|
// a negative.
|
|
// Equivalent to:
|
|
// -(result & ~(0x80 << (8 * (value.length - 1))))
|
|
if (value[value.length - 1] & 0x80)
|
|
result.setn((value.length * 8) - 1, 0).ineg();
|
|
|
|
return result;
|
|
};
|
|
|
|
/**
|
|
* Create a script array. Will convert Numbers and big
|
|
* numbers to a little-endian buffer while taking into
|
|
* account negative zero, minimaldata, etc.
|
|
* @example
|
|
* assert.deepEqual(Script.array(0), new Buffer(0));
|
|
* assert.deepEqual(Script.array(0xffee), new Buffer('eeff00', 'hex'));
|
|
* assert.deepEqual(Script.array(new BN(0xffee)), new Buffer('eeff00', 'hex'));
|
|
* assert.deepEqual(Script.array(new BN(0x1e).ineg()), new Buffer('9e', 'hex'));
|
|
* @param {Number|BN} value
|
|
* @returns {Buffer}
|
|
*/
|
|
|
|
exports.array = function(value) {
|
|
var neg, result;
|
|
|
|
if (util.isNumber(value))
|
|
value = new BN(value);
|
|
|
|
assert(BN.isBN(value));
|
|
|
|
if (value.cmpn(0) === 0)
|
|
return exports.STACK_FALSE;
|
|
|
|
// If the most significant byte is >= 0x80
|
|
// and the value is positive, push a new
|
|
// zero-byte to make the significant
|
|
// byte < 0x80 again.
|
|
|
|
// If the most significant byte is >= 0x80
|
|
// and the value is negative, push a new
|
|
// 0x80 byte that will be popped off when
|
|
// converting to an integral.
|
|
|
|
// If the most significant byte is < 0x80
|
|
// and the value is negative, add 0x80 to
|
|
// it, since it will be subtracted and
|
|
// interpreted as a negative when
|
|
// converting to an integral.
|
|
|
|
neg = value.cmpn(0) < 0;
|
|
result = value.toArray('le');
|
|
|
|
if (result[result.length - 1] & 0x80)
|
|
result.push(neg ? 0x80 : 0);
|
|
else if (neg)
|
|
result[result.length - 1] |= 0x80;
|
|
|
|
return new Buffer(result);
|
|
};
|
|
|
|
/**
|
|
* An error thrown from the scripting system,
|
|
* potentially pertaining to Script execution.
|
|
* @alias module:script.ScriptError
|
|
* @constructor
|
|
* @extends Error
|
|
* @param {String} code - Error code.
|
|
* @param {Number} op - Opcode.
|
|
* @param {Number?} ip - Instruction pointer.
|
|
* @property {String} message - Error message.
|
|
* @property {String} code - Original code passed in.
|
|
* @property {Number} op - Opcode.
|
|
* @property {Number} ip - Instruction pointer.
|
|
*/
|
|
|
|
exports.ScriptError = function ScriptError(code, op, ip) {
|
|
if (!(this instanceof ScriptError))
|
|
return new ScriptError(code, op, ip);
|
|
|
|
Error.call(this);
|
|
|
|
if (Error.captureStackTrace)
|
|
Error.captureStackTrace(this, ScriptError);
|
|
|
|
this.type = 'ScriptError';
|
|
this.code = code;
|
|
this.message = code;
|
|
this.op = op != null ? op : -1;
|
|
this.ip = ip != null ? ip : -1;
|
|
|
|
if (typeof op === 'number') {
|
|
if (exports.opcodesByVal[op])
|
|
op = exports.opcodesByVal[op];
|
|
else if (op !== -1)
|
|
op = util.hex8(op);
|
|
|
|
this.message += ' (op=' + op + ',';
|
|
this.message += ' ip=' + this.ip + ')';
|
|
} else if (typeof op === 'string') {
|
|
this.message = op;
|
|
this.op = -1;
|
|
this.ip = -1;
|
|
}
|
|
};
|
|
|
|
util.inherits(exports.ScriptError, Error);
|
|
</code></pre>
|
|
</article>
|
|
</section>
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
<nav>
|
|
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-bcoin.html">bcoin</a></li><li><a href="module-bip70.html">bip70</a></li><li><a href="module-bip70_pk.html">bip70/pk</a></li><li><a href="module-bip70_x509.html">bip70/x509</a></li><li><a href="module-blockchain.html">blockchain</a></li><li><a href="module-blockchain_common.html">blockchain/common</a></li><li><a href="module-btc.html">btc</a></li><li><a href="module-coins.html">coins</a></li><li><a href="module-crypto.html">crypto</a></li><li><a href="module-crypto_chachapoly.html">crypto/chachapoly</a></li><li><a href="module-crypto_ec.html">crypto/ec</a></li><li><a href="module-crypto_pk.html">crypto/pk</a></li><li><a href="module-crypto_schnorr.html">crypto/schnorr</a></li><li><a href="module-crypto_siphash.html">crypto/siphash</a></li><li><a href="module-db.html">db</a></li><li><a href="module-hd.html">hd</a></li><li><a href="module-http.html">http</a></li><li><a href="module-mempool.html">mempool</a></li><li><a href="module-mining.html">mining</a></li><li><a href="module-net.html">net</a></li><li><a href="module-net_bip152.html">net/bip152</a></li><li><a href="module-net_common.html">net/common</a></li><li><a href="module-net_dns.html">net/dns</a></li><li><a href="module-net_packets.html">net/packets</a></li><li><a href="module-net_socks.html">net/socks</a></li><li><a href="module-net_tcp.html">net/tcp</a></li><li><a href="module-node.html">node</a></li><li><a href="module-node_config.html">node/config</a></li><li><a href="module-primitives.html">primitives</a></li><li><a href="module-protocol.html">protocol</a></li><li><a href="module-protocol_consensus.html">protocol/consensus</a></li><li><a href="module-protocol_errors.html">protocol/errors</a></li><li><a href="module-protocol_networks.html">protocol/networks</a></li><li><a href="module-protocol_policy.html">protocol/policy</a></li><li><a href="module-script.html">script</a></li><li><a href="module-script_common.html">script/common</a></li><li><a href="module-utils.html">utils</a></li><li><a href="module-utils_asn1.html">utils/asn1</a></li><li><a href="module-utils_base32.html">utils/base32</a></li><li><a href="module-utils_base58.html">utils/base58</a></li><li><a href="module-utils_co.html">utils/co</a></li><li><a href="module-utils_encoding.html">utils/encoding</a></li><li><a href="module-utils_ip.html">utils/ip</a></li><li><a href="module-utils_pem.html">utils/pem</a></li><li><a href="module-utils_protobuf.html">utils/protobuf</a></li><li><a href="module-utils_util.html">utils/util</a></li><li><a href="module-wallet.html">wallet</a></li><li><a href="module-wallet_common.html">wallet/common</a></li><li><a href="module-wallet_records.html">wallet/records</a></li><li><a href="module-workers.html">workers</a></li><li><a href="module-workers_jobs.html">workers/jobs</a></li><li><a href="module-workers_packets.html">workers/packets</a></li></ul><h3>Classes</h3><ul><li><a href="Environment.html">Environment</a></li><li><a href="module-bip70.Payment.html">Payment</a></li><li><a href="module-bip70.PaymentACK.html">PaymentACK</a></li><li><a href="module-bip70.PaymentDetails.html">PaymentDetails</a></li><li><a href="module-bip70.PaymentRequest.html">PaymentRequest</a></li><li><a href="module-blockchain.Chain.html">Chain</a></li><li><a href="module-blockchain.ChainDB.html">ChainDB</a></li><li><a href="module-blockchain.ChainEntry.html">ChainEntry</a></li><li><a href="module-blockchain.ChainFlags.html">ChainFlags</a></li><li><a href="module-blockchain.ChainOptions.html">ChainOptions</a></li><li><a href="module-blockchain.ChainState.html">ChainState</a></li><li><a href="module-blockchain.DeploymentState.html">DeploymentState</a></li><li><a href="module-blockchain.StateCache.html">StateCache</a></li><li><a href="module-btc.Amount.html">Amount</a></li><li><a href="module-btc.URI.html">URI</a></li><li><a href="module-coins.CoinEntry.html">CoinEntry</a></li><li><a href="module-coins.Coins.html">Coins</a></li><li><a href="module-coins.CoinView.html">CoinView</a></li><li><a href="module-coins.UndoCoin.html">UndoCoin</a></li><li><a href="module-coins.UndoCoins.html">UndoCoins</a></li><li><a href="module-crypto_aes.AESCipher.html">AESCipher</a></li><li><a href="module-crypto_aes.AESDecipher.html">AESDecipher</a></li><li><a href="module-crypto_aes.AESKey.html">AESKey</a></li><li><a href="module-crypto_chachapoly.AEAD.html">AEAD</a></li><li><a href="module-crypto_chachapoly.ChaCha20.html">ChaCha20</a></li><li><a href="module-crypto_chachapoly.Poly1305.html">Poly1305</a></li><li><a href="module-crypto_sha256.SHA256.html">SHA256</a></li><li><a href="module-crypto_sha256.SHA256Hmac.html">SHA256Hmac</a></li><li><a href="module-db.LowlevelUp.html">LowlevelUp</a></li><li><a href="module-db.RBT.html">RBT</a></li><li><a href="module-hd.Mnemonic.html">Mnemonic</a></li><li><a href="module-hd.PrivateKey.html">PrivateKey</a></li><li><a href="module-hd.PublicKey.html">PublicKey</a></li><li><a href="module-http.Base.html">Base</a></li><li><a href="module-http.Client.html">Client</a></li><li><a href="module-http.HTTPBaseOptions.html">HTTPBaseOptions</a></li><li><a href="module-http.HTTPOptions.html">HTTPOptions</a></li><li><a href="module-http.Request.html">Request</a></li><li><a href="module-http.RPC.html">RPC</a></li><li><a href="module-http.RPCClient.html">RPCClient</a></li><li><a href="module-http.Server.html">Server</a></li><li><a href="module-http.Wallet.html">Wallet</a></li><li><a href="module-mempool.ConfirmStats.html">ConfirmStats</a></li><li><a href="module-mempool.Mempool.html">Mempool</a></li><li><a href="module-mempool.MempoolEntry.html">MempoolEntry</a></li><li><a href="module-mempool.MempoolOptions.html">MempoolOptions</a></li><li><a href="module-mempool.PolicyEstimator.html">PolicyEstimator</a></li><li><a href="module-mining.BlockEntry.html">BlockEntry</a></li><li><a href="module-mining.Miner.html">Miner</a></li><li><a href="module-mining.MinerBlock.html">MinerBlock</a></li><li><a href="module-mining.MinerOptions.html">MinerOptions</a></li><li><a href="module-net.AuthDB.html">AuthDB</a></li><li><a href="module-net.BIP150.html">BIP150</a></li><li><a href="module-net.BIP151.html">BIP151</a></li><li><a href="module-net.BIP151Stream.html">BIP151Stream</a></li><li><a href="module-net.BroadcastItem.html">BroadcastItem</a></li><li><a href="module-net.Framer.html">Framer</a></li><li><a href="module-net.HostEntry.html">HostEntry</a></li><li><a href="module-net.HostList.html">HostList</a></li><li><a href="module-net.Parser.html">Parser</a></li><li><a href="module-net.Peer.html">Peer</a></li><li><a href="module-net.PeerList.html">PeerList</a></li><li><a href="module-net.PeerOptions.html">PeerOptions</a></li><li><a href="module-net.Pool.html">Pool</a></li><li><a href="module-net.PoolOptions.html">PoolOptions</a></li><li><a href="module-net_bip152-CompactBlock.html">CompactBlock</a></li><li><a href="module-net_bip152-PrefilledTX.html">PrefilledTX</a></li><li><a href="module-net_bip152-TXRequest.html">TXRequest</a></li><li><a href="module-net_bip152-TXResponse.html">TXResponse</a></li><li><a href="module-net_packets-AddrPacket.html">AddrPacket</a></li><li><a href="module-net_packets-AuthChallengePacket.html">AuthChallengePacket</a></li><li><a href="module-net_packets-AuthProposePacket.html">AuthProposePacket</a></li><li><a href="module-net_packets-AuthReplyPacket.html">AuthReplyPacket</a></li><li><a href="module-net_packets-BlockPacket.html">BlockPacket</a></li><li><a href="module-net_packets-BlockTxnPacket.html">BlockTxnPacket</a></li><li><a href="module-net_packets-CmpctBlockPacket.html">CmpctBlockPacket</a></li><li><a href="module-net_packets-EncackPacket.html">EncackPacket</a></li><li><a href="module-net_packets-EncinitPacket.html">EncinitPacket</a></li><li><a href="module-net_packets-FeeFilterPacket.html">FeeFilterPacket</a></li><li><a href="module-net_packets-FilterAddPacket.html">FilterAddPacket</a></li><li><a href="module-net_packets-FilterClearPacket.html">FilterClearPacket</a></li><li><a href="module-net_packets-FilterLoadPacket.html">FilterLoadPacket</a></li><li><a href="module-net_packets-GetAddrPacket.html">GetAddrPacket</a></li><li><a href="module-net_packets-GetBlocksPacket.html">GetBlocksPacket</a></li><li><a href="module-net_packets-GetBlockTxnPacket.html">GetBlockTxnPacket</a></li><li><a href="module-net_packets-GetDataPacket.html">GetDataPacket</a></li><li><a href="module-net_packets-GetHeadersPacket.html">GetHeadersPacket</a></li><li><a href="module-net_packets-HeadersPacket.html">HeadersPacket</a></li><li><a href="module-net_packets-InvPacket.html">InvPacket</a></li><li><a href="module-net_packets-MempoolPacket.html">MempoolPacket</a></li><li><a href="module-net_packets-MerkleBlockPacket.html">MerkleBlockPacket</a></li><li><a href="module-net_packets-NotFoundPacket.html">NotFoundPacket</a></li><li><a href="module-net_packets-Packet.html">Packet</a></li><li><a href="module-net_packets-PingPacket.html">PingPacket</a></li><li><a href="module-net_packets-PongPacket.html">PongPacket</a></li><li><a href="module-net_packets-RejectPacket.html">RejectPacket</a></li><li><a href="module-net_packets-SendCmpctPacket.html">SendCmpctPacket</a></li><li><a href="module-net_packets-SendHeadersPacket.html">SendHeadersPacket</a></li><li><a href="module-net_packets-TXPacket.html">TXPacket</a></li><li><a href="module-net_packets-UnknownPacket.html">UnknownPacket</a></li><li><a href="module-net_packets-VerackPacket.html">VerackPacket</a></li><li><a href="module-net_packets-VersionPacket.html">VersionPacket</a></li><li><a href="module-net_socks-Proxy.html">Proxy</a></li><li><a href="module-net_socks-SOCKS.html">SOCKS</a></li><li><a href="module-node.FullNode.html">FullNode</a></li><li><a href="module-node.Logger.html">Logger</a></li><li><a href="module-node.Node.html">Node</a></li><li><a href="module-node.NodeClient.html">NodeClient</a></li><li><a href="module-node.SPVNode.html">SPVNode</a></li><li><a href="module-primitives.AbstractBlock.html">AbstractBlock</a></li><li><a href="module-primitives.Address.html">Address</a></li><li><a href="module-primitives.Block.html">Block</a></li><li><a href="module-primitives.Coin.html">Coin</a></li><li><a href="module-primitives.CoinSelector.html">CoinSelector</a></li><li><a href="module-primitives.Headers.html">Headers</a></li><li><a href="module-primitives.Input.html">Input</a></li><li><a href="module-primitives.InvItem.html">InvItem</a></li><li><a href="module-primitives.KeyRing.html">KeyRing</a></li><li><a href="module-primitives.MemBlock.html">MemBlock</a></li><li><a href="module-primitives.MerkleBlock.html">MerkleBlock</a></li><li><a href="module-primitives.MTX.html">MTX</a></li><li><a href="module-primitives.NetAddress.html">NetAddress</a></li><li><a href="module-primitives.Outpoint.html">Outpoint</a></li><li><a href="module-primitives.Output.html">Output</a></li><li><a href="module-primitives.TX.html">TX</a></li><li><a href="module-primitives.TXMeta.html">TXMeta</a></li><li><a href="module-protocol.Network.html">Network</a></li><li><a href="module-protocol.TimeData.html">TimeData</a></li><li><a href="module-protocol_errors-VerifyError.html">VerifyError</a></li><li><a href="module-protocol_errors-VerifyResult.html">VerifyResult</a></li><li><a href="module-script.Opcode.html">Opcode</a></li><li><a href="module-script.Program.html">Program</a></li><li><a href="module-script.Script.html">Script</a></li><li><a href="module-script.ScriptError.html">ScriptError</a></li><li><a href="module-script.SigCache.html">SigCache</a></li><li><a href="module-script.Stack.html">Stack</a></li><li><a href="module-script.Witness.html">Witness</a></li><li><a href="module-utils.AsyncEmitter.html">AsyncEmitter</a></li><li><a href="module-utils.AsyncObject.html">AsyncObject</a></li><li><a href="module-utils.Bloom.html">Bloom</a></li><li><a href="module-utils.BufferReader.html">BufferReader</a></li><li><a href="module-utils.BufferWriter.html">BufferWriter</a></li><li><a href="module-utils.List.html">List</a></li><li><a href="module-utils.ListItem.html">ListItem</a></li><li><a href="module-utils.Lock.html">Lock</a></li><li><a href="module-utils.LRU.html">LRU</a></li><li><a href="module-utils.LRUBatch.html">LRUBatch</a></li><li><a href="module-utils.LRUItem.html">LRUItem</a></li><li><a href="module-utils.LRUOp.html">LRUOp</a></li><li><a href="module-utils.Map.html">Map</a></li><li><a href="module-utils.MappedLock.html">MappedLock</a></li><li><a href="module-utils.RollingFilter.html">RollingFilter</a></li><li><a href="module-utils.StaticWriter.html">StaticWriter</a></li><li><a href="module-utils_ip.Address.html">Address</a></li><li><a href="module-utils_protobuf-ProtoReader.html">ProtoReader</a></li><li><a href="module-utils_protobuf-ProtoWriter.html">ProtoWriter</a></li><li><a href="module-wallet.Account.html">Account</a></li><li><a href="module-wallet.Balance.html">Balance</a></li><li><a href="module-wallet.BlockRecord.html">BlockRecord</a></li><li><a href="module-wallet.ChainState.html">ChainState</a></li><li><a href="module-wallet.Credit.html">Credit</a></li><li><a href="module-wallet.Details.html">Details</a></li><li><a href="module-wallet.DetailsMember.html">DetailsMember</a></li><li><a href="module-wallet.MasterKey.html">MasterKey</a></li><li><a href="module-wallet.Path.html">Path</a></li><li><a href="module-wallet.TXDB.html">TXDB</a></li><li><a href="module-wallet.Wallet.html">Wallet</a></li><li><a href="module-wallet.WalletClient.html">WalletClient</a></li><li><a href="module-wallet.WalletDB.html">WalletDB</a></li><li><a href="module-wallet.WalletKey.html">WalletKey</a></li><li><a href="module-wallet.WalletOptions.html">WalletOptions</a></li><li><a href="module-wallet_records-BlockMapRecord.html">BlockMapRecord</a></li><li><a href="module-wallet_records-BlockMeta.html">BlockMeta</a></li><li><a href="module-wallet_records-ChainState.html">ChainState</a></li><li><a href="module-wallet_records-OutpointMapRecord.html">OutpointMapRecord</a></li><li><a href="module-wallet_records-PathMapRecord.html">PathMapRecord</a></li><li><a href="module-wallet_records-TXMapRecord.html">TXMapRecord</a></li><li><a href="module-wallet_records-TXRecord.html">TXRecord</a></li><li><a href="module-workers.Framer.html">Framer</a></li><li><a href="module-workers.Master.html">Master</a></li><li><a href="module-workers.Parser.html">Parser</a></li><li><a href="module-workers.ParserClient.html">ParserClient</a></li><li><a href="module-workers.Worker.html">Worker</a></li><li><a href="module-workers.WorkerPool.html">WorkerPool</a></li><li><a href="module-workers_packets-ECSignPacket.html">ECSignPacket</a></li><li><a href="module-workers_packets-ECSignResultPacket.html">ECSignResultPacket</a></li><li><a href="module-workers_packets-ECVerifyPacket.html">ECVerifyPacket</a></li><li><a href="module-workers_packets-ECVerifyResultPacket.html">ECVerifyResultPacket</a></li><li><a href="module-workers_packets-ErrorPacket.html">ErrorPacket</a></li><li><a href="module-workers_packets-ErrorResultPacket.html">ErrorResultPacket</a></li><li><a href="module-workers_packets-EventPacket.html">EventPacket</a></li><li><a href="module-workers_packets-LogPacket.html">LogPacket</a></li><li><a href="module-workers_packets-MinePacket.html">MinePacket</a></li><li><a href="module-workers_packets-MineResultPacket.html">MineResultPacket</a></li><li><a href="module-workers_packets-Packet.html">Packet</a></li><li><a href="module-workers_packets-ScryptPacket.html">ScryptPacket</a></li><li><a href="module-workers_packets-ScryptResultPacket.html">ScryptResultPacket</a></li><li><a href="module-workers_packets-SignInputPacket.html">SignInputPacket</a></li><li><a href="module-workers_packets-SignInputResultPacket.html">SignInputResultPacket</a></li><li><a href="module-workers_packets-SignPacket.html">SignPacket</a></li><li><a href="module-workers_packets-SignResultPacket.html">SignResultPacket</a></li><li><a href="module-workers_packets-VerifyInputPacket.html">VerifyInputPacket</a></li><li><a href="module-workers_packets-VerifyInputResultPacket.html">VerifyInputResultPacket</a></li><li><a href="module-workers_packets-VerifyPacket.html">VerifyPacket</a></li><li><a href="module-workers_packets-VerifyResultPacket.html">VerifyResultPacket</a></li></ul><h3>Namespaces</h3><ul><li><a href="module-crypto_pk.ecdsa.html">ecdsa</a></li><li><a href="module-crypto_pk.rsa.html">rsa</a></li></ul><h3>Global</h3><ul><li><a href="global.html"></a></li><li><a href="global.html#DoubleMap">DoubleMap</a></li><li><a href="global.html#StatEntry">StatEntry</a></li></ul>
|
|
</nav>
|
|
|
|
<br class="clear">
|
|
|
|
<footer>
|
|
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Fri Feb 10 2017 09:40:23 GMT-0800 (PST)
|
|
</footer>
|
|
|
|
<script> prettyPrint(); </script>
|
|
<script src="scripts/linenumber.js"> </script>
|
|
</body>
|
|
</html>
|
|
|