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) fromArray(code) → {Script}

Instantiate script from an array of buffers and numbers.

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

Instantiate script from stack.

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

(static) isScript(obj) → {Boolean}

Test whether an object a Script.

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

(static) verify(witness, addr, tx, index, value, flags)

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

Parameters:
Name Type Description
witness Witness
addr Address
tx TX
index Number
value Amount
flags VerifyFlags
Source:
Throws:
ScriptError

clear() → {Script}

Clear the script.

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

(private) decode(data)

Inject properties from serialized data.

Parameters:
Name Type Description
data Buffer
Source:

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

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)

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.

Source:
Throws:

Will be thrown on VERIFY failures.

Type
ScriptError

format() → {String}

Inspect the script.

Source:
Returns:

Human-readable script code.

Type
String

(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) 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) fromOptions(options)

Inject properties from options object.

Parameters:
Name Type Description
options Object
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) 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.

getJSON() → {String}

Convert script to a hex string.

Source:
Returns:
Type
String

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

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

getSigops() → {Number}

Count the sigops in the script.

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

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

isCode() → {Boolean}

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

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

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

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

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

isUnspendable() → {Boolean}

Test whether the output script is unspendable.

Source:
Returns:
Type
Boolean

(private) read(br)

Inject properties from buffer reader.

Parameters:
Name Type Description
br BufferReader
Source:

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

sha3() → {Hash}

Get the sha3 of the raw script.

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>

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

values() → {ScriptIterator}

Instantiate a value-only iterator.

Source:
Returns:
Type
ScriptIterator

write(bw)

Write the script to a buffer writer.

Parameters:
Name Type Description
bw BufferWriter
Source: