Class: Script

script.Script(optionsopt, nullable)

Script Represents a input or output script.

Constructor

new Script(optionsopt, nullable)

Create a script.

Parameters:
Name Type Attributes Description
options Object <optional>
<nullable>
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:

code :Array.<Opcode>

Type:
  • Array.<Opcode>
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.<Opcode>
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 AmountValue
flags VerifyFlags
Source:
Throws:
ScriptError

clear() → {this}

Clear the script.

Source:
Returns:
Type
this

compare(script) → {Number}

Compare against another script.

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

compile()

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

Source:

decode(data) → {this}

Inject properties from serialized data.

Parameters:
Name Type Description
data Buffer
Source:
Returns:
Type
this

encode() → {Buffer}

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

Source:
Returns:

Serialized script.

Type
Buffer

entries()

Instantiate a key and value iterator.

Source:

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 AmountValue <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

fromArray(code) → {this}

Inject properties from an array of of buffers and numbers.

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

fromItems(items) → {this}

Inject data from stack items.

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

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:

fromOptions(options) → {this}

Inject properties from options object.

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

fromPubkey(key) → {this}

Inject properties from a pay-to-pubkey script.

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

(private) fromPubkeyhash(hash)

Inject properties from a pay-to-pubkeyhash script.

Parameters:
Name Type Description
hash Buffer
Source:

fromStack(stack) → {this}

Inject data from stack.

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

fromString(code)

Inject properties from bitcoind test string.

Parameters:
Name Type Description
code 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) → {this}

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
this

getType() → {common.types}

Get the standard script type.

Source:
Returns:
Type
common.types

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

inject(script) → {this}

Inject properties from script. Used for cloning.

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

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

read(br)

Inject properties from buffer reader.

Parameters:
Name Type Description
br bio.BufferReader
Source:

removeSeparators() → {this}

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:

.

Type
this

sha3() → {Hash}

Get the sha3 of the raw script.

Source:
Returns:
Type
Hash

Symbol.iterator()

Instantiate a value-only iterator.

Source:

test(filter) → {Boolean}

Test the script against a bloom filter.

Parameters:
Name Type Description
filter BloomFilter
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()

Instantiate a value-only iterator.

Source:

write(bw) → {BufioWriter}

Write the script to a buffer writer.

Parameters:
Name Type Description
bw BufioWriter
Source:
Returns:
Type
BufioWriter