Class: Script

Script

new Script(code, mutable)

Represents a input or output script.

Parameters:
Name Type Description
code Buffer | Array | Object | NakedScript

Array of script code or a serialized script Buffer.

mutable Boolean

Whether the script will be changed in the future.

Properties:
Name Type Attributes Description
code Array

Script code.

raw Buffer <nullable>

Serialized script.

redeem Script <nullable>

Redeem script.

mutable Boolean
Source:

Methods

(static) array(value) → {Buffer}

Create a script array. Will convert Numbers and big numbers to a little-endian buffer while taking into account negative zero, minimaldata, etc.

Parameters:
Name Type Description
value Buffer | Number | BN
Source:
Returns:
Type
Buffer
Example
assert.deepEqual(Script.array(0), new Buffer([]));
assert.deepEqual(Script.array(0xffee), new Buffer([0xee, 0xff]));
assert.deepEqual(Script.array(new bn(0xffee)), new Buffer([0xee, 0xff]));

(static) bool(value) → {Boolean}

Cast a big number or Buffer to a bool.

Parameters:
Name Type Description
value BN | Buffer
Source:
See:
  • CastToBool
Returns:
Type
Boolean

(static) checkLocktime(locktime, tx, index) → {Boolean}

Verify the nLockTime of a transaction.

Parameters:
Name Type Description
locktime Number

Locktime to verify against (max=u32).

tx TX

Transaction to verify.

index Number

Index of input being verified (for IsFinal).

Source:
Returns:
Type
Boolean

(static) checkMinimal(value, flagsnullable) → {Boolean}

Check to see if a pushdata Buffer abides by minimaldata.

Parameters:
Name Type Attributes Description
value Buffer

Pushdata op from script code (must be from a deserialized script).

flags Number <nullable>
Source:
Returns:
Type
Boolean

(static) checkPush(value) → {Boolean}

Perform some range checking on the pushdatas (exactly what GetOp2 does). Note that this must be done during execution, not parsing.

Parameters:
Name Type Description
value Buffer

Pushdata op from script code (must be from a deserialized script).

Source:
See:
  • GetOp2
Returns:
Type
Boolean

(static) checkSequence(sequence, tx, index) → {Boolean}

Verify the nSequence locktime of a transaction.

Parameters:
Name Type Description
sequence Number

Locktime to verify against (max=u32).

tx TX

Transaction to verify.

index Number

Index of input being verified.

Source:
Returns:
Type
Boolean

(static) checksig(msg, sig, key, flagsnullable) → {Boolean}

Verify a signature, taking into account sighash type and whether the signature is historical.

Parameters:
Name Type Attributes Description
msg Buffer

Signature hash.

sig Buffer
key Buffer
flags VerifyFlags <nullable>

If none of VERIFY_DERSIG, VERIFY_LOW_S, or VERIFY_STRICTENC are enabled, the signature is treated as historical, allowing odd signature lengths and high S values.

Source:
Returns:
Type
Boolean

(static) concat(scripts) → {Array}

Concatenate scripts, inserting code separators in between them.

Parameters:
Name Type Description
scripts Array.<Script>
Source:
Returns:

code

Type
Array

(static) createCommitment(hash) → {Script}

Create a witness block commitment.

Parameters:
Name Type Description
hash Buffer
Source:
Returns:
Type
Script

(static) createMultisig(keys, m, n) → {Script}

Create a pay-to-multisig script.

Parameters:
Name Type Description
keys Array.<Buffer>
m Number
n Number
Source:
Returns:
Type
Script

(static) createNulldata(flags) → {Script}

Create a nulldata/opreturn script.

Parameters:
Name Type Description
flags Buffer
Source:
Returns:
Type
Script

(static) createOutputScript(options) → {Script}

Automatically build an output script from any number of options.

Parameters:
Name Type Description
options Object
Properties
Name Type Attributes Description
address Base58Address <nullable>

Base58 address to send to.

flags Buffer <nullable>

Nulldata flags.

key Buffer <nullable>

Key for pay-to-pubkey.

keyHash Buffer <nullable>

Key has for pay-to-pubkeyhash.

keys Array.<Buffer> <nullable>

Keys for pay-to-multisig.

scriptHash Boolean | Array.<Buffer>

Whether to create a scripthash

Source:
Returns:
Type
Script
Example
Script.createOutputScript({ address: '1HT7xU2Ngenf7D4yocz2SAcnNLW7rK8d4E' });

(static) createPubkey(key) → {Script}

Create a pay-to-pubkey script.

Parameters:
Name Type Description
key Buffer
Source:
Returns:
Type
Script

(static) createPubkeyhash(hash) → {Script}

Create a pay-to-pubkeyhash script.

Parameters:
Name Type Description
hash Buffer
Source:
Returns:
Type
Script

(static) createScripthash(hash) → {Script}

Create a pay-to-scripthash script.

Parameters:
Name Type Description
hash Buffer
Source:
Returns:
Type
Script

(static) createWitnessProgram(version, data) → {Script}

Create a witness program.

Parameters:
Name Type Description
version Number
data Buffer
Source:
Returns:
Type
Script

(static) decode(buf) → {Array}

Decode a serialized script into script code. Note that the serialized script must not include the varint size before it. Note that this will apply hidden pushdata properties to each Buffer if the buffer was created from a non-standard pushdata.

This function does not do bounds checking on buffers because some jackass could do a direct push of 30 bytes with only 20 bytes after it. That script would be perfectly fine until it is executed. There are output scripts on the blockchain that can never be redeemed due to this, but they are in valid blocks, therefore we cannot fail parsing them.

Also note that this function uses reference Buffer slices. Larger buffer slices should never be passed in here.

Parameters:
Name Type Description
buf Buffer

Serialized script.

Source:
Returns:

Script code.

Type
Array

(static) encode(code) → {Buffer}

Encode and serialize script code. This will not include the varint size at the start. This will correctly reserialize non-standard pushdata ops if the code was originally created from {Script.decode}. Otherwise, it will convert the code's pushdatas to minimaldata representations.

Parameters:
Name Type Description
code Array

Script code.

Source:
Returns:

Serialized script.

Type
Buffer

(static) format(code) → {String}

Format script code into a human readable-string.

Parameters:
Name Type Description
code Array
Source:
Returns:

Human-readable string.

Type
String

(static) fromString(items) → {Script}

Parse a formatted script string into a script object.

Parameters:
Name Type Description
items String

Script string.

Source:
Throws:

Parse error.

Returns:
Type
Script

(static) fromSymbolic(items) → {Script}

Parse an array of opcodes and pushdatas (Buffers) with the opcodes as strings representing their symbolic name. Script.fromSymbolic(['OP_1', new Buffer([2]), 'OP_ADD']);

Parameters:
Name Type Description
items Array

Array of strings and Buffers.

Source:
Throws:

Parse error on unknown opcode.

Returns:
Type
Script

(static) fromTestString(items) → {Script}

Parse a bitcoind test script string into a script object.

Parameters:
Name Type Description
items String

Script string.

Source:
Throws:

Parse error.

Returns:
Type
Script

(static) getCoinbaseHeight() → {Number}

Get coinbase height.

Source:
Returns:

-1 if not present.

Type
Number

(static) getRedeem(code) → {Script|null}

Grab and deserialize the redeem script from script code.

Parameters:
Name Type Description
code Array
Source:
Returns:

Redeem script.

Type
Script | null

(static) getSmall(index) → {Number}

Get a small integer from an opcode (OP_0-OP_16).

Parameters:
Name Type Description
index Number
Source:
Returns:
Type
Number

(static) isCode(buf) → {Boolean}

Test a buffer to see if it is valid script code (no non-existent opcodes).

Parameters:
Name Type Description
buf Buffer
Source:
Returns:
Type
Boolean

(static) isDummy(datanullable) → {Boolean}

Test whether the data element is a null dummy (a zero-length array).

Parameters:
Name Type Attributes Description
data Buffer <nullable>
Source:
Returns:
Type
Boolean

(static) isHash(hashnullable) → {Boolean}

Test whether the data element is a ripemd160 hash.

Parameters:
Name Type Attributes Description
hash Buffer <nullable>
Source:
Returns:
Type
Boolean

(static) isHashType(sig) → {Boolean}

Test a signature to see whether it contains a valid sighash type.

Parameters:
Name Type Description
sig Buffer
Source:
Returns:
Type
Boolean

(static) isKey(keynullable) → {Boolean}

Test whether the data element is a public key. Note that this does not verify the format of the key, only the length.

Parameters:
Name Type Attributes Description
key Buffer <nullable>
Source:
Returns:
Type
Boolean

(static) isKeyEncoding(key) → {Boolean}

Test whether the data element is a valid key.

Parameters:
Name Type Description
key Buffer
Source:
Returns:
Type
Boolean

(static) isLowDER(sig) → {Boolean}

Test a signature to see whether it contains a low S value.

Parameters:
Name Type Description
sig Buffer
Source:
Returns:
Type
Boolean

(static) isPush(valuenullable) → {Boolean}

Test whether an op is a buffer, also check for buffer underflows.

Parameters:
Name Type Attributes Description
value Buffer <nullable>
Source:
Returns:
Type
Boolean

(static) isScript(obj) → {Boolean}

Test an object to see if it is a Script.

Parameters:
Name Type Description
obj Object
Source:
Returns:
Type
Boolean

(static) isSignature(signullable) → {Boolean}

Test whether the data element is a signature. Note that this does not verify the format of the signature, only the length.

Parameters:
Name Type Attributes Description
sig Buffer <nullable>
Source:
Returns:
Type
Boolean

(static) isSignatureEncoding(sig) → {Boolean}

Test a signature to see if it abides by BIP66.

Parameters:
Name Type Description
sig Buffer
Source:
See:
Returns:
Type
Boolean

(static) isValidKey(key, flagsnullable) → {Boolean}

Test whether the data element is a valid key if VERIFY_STRICTENC is enabled.

Parameters:
Name Type Attributes Description
key Buffer
flags VerifyFlags <nullable>
Source:
Returns:
Type
Boolean

(static) isValidSignature(sig, flagsnullable) → {Boolean}

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.

Parameters:
Name Type Attributes Description
sig Buffer
flags VerifyFlags <nullable>
Source:
Returns:
Type
Boolean

(private, static) isZero(datanullable) → {Boolean}

Test whether the data element is a null dummy or an OP_0.

Parameters:
Name Type Attributes Description
data Buffer <nullable>
Source:
Returns:
Type
Boolean

(static) num(value, flagsnullable, sizenullable) → {BN}

Create a CScriptNum.

Parameters:
Name Type Attributes Description
value Buffer
flags Number <nullable>

Script standard flags.

size Number <nullable>

Max size in bytes.

Source:
Throws:
Returns:
Type
BN

(static) parseScript(buf) → {Object}

Parse a serialized script, returning the "naked" representation of a Script object (the same properties, but it is not instantiated -- suitable as an options object for Script).

Parameters:
Name Type Description
buf Buffer

Serialized script.

Source:
Returns:

Naked script object.

Type
Object

(static) sign(msg, key, type) → {Buffer}

Sign a message, appending the sighash type.

Parameters:
Name Type Description
msg Buffer

Signature hash.

key Buffer

Public key.

type Number

Sighash type.

Source:
Returns:

signature

Type
Buffer

(static) toSmall(num) → {Number}

Convert a number to a small integer (OP_0-OP_16).

Parameters:
Name Type Description
num Number
Source:
Returns:

opcode

Type
Number

(static) verify(input, witness, output, tx, i, flags) → {Boolean}

Verify an input and output script, and a witness if present.

Parameters:
Name Type Description
input Script
witness Witness
output Script
tx TX
i Number
flags VerifyFlags
Source:
Returns:
Type
Boolean

(static) verifyProgram(witness, output, flags, tx, i)

Verify a witness program. This runs after regular script execution if a witness program is present. It will convert the witness to a stack and execute the program.

Parameters:
Name Type Description
witness Witness
output Script
flags VerifyFlags
tx TX
i Number
Source:

clone() → {Script}

Clone the script.

Source:
Returns:

Cloned script.

Type
Script

concat(scripts) → {Array}

Concatenate scripts, inserting code separators in between them.

Parameters:
Name Type Description
scripts Array.<Script>
Source:
Returns:

code

Type
Array

encode() → {Buffer}

Encode the script to a Buffer. Note that this will not contain the varint size before it. This allows it to be hashed for scripthashes.

Source:
Returns:

Serialized script.

Type
Buffer

execute(stack, flagsnullable, txnullable, indexnullable, versionnullable) → {Boolean}

Execute the script and return false on script execution errors.

Parameters:
Name Type Attributes Description
stack Stack

Script execution stack.

flags Number <nullable>

Script standard flags.

tx TX <nullable>

Transaction being verified.

index Number <nullable>

Index of input being verified.

version Number <nullable>

Signature hash version (0=legacy, 1=segwit).

Source:
Returns:

Whether the execution was successful.

Type
Boolean

getAddress() → {Base58Address|null}

Get the address of the script if present. Note that pubkey and multisig scripts will be treated as though they are pubkeyhash and scripthashes respectively.

Source:
Returns:
Type
Base58Address | null

getArgs() → {Number}

Calculate the number of expected "arguments" (pushdata ops in the input script) for an output script. Used for standardness verification.

Source:
Returns:
Type
Number

getCoinbaseData() → {Object}

Get info about a coinbase script.

Source:
Returns:

Object containing height, extraNonce, flags, and text.

Type
Object

getCoinbaseHeight() → {Number}

Get coinbase height.

Source:
Returns:

-1 if not present.

Type
Number

getCommitmentHash() → {Buffer|null}

Get the commitment hash if present.

Source:
Returns:
Type
Buffer | null

getHash() → {Hash|null}

Get the address hash of the script if present. Note that pubkey and multisig scripts will be treated as though they are pubkeyhash and scripthashes respectively.

Source:
Returns:
Type
Hash | null

getInputAddress() → {Base58Address|null}

"Guess" the address of the input script. This method is not 100% reliable.

Source:
Returns:
Type
Base58Address | null

getInputHash() → {Hash|null}

"Guess" the address hash of the input script. This method is not 100% reliable.

Source:
Returns:
Type
Hash | null

getInputType() → {String}

"Guess" the type of the input script. This method is not 100% reliable.

Source:
Returns:
Type
String

getRedeem() → {Script|null}

Grab and deserialize the redeem script.

Source:
Returns:

Redeem script.

Type
Script | null

getSigops(accurate) → {Number}

Count the sigops in the script.

Parameters:
Name Type Description
accurate Boolean

Whether to enable accurate counting. This will take into account the n value for OP_CHECKMULTISIG(VERIFY).

Source:
Returns:

sigop count

Type
Number

getSize() → {Number}

Calculate size of script excluding the varint size bytes.

Source:
Returns:
Type
Number

getSmall(index) → {Number}

Get a small integer from an opcode (OP_0-OP_16).

Parameters:
Name Type Description
index Number
Source:
Returns:
Type
Number

getSubscript(lastSepnullable) → {Script}

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.

Parameters:
Name Type Attributes Description
lastSep Number <nullable>

The last separator to sign/verify beyond.

Source:
Returns:

Subscript.

Type
Script

getType() → {String}

Get the standard script type.

Source:
Returns:

Script script (can be any of 'witnesspubkeyhash', 'witnessscripthash', 'pubkey', 'multisig', 'scripthash', 'nulldata', or 'unknown').

Type
String

getWitnessProgram() → {Program|null}

Get the witness program if present.

Source:
Returns:
Type
Program | null

indexOf(data) → {Number}

Find a data element in a script.

Parameters:
Name Type Description
data Buffer

Data element to match against.

Source:
Returns:

Index (-1 if not present).

Type
Number

inspect() → {String}

Inspect the script.

Source:
Returns:

Human-readable script code.

Type
String

interpret(stack, flagsnullable, txnullable, indexnullable, versionnullable) → {Boolean}

Execute and interpret the script.

Parameters:
Name Type Attributes Description
stack Stack

Script execution stack.

flags Number <nullable>

Script standard flags.

tx TX <nullable>

Transaction being verified.

index Number <nullable>

Index of input being verified.

version Number <nullable>

Signature hash version (0=legacy, 1=segwit).

Source:
Throws:

Will be thrown on VERIFY failures, among other things.

Type
ScriptError
Returns:

Whether the execution was successful.

Type
Boolean

isCommitment() → {Boolean}

Test whether the output script is a segregated witness commitment. Note that commitments are a superset of nulldata as they can have multiple pushdata ops. When validating for standardness, commitments should not be allowed in anything but coinbases.

Source:
Returns:
Type
Boolean

isMultisig() → {Boolean}

Test whether the output script is pay-to-multisig.

Source:
Returns:
Type
Boolean

isMultisigInput() → {Boolean}

"Guess" whether the input script is pay-to-multisig. This method is not 100% reliable.

Source:
Returns:
Type
Boolean

isNulldata() → {Boolean}

Test whether the output script is nulldata/opreturn. This will fail if the pushdata is greater than 80 bytes.

Source:
Returns:
Type
Boolean

isPubkey() → {Boolean}

Test whether the output script is pay-to-pubkey.

Source:
Returns:
Type
Boolean

isPubkeyhash() → {Boolean}

Test whether the output script is pay-to-pubkeyhash.

Source:
Returns:
Type
Boolean

isPubkeyhashInput() → {Boolean}

"Guess" whether the input script is pay-to-pubkeyhash. This method is not 100% reliable.

Source:
Returns:
Type
Boolean

isPubkeyInput() → {Boolean}

"Guess" whether the input script is pay-to-pubkey. This method is not 100% reliable.

Source:
Returns:
Type
Boolean

isPushOnly() → {Boolean}

Test the script to see if it contains only push ops. Push ops are: OP_1NEGATE, OP_0-OP_16 and all PUSHDATAs.

Source:
Returns:
Type
Boolean

isScripthash() → {Boolean}

Test whether the output script is pay-to-scripthash. Note that bitcoin itself requires scripthashes to be in strict minimaldata encoding. Using OP_HASH160 OP_PUSHDATA1 [hash] OP_EQUAL will not be recognized as a scripthash.

Source:
Returns:
Type
Boolean

isScripthashInput() → {Boolean}

"Guess" whether the input script is pay-to-scripthash. This method is not 100% reliable.

Source:
Returns:
Type
Boolean

isStandard() → {Boolean}

Test whether the script is standard by policy standards.

Source:
Returns:
Type
Boolean

isStandardProgram() → {Boolean}

Test whether the program is standard (i.e. would it fail verification with non-mandatory flags).

Source:
Returns:
Type
Boolean

isUnknown() → {Boolean}

Test whether a script is of an unknown/non-standard type.

Source:
Returns:
Type
Boolean

isUnknownInput() → {Boolean}

"Guess" whether the input script is an unknown/non-standard type. This method is not 100% reliable.

Source:
Returns:
Type
Boolean

isWitnessProgram() → {Boolean}

Test whether the output script is a witness program. Note that this will return true even for malformed witness v0 programs.

Source:
Returns:
Type
Boolean

isWitnessPubkeyhash() → {Boolean}

Test whether the output script is a pay-to-witness-pubkeyhash script.

Source:
Returns:
Type
Boolean

isWitnessScripthash() → {Boolean}

Test whether the output script is a pay-to-witness-scripthash script.

Source:
Returns:
Type
Boolean

removeData(data) → {Number}

Remove all matched data elements from a script's code (used to remove signatures before verification). Note that this compares and removes data on the byte level. It also reserializes the data to a single script with minimaldata encoding beforehand. A signature will not be removed if it is not minimaldata.

Parameters:
Name Type Description
data Buffer

Data element to match against.

Source:
See:
Returns:

Total.

Type
Number