Class: Script

script.Script

new Script(code)

Represents a input or output script.

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

Array of script code or a serialized script Buffer.

Properties:
Name Type Attributes Description
code Array

Parsed script code.

raw Buffer <nullable>

Serialized script.

length Number

Number of parsed opcodes.

Source:

Members

(static) flags :Number

Script and locktime flags. See VerifyFlags.

Type:
  • Number
Source:

(static) hashType :SighashType

Sighash Types.

Type:
Source:

(static, constant) hashTypeByVal :RevMap

Sighash types by value.

Type:
Source:

(static) opcodes :Number

Script opcodes.

Type:
  • Number
Source:

(static, constant) opcodesByVal :RevMap

Opcodes by value.

Type:
Source:

(static) types :Number

Output script types.

Type:
  • Number
Source:

(static, constant) typesByVal :RevMap

Output script types by value.

Type:
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 Number | BN
Source:
Returns:
Type
Buffer
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'));

(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) 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) fromAddress(address) → {Script}

Create an output script from an address.

Parameters:
Name Type Description
address Address | Base58Address
Source:
Returns:
Type
Script

(static) fromArray(code) → {Script}

Instantiate script from an array of buffers and numbers.

Parameters:
Name Type Description
code Array
Source:
Returns:
Type
Script

(static) fromCode(code) → {Script}

Instantiate script from an array of opcodes.

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

(static) fromCommitment(hash, flags) → {Script}

Create a witness block commitment.

Parameters:
Name Type Description
hash Buffer
flags String | Buffer
Source:
Returns:
Type
Script

(static) fromJSON() → {Script}

Instantiate script from a hex string.

Source:
Returns:
Type
Script

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

Create a pay-to-multisig script.

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

(static) fromNulldata(flags) → {Script}

Create a nulldata/opreturn script.

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

(static) fromOptions(options) → {Script}

Insantiate script from options object.

Parameters:
Name Type Description
options Object
Source:
Returns:
Type
Script

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

Create a witness program.

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

(static) fromPubkey(key) → {Script}

Create a pay-to-pubkey script.

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

(static) fromPubkeyhash(hash) → {Script}

Create a pay-to-pubkeyhash script.

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

(static) fromRaw(data, encnullable) → {Script}

Create a script from a serialized buffer.

Parameters:
Name Type Attributes Description
data Buffer | String

Serialized script.

enc String <nullable>

Either "hex" or null.

Source:
Returns:
Type
Script

(static) fromReader(br, encnullable) → {Script}

Create a script from buffer reader.

Parameters:
Name Type Attributes Description
br BufferReader
enc String <nullable>

Either "hex" or null.

Source:
Returns:
Type
Script

(static) fromScripthash(hash) → {Script}

Create a pay-to-scripthash script.

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

(static) fromString(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(raw) → {Number}

Get coinbase height.

Parameters:
Name Type Description
raw Buffer

Raw script.

Source:
Returns:

-1 if not present.

Type
Number

(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) getWitnessSigops(input, output, witness, flags) → {Number}

Count the sigops in a script, taking into account witness programs.

Parameters:
Name Type Description
input Script
output Script
witness Witness
flags VerifyFlags
Source:
Returns:

sigop count

Type
Number

(static) isCode(raw) → {Boolean}

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

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

(static) isCompressedEncoding(key) → {Boolean}

Test whether the data element is a compressed key.

Parameters:
Name Type Description
key 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) isMinimal(data, opcode, flagsnullable) → {Boolean}

Check to see if a pushdata Buffer abides by minimaldata.

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

(static) isScript(obj) → {Boolean}

Test whether an object 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) 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:
ScriptError
Returns:
Type
BN

(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) validateKey(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:
Throws:
ScriptError
Returns:
Type
Boolean

(static) validateSignature(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:
Throws:
ScriptError
Returns:
Type
Boolean

(static) verify(input, witness, output, tx, i, value, 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
value Amount
flags VerifyFlags
Source:
Throws:
ScriptError
Returns:
Type
Boolean

(static) verifyMast(program, stack, output, flags, tx, i, value) → {Boolean}

Verify a MAST witness program.

Parameters:
Name Type Description
program Program
stack Stack
output Script
flags VerifyFlags
tx TX
i Number
value Amount
Source:
Throws:
ScriptError
Returns:
Type
Boolean

(static) verifyProgram(witness, output, flags, tx, i, value) → {Boolean}

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
value Amount
Source:
Throws:
ScriptError
Returns:
Type
Boolean

(static) witnessSigops(program, witness, flags) → {Number}

Count the sigops for a program.

Parameters:
Name Type Description
program Program
witness Witness
flags VerifyFlags
Source:
Returns:

sigop count

Type
Number

clear()

Clear the script code.

Source:

clone() → {Script}

Clone the script.

Source:
Returns:

Cloned script.

Type
Script

compile()

Re-encode the script internally. Useful if you changed something manually in the code array.

Source:

execute(stack, flagsnullable, txnullable, indexnullable, valuenullable, 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.

value Amount <nullable>

Previous output value.

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

forWitness() → {Program|null}

Get the script to the equivalent witness program (mimics bitcoind's scriptForWitness).

Source:
Returns:
Type
Program | null

(private) fromAddress(address)

Inject properties from an address.

Parameters:
Name Type Description
address Address | Base58Address
Source:

(private) fromArray(code) → {Script}

Inject properties from an array of of buffers and numbers.

Parameters:
Name Type Description
code Array
Source:
Returns:
Type
Script

(private) fromCode(code)

Inject properties from an array of opcodes.

Parameters:
Name Type Description
code Array:.<Opcode:>
Source:

(private) fromCommitment(hash, flags)

Inject properties from a witness block commitment.

Parameters:
Name Type Description
hash Buffer
flags String | Buffer
Source:

(private) fromJSON(json)

Inject properties from json object.

Parameters:
Name Type Description
json String
Source:

(private) fromMultisig(m, n, keys)

Inject properties from pay-to-multisig script.

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

(private) fromNulldata(flags)

Inject properties from a nulldata/opreturn script.

Parameters:
Name Type Description
flags Buffer
Source:

(private) fromOptions(options)

Inject properties from options object.

Parameters:
Name Type Description
options Object
Source:

(private) fromProgram(version, data)

Inject properties from a witness program.

Parameters:
Name Type Description
version Number
data Buffer
Source:

(private) fromPubkey(key)

Inject properties from a pay-to-pubkey script.

Parameters:
Name Type Description
key Buffer
Source:

(private) fromPubkeyhash(hash)

Inject properties from a pay-to-pubkeyhash script.

Parameters:
Name Type Description
hash Buffer
Source:

(private) fromRaw(data)

Inject properties from serialized data.

Parameters:
Name Type Description
data Buffer
Source:

(private) fromReader(br)

Inject properties from buffer reader.

Parameters:
Name Type Description
br BufferReader
Source:

(private) fromScripthash(hash)

Inject properties from a pay-to-scripthash script.

Parameters:
Name Type Description
hash Buffer
Source:

(private) fromString(items)

Inject properties from bitcoind test string.

Parameters:
Name Type Description
items String

Script string.

Source:
Throws:

Parse error.

get(index) → {Buffer|Number}

Get an item from the code array.

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

getAddress() → {Address|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
Address | null

getCodeSize() → {Number}

Calculate size of code to be compiled.

Source:
Returns:
Type
Number

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

getInputAddress() → {Address|null}

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

Source:
Returns:
Type
Address | null

getInputType() → {ScriptType}

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

Source:
Returns:
Type
ScriptType

getNumber() → {BN}

Get a number from the code array (5-byte limit).

Source:
Returns:
Type
BN

getRedeem() → {Script|null}

Grab and deserialize the redeem script.

Source:
Returns:

Redeem script.

Type
Script | null

getScripthashSigops(input) → {Number}

Count the sigops in the script, taking into account redeem scripts.

Parameters:
Name Type Description
input Script

Input script, needed for access to redeem script.

Source:
Returns:

sigop count

Type
Number

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 the size of the 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

getString() → {String}

Get a string from the code array (utf8).

Source:
Returns:
Type
String

getSubscript(lastSepnullable) → {Script}

Get the script's "subscript" starting at a separator.

Parameters:
Name Type Attributes Description
lastSep Number <nullable>

The last separator to sign/verify beyond.

Source:
Returns:

Subscript.

Type
Script

getType() → {ScriptType}

Get the standard script type.

Source:
Returns:
Type
ScriptType

getVarSize() → {Number}

Calculate the size of the script including the varint size bytes.

Source:
Returns:
Type
Number

hash160(encnullable) → {Hash}

Get the hash160 of the raw script.

Parameters:
Name Type Attributes Description
enc String <nullable>
Source:
Returns:
Type
Hash

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

(private) inject(script) → {Script}

Inject properties from script. Used for cloning.

Parameters:
Name Type Description
script Script
Source:
Returns:
Type
Script

insert(index, data)

Insert an item into the code array.

Parameters:
Name Type Description
index Number
data Number | String | BN | Buffer
Source:

inspect() → {String}

Inspect the script.

Source:
Returns:

Human-readable script code.

Type
String

isCommitment() → {Boolean}

Test whether the output script is a segregated witness commitment.

Source:
Returns:
Type
Boolean

isMultisig(minimalopt) → {Boolean}

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

Parameters:
Name Type Attributes Default Description
minimal Boolean <optional>
false

Minimaldata only.

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(minimalopt) → {Boolean}

Test whether the output script is nulldata/opreturn.

Parameters:
Name Type Attributes Default Description
minimal Boolean <optional>
false

Minimaldata only.

Source:
Returns:
Type
Boolean

isProgram() → {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

isPubkey(minimalopt) → {Boolean}

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

Parameters:
Name Type Attributes Default Description
minimal Boolean <optional>
false

Minimaldata only.

Source:
Returns:
Type
Boolean

isPubkeyhash(minimalopt) → {Boolean}

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

Parameters:
Name Type Attributes Default Description
minimal Boolean <optional>
false

Minimaldata only.

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

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

isUnspendable() → {Boolean}

Test whether the output script is unspendable.

Source:
Returns:
Type
Boolean

isWitnessMasthash() → {Boolean}

Test whether the output script is a pay-to-mast program.

Source:
Returns:
Type
Boolean

isWitnessPubkeyhash() → {Boolean}

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

Source:
Returns:
Type
Boolean

isWitnessScripthash() → {Boolean}

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

Source:
Returns:
Type
Boolean

(private) length_getter() → {Number}

Getter to retrieve code length.

Source:
Returns:
Type
Number

(private) length_setter(value) → {Number}

Setter to set code length.

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

pop() → {Buffer|Number}

Pop an item off of the code array.

Source:
Returns:
Type
Buffer | Number

push(data) → {Number}

Push an item onto the code array.

Parameters:
Name Type Description
data Number | String | BN | Buffer
Source:
Returns:

Length.

Type
Number

remove(index) → {Buffer|Number}

Remove an item from the code array.

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

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

removeSeparators() → {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.

Source:
Returns:

Subscript.

Type
Script

set(index, data)

Set an item in the code array.

Parameters:
Name Type Description
index Number
data Buffer | Number | String | BN
Source:

sha256(encnullable) → {Hash}

Get the sha256 of the raw script.

Parameters:
Name Type Attributes Description
enc String <nullable>
Source:
Returns:
Type
Hash

shift() → {Buffer|Number}

Shift an item off of the code array.

Source:
Returns:
Type
Buffer | Number

test(filter) → {Boolean}

Test the script against a bloom filter.

Parameters:
Name Type Description
filter Bloom
Source:
Returns:
Type
Boolean

toArray() → {Array}

Convert the script to an array of Buffers (pushdatas) and Numbers (opcodes).

Source:
Returns:
Type
Array

toASM(decodenullable) → {String}

Format the script as bitcoind asm.

Parameters:
Name Type Attributes Description
decode Boolean <nullable>

Attempt to decode hash types.

Source:
Returns:

Human-readable script.

Type
String

toCode() → {Array:.<Opcode:>}

Return an array of opcodes.

Source:
Returns:
Type
Array:.<Opcode:>

toJSON() → {String}

Convert script to a hex string.

Source:
Returns:
Type
String

toProgram() → {Program|null}

Get the witness program if present.

Source:
Returns:
Type
Program | null

toRaw(enc) → {Buffer|String}

Encode the script to a Buffer. See Script#encode.

Parameters:
Name Type Description
enc String

Encoding, either 'hex' or null.

Source:
Returns:

Serialized script.

Type
Buffer | String

toString() → {String}

Convert the script to a bitcoind test string.

Source:
Returns:

Human-readable script code.

Type
String

toWriter(bw)

Write the script to a buffer writer.

Parameters:
Name Type Description
bw BufferWriter
Source:

unshift(data) → {Number}

Unshift an item onto the code array.

Parameters:
Name Type Description
data Number | String | BN | Buffer
Source:
Returns:

Length.

Type
Number