Class: Script

script~Script(code)

Script Represents a input or output script.

Constructor

new Script(code)

Create a script.

Parameters:
Name Type Description
code Buffer | Array | Object
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:
  • RevMap
Source:

(static) opcodes :Number

Script opcodes.

Type:
  • Number
Source:

(static, constant) opcodesByVal :RevMap

Opcodes by value.

Type:
  • RevMap
Source:

(static) types :Number

Output script types.

Type:
  • Number
Source:

(static, constant) typesByVal :RevMap

Output script types by value.

Type:
  • RevMap
Source:

length

Get length.

Source:

length

Set length.

Source:

Methods

(static) fromAddress(address) → {Script}

Create an output script from an address.

Parameters:
Name Type Description
address Address | AddressString
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) fromCommitment(hash, flags) → {Script}

Create a witness block commitment.

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

(static) fromItems(items) → {Script}

Instantiate script from stack items.

Parameters:
Name Type Description
items Array.<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) fromStack(stack) → {Script}

Instantiate script from stack.

Parameters:
Name Type Description
stack Stack
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) isScript(obj) → {Boolean}

Test whether an object a Script.

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

(static) verify(input, witness, output, tx, index, value, flags)

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

Parameters:
Name Type Description
input Script
witness Witness
output Script
tx TX
index Number
value Amount
flags VerifyFlags
Source:
Throws:
ScriptError

(static) verifyProgram(witness, output, flags, tx, index, value)

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
index Number
value Amount
Source:
Throws:
ScriptError

clear() → {Script}

Clear the script.

Source:
Returns:
Type
Script

clone() → {Script}

Clone the script.

Source:
Returns:

Cloned script.

Type
Script

compare(script) → {Number}

Compare against another script.

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

compile() → {Script}

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

Source:
Returns:
Type
Script

entries() → {ScriptIterator}

Instantiate a key and value iterator.

Source:
Returns:
Type
ScriptIterator

equals(script) → {Boolean}

Test equality against script.

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

execute(stack, flagsnullable, txnullable, indexnullable, valuenullable, versionnullable)

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.

Type
ScriptError

findAndDelete(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

forWitness() → {Script|null}

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

Source:
Returns:
Type
Script | null

(private) fromAddress(address)

Inject properties from an address.

Parameters:
Name Type Description
address Address | AddressString
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) fromCommitment(hash, flags)

Inject properties from a witness block commitment.

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

(private) fromItems(items) → {Script}

Inject data from stack items.

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

(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) fromStack(stack) → {Script}

Inject data from stack.

Parameters:
Name Type Description
stack Stack
Source:
Returns:
Type
Script

(private) fromString(items)

Inject properties from bitcoind test string.

Parameters:
Name Type Description
items String

Script string.

Source:
Throws:

Parse error.

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

getCoinbaseHeight() → {Number}

Get coinbase height.

Source:
Returns:

-1 if not present.

Type
Number

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

getMultisig(minimalopt) → {Array}

Get multisig m and n values if present.

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

Minimaldata only.

Source:
Returns:

[m, n]

Type
Array

getMultisigInput() → {Array.<Buffer>|null}

Get multisig signatures if present.

Source:
Returns:
Type
Array.<Buffer> | null

getNulldata(minimalopt) → {Buffer|null}

Get OP_RETURN data if present.

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

Minimaldata only.

Source:
Returns:
Type
Buffer | null

getProgram() → {Program|null}

Get the witness program if present.

Source:
Returns:
Type
Program | null

getPubkey(minimalopt) → {Buffer|null}

Get P2PK key if present.

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

Minimaldata only.

Source:
Returns:
Type
Buffer | null

getPubkeyhash(minimalopt) → {Buffer|null}

Get P2PKH hash if present.

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

Minimaldata only.

Source:
Returns:
Type
Buffer | null

getPubkeyhashInput() → {Array}

Get P2PKH signature and key if present.

Source:
Returns:

[sig, key]

Type
Array

getPubkeyInput() → {Buffer|null}

Get P2PK signature if present.

Source:
Returns:
Type
Buffer | null

getRedeem() → {Script|null}

Grab and deserialize the redeem script.

Source:
Returns:

Redeem script.

Type
Script | null

getScripthash() → {Buffer|null}

Get P2SH hash if present.

Source:
Returns:
Type
Buffer | null

getScripthashInput() → {Buffer|null}

Get P2SH redeem script if present.

Source:
Returns:
Type
Buffer | 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

getSubscript(index) → {Script}

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

Parameters:
Name Type Description
index Number

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

getWitnessPubkeyhash() → {Buffer|null}

Get P2WPKH hash if present.

Source:
Returns:
Type
Buffer | null

getWitnessScripthash() → {Buffer|null}

Get P2WSH hash if present.

Source:
Returns:
Type
Buffer | null

getWitnessSigops(input, witness) → {Number}

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

Parameters:
Name Type Description
input Script
witness Witness
Source:
Returns:

sigop count

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

inspectSymbol() → {String}

Inspect the script.

Source:
Returns:

Human-readable script code.

Type
String

isCode() → {Boolean}

Test a script to see if it is likely to be script code (no weird opcodes).

Source:
Returns:
Type
Boolean

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

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

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

sha256(encnullable) → {Hash}

Get the sha256 of the raw script.

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

Symbol.iterator() → {ScriptIterator}

Instantiate a value-only iterator.

Source:
Returns:
Type
ScriptIterator

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

toItems() → {Array.<Buffer>}

Convert script to stack items.

Source:
Returns:
Type
Array.<Buffer>

toJSON() → {String}

Convert script to a hex string.

Source:
Returns:
Type
String

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

toStack() → {Stack}

Convert script to stack.

Source:
Returns:
Type
Stack

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:

values() → {ScriptIterator}

Instantiate a value-only iterator.

Source:
Returns:
Type
ScriptIterator