itns-sidechain/lib/script/script.js

2467 lines
54 KiB
JavaScript
Raw Permalink Normal View History

/*!
2018-08-01 20:00:09 -07:00
* script.js - script interpreter for hsd
2018-02-01 13:40:45 -08:00
* Copyright (c) 2017-2018, Christopher Jeffrey (MIT License).
2018-08-01 20:00:09 -07:00
* https://github.com/handshake-org/hsd
2016-03-14 20:43:32 -07:00
*/
2016-06-13 01:06:01 -07:00
'use strict';
2018-07-19 05:40:48 -07:00
const assert = require('bsert');
2017-11-16 11:43:24 -08:00
const bio = require('bufio');
2017-11-01 15:41:32 -07:00
const ripemd160 = require('bcrypto/lib/ripemd160');
const sha1 = require('bcrypto/lib/sha1');
const sha256 = require('bcrypto/lib/sha256');
const hash160 = require('bcrypto/lib/hash160');
const hash256 = require('bcrypto/lib/hash256');
2018-01-02 20:24:56 -08:00
const blake2b = require('bcrypto/lib/blake2b');
const sha3 = require('bcrypto/lib/sha3');
const keccak = require('bcrypto/lib/keccak');
2017-10-26 04:07:36 -07:00
const secp256k1 = require('bcrypto/lib/secp256k1');
const consensus = require('../protocol/consensus');
2017-06-29 20:54:07 -07:00
const Opcode = require('./opcode');
const Stack = require('./stack');
const ScriptError = require('./scripterror');
const ScriptNum = require('./scriptnum');
2017-06-29 20:54:07 -07:00
const common = require('./common');
const opcodes = common.opcodes;
const scriptTypes = common.types;
2017-11-16 11:43:24 -08:00
const {encoding} = bio;
/** @typedef {import('bfilter').BloomFilter} BloomFilter */
/** @typedef {import('../types').BufioWriter} BufioWriter */
/** @typedef {import('../types').SighashType} SighashType */
/** @typedef {import('../types').VerifyFlags} VerifyFlags */
/** @typedef {import('../types').Amount} AmountValue */
/** @typedef {import('../types').Hash} Hash */
/** @typedef {import('./witness')} Witness */
/** @typedef {import('../primitives/address')} Address */
/** @typedef {import('../primitives/tx')} TX */
2017-11-16 11:43:24 -08:00
/*
* Constants
*/
const EMPTY_BUFFER = Buffer.alloc(0);
2016-03-24 13:28:24 -07:00
2016-03-14 20:43:32 -07:00
/**
2017-11-16 18:44:38 -08:00
* Script
* Represents a input or output script.
2017-02-03 22:47:26 -08:00
* @alias module:script.Script
2016-06-20 01:09:27 -07:00
* @property {Array} code - Parsed script code.
2016-04-17 19:34:03 -07:00
* @property {Buffer?} raw - Serialized script.
2016-06-20 01:09:27 -07:00
* @property {Number} length - Number of parsed opcodes.
2016-03-14 20:43:32 -07:00
*/
class Script extends bio.Struct {
2017-11-16 18:44:38 -08:00
/**
* Create a script.
* @constructor
* @param {Object?} [options]
2017-11-16 18:44:38 -08:00
*/
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
constructor(options) {
super();
2017-11-16 18:44:38 -08:00
this.raw = EMPTY_BUFFER;
/** @type {Opcode[]} */
2017-11-16 18:44:38 -08:00
this.code = [];
2016-04-17 19:34:03 -07:00
2017-11-16 18:44:38 -08:00
if (options)
this.fromOptions(options);
}
2017-01-06 09:57:55 -08:00
2017-11-16 18:44:38 -08:00
/**
* Get length.
* @returns {Number}
*/
2017-01-06 09:57:55 -08:00
2017-11-16 18:44:38 -08:00
get length() {
return this.code.length;
}
2017-01-06 09:57:55 -08:00
2017-11-16 18:44:38 -08:00
/**
* Set length.
* @param {Number} value
*/
2017-01-06 09:57:55 -08:00
2017-11-16 18:44:38 -08:00
set length(value) {
this.code.length = value;
}
2017-01-06 09:57:55 -08:00
2017-11-16 18:44:38 -08:00
/**
* Inject properties from options object.
* @param {Object} options
* @returns {this}
2017-11-16 18:44:38 -08:00
*/
2017-01-06 09:57:55 -08:00
2017-11-16 18:44:38 -08:00
fromOptions(options) {
assert(options, 'Script data is required.');
2017-01-06 09:57:55 -08:00
2017-11-16 18:44:38 -08:00
if (Buffer.isBuffer(options))
return this.decode(options);
2017-01-06 09:57:55 -08:00
2017-11-16 18:44:38 -08:00
if (Array.isArray(options))
return this.fromArray(options);
2017-01-06 09:57:55 -08:00
2017-11-16 18:44:38 -08:00
if (options.raw) {
if (!options.code)
return this.decode(options.raw);
2017-11-16 18:44:38 -08:00
assert(Buffer.isBuffer(options.raw), 'Raw must be a Buffer.');
this.raw = options.raw;
}
2017-01-06 09:57:55 -08:00
2017-11-16 18:44:38 -08:00
if (options.code) {
if (!options.raw)
return this.fromArray(options.code);
assert(Array.isArray(options.code), 'Code must be an array.');
this.code = options.code;
}
2017-01-06 09:57:55 -08:00
2017-11-16 18:44:38 -08:00
return this;
}
2017-01-06 09:57:55 -08:00
2017-11-16 18:44:38 -08:00
/**
* Instantiate a value-only iterator.
*/
2017-01-06 09:57:55 -08:00
2017-11-16 18:44:38 -08:00
values() {
return this.code.values();
}
2016-06-20 03:16:23 -07:00
2017-11-16 18:44:38 -08:00
/**
* Instantiate a key and value iterator.
*/
2016-06-20 01:09:27 -07:00
2017-11-16 18:44:38 -08:00
entries() {
return this.code.entries();
}
2016-06-17 01:34:59 -07:00
2017-11-16 18:44:38 -08:00
/**
* Instantiate a value-only iterator.
*/
2016-06-17 01:34:59 -07:00
2017-11-16 18:44:38 -08:00
[Symbol.iterator]() {
return this.code[Symbol.iterator]();
2016-07-01 06:03:48 -07:00
}
2016-06-13 21:25:42 -07:00
2017-11-16 18:44:38 -08:00
/**
* Convert the script to an array of
* Buffers (pushdatas) and Numbers
* (opcodes).
* @returns {Array}
*/
2016-06-17 01:34:59 -07:00
2017-11-16 18:44:38 -08:00
toArray() {
return this.code.slice();
}
2016-06-17 01:34:59 -07:00
2017-11-16 18:44:38 -08:00
/**
* Inject properties from an array of
* of buffers and numbers.
* @param {Opcode[]} code
* @returns {this}
2017-11-16 18:44:38 -08:00
*/
2016-06-20 01:09:27 -07:00
2017-11-16 18:44:38 -08:00
fromArray(code) {
assert(Array.isArray(code));
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
this.clear();
2017-11-16 18:44:38 -08:00
for (const op of code)
this.push(op);
2017-11-16 18:44:38 -08:00
return this.compile();
}
2017-11-16 18:44:38 -08:00
/**
* Instantiate script from an array
* of buffers and numbers.
* @param {Opcode[]} code
2017-11-16 18:44:38 -08:00
* @returns {Script}
*/
2017-11-16 18:44:38 -08:00
static fromArray(code) {
return new this().fromArray(code);
}
2017-11-16 18:44:38 -08:00
/**
* Convert script to stack items.
* @returns {Buffer[]}
*/
2017-11-16 18:44:38 -08:00
toItems() {
const items = [];
2016-06-14 16:26:26 -07:00
2017-11-16 18:44:38 -08:00
for (const op of this.code) {
const data = op.toPush();
2016-06-13 21:25:42 -07:00
2017-11-16 18:44:38 -08:00
if (!data)
throw new Error('Non-push opcode in script.');
2016-07-01 05:42:04 -07:00
2017-11-16 18:44:38 -08:00
items.push(data);
}
2016-12-10 21:20:22 -08:00
2017-11-16 18:44:38 -08:00
return items;
}
2016-12-10 21:20:22 -08:00
2017-11-16 18:44:38 -08:00
/**
* Inject data from stack items.
* @param {Buffer[]} items
* @returns {this}
2017-11-16 18:44:38 -08:00
*/
2016-12-10 21:20:22 -08:00
2017-11-16 18:44:38 -08:00
fromItems(items) {
assert(Array.isArray(items));
2016-07-01 05:42:04 -07:00
2017-11-16 18:44:38 -08:00
this.clear();
2016-06-14 16:26:26 -07:00
2017-11-16 18:44:38 -08:00
for (const item of items)
this.pushData(item);
2016-06-13 21:25:42 -07:00
2017-11-16 18:44:38 -08:00
return this.compile();
}
2016-12-10 21:20:22 -08:00
2017-11-16 18:44:38 -08:00
/**
* Instantiate script from stack items.
* @param {Buffer[]} items
* @returns {Script}
*/
2017-11-16 18:44:38 -08:00
static fromItems(items) {
return new this().fromItems(items);
}
2017-11-16 18:44:38 -08:00
/**
* Convert script to stack.
* @returns {Stack}
*/
2017-11-16 18:44:38 -08:00
toStack() {
return new Stack(this.toItems());
}
2017-11-16 18:44:38 -08:00
/**
* Inject data from stack.
* @param {Stack} stack
* @returns {this}
2017-11-16 18:44:38 -08:00
*/
2016-12-10 21:20:22 -08:00
2017-11-16 18:44:38 -08:00
fromStack(stack) {
return this.fromItems(stack.items);
}
2016-12-10 21:20:22 -08:00
2017-11-16 18:44:38 -08:00
/**
* Instantiate script from stack.
* @param {Stack} stack
* @returns {Script}
*/
2016-12-10 21:20:22 -08:00
2017-11-16 18:44:38 -08:00
static fromStack(stack) {
return new this().fromStack(stack);
}
2016-12-10 21:20:22 -08:00
2017-11-16 18:44:38 -08:00
/**
* Inject properties from script.
* Used for cloning.
* @param {this} script
* @returns {this}
2017-11-16 18:44:38 -08:00
*/
inject(script) {
this.raw = script.raw;
this.code = script.code.slice();
return this;
}
2017-11-16 18:44:38 -08:00
/**
* Test equality against script.
* @param {Script} script
* @returns {Boolean}
*/
2017-11-16 18:44:38 -08:00
equals(script) {
assert(Script.isScript(script));
return this.raw.equals(script.raw);
}
2017-11-16 18:44:38 -08:00
/**
* Compare against another script.
* @param {Script} script
* @returns {Number}
*/
2017-11-16 18:44:38 -08:00
compare(script) {
assert(Script.isScript(script));
return this.raw.compare(script.raw);
}
2017-11-16 18:44:38 -08:00
/**
* Clear the script.
* @returns {this}
2017-11-16 18:44:38 -08:00
*/
2016-12-10 21:20:22 -08:00
2017-11-16 18:44:38 -08:00
clear() {
this.raw = EMPTY_BUFFER;
this.code.length = 0;
return this;
}
2016-12-10 21:20:22 -08:00
2017-11-16 18:44:38 -08:00
/**
* Inspect the script.
* @returns {String} Human-readable script code.
*/
2016-12-10 21:20:22 -08:00
format() {
2017-11-16 18:44:38 -08:00
return `<Script: ${this.toString()}>`;
}
2017-11-16 18:44:38 -08:00
/**
* Convert the script to a bitcoind test string.
* @returns {String} Human-readable script code.
*/
2017-01-15 15:14:48 -08:00
2017-11-16 18:44:38 -08:00
toString() {
const out = [];
2017-01-15 15:14:48 -08:00
2017-11-16 18:44:38 -08:00
for (const op of this.code)
out.push(op.toFormat());
2017-11-16 18:44:38 -08:00
return out.join(' ');
}
2017-11-16 18:44:38 -08:00
/**
* Format the script as bitcoind asm.
* @param {Boolean?} decode - Attempt to decode hash types.
* @returns {String} Human-readable script.
*/
2017-11-16 18:44:38 -08:00
toASM(decode) {
const out = [];
2017-11-16 18:44:38 -08:00
for (const op of this.code)
out.push(op.toASM(decode));
2017-11-16 18:44:38 -08:00
return out.join(' ');
}
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
/**
* Re-encode the script internally. Useful if you
* changed something manually in the `code` array.
*/
2017-11-16 18:44:38 -08:00
compile() {
if (this.code.length === 0)
return this.clear();
2017-11-16 18:44:38 -08:00
let size = 0;
2017-11-16 18:44:38 -08:00
for (const op of this.code)
size += op.getSize();
2017-11-16 18:44:38 -08:00
const bw = bio.write(size);
2017-11-16 18:44:38 -08:00
for (const op of this.code)
op.write(bw);
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
this.raw = bw.render();
return this;
}
2016-04-29 21:45:18 -07:00
2017-11-16 18:44:38 -08:00
/**
* Write the script to a buffer writer.
* @param {BufioWriter} bw
* @returns {BufioWriter}
2017-11-16 18:44:38 -08:00
*/
2016-12-12 12:31:27 -08:00
write(bw) {
2017-11-16 18:44:38 -08:00
bw.writeVarBytes(this.raw);
return bw;
}
2016-12-12 12:31:27 -08:00
2017-11-16 18:44:38 -08:00
/**
* Encode the script to a Buffer. See {@link Script#encode}.
* @returns {Buffer} Serialized script.
2017-11-16 18:44:38 -08:00
*/
2016-12-12 12:31:27 -08:00
encode() {
2017-11-16 18:44:38 -08:00
return this.raw;
}
2016-12-12 12:31:27 -08:00
2017-11-16 18:44:38 -08:00
/**
* Convert script to a hex string.
* @returns {String}
*/
2016-06-14 16:26:26 -07:00
getJSON() {
return this.toHex();
2017-11-16 18:44:38 -08:00
}
2017-11-16 18:44:38 -08:00
/**
* Inject properties from json object.
* @param {String} json
*/
2017-11-16 18:44:38 -08:00
fromJSON(json) {
assert(typeof json === 'string', 'Code must be a string.');
return this.decode(Buffer.from(json, 'hex'));
2017-11-16 18:44:38 -08:00
}
2016-12-03 18:02:10 -08:00
2017-11-16 18:44:38 -08:00
/**
* Get the script's "subscript" starting at a separator.
* @param {Number} index - The last separator to sign/verify beyond.
* @returns {this} Subscript.
2017-11-16 18:44:38 -08:00
*/
2016-12-03 18:02:10 -08:00
2017-11-16 18:44:38 -08:00
getSubscript(index) {
if (index === 0)
return this.clone();
2016-12-03 18:02:10 -08:00
/** @type {this} */
2017-11-16 18:44:38 -08:00
const script = new Script();
2016-12-03 18:02:10 -08:00
2017-11-16 18:44:38 -08:00
for (let i = index; i < this.code.length; i++) {
const op = this.code[i];
2016-06-13 21:25:42 -07:00
2017-11-16 18:44:38 -08:00
if (op.value === -1)
break;
2016-04-19 11:39:55 -07:00
2017-11-16 18:44:38 -08:00
script.code.push(op);
}
2016-04-19 11:39:55 -07:00
2017-11-16 18:44:38 -08:00
return script.compile();
}
2016-06-20 01:09:27 -07:00
2017-11-16 18:44:38 -08:00
/**
* Get the script's "subscript" starting at a separator.
* Remove all OP_CODESEPARATORs if present. This bizarre
* behavior is necessary for signing and verification when
* code separators are present.
* @returns {this}.
2017-11-16 18:44:38 -08:00
*/
removeSeparators() {
let found = false;
// Optimizing for the common case:
// Check for any separators first.
for (const op of this.code) {
if (op.value === -1)
break;
2016-06-17 02:26:48 -07:00
2017-11-16 18:44:38 -08:00
if (op.value === opcodes.OP_CODESEPARATOR) {
found = true;
break;
}
}
2016-06-27 17:32:23 -07:00
2017-11-16 18:44:38 -08:00
if (!found)
return this;
2016-06-27 17:32:23 -07:00
2017-11-16 18:44:38 -08:00
// Uncommon case: someone actually
// has a code separator. Go through
// and remove them all.
/** @type {this} */
2017-11-16 18:44:38 -08:00
const script = new Script();
2016-06-20 01:09:27 -07:00
2017-11-16 18:44:38 -08:00
for (const op of this.code) {
if (op.value === -1)
break;
2016-06-17 02:26:48 -07:00
2017-11-16 18:44:38 -08:00
if (op.value !== opcodes.OP_CODESEPARATOR)
script.code.push(op);
}
2017-11-16 18:44:38 -08:00
return script.compile();
}
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
/**
* Execute and interpret the script.
* @param {Stack} stack - Script execution stack.
* @param {Number?} flags - Script standard flags.
* @param {TX?} tx - Transaction being verified.
* @param {Number?} index - Index of input being verified.
* @param {AmountValue?} value - Previous output value.
2017-11-16 20:40:01 -08:00
* @throws {ScriptError} Will be thrown on VERIFY failures.
2017-11-16 18:44:38 -08:00
*/
2018-01-02 20:24:56 -08:00
execute(stack, flags, tx, index, value) {
2017-11-16 18:44:38 -08:00
if (flags == null)
flags = Script.flags.STANDARD_VERIFY_FLAGS;
2017-01-15 15:14:48 -08:00
2017-11-16 18:44:38 -08:00
if (this.raw.length > consensus.MAX_SCRIPT_SIZE)
throw new ScriptError('SCRIPT_SIZE');
2016-04-27 20:06:55 -07:00
2017-11-16 18:44:38 -08:00
const state = [];
const alt = [];
2016-04-27 20:06:55 -07:00
2017-11-16 18:44:38 -08:00
let lastSep = 0;
let opCount = 0;
let negate = 0;
let minimal = false;
2016-04-27 20:06:55 -07:00
2017-11-16 18:44:38 -08:00
if (flags & Script.flags.VERIFY_MINIMALDATA)
minimal = true;
2016-04-27 20:06:55 -07:00
2017-11-16 18:44:38 -08:00
for (let ip = 0; ip < this.code.length; ip++) {
const op = this.code[ip];
2017-01-15 15:14:48 -08:00
2017-11-16 18:44:38 -08:00
if (op.value === -1)
throw new ScriptError('BAD_OPCODE', op, ip);
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
if (op.data && op.data.length > consensus.MAX_SCRIPT_PUSH)
throw new ScriptError('PUSH_SIZE', op, ip);
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
if (op.value > opcodes.OP_16 && ++opCount > consensus.MAX_SCRIPT_OPS)
throw new ScriptError('OP_COUNT', op, ip);
2017-01-15 15:14:48 -08:00
2017-11-16 18:44:38 -08:00
if (op.isDisabled())
throw new ScriptError('DISABLED_OPCODE', op, ip);
2017-01-15 15:14:48 -08:00
2017-11-16 18:44:38 -08:00
if (negate && !op.isBranch()) {
if (stack.length + alt.length > consensus.MAX_SCRIPT_STACK)
throw new ScriptError('STACK_SIZE', op, ip);
continue;
}
2017-01-15 15:14:48 -08:00
2017-11-16 18:44:38 -08:00
if (op.data) {
if (minimal && !op.isMinimal())
throw new ScriptError('MINIMALDATA', op, ip);
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
stack.push(op.data);
2017-11-16 18:44:38 -08:00
if (stack.length + alt.length > consensus.MAX_SCRIPT_STACK)
throw new ScriptError('STACK_SIZE', op, ip);
2017-11-16 18:44:38 -08:00
continue;
}
2017-11-16 18:44:38 -08:00
switch (op.value) {
case opcodes.OP_0: {
stack.pushInt(0);
break;
}
case opcodes.OP_1NEGATE: {
stack.pushInt(-1);
break;
}
case opcodes.OP_1:
case opcodes.OP_2:
case opcodes.OP_3:
case opcodes.OP_4:
case opcodes.OP_5:
case opcodes.OP_6:
case opcodes.OP_7:
case opcodes.OP_8:
case opcodes.OP_9:
case opcodes.OP_10:
case opcodes.OP_11:
case opcodes.OP_12:
case opcodes.OP_13:
case opcodes.OP_14:
case opcodes.OP_15:
case opcodes.OP_16: {
stack.pushInt(op.value - 0x50);
break;
}
case opcodes.OP_NOP: {
break;
}
2018-04-25 20:01:06 -07:00
case opcodes.OP_TYPE: {
if (!tx)
throw new ScriptError('UNKNOWN_ERROR', 'No TX passed in.');
if (index >= tx.outputs.length) {
stack.pushInt(0);
break;
}
const {covenant} = tx.outputs[index];
stack.pushInt(covenant.type);
break;
}
2017-11-16 18:44:38 -08:00
case opcodes.OP_CHECKLOCKTIMEVERIFY: {
if (!tx)
throw new ScriptError('UNKNOWN_ERROR', 'No TX passed in.');
2017-11-16 18:44:38 -08:00
if (stack.length === 0)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
const num = stack.getNum(-1, minimal, 5);
2017-11-16 18:44:38 -08:00
if (num.isNeg())
throw new ScriptError('NEGATIVE_LOCKTIME', op, ip);
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
const locktime = num.toDouble();
2016-04-20 09:20:45 -07:00
2017-11-16 18:44:38 -08:00
if (!tx.verifyLocktime(index, locktime))
throw new ScriptError('UNSATISFIED_LOCKTIME', op, ip);
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
break;
}
case opcodes.OP_CHECKSEQUENCEVERIFY: {
if (!tx)
throw new ScriptError('UNKNOWN_ERROR', 'No TX passed in.');
2016-09-30 11:20:58 -07:00
2017-11-16 18:44:38 -08:00
if (stack.length === 0)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
2017-11-16 18:44:38 -08:00
const num = stack.getNum(-1, minimal, 5);
2017-11-16 18:44:38 -08:00
if (num.isNeg())
throw new ScriptError('NEGATIVE_LOCKTIME', op, ip);
2017-11-16 18:44:38 -08:00
const locktime = num.toDouble();
2017-11-16 18:44:38 -08:00
if (!tx.verifySequence(index, locktime))
throw new ScriptError('UNSATISFIED_LOCKTIME', op, ip);
2016-04-17 06:39:31 -07:00
2017-11-16 18:44:38 -08:00
break;
}
case opcodes.OP_NOP1:
case opcodes.OP_NOP4:
case opcodes.OP_NOP5:
case opcodes.OP_NOP6:
case opcodes.OP_NOP7:
case opcodes.OP_NOP8:
case opcodes.OP_NOP9:
case opcodes.OP_NOP10: {
2017-01-06 09:57:55 -08:00
if (flags & Script.flags.VERIFY_DISCOURAGE_UPGRADABLE_NOPS)
2016-04-19 21:43:40 -07:00
throw new ScriptError('DISCOURAGE_UPGRADABLE_NOPS', op, ip);
break;
}
2017-11-16 18:44:38 -08:00
case opcodes.OP_IF:
case opcodes.OP_NOTIF: {
let val = false;
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
if (!negate) {
if (stack.length < 1)
throw new ScriptError('UNBALANCED_CONDITIONAL', op, ip);
2016-03-14 20:43:32 -07:00
2018-01-02 20:24:56 -08:00
if (flags & Script.flags.VERIFY_MINIMALIF) {
2017-11-16 18:44:38 -08:00
const item = stack.get(-1);
2016-04-19 21:43:40 -07:00
2017-11-16 18:44:38 -08:00
if (item.length > 1)
throw new ScriptError('MINIMALIF');
2016-04-19 21:43:40 -07:00
2017-11-16 18:44:38 -08:00
if (item.length === 1 && item[0] !== 1)
throw new ScriptError('MINIMALIF');
}
2016-04-19 21:43:40 -07:00
2017-11-16 18:44:38 -08:00
val = stack.getBool(-1);
2016-04-19 21:43:40 -07:00
2017-11-16 18:44:38 -08:00
if (op.value === opcodes.OP_NOTIF)
val = !val;
2017-11-16 18:44:38 -08:00
stack.pop();
}
2016-04-19 21:43:40 -07:00
2017-11-16 18:44:38 -08:00
state.push(val);
2016-04-19 21:43:40 -07:00
2017-11-16 18:44:38 -08:00
if (!val)
negate += 1;
2016-04-19 21:43:40 -07:00
2017-11-16 18:44:38 -08:00
break;
}
case opcodes.OP_ELSE: {
if (state.length === 0)
throw new ScriptError('UNBALANCED_CONDITIONAL', op, ip);
2016-04-19 21:43:40 -07:00
2017-11-16 18:44:38 -08:00
state[state.length - 1] = !state[state.length - 1];
2016-04-19 21:43:40 -07:00
2017-11-16 18:44:38 -08:00
if (!state[state.length - 1])
negate += 1;
else
negate -= 1;
2016-04-19 21:43:40 -07:00
2017-11-16 18:44:38 -08:00
break;
}
case opcodes.OP_ENDIF: {
if (state.length === 0)
throw new ScriptError('UNBALANCED_CONDITIONAL', op, ip);
2016-04-19 21:43:40 -07:00
2017-11-16 18:44:38 -08:00
if (!state.pop())
negate -= 1;
2017-11-16 18:44:38 -08:00
break;
}
case opcodes.OP_VERIFY: {
if (stack.length === 0)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
2017-11-16 18:44:38 -08:00
if (!stack.getBool(-1))
throw new ScriptError('VERIFY', op, ip);
2017-11-16 18:44:38 -08:00
stack.pop();
2017-11-16 18:44:38 -08:00
break;
}
case opcodes.OP_RETURN: {
throw new ScriptError('OP_RETURN', op, ip);
}
case opcodes.OP_TOALTSTACK: {
if (stack.length === 0)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
2017-11-16 18:44:38 -08:00
alt.push(stack.pop());
break;
}
case opcodes.OP_FROMALTSTACK: {
if (alt.length === 0)
throw new ScriptError('INVALID_ALTSTACK_OPERATION', op, ip);
2017-11-16 18:44:38 -08:00
stack.push(alt.pop());
break;
}
case opcodes.OP_2DROP: {
if (stack.length < 2)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
stack.pop();
2017-11-16 18:44:38 -08:00
stack.pop();
break;
}
2017-11-16 18:44:38 -08:00
case opcodes.OP_2DUP: {
if (stack.length < 2)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
2017-11-16 18:44:38 -08:00
const v1 = stack.get(-2);
const v2 = stack.get(-1);
2017-11-16 18:44:38 -08:00
stack.push(v1);
stack.push(v2);
break;
}
case opcodes.OP_3DUP: {
if (stack.length < 3)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
2017-11-16 18:44:38 -08:00
const v1 = stack.get(-3);
const v2 = stack.get(-2);
const v3 = stack.get(-1);
2017-11-16 18:44:38 -08:00
stack.push(v1);
stack.push(v2);
stack.push(v3);
break;
}
case opcodes.OP_2OVER: {
if (stack.length < 4)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
2017-11-16 18:44:38 -08:00
const v1 = stack.get(-4);
const v2 = stack.get(-3);
2017-11-16 18:44:38 -08:00
stack.push(v1);
stack.push(v2);
break;
}
case opcodes.OP_2ROT: {
if (stack.length < 6)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
2017-11-16 18:44:38 -08:00
const v1 = stack.get(-6);
const v2 = stack.get(-5);
2017-11-16 18:44:38 -08:00
stack.erase(-6, -4);
stack.push(v1);
stack.push(v2);
break;
}
case opcodes.OP_2SWAP: {
if (stack.length < 4)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
2017-11-16 18:44:38 -08:00
stack.swap(-4, -2);
stack.swap(-3, -1);
break;
}
case opcodes.OP_IFDUP: {
if (stack.length === 0)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
2017-11-16 18:44:38 -08:00
if (stack.getBool(-1)) {
const val = stack.get(-1);
stack.push(val);
}
2017-11-16 18:44:38 -08:00
break;
}
case opcodes.OP_DEPTH: {
stack.pushInt(stack.length);
break;
}
case opcodes.OP_DROP: {
if (stack.length === 0)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
2017-11-16 18:44:38 -08:00
stack.pop();
break;
}
case opcodes.OP_DUP: {
if (stack.length === 0)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
2017-11-16 18:44:38 -08:00
stack.push(stack.get(-1));
break;
}
case opcodes.OP_NIP: {
if (stack.length < 2)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
2017-11-16 18:44:38 -08:00
stack.remove(-2);
break;
}
case opcodes.OP_OVER: {
if (stack.length < 2)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
2017-11-16 18:44:38 -08:00
stack.push(stack.get(-2));
break;
}
case opcodes.OP_PICK:
case opcodes.OP_ROLL: {
if (stack.length < 2)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
2017-11-16 18:44:38 -08:00
const num = stack.getInt(-1, minimal, 4);
stack.pop();
2017-11-16 18:44:38 -08:00
if (num < 0 || num >= stack.length)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
2017-11-16 18:44:38 -08:00
const val = stack.get(-num - 1);
2017-11-16 18:44:38 -08:00
if (op.value === opcodes.OP_ROLL)
stack.remove(-num - 1);
stack.push(val);
2017-11-16 18:44:38 -08:00
break;
}
2017-11-16 18:44:38 -08:00
case opcodes.OP_ROT: {
if (stack.length < 3)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
2017-11-16 18:44:38 -08:00
stack.swap(-3, -2);
stack.swap(-2, -1);
break;
}
case opcodes.OP_SWAP: {
if (stack.length < 2)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
2017-11-16 18:44:38 -08:00
stack.swap(-2, -1);
break;
}
case opcodes.OP_TUCK: {
if (stack.length < 2)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
2017-11-16 18:44:38 -08:00
stack.insert(-2, stack.get(-1));
break;
}
case opcodes.OP_SIZE: {
if (stack.length < 1)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
2017-11-16 18:44:38 -08:00
stack.pushInt(stack.get(-1).length);
break;
}
case opcodes.OP_EQUAL:
case opcodes.OP_EQUALVERIFY: {
if (stack.length < 2)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
2017-11-16 18:44:38 -08:00
const v1 = stack.get(-2);
const v2 = stack.get(-1);
2017-11-16 18:44:38 -08:00
const res = v1.equals(v2);
2017-11-16 18:44:38 -08:00
stack.pop();
stack.pop();
2017-11-16 18:44:38 -08:00
stack.pushBool(res);
2017-11-16 18:44:38 -08:00
if (op.value === opcodes.OP_EQUALVERIFY) {
if (!res)
throw new ScriptError('EQUALVERIFY', op, ip);
stack.pop();
}
2017-11-16 18:44:38 -08:00
break;
}
case opcodes.OP_1ADD:
case opcodes.OP_1SUB:
case opcodes.OP_NEGATE:
case opcodes.OP_ABS:
case opcodes.OP_NOT:
case opcodes.OP_0NOTEQUAL: {
if (stack.length < 1)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
let num = stack.getNum(-1, minimal, 4);
let cmp;
switch (op.value) {
case opcodes.OP_1ADD:
num.iaddn(1);
break;
case opcodes.OP_1SUB:
num.isubn(1);
break;
case opcodes.OP_NEGATE:
num.ineg();
break;
case opcodes.OP_ABS:
num.iabs();
break;
case opcodes.OP_NOT:
cmp = num.isZero();
num = ScriptNum.fromBool(cmp);
break;
case opcodes.OP_0NOTEQUAL:
cmp = !num.isZero();
num = ScriptNum.fromBool(cmp);
break;
default:
assert(false, 'Fatal script error.');
break;
}
2017-11-16 18:44:38 -08:00
stack.pop();
stack.pushNum(num);
2017-11-16 18:44:38 -08:00
break;
}
case opcodes.OP_ADD:
case opcodes.OP_SUB:
case opcodes.OP_BOOLAND:
case opcodes.OP_BOOLOR:
case opcodes.OP_NUMEQUAL:
case opcodes.OP_NUMEQUALVERIFY:
case opcodes.OP_NUMNOTEQUAL:
case opcodes.OP_LESSTHAN:
case opcodes.OP_GREATERTHAN:
case opcodes.OP_LESSTHANOREQUAL:
case opcodes.OP_GREATERTHANOREQUAL:
case opcodes.OP_MIN:
case opcodes.OP_MAX: {
if (stack.length < 2)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
const n1 = stack.getNum(-2, minimal, 4);
const n2 = stack.getNum(-1, minimal, 4);
2018-04-25 20:01:06 -07:00
2017-11-16 18:44:38 -08:00
let num, cmp;
switch (op.value) {
case opcodes.OP_ADD:
num = n1.iadd(n2);
break;
case opcodes.OP_SUB:
num = n1.isub(n2);
break;
case opcodes.OP_BOOLAND:
cmp = n1.toBool() && n2.toBool();
num = ScriptNum.fromBool(cmp);
break;
case opcodes.OP_BOOLOR:
cmp = n1.toBool() || n2.toBool();
num = ScriptNum.fromBool(cmp);
break;
case opcodes.OP_NUMEQUAL:
cmp = n1.eq(n2);
num = ScriptNum.fromBool(cmp);
break;
case opcodes.OP_NUMEQUALVERIFY:
cmp = n1.eq(n2);
num = ScriptNum.fromBool(cmp);
break;
case opcodes.OP_NUMNOTEQUAL:
cmp = !n1.eq(n2);
num = ScriptNum.fromBool(cmp);
break;
case opcodes.OP_LESSTHAN:
cmp = n1.lt(n2);
num = ScriptNum.fromBool(cmp);
break;
case opcodes.OP_GREATERTHAN:
cmp = n1.gt(n2);
num = ScriptNum.fromBool(cmp);
break;
case opcodes.OP_LESSTHANOREQUAL:
cmp = n1.lte(n2);
num = ScriptNum.fromBool(cmp);
break;
case opcodes.OP_GREATERTHANOREQUAL:
cmp = n1.gte(n2);
num = ScriptNum.fromBool(cmp);
break;
case opcodes.OP_MIN:
num = ScriptNum.min(n1, n2);
break;
case opcodes.OP_MAX:
num = ScriptNum.max(n1, n2);
break;
default:
assert(false, 'Fatal script error.');
break;
}
2017-11-16 18:44:38 -08:00
stack.pop();
stack.pop();
stack.pushNum(num);
2017-11-16 18:44:38 -08:00
if (op.value === opcodes.OP_NUMEQUALVERIFY) {
if (!stack.getBool(-1))
throw new ScriptError('NUMEQUALVERIFY', op, ip);
stack.pop();
}
2017-11-16 18:44:38 -08:00
break;
}
case opcodes.OP_WITHIN: {
if (stack.length < 3)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
2017-11-16 18:44:38 -08:00
const n1 = stack.getNum(-3, minimal, 4);
const n2 = stack.getNum(-2, minimal, 4);
const n3 = stack.getNum(-1, minimal, 4);
2017-11-16 18:44:38 -08:00
const val = n2.lte(n1) && n1.lt(n3);
stack.pop();
2017-11-16 18:44:38 -08:00
stack.pop();
stack.pop();
2017-11-16 18:44:38 -08:00
stack.pushBool(val);
break;
2016-03-14 20:43:32 -07:00
}
2017-11-16 18:44:38 -08:00
case opcodes.OP_RIPEMD160: {
if (stack.length === 0)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
2017-11-16 18:44:38 -08:00
stack.push(ripemd160.digest(stack.pop()));
break;
2016-03-14 20:43:32 -07:00
}
2017-11-16 18:44:38 -08:00
case opcodes.OP_SHA1: {
if (stack.length === 0)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
stack.push(sha1.digest(stack.pop()));
break;
}
2017-11-16 18:44:38 -08:00
case opcodes.OP_SHA256: {
if (stack.length === 0)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
2017-11-16 18:44:38 -08:00
stack.push(sha256.digest(stack.pop()));
break;
}
case opcodes.OP_HASH160: {
if (stack.length === 0)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
2017-11-16 18:44:38 -08:00
stack.push(hash160.digest(stack.pop()));
break;
}
case opcodes.OP_HASH256: {
if (stack.length === 0)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
2017-11-16 18:44:38 -08:00
stack.push(hash256.digest(stack.pop()));
break;
}
case opcodes.OP_BLAKE160: {
if (stack.length === 0)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
stack.push(blake2b.digest(stack.pop(), 20));
break;
}
case opcodes.OP_BLAKE256: {
2018-01-02 20:24:56 -08:00
if (stack.length === 0)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
stack.push(blake2b.digest(stack.pop()));
break;
}
case opcodes.OP_SHA3: {
if (stack.length === 0)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
stack.push(sha3.digest(stack.pop()));
break;
}
case opcodes.OP_KECCAK: {
if (stack.length === 0)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
stack.push(keccak.digest(stack.pop()));
break;
}
2017-11-16 18:44:38 -08:00
case opcodes.OP_CODESEPARATOR: {
lastSep = ip + 1;
break;
}
case opcodes.OP_CHECKSIG:
case opcodes.OP_CHECKSIGVERIFY: {
if (!tx)
throw new ScriptError('UNKNOWN_ERROR', 'No TX passed in.');
2017-11-16 18:44:38 -08:00
if (stack.length < 2)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
2017-11-16 18:44:38 -08:00
const sig = stack.get(-2);
const key = stack.get(-1);
2017-11-16 18:44:38 -08:00
const subscript = this.getSubscript(lastSep);
2017-11-16 18:44:38 -08:00
validateSignature(sig, flags);
2018-01-02 20:24:56 -08:00
validateKey(key, flags);
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
let res = false;
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
if (sig.length > 0) {
const type = sig[sig.length - 1];
2017-11-16 20:40:01 -08:00
const hash = tx.signatureHash(
index,
subscript,
value,
2018-01-02 20:24:56 -08:00
type
2017-11-16 20:40:01 -08:00
);
2017-11-16 18:44:38 -08:00
res = checksig(hash, sig, key);
}
2017-11-16 18:44:38 -08:00
if (!res && (flags & Script.flags.VERIFY_NULLFAIL)) {
if (sig.length !== 0)
throw new ScriptError('NULLFAIL', op, ip);
}
2016-04-18 19:19:06 -07:00
2017-11-16 18:44:38 -08:00
stack.pop();
stack.pop();
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
stack.pushBool(res);
2017-11-16 18:44:38 -08:00
if (op.value === opcodes.OP_CHECKSIGVERIFY) {
if (!res)
throw new ScriptError('CHECKSIGVERIFY', op, ip);
stack.pop();
}
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
break;
2016-09-18 11:22:10 -07:00
}
2017-11-16 18:44:38 -08:00
case opcodes.OP_CHECKMULTISIG:
case opcodes.OP_CHECKMULTISIGVERIFY: {
if (!tx)
throw new ScriptError('UNKNOWN_ERROR', 'No TX passed in.');
2016-09-18 11:22:10 -07:00
2017-11-16 18:44:38 -08:00
let i = 1;
if (stack.length < i)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
2016-10-03 14:16:07 -07:00
2017-11-16 18:44:38 -08:00
let n = stack.getInt(-i, minimal, 4);
let okey = n + 2;
let ikey, isig;
2017-11-16 18:44:38 -08:00
if (n < 0 || n > consensus.MAX_MULTISIG_PUBKEYS)
throw new ScriptError('PUBKEY_COUNT', op, ip);
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
opCount += n;
2016-03-27 18:54:13 -07:00
2017-11-16 18:44:38 -08:00
if (opCount > consensus.MAX_SCRIPT_OPS)
throw new ScriptError('OP_COUNT', op, ip);
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
i += 1;
ikey = i;
i += n;
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
if (stack.length < i)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
let m = stack.getInt(-i, minimal, 4);
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
if (m < 0 || m > n)
throw new ScriptError('SIG_COUNT', op, ip);
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
i += 1;
isig = i;
i += m;
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
if (stack.length < i)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
const subscript = this.getSubscript(lastSep);
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
let res = true;
while (res && m > 0) {
const sig = stack.get(-isig);
const key = stack.get(-ikey);
validateSignature(sig, flags);
2018-01-02 20:24:56 -08:00
validateKey(key, flags);
2017-11-16 18:44:38 -08:00
if (sig.length > 0) {
const type = sig[sig.length - 1];
const hash = tx.signatureHash(
index,
subscript,
value,
2018-01-02 20:24:56 -08:00
type
2017-11-16 18:44:38 -08:00
);
if (checksig(hash, sig, key)) {
isig += 1;
m -= 1;
}
}
2016-04-18 19:19:06 -07:00
2017-11-16 18:44:38 -08:00
ikey += 1;
n -= 1;
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
if (m > n)
res = false;
}
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
while (i > 1) {
if (!res && (flags & Script.flags.VERIFY_NULLFAIL)) {
if (okey === 0 && stack.get(-1).length !== 0)
throw new ScriptError('NULLFAIL', op, ip);
}
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
if (okey > 0)
okey -= 1;
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
stack.pop();
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
i -= 1;
2016-04-18 19:19:06 -07:00
}
2017-11-16 18:44:38 -08:00
if (stack.length < 1)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
2016-03-14 20:43:32 -07:00
2018-01-02 20:24:56 -08:00
if (stack.get(-1).length !== 0)
throw new ScriptError('SIG_NULLDUMMY', op, ip);
2016-04-18 19:19:06 -07:00
stack.pop();
2017-11-16 18:44:38 -08:00
stack.pushBool(res);
2016-04-18 19:19:06 -07:00
2017-11-16 18:44:38 -08:00
if (op.value === opcodes.OP_CHECKMULTISIGVERIFY) {
if (!res)
throw new ScriptError('CHECKMULTISIGVERIFY', op, ip);
stack.pop();
}
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
break;
}
default: {
throw new ScriptError('BAD_OPCODE', op, ip);
2016-03-14 20:43:32 -07:00
}
2017-11-16 18:44:38 -08:00
}
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
if (stack.length + alt.length > consensus.MAX_SCRIPT_STACK)
throw new ScriptError('STACK_SIZE', op, ip);
}
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
if (state.length !== 0)
throw new ScriptError('UNBALANCED_CONDITIONAL');
}
2017-11-16 18:44:38 -08:00
/**
* Find a data element in a script.
* @param {Buffer} data - Data element to match against.
* @returns {Number} Index (`-1` if not present).
*/
2016-05-04 01:06:34 -07:00
2017-11-16 18:44:38 -08:00
indexOf(data) {
for (let i = 0; i < this.code.length; i++) {
const op = this.code[i];
2016-05-15 23:51:18 -07:00
2017-11-16 18:44:38 -08:00
if (op.value === -1)
break;
2016-05-15 23:51:18 -07:00
2017-11-16 18:44:38 -08:00
if (!op.data)
continue;
2016-05-15 23:51:18 -07:00
2017-11-16 18:44:38 -08:00
if (op.data.equals(data))
return i;
}
2016-05-15 23:51:18 -07:00
2017-11-16 18:44:38 -08:00
return -1;
2016-05-15 23:51:18 -07:00
}
2017-11-16 18:44:38 -08:00
/**
* Test a script to see if it is likely
* to be script code (no weird opcodes).
* @returns {Boolean}
*/
2016-04-17 19:34:03 -07:00
2017-11-16 18:44:38 -08:00
isCode() {
for (const op of this.code) {
if (op.value === -1)
return false;
2017-11-16 18:44:38 -08:00
if (op.isDisabled())
return false;
2016-06-14 16:26:26 -07:00
2017-11-16 18:44:38 -08:00
switch (op.value) {
case opcodes.OP_RESERVED:
case opcodes.OP_NOP:
case opcodes.OP_VER:
case opcodes.OP_VERIF:
case opcodes.OP_VERNOTIF:
case opcodes.OP_RESERVED1:
case opcodes.OP_RESERVED2:
return false;
}
2017-08-24 23:46:33 -07:00
2017-11-16 18:44:38 -08:00
if (op.value > opcodes.OP_CHECKSEQUENCEVERIFY)
2017-08-24 23:46:33 -07:00
return false;
}
2017-11-16 18:44:38 -08:00
return true;
2016-03-14 20:43:32 -07:00
}
2017-11-16 18:44:38 -08:00
/**
* Inject properties from a pay-to-pubkey script.
* @param {Buffer} key
* @returns {this}
2017-11-16 18:44:38 -08:00
*/
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
fromPubkey(key) {
2018-01-03 19:21:54 -08:00
assert(Buffer.isBuffer(key) && key.length === 33);
2018-01-03 19:21:54 -08:00
this.raw = Buffer.allocUnsafe(35);
this.raw[0] = 33;
2017-11-16 18:44:38 -08:00
key.copy(this.raw, 1);
2018-01-03 19:21:54 -08:00
this.raw[34] = opcodes.OP_CHECKSIG;
2016-03-14 20:43:32 -07:00
2018-01-03 19:21:54 -08:00
key = this.raw.slice(1, 34);
2017-11-16 18:44:38 -08:00
this.code.length = 0;
this.code.push(Opcode.fromPush(key));
this.code.push(Opcode.fromOp(opcodes.OP_CHECKSIG));
2017-11-16 18:44:38 -08:00
return this;
}
2017-11-16 18:44:38 -08:00
/**
* Create a pay-to-pubkey script.
* @param {Buffer} key
* @returns {Script}
*/
2017-11-16 18:44:38 -08:00
static fromPubkey(key) {
return new this().fromPubkey(key);
}
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
/**
* Inject properties from a pay-to-pubkeyhash script.
* @private
* @param {Buffer} hash
*/
2016-07-01 05:42:04 -07:00
2017-11-16 18:44:38 -08:00
fromPubkeyhash(hash) {
assert(Buffer.isBuffer(hash) && hash.length === 20);
2016-07-01 05:42:04 -07:00
2017-11-16 18:44:38 -08:00
this.raw = Buffer.allocUnsafe(25);
this.raw[0] = opcodes.OP_DUP;
2018-03-11 18:16:46 -07:00
this.raw[1] = opcodes.OP_BLAKE160;
2017-11-16 18:44:38 -08:00
this.raw[2] = 0x14;
hash.copy(this.raw, 3);
this.raw[23] = opcodes.OP_EQUALVERIFY;
this.raw[24] = opcodes.OP_CHECKSIG;
2017-11-16 18:44:38 -08:00
hash = this.raw.slice(3, 23);
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
this.code.length = 0;
this.code.push(Opcode.fromOp(opcodes.OP_DUP));
2018-03-11 18:16:46 -07:00
this.code.push(Opcode.fromOp(opcodes.OP_BLAKE160));
2017-11-16 18:44:38 -08:00
this.code.push(Opcode.fromPush(hash));
this.code.push(Opcode.fromOp(opcodes.OP_EQUALVERIFY));
this.code.push(Opcode.fromOp(opcodes.OP_CHECKSIG));
2016-03-29 20:46:09 -07:00
2017-11-16 18:44:38 -08:00
return this;
}
2016-03-29 20:46:09 -07:00
2017-11-16 18:44:38 -08:00
/**
* Create a pay-to-pubkeyhash script.
* @param {Buffer} hash
* @returns {Script}
*/
2016-07-01 14:48:23 -07:00
2017-11-16 18:44:38 -08:00
static fromPubkeyhash(hash) {
return new this().fromPubkeyhash(hash);
}
2016-03-29 20:46:09 -07:00
2017-11-16 18:44:38 -08:00
/**
* Inject properties from pay-to-multisig script.
* @private
* @param {Number} m
* @param {Number} n
* @param {Buffer[]} keys
*/
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
fromMultisig(m, n, keys) {
assert((m & 0xff) === m && (n & 0xff) === n);
assert(Array.isArray(keys));
assert(keys.length === n, '`n` keys are required for multisig.');
assert(m >= 1 && m <= n);
assert(n >= 1 && n <= 15);
2017-11-16 18:44:38 -08:00
this.clear();
2016-07-01 05:42:04 -07:00
2017-11-16 18:44:38 -08:00
this.pushSmall(m);
2016-07-01 05:42:04 -07:00
2017-11-16 18:44:38 -08:00
for (const key of sortKeys(keys))
this.pushData(key);
2017-11-16 18:44:38 -08:00
this.pushSmall(n);
this.pushOp(opcodes.OP_CHECKMULTISIG);
2017-11-16 18:44:38 -08:00
return this.compile();
}
2017-11-16 18:44:38 -08:00
/**
* Create a pay-to-multisig script.
* @param {Number} m
* @param {Number} n
* @param {Buffer[]} keys
* @returns {Script}
*/
2017-11-16 18:44:38 -08:00
static fromMultisig(m, n, keys) {
return new this().fromMultisig(m, n, keys);
}
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
/**
* Get the standard script type.
* @returns {common.types}
2017-11-16 18:44:38 -08:00
*/
2016-07-02 04:53:42 -07:00
2017-11-16 18:44:38 -08:00
getType() {
if (this.isPubkey())
return scriptTypes.PUBKEY;
2016-07-02 04:53:42 -07:00
2017-11-16 18:44:38 -08:00
if (this.isPubkeyhash())
return scriptTypes.PUBKEYHASH;
2016-07-02 04:53:42 -07:00
2017-11-16 18:44:38 -08:00
if (this.isMultisig())
return scriptTypes.MULTISIG;
2017-11-16 18:44:38 -08:00
return scriptTypes.NONSTANDARD;
}
2016-07-02 04:53:42 -07:00
2017-11-16 18:44:38 -08:00
/**
* Test whether a script is of an unknown/non-standard type.
* @returns {Boolean}
*/
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
isUnknown() {
return this.getType() === scriptTypes.NONSTANDARD;
2016-07-02 04:53:42 -07:00
}
2017-11-16 18:44:38 -08:00
/**
* Test whether the script is standard by policy standards.
* @returns {Boolean}
*/
2017-11-16 18:44:38 -08:00
isStandard() {
const [m, n] = this.getMultisig();
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
if (m !== -1) {
if (n < 1 || n > 3)
return false;
2017-11-16 18:44:38 -08:00
if (m < 1 || m > n)
return false;
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
return true;
}
2017-11-16 18:44:38 -08:00
return this.getType() !== scriptTypes.NONSTANDARD;
}
2017-11-16 18:44:38 -08:00
/**
* Calculate the size of the script
* excluding the varint size bytes.
* @returns {Number}
*/
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
getSize() {
return this.raw.length;
}
2017-11-16 18:44:38 -08:00
/**
* Calculate the size of the script
* including the varint size bytes.
* @returns {Number}
*/
2016-03-26 03:49:45 -07:00
2017-11-16 18:44:38 -08:00
getVarSize() {
return encoding.sizeVarBytes(this.raw);
}
2016-08-18 18:27:17 -07:00
2017-11-16 18:44:38 -08:00
/**
* Get the sha3 of the raw script.
2017-11-16 18:44:38 -08:00
* @returns {Hash}
*/
sha3() {
return sha3.digest(this.encode());
2017-11-16 18:44:38 -08:00
}
2017-11-16 18:44:38 -08:00
/**
* Test whether the output script is pay-to-pubkey.
* @param {Boolean} [minimal=false] - Minimaldata only.
* @returns {Boolean}
*/
isPubkey(minimal) {
if (minimal) {
2018-01-03 19:21:54 -08:00
return this.raw.length === 35
&& this.raw[0] === 33
2017-11-16 18:44:38 -08:00
&& this.raw[0] + 2 === this.raw.length
&& this.raw[this.raw.length - 1] === opcodes.OP_CHECKSIG;
}
2017-11-16 18:44:38 -08:00
if (this.code.length !== 2)
return false;
2018-01-03 19:21:54 -08:00
return this.getLength(0) === 33
2017-11-16 18:44:38 -08:00
&& this.getOp(1) === opcodes.OP_CHECKSIG;
}
2017-11-16 18:44:38 -08:00
/**
* Get P2PK key if present.
* @param {Boolean} [minimal=false] - Minimaldata only.
* @returns {Buffer|null}
*/
2017-11-16 18:44:38 -08:00
getPubkey(minimal) {
if (!this.isPubkey(minimal))
return null;
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
if (minimal)
return this.raw.slice(1, 1 + this.raw[0]);
2017-11-16 18:44:38 -08:00
return this.getData(0);
2016-06-13 21:25:42 -07:00
}
2017-11-16 18:44:38 -08:00
/**
* Test whether the output script is pay-to-pubkeyhash.
* @param {Boolean} [minimal=false] - Minimaldata only.
* @returns {Boolean}
*/
isPubkeyhash(minimal) {
if (minimal || this.raw.length === 25) {
return this.raw.length === 25
&& this.raw[0] === opcodes.OP_DUP
2018-03-11 18:16:46 -07:00
&& this.raw[1] === opcodes.OP_BLAKE160
2017-11-16 18:44:38 -08:00
&& this.raw[2] === 0x14
&& this.raw[23] === opcodes.OP_EQUALVERIFY
&& this.raw[24] === opcodes.OP_CHECKSIG;
}
2017-11-16 18:44:38 -08:00
if (this.code.length !== 5)
return false;
2017-11-16 18:44:38 -08:00
return this.getOp(0) === opcodes.OP_DUP
2018-03-11 18:16:46 -07:00
&& this.getOp(1) === opcodes.OP_BLAKE160
2017-11-16 18:44:38 -08:00
&& this.getLength(2) === 20
&& this.getOp(3) === opcodes.OP_EQUALVERIFY
&& this.getOp(4) === opcodes.OP_CHECKSIG;
}
2017-11-16 18:44:38 -08:00
/**
* Get P2PKH hash if present.
* @param {Boolean} [minimal=false] - Minimaldata only.
* @returns {Buffer|null}
*/
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
getPubkeyhash(minimal) {
if (!this.isPubkeyhash(minimal))
return null;
2017-11-16 18:44:38 -08:00
if (minimal)
return this.raw.slice(3, 23);
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
return this.getData(2);
}
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
/**
* Test whether the output script is pay-to-multisig.
* @param {Boolean} [minimal=false] - Minimaldata only.
* @returns {Boolean}
*/
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
isMultisig(minimal) {
if (this.code.length < 4 || this.code.length > 19)
return false;
2016-04-18 19:19:06 -07:00
2017-11-16 18:44:38 -08:00
if (this.getOp(-1) !== opcodes.OP_CHECKMULTISIG)
return false;
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
const m = this.getSmall(0);
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
if (m < 1)
return false;
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
const n = this.getSmall(-2);
2016-07-01 15:09:33 -07:00
2017-11-16 18:44:38 -08:00
if (n < 1 || m > n)
2016-03-14 20:43:32 -07:00
return false;
2016-07-01 15:09:33 -07:00
2017-11-16 18:44:38 -08:00
if (this.code.length !== n + 3)
2017-07-03 02:34:56 -07:00
return false;
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
for (let i = 1; i < n + 1; i++) {
const op = this.code[i];
2016-03-14 20:43:32 -07:00
2018-01-03 19:21:54 -08:00
if (op.toLength() !== 33)
2017-11-16 18:44:38 -08:00
return false;
2017-11-16 18:44:38 -08:00
if (minimal && !op.isMinimal())
return false;
}
2017-11-16 18:44:38 -08:00
return true;
}
2017-11-16 18:44:38 -08:00
/**
* Get multisig m and n values if present.
* @param {Boolean} [minimal=false] - Minimaldata only.
* @returns {Array} [m, n]
*/
2017-11-16 18:44:38 -08:00
getMultisig(minimal) {
if (!this.isMultisig(minimal))
return [-1, -1];
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
return [this.getSmall(0), this.getSmall(-2)];
}
2017-11-16 18:44:38 -08:00
/**
2018-01-02 20:24:56 -08:00
* Test whether the output script is unspendable.
2017-11-16 18:44:38 -08:00
* @returns {Boolean}
*/
2018-01-02 20:24:56 -08:00
isUnspendable() {
if (this.raw.length > consensus.MAX_SCRIPT_SIZE)
return true;
2018-01-02 20:24:56 -08:00
return this.raw.length > 0 && this.raw[0] === opcodes.OP_RETURN;
2017-11-16 18:44:38 -08:00
}
2016-04-17 19:34:03 -07:00
2017-11-16 18:44:38 -08:00
/**
2018-01-02 20:24:56 -08:00
* Test the script against a bloom filter.
* @param {BloomFilter} filter
2017-11-16 18:44:38 -08:00
* @returns {Boolean}
*/
2016-04-17 19:34:03 -07:00
2018-01-02 20:24:56 -08:00
test(filter) {
for (const op of this.code) {
if (op.value === -1)
break;
2016-06-13 21:25:42 -07:00
2018-01-02 20:24:56 -08:00
if (!op.data || op.data.length === 0)
continue;
2016-07-02 04:53:42 -07:00
2018-01-02 20:24:56 -08:00
if (filter.test(op.data))
return true;
2017-11-16 18:44:38 -08:00
}
2016-07-02 04:53:42 -07:00
2018-01-02 20:24:56 -08:00
return false;
}
/**
* Test the script to see if it contains only push ops.
* Push ops are: OP_1NEGATE, OP_0-OP_16 and all PUSHDATAs.
* @returns {Boolean}
*/
2018-01-02 20:24:56 -08:00
isPushOnly() {
for (const op of this.code) {
2017-11-16 18:44:38 -08:00
if (op.value === -1)
return false;
if (op.value > opcodes.OP_16)
return false;
}
return true;
2016-07-01 15:09:33 -07:00
}
2016-04-17 19:34:03 -07:00
2017-11-16 18:44:38 -08:00
/**
2018-01-02 20:24:56 -08:00
* Count the sigops in the script.
* @returns {Number} sigop count
2017-11-16 18:44:38 -08:00
*/
2016-03-14 20:43:32 -07:00
2018-01-02 20:24:56 -08:00
getSigops() {
let total = 0;
let lastOp = -1;
2018-01-02 20:24:56 -08:00
for (const op of this.code) {
if (op.value === -1)
break;
2017-11-16 18:44:38 -08:00
switch (op.value) {
case opcodes.OP_CHECKSIG:
case opcodes.OP_CHECKSIGVERIFY:
total += 1;
break;
case opcodes.OP_CHECKMULTISIG:
case opcodes.OP_CHECKMULTISIGVERIFY:
2018-01-02 20:24:56 -08:00
if (lastOp >= opcodes.OP_1 && lastOp <= opcodes.OP_16)
2017-11-16 18:44:38 -08:00
total += lastOp - 0x50;
else
total += consensus.MAX_MULTISIG_PUBKEYS;
break;
}
2017-11-16 18:44:38 -08:00
lastOp = op.value;
}
2017-11-16 18:44:38 -08:00
return total;
}
2017-11-16 18:44:38 -08:00
/*
* Mutation
*/
2017-11-16 18:44:38 -08:00
get(index) {
if (index < 0)
index += this.code.length;
if (index < 0 || index >= this.code.length)
return null;
return this.code[index];
}
2017-11-16 18:44:38 -08:00
pop() {
const op = this.code.pop();
return op || null;
}
2016-06-14 16:26:26 -07:00
2017-11-16 18:44:38 -08:00
shift() {
const op = this.code.shift();
return op || null;
}
2016-06-14 18:13:51 -07:00
2017-11-16 18:44:38 -08:00
remove(index) {
if (index < 0)
index += this.code.length;
2017-11-16 18:44:38 -08:00
if (index < 0 || index >= this.code.length)
return null;
2017-11-16 18:44:38 -08:00
const items = this.code.splice(index, 1);
2016-03-25 02:07:47 -07:00
2017-11-16 18:44:38 -08:00
if (items.length === 0)
return null;
2016-06-14 18:13:51 -07:00
2017-11-16 18:44:38 -08:00
return items[0];
}
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
set(index, op) {
if (index < 0)
index += this.code.length;
2017-11-16 18:44:38 -08:00
assert(Opcode.isOpcode(op));
assert(index >= 0 && index <= this.code.length);
2016-06-14 18:13:51 -07:00
2017-11-16 18:44:38 -08:00
this.code[index] = op;
2017-11-16 18:44:38 -08:00
return this;
}
2017-11-16 18:44:38 -08:00
push(op) {
assert(Opcode.isOpcode(op));
this.code.push(op);
return this;
}
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
unshift(op) {
assert(Opcode.isOpcode(op));
this.code.unshift(op);
return this;
}
2016-06-14 18:13:51 -07:00
2017-11-16 18:44:38 -08:00
insert(index, op) {
if (index < 0)
index += this.code.length;
2017-11-16 18:44:38 -08:00
assert(Opcode.isOpcode(op));
assert(index >= 0 && index <= this.code.length);
2017-11-16 18:44:38 -08:00
this.code.splice(index, 0, op);
2016-06-14 18:13:51 -07:00
2017-11-16 18:44:38 -08:00
return this;
}
2017-11-16 18:44:38 -08:00
/*
* Op
*/
2016-06-14 18:13:51 -07:00
2017-11-16 18:44:38 -08:00
getOp(index) {
const op = this.get(index);
return op ? op.value : -1;
}
2016-06-14 18:13:51 -07:00
2017-11-16 18:44:38 -08:00
popOp() {
const op = this.pop();
return op ? op.value : -1;
}
2016-06-14 18:13:51 -07:00
2017-11-16 18:44:38 -08:00
shiftOp() {
const op = this.shift();
return op ? op.value : -1;
}
2016-06-14 18:13:51 -07:00
2017-11-16 18:44:38 -08:00
removeOp(index) {
const op = this.remove(index);
return op ? op.value : -1;
}
2016-06-14 16:26:26 -07:00
2017-11-16 18:44:38 -08:00
setOp(index, value) {
return this.set(index, Opcode.fromOp(value));
}
2016-06-14 18:13:51 -07:00
2017-11-16 18:44:38 -08:00
pushOp(value) {
return this.push(Opcode.fromOp(value));
}
2017-11-16 18:44:38 -08:00
unshiftOp(value) {
return this.unshift(Opcode.fromOp(value));
}
2017-11-16 18:44:38 -08:00
insertOp(index, value) {
return this.insert(index, Opcode.fromOp(value));
}
2016-06-14 16:26:26 -07:00
2017-11-16 18:44:38 -08:00
/*
* Data
*/
2016-07-01 05:42:04 -07:00
2017-11-16 18:44:38 -08:00
getData(index) {
const op = this.get(index);
return op ? op.data : null;
}
2016-07-01 05:42:04 -07:00
2017-11-16 18:44:38 -08:00
popData() {
const op = this.pop();
return op ? op.data : null;
}
2017-11-16 18:44:38 -08:00
shiftData() {
const op = this.shift();
return op ? op.data : null;
}
2017-11-16 18:44:38 -08:00
removeData(index) {
const op = this.remove(index);
return op ? op.data : null;
}
2017-11-16 18:44:38 -08:00
setData(index, data) {
return this.set(index, Opcode.fromData(data));
}
2016-06-14 18:13:51 -07:00
2017-11-16 18:44:38 -08:00
pushData(data) {
return this.push(Opcode.fromData(data));
}
2016-06-14 16:26:26 -07:00
2017-11-16 18:44:38 -08:00
unshiftData(data) {
return this.unshift(Opcode.fromData(data));
}
2017-11-16 18:44:38 -08:00
insertData(index, data) {
return this.insert(index, Opcode.fromData(data));
}
2017-11-16 18:44:38 -08:00
/*
* Length
*/
2017-11-16 18:44:38 -08:00
getLength(index) {
const op = this.get(index);
return op ? op.toLength() : -1;
}
2017-11-16 18:44:38 -08:00
/*
* Push
*/
2017-11-16 18:44:38 -08:00
getPush(index) {
const op = this.get(index);
return op ? op.toPush() : null;
}
2017-11-16 18:44:38 -08:00
popPush() {
const op = this.pop();
return op ? op.toPush() : null;
}
2017-11-16 18:44:38 -08:00
shiftPush() {
const op = this.shift();
return op ? op.toPush() : null;
}
2017-11-16 18:44:38 -08:00
removePush(index) {
const op = this.remove(index);
return op ? op.toPush() : null;
}
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
setPush(index, data) {
return this.set(index, Opcode.fromPush(data));
}
2017-11-16 18:44:38 -08:00
pushPush(data) {
return this.push(Opcode.fromPush(data));
}
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
unshiftPush(data) {
return this.unshift(Opcode.fromPush(data));
}
2017-11-16 18:44:38 -08:00
insertPush(index, data) {
return this.insert(index, Opcode.fromPush(data));
}
2017-11-16 18:44:38 -08:00
/*
* String
*/
2017-11-16 18:44:38 -08:00
getString(index, enc) {
const op = this.get(index);
return op ? op.toString(enc) : null;
}
2017-11-16 18:44:38 -08:00
popString(enc) {
const op = this.pop();
return op ? op.toString(enc) : null;
}
2017-11-16 18:44:38 -08:00
shiftString(enc) {
const op = this.shift();
return op ? op.toString(enc) : null;
}
2017-11-16 18:44:38 -08:00
removeString(index, enc) {
const op = this.remove(index);
return op ? op.toString(enc) : null;
}
2017-11-16 18:44:38 -08:00
setString(index, str, enc) {
return this.set(index, Opcode.fromString(str, enc));
}
2017-11-16 18:44:38 -08:00
pushString(str, enc) {
return this.push(Opcode.fromString(str, enc));
}
2016-06-14 16:26:26 -07:00
2017-11-16 18:44:38 -08:00
unshiftString(str, enc) {
return this.unshift(Opcode.fromString(str, enc));
}
2016-06-14 16:26:26 -07:00
2017-11-16 18:44:38 -08:00
insertString(index, str, enc) {
return this.insert(index, Opcode.fromString(str, enc));
}
2016-06-14 16:26:26 -07:00
2017-11-16 18:44:38 -08:00
/*
* Small
*/
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
getSmall(index) {
const op = this.get(index);
return op ? op.toSmall() : -1;
}
2017-11-16 18:44:38 -08:00
popSmall() {
const op = this.pop();
return op ? op.toSmall() : -1;
}
2017-11-16 18:44:38 -08:00
shiftSmall() {
const op = this.shift();
return op ? op.toSmall() : -1;
}
2017-11-16 18:44:38 -08:00
removeSmall(index) {
const op = this.remove(index);
return op ? op.toSmall() : -1;
}
2017-11-16 18:44:38 -08:00
setSmall(index, num) {
return this.set(index, Opcode.fromSmall(num));
}
2017-11-16 18:44:38 -08:00
pushSmall(num) {
return this.push(Opcode.fromSmall(num));
}
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
unshiftSmall(num) {
return this.unshift(Opcode.fromSmall(num));
}
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
insertSmall(index, num) {
return this.insert(index, Opcode.fromSmall(num));
}
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
/*
* Num
*/
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
getNum(index, minimal, limit) {
const op = this.get(index);
return op ? op.toNum(minimal, limit) : null;
}
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
popNum(minimal, limit) {
const op = this.pop();
return op ? op.toNum(minimal, limit) : null;
}
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
shiftNum(minimal, limit) {
const op = this.shift();
return op ? op.toNum(minimal, limit) : null;
}
2017-11-16 18:44:38 -08:00
removeNum(index, minimal, limit) {
const op = this.remove(index);
return op ? op.toNum(minimal, limit) : null;
}
2017-11-16 18:44:38 -08:00
setNum(index, num) {
return this.set(index, Opcode.fromNum(num));
}
2016-04-27 14:44:58 -07:00
2017-11-16 18:44:38 -08:00
pushNum(num) {
return this.push(Opcode.fromNum(num));
}
2016-04-27 14:44:58 -07:00
2017-11-16 18:44:38 -08:00
unshiftNum(num) {
return this.unshift(Opcode.fromNum(num));
}
2016-12-11 10:08:46 -08:00
2017-11-16 18:44:38 -08:00
insertNum(index, num) {
return this.insert(index, Opcode.fromNum(num));
}
2017-11-16 18:44:38 -08:00
/*
* Int
*/
2016-06-14 16:26:26 -07:00
2017-11-16 18:44:38 -08:00
getInt(index, minimal, limit) {
const op = this.get(index);
return op ? op.toInt(minimal, limit) : -1;
}
2016-06-14 16:26:26 -07:00
2017-11-16 18:44:38 -08:00
popInt(minimal, limit) {
const op = this.pop();
return op ? op.toInt(minimal, limit) : -1;
}
2016-04-27 14:44:58 -07:00
2017-11-16 18:44:38 -08:00
shiftInt(minimal, limit) {
const op = this.shift();
return op ? op.toInt(minimal, limit) : -1;
}
2016-04-27 14:44:58 -07:00
2017-11-16 18:44:38 -08:00
removeInt(index, minimal, limit) {
const op = this.remove(index);
return op ? op.toInt(minimal, limit) : -1;
}
2016-04-27 14:44:58 -07:00
2017-11-16 18:44:38 -08:00
setInt(index, num) {
return this.set(index, Opcode.fromInt(num));
}
2017-11-16 18:44:38 -08:00
pushInt(num) {
return this.push(Opcode.fromInt(num));
}
2016-04-27 14:44:58 -07:00
2017-11-16 18:44:38 -08:00
unshiftInt(num) {
return this.unshift(Opcode.fromInt(num));
}
2017-11-16 18:44:38 -08:00
insertInt(index, num) {
return this.insert(index, Opcode.fromInt(num));
}
2017-11-16 18:44:38 -08:00
/*
* Bool
*/
2017-11-16 18:44:38 -08:00
getBool(index) {
const op = this.get(index);
return op ? op.toBool() : false;
}
2017-11-16 18:44:38 -08:00
popBool() {
const op = this.pop();
return op ? op.toBool() : false;
}
2017-11-16 18:44:38 -08:00
shiftBool() {
const op = this.shift();
return op ? op.toBool() : false;
}
2017-11-16 18:44:38 -08:00
removeBool(index) {
const op = this.remove(index);
return op ? op.toBool() : false;
}
2017-11-16 18:44:38 -08:00
setBool(index, value) {
return this.set(index, Opcode.fromBool(value));
}
2016-04-27 14:44:58 -07:00
2017-11-16 18:44:38 -08:00
pushBool(value) {
return this.push(Opcode.fromBool(value));
}
2016-04-27 14:44:58 -07:00
2017-11-16 18:44:38 -08:00
unshiftBool(value) {
return this.unshift(Opcode.fromBool(value));
}
2016-04-27 14:44:58 -07:00
2017-11-16 18:44:38 -08:00
insertBool(index, value) {
return this.insert(index, Opcode.fromBool(value));
}
2016-04-27 14:44:58 -07:00
2017-11-16 18:44:38 -08:00
/*
* Symbol
*/
2017-11-16 18:44:38 -08:00
getSym(index) {
const op = this.get(index);
return op ? op.toSymbol() : null;
}
2017-11-16 18:44:38 -08:00
popSym() {
const op = this.pop();
return op ? op.toSymbol() : null;
}
2017-11-16 18:44:38 -08:00
shiftSym() {
const op = this.shift();
return op ? op.toSymbol() : null;
}
2017-11-16 18:44:38 -08:00
removeSym(index) {
const op = this.remove(index);
return op ? op.toSymbol() : null;
}
2017-11-16 18:44:38 -08:00
setSym(index, symbol) {
return this.set(index, Opcode.fromSymbol(symbol));
}
2016-04-27 14:44:58 -07:00
2017-11-16 18:44:38 -08:00
pushSym(symbol) {
return this.push(Opcode.fromSymbol(symbol));
}
2016-04-27 14:44:58 -07:00
2017-11-16 18:44:38 -08:00
unshiftSym(symbol) {
return this.unshift(Opcode.fromSymbol(symbol));
}
2017-11-16 18:44:38 -08:00
insertSym(index, symbol) {
return this.insert(index, Opcode.fromSymbol(symbol));
}
2017-11-16 18:44:38 -08:00
/**
* Inject properties from bitcoind test string.
* @param {String} code - Script string.
2017-11-16 18:44:38 -08:00
* @throws Parse error.
*/
2017-11-16 18:44:38 -08:00
fromString(code) {
assert(typeof code === 'string');
2017-11-16 18:44:38 -08:00
code = code.trim();
2017-11-16 18:44:38 -08:00
if (code.length === 0)
return this;
2017-11-16 18:44:38 -08:00
const items = code.split(/\s+/);
const bw = bio.write();
2016-04-27 14:44:58 -07:00
2017-11-16 18:44:38 -08:00
for (const item of items) {
let symbol = item;
2016-04-19 01:30:12 -07:00
2017-11-16 18:44:38 -08:00
if (symbol.charCodeAt(0) & 32)
symbol = symbol.toUpperCase();
2016-04-19 01:30:12 -07:00
2017-11-16 18:44:38 -08:00
if (!/^OP_/.test(symbol))
symbol = `OP_${symbol}`;
2016-04-19 01:30:12 -07:00
2017-11-16 18:44:38 -08:00
const value = opcodes[symbol];
2016-04-19 01:30:12 -07:00
2017-11-16 18:44:38 -08:00
if (value == null) {
if (item[0] === '\'') {
assert(item[item.length - 1] === '\'', 'Invalid string.');
const str = item.slice(1, -1);
const op = Opcode.fromString(str);
bw.writeBytes(op.encode());
2017-11-16 18:44:38 -08:00
continue;
}
2016-04-19 01:30:12 -07:00
2017-11-16 18:44:38 -08:00
if (/^-?\d+$/.test(item)) {
const num = ScriptNum.fromString(item, 10);
const op = Opcode.fromNum(num);
bw.writeBytes(op.encode());
2017-11-16 18:44:38 -08:00
continue;
}
2016-04-19 01:30:12 -07:00
2017-11-16 18:44:38 -08:00
assert(item.indexOf('0x') === 0, 'Unknown opcode.');
2016-12-10 21:20:22 -08:00
2017-11-16 18:44:38 -08:00
const hex = item.substring(2);
const data = Buffer.from(hex, 'hex');
2016-04-19 01:30:12 -07:00
2017-11-16 18:44:38 -08:00
assert(data.length === hex.length / 2, 'Invalid hex string.');
2017-11-16 18:44:38 -08:00
bw.writeBytes(data);
2016-04-19 01:30:12 -07:00
continue;
}
2017-11-16 18:44:38 -08:00
bw.writeU8(value);
}
return this.decode(bw.render());
2017-11-16 18:44:38 -08:00
}
2017-11-16 18:44:38 -08:00
/**
* Verify an input and output script, and a witness if present.
* @param {Witness} witness
2018-01-02 20:24:56 -08:00
* @param {Address} addr
2017-11-16 18:44:38 -08:00
* @param {TX} tx
* @param {Number} index
* @param {AmountValue} value
2017-11-16 18:44:38 -08:00
* @param {VerifyFlags} flags
* @throws {ScriptError}
*/
2018-01-02 20:24:56 -08:00
static verify(witness, addr, tx, index, value, flags) {
2017-11-16 18:44:38 -08:00
if (flags == null)
flags = Script.flags.STANDARD_VERIFY_FLAGS;
assert(addr.version <= 31);
if (addr.version === 31)
throw new ScriptError('OP_RETURN');
2018-08-01 04:40:59 -07:00
if (witness.items.length > consensus.MAX_SCRIPT_STACK)
throw new ScriptError('STACK_SIZE');
2017-11-16 18:44:38 -08:00
const stack = witness.toStack();
2018-01-02 20:24:56 -08:00
let redeem = null;
if (addr.version === 0) {
if (addr.hash.length === 32) {
2017-11-16 18:44:38 -08:00
if (stack.length === 0)
throw new ScriptError('WITNESS_PROGRAM_WITNESS_EMPTY');
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
const witnessScript = stack.pop();
2016-03-14 20:43:32 -07:00
2018-08-01 04:40:59 -07:00
if (witnessScript.length > consensus.MAX_SCRIPT_SIZE)
throw new ScriptError('SCRIPT_SIZE');
if (!sha3.digest(witnessScript).equals(addr.hash))
2017-11-16 18:44:38 -08:00
throw new ScriptError('WITNESS_PROGRAM_MISMATCH');
2016-03-14 20:43:32 -07:00
redeem = Script.decode(witnessScript);
2018-01-02 20:24:56 -08:00
} else if (addr.hash.length === 20) {
2017-11-16 18:44:38 -08:00
if (stack.length !== 2)
throw new ScriptError('WITNESS_PROGRAM_MISMATCH');
2016-03-14 20:43:32 -07:00
2018-01-02 20:24:56 -08:00
redeem = Script.fromPubkeyhash(addr.hash);
2017-11-16 18:44:38 -08:00
} else {
// Failure on version=0 (bad program data length).
throw new ScriptError('WITNESS_PROGRAM_WRONG_LENGTH');
}
} else {
if (flags & Script.flags.VERIFY_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM)
throw new ScriptError('DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM');
return;
2016-03-14 20:43:32 -07:00
}
2017-11-16 18:44:38 -08:00
// Witnesses still have push limits.
for (let j = 0; j < stack.length; j++) {
if (stack.get(j).length > consensus.MAX_SCRIPT_PUSH)
throw new ScriptError('PUSH_SIZE');
}
2016-03-14 20:43:32 -07:00
2017-11-16 18:44:38 -08:00
// Verify the redeem script.
2018-01-02 20:24:56 -08:00
redeem.execute(stack, flags, tx, index, value);
2017-11-16 18:44:38 -08:00
// Verify the stack values.
if (stack.length !== 1 || !stack.getBool(-1))
throw new ScriptError('EVAL_FALSE');
2016-03-14 20:43:32 -07:00
}
2017-11-16 18:44:38 -08:00
/**
* Inject properties from buffer reader.
* @param {bio.BufferReader} br
2017-11-16 18:44:38 -08:00
*/
read(br) {
return this.decode(br.readVarBytes());
2016-09-18 14:15:46 -07:00
}
2017-11-16 18:44:38 -08:00
/**
* Inject properties from serialized data.
* @param {Buffer} data
* @returns {this}
2017-11-16 18:44:38 -08:00
*/
2016-09-18 14:15:46 -07:00
decode(data) {
const br = bio.read(data);
2016-09-18 14:15:46 -07:00
2017-11-16 18:44:38 -08:00
this.raw = data;
2016-09-18 14:15:46 -07:00
2017-11-16 18:44:38 -08:00
while (br.left())
this.code.push(Opcode.read(br));
2016-09-18 14:15:46 -07:00
2017-11-16 18:44:38 -08:00
return this;
}
2016-09-18 14:15:46 -07:00
2017-11-16 18:44:38 -08:00
/**
* Test whether an object a Script.
* @param {Object} obj
* @returns {Boolean}
*/
static isScript(obj) {
return obj instanceof Script;
}
2017-11-16 18:44:38 -08:00
}
2016-09-18 14:15:46 -07:00
2016-12-03 18:02:10 -08:00
/**
2017-11-16 18:44:38 -08:00
* Script opcodes.
* @enum {Number}
* @default
2016-12-03 18:02:10 -08:00
*/
2017-11-16 18:44:38 -08:00
Script.opcodes = common.opcodes;
2016-12-03 18:02:10 -08:00
/**
2017-11-16 18:44:38 -08:00
* Opcodes by value.
* @const {RevMap}
*/
2017-11-16 18:44:38 -08:00
Script.opcodesByVal = common.opcodesByVal;
/**
* Script and locktime flags. See {@link VerifyFlags}.
* @enum {Number}
*/
2016-04-19 11:39:55 -07:00
2017-11-16 18:44:38 -08:00
Script.flags = common.flags;
2016-12-03 18:02:10 -08:00
2017-11-16 18:44:38 -08:00
/**
* Sighash Types.
* @enum {SighashType}
* @default
*/
2016-04-19 11:39:55 -07:00
2017-11-16 18:44:38 -08:00
Script.hashType = common.hashType;
2016-04-19 11:39:55 -07:00
2016-12-03 18:02:10 -08:00
/**
2017-11-16 18:44:38 -08:00
* Sighash types by value.
* @const {RevMap}
2016-12-03 18:02:10 -08:00
*/
2017-11-16 18:44:38 -08:00
Script.hashTypeByVal = common.hashTypeByVal;
2016-12-03 18:02:10 -08:00
2016-06-20 01:09:27 -07:00
/**
2017-11-16 18:44:38 -08:00
* Output script types.
* @enum {Number}
2016-06-20 01:09:27 -07:00
*/
2017-11-16 18:44:38 -08:00
Script.types = common.types;
2016-03-31 02:55:19 -07:00
2016-06-14 16:26:26 -07:00
/**
2017-11-16 18:44:38 -08:00
* Output script types by value.
* @const {RevMap}
2016-06-14 16:26:26 -07:00
*/
2016-06-13 21:25:42 -07:00
2017-11-16 18:44:38 -08:00
Script.typesByVal = common.typesByVal;
2016-06-13 21:25:42 -07:00
2016-11-19 05:29:16 -08:00
/*
* Helpers
*/
function sortKeys(keys) {
2017-06-29 20:54:07 -07:00
return keys.slice().sort((a, b) => {
return a.compare(b);
2016-11-19 05:29:16 -08:00
});
}
/**
* Test whether the data element is a valid key if VERIFY_STRICTENC is enabled.
* @param {Buffer} key
* @param {VerifyFlags?} flags
* @returns {Boolean}
* @throws {ScriptError}
*/
2018-01-02 20:24:56 -08:00
function validateKey(key, flags) {
assert(Buffer.isBuffer(key));
assert(typeof flags === 'number');
2018-01-02 20:24:56 -08:00
if (!common.isKeyEncoding(key))
throw new ScriptError('PUBKEY_ENCODING');
return true;
}
/**
* Test whether the data element is a valid signature based
* on the encoding, S value, and sighash type. Requires
* VERIFY_DERSIG|VERIFY_LOW_S|VERIFY_STRICTENC, VERIFY_LOW_S
* and VERIFY_STRING_ENC to be enabled respectively. Note that
* this will allow zero-length signatures.
* @param {Buffer} sig
* @param {VerifyFlags?} flags
* @returns {Boolean}
* @throws {ScriptError}
*/
function validateSignature(sig, flags) {
assert(Buffer.isBuffer(sig));
assert(typeof flags === 'number');
// Allow empty sigs
if (sig.length === 0)
return true;
2018-01-02 20:24:56 -08:00
if (!common.isSignatureEncoding(sig))
throw new ScriptError('SIG_ENCODING');
return true;
}
2017-06-13 18:45:42 -07:00
/**
* Verify a signature, taking into account sighash type.
* @param {Buffer} msg - Signature hash.
* @param {Buffer} sig
* @param {Buffer} key
* @returns {Boolean}
*/
function checksig(msg, sig, key) {
2018-01-04 14:33:39 -08:00
return secp256k1.verify(msg, sig.slice(0, -1), key);
2017-06-13 18:45:42 -07:00
}
2016-05-15 18:07:06 -07:00
/*
* Expose
*/
2017-01-06 09:57:55 -08:00
module.exports = Script;