View Source Bitcoinex.Script (bitcoinex v0.1.8)

a module for manipulating Bitcoin Scripts

Link to this section Summary

Functions

byte_length returns the byte length of the serialized script.

create_multi creates a raw multisig script using m and the list of public keys.

create_p2pk creates a p2pk script using the passed public key
create_p2pkh creates a p2pkh script using the passed 20-byte public key hash
create_p2sh creates a p2sh script using the passed 20-byte public key hash

create_p2sh_multi returns both a P2SH-wrapped multisig script and the underlying raw multisig script using m and the list of public keys.

create_p2sh_p2wpkh creates a p2wsh script using the passed 20-byte public key hash
create_p2tr creates a p2tr script using the passed 32-byte public key

or Point. If a point is passed, it's interpreted as q, the full witness program or taproot output key per BIP 341 rather than the keyspend pubkey.

create_p2wpkh creates a p2wpkh script using the passed 20-byte public key hash
create_p2wsh creates a p2wsh script using the passed 32-byte script hash

create_p2wsh_multi returns both a P2WSH-wrapped multisig script and the underlying raw multisig script using m and the list of public keys.

create_witness_scriptpubkey creates any witness script from a witness version
and witness program. It performs no validity checks.
display_script returns a human readable string of the script, with
op_codes shown by name rather than number.
empty? returns true for empty scripts, false otherwise.

extract_multi_policy takes in a raw multisig script and returns the m, the number of signatures required and the n authorized public keys.

from_address produces the scriptpubkey from an address.
get_op_atom returns the atom associated with the passed opcode integer.
get_op_num returns the integer associated with the passed opcode atom.
get_script_type determines the type of a script based on its elements
returns :non_standard if no type matches

hash160 is a helper function which returns the hash160 digest of the serialized script, as used in P2SH scripts.

is_multi? returns whether a given script is of the raw multisig format:
OP_(INT) [Public Keys] OP_(INT) OP_CHECKMULTISIG
is_p2pk? returns whether a given script is of the p2pk format:
<33-byte or 65-byte pubkey> OP_CHECKSIG
is_p2pkh? returns whether a given script is of the p2pkh format:
OP_DUP OP_HASH160 OP_PUSHBYTES_20 <20-byte hash> OP_EQUALVERIFY OP_CHECKSIG
is_p2sh? returns whether a given script is of the p2sh format:
OP_HASH160 OP_PUSHBYTES_20 <20-byte hash> OP_EQUAL
is_p2tr? returns whether a given script is of the p2tr format:
OP_1 OP_PUSHBYTES_32 <32-byte hash>
is_p2wpkh? returns whether a given script is of the p2wpkh format:
OP_0 OP_PUSHBYTES_20 <20-byte hash>
is_p2wsh? returns whether a given script is of the p2wsh format:
OP_0 OP_PUSHBYTES_32 <32-byte hash>
new returns an empty script object.
parse_script parses a binary or hex string into a script.
pop returns the first element of the script and the remaining script.
Returns nil if script is empty
public_key_hash takes the hash160 of the public key's compressed sec encoding.
Can be used to create a pkh script.
public_key_to_p2pkh creates a p2pkh script from a public key.
All public keys are compressed.
public_key_to_p2sh_p2wpkh creates a p2sh-p2wpkh script from a public key.
All public keys are compressed.
public_key_to_p2wpkh creates a p2wpkh script from a public key.
All public keys are compressed.
push_data returns a script with the binary data and any
accompanying pushdata or pushbytes opcodes added to the front of the script.
push_op pushes a single opcode to the script as an integer and returns the script.
raw_combine directly concatenates two scripts with no checks.
script_length returns the number of items in the script.
serialize_script serializes the script into binary
according to Bitcoin's standard.

hash256 is a helper function which returns the hash256 digest of the serialized script, as used in P2WSH scripts.

to_address converts a script object into the proper address type
to_hex returns the hex of a serialized script.
to_list returns the script as a list of items

to_p2sh wraps any script in a p2sh by first hashing it (hash160) and then wrapping then script hash in a p2sh script.

to_p2wsh converts any script into a p2wsh script by hashing it (SHA256) then wrapping the script hash as a p2wsh script.

Link to this section Types

@type script_type() ::
  :p2pk | :p2pkh | :p2sh | :p2wpkh | :p2wsh | :p2tr | :multi | :non_standard
@type t() :: %Bitcoinex.Script{items: list()}

Link to this section Functions

@spec byte_length(t()) :: non_neg_integer()
byte_length returns the byte length of the serialized script.
Link to this function

create_multi(m, pubkeys)

View Source
@spec create_multi(non_neg_integer(), [Bitcoinex.Secp256k1.Point.t()]) ::
  {:ok, t()} | {:error, String.t()}

create_multi creates a raw multisig script using m and the list of public keys.

@spec create_p2pk(binary()) :: {:ok, t()} | {:error, String.t()}
create_p2pk creates a p2pk script using the passed public key
@spec create_p2pkh(binary()) :: {:ok, t()} | {:error, String.t()}
create_p2pkh creates a p2pkh script using the passed 20-byte public key hash
@spec create_p2sh(binary()) :: {:ok, t()} | {:error, String.t()}
create_p2sh creates a p2sh script using the passed 20-byte public key hash
Link to this function

create_p2sh_multi(m, pubkeys)

View Source
@spec create_p2sh_multi(non_neg_integer(), [Bitcoinex.Secp256k1.Point.t()]) ::
  {:ok, t(), t()} | {:error, String.t()}

create_p2sh_multi returns both a P2SH-wrapped multisig script and the underlying raw multisig script using m and the list of public keys.

Link to this function

create_p2sh_p2wpkh(arg1)

View Source
@spec create_p2sh_p2wpkh(binary()) :: {:ok, t(), t()}
create_p2sh_p2wpkh creates a p2wsh script using the passed 20-byte public key hash
@spec create_p2tr(binary() | Bitcoinex.Secp256k1.Point.t()) :: {:ok, t()}
create_p2tr creates a p2tr script using the passed 32-byte public key

or Point. If a point is passed, it's interpreted as q, the full witness program or taproot output key per BIP 341 rather than the keyspend pubkey.

@spec create_p2wpkh(binary()) :: {:ok, t()}
create_p2wpkh creates a p2wpkh script using the passed 20-byte public key hash
@spec create_p2wsh(binary()) :: {:ok, t()}
create_p2wsh creates a p2wsh script using the passed 32-byte script hash
Link to this function

create_p2wsh_multi(m, pubkeys)

View Source
@spec create_p2wsh_multi(non_neg_integer(), [Bitcoinex.Secp256k1.Point.t()]) ::
  {:ok, t(), t()} | {:error, String.t()}

create_p2wsh_multi returns both a P2WSH-wrapped multisig script and the underlying raw multisig script using m and the list of public keys.

Link to this function

create_witness_scriptpubkey(version, witness_program)

View Source
@spec create_witness_scriptpubkey(non_neg_integer(), binary()) :: {:ok, t()}
create_witness_scriptpubkey creates any witness script from a witness version
and witness program. It performs no validity checks.
@spec display_script(t()) :: String.t()
display_script returns a human readable string of the script, with
op_codes shown by name rather than number.
@spec empty?(t()) :: bool()
empty? returns true for empty scripts, false otherwise.
Link to this function

extract_multi_policy(script)

View Source
@spec extract_multi_policy(t()) ::
  {:ok, non_neg_integer(), [Bitcoinex.Secp256k1.Point.t()]}
  | {:error, String.t()}

extract_multi_policy takes in a raw multisig script and returns the m, the number of signatures required and the n authorized public keys.

@spec from_address(String.t()) ::
  {:error, String.t()} | {:ok, t(), Bitcoinex.Network.network_name()}
from_address produces the scriptpubkey from an address.
@spec get_op_atom(non_neg_integer()) :: non_neg_integer() | {:ok, atom()}
get_op_atom returns the atom associated with the passed opcode integer.
@spec get_op_num(atom()) :: {:ok, non_neg_integer()} | :error
get_op_num returns the integer associated with the passed opcode atom.
@spec get_script_type(t()) :: script_type()
get_script_type determines the type of a script based on its elements
returns :non_standard if no type matches
@spec hash160(t()) :: binary()

hash160 is a helper function which returns the hash160 digest of the serialized script, as used in P2SH scripts.

@spec is_multi?(t()) :: boolean()
is_multi? returns whether a given script is of the raw multisig format:
OP_(INT) [Public Keys] OP_(INT) OP_CHECKMULTISIG
@spec is_p2pk?(t()) :: boolean()
is_p2pk? returns whether a given script is of the p2pk format:
<33-byte or 65-byte pubkey> OP_CHECKSIG
@spec is_p2pkh?(t()) :: boolean()
is_p2pkh? returns whether a given script is of the p2pkh format:
OP_DUP OP_HASH160 OP_PUSHBYTES_20 <20-byte hash> OP_EQUALVERIFY OP_CHECKSIG
@spec is_p2sh?(t()) :: boolean()
is_p2sh? returns whether a given script is of the p2sh format:
OP_HASH160 OP_PUSHBYTES_20 <20-byte hash> OP_EQUAL
@spec is_p2tr?(t()) :: boolean()
is_p2tr? returns whether a given script is of the p2tr format:
OP_1 OP_PUSHBYTES_32 <32-byte hash>
@spec is_p2wpkh?(t()) :: boolean()
is_p2wpkh? returns whether a given script is of the p2wpkh format:
OP_0 OP_PUSHBYTES_20 <20-byte hash>
@spec is_p2wsh?(t()) :: boolean()
is_p2wsh? returns whether a given script is of the p2wsh format:
OP_0 OP_PUSHBYTES_32 <32-byte hash>
Link to this macro

is_valid_multi(m, pubkeys)

View Source (macro)
@spec new() :: t()
new returns an empty script object.
Link to this function

parse_script(script_str)

View Source
@spec parse_script(binary()) :: {:ok, t()} | {:error, String.t()}
parse_script parses a binary or hex string into a script.
@spec pop(t()) :: nil | {:ok, non_neg_integer() | binary(), t()}
pop returns the first element of the script and the remaining script.
Returns nil if script is empty
@spec public_key_hash(Bitcoinex.Secp256k1.Point.t()) :: binary()
public_key_hash takes the hash160 of the public key's compressed sec encoding.
Can be used to create a pkh script.
@spec public_key_to_p2pkh(Bitcoinex.Secp256k1.Point.t()) :: {:ok, t()}
public_key_to_p2pkh creates a p2pkh script from a public key.
All public keys are compressed.
Link to this function

public_key_to_p2sh_p2wpkh(p)

View Source
@spec public_key_to_p2sh_p2wpkh(Bitcoinex.Secp256k1.Point.t()) :: {:ok, t(), t()}
public_key_to_p2sh_p2wpkh creates a p2sh-p2wpkh script from a public key.
All public keys are compressed.
@spec public_key_to_p2wpkh(Bitcoinex.Secp256k1.Point.t()) :: {:ok, t()}
public_key_to_p2wpkh creates a p2wpkh script from a public key.
All public keys are compressed.
@spec push_data(t(), binary()) :: {:ok, t()} | {:error, String.t()}
push_data returns a script with the binary data and any
accompanying pushdata or pushbytes opcodes added to the front of the script.
@spec push_op(t(), atom() | non_neg_integer()) :: {:ok, t()} | {:error, String.t()}
push_op pushes a single opcode to the script as an integer and returns the script.
Link to this function

raw_combine(script1, script2)

View Source
@spec raw_combine(t(), t()) :: t()
raw_combine directly concatenates two scripts with no checks.
@spec script_length(t()) :: non_neg_integer()
script_length returns the number of items in the script.
Link to this function

serialize_script(script)

View Source
@spec serialize_script(t()) :: binary()
serialize_script serializes the script into binary
according to Bitcoin's standard.
@spec sha256(t()) :: binary()

hash256 is a helper function which returns the hash256 digest of the serialized script, as used in P2WSH scripts.

Link to this function

to_address(script, network)

View Source
@spec to_address(t(), Bitcoinex.Network.network_name()) ::
  {:ok, String.t()} | {:error, String.t()}
to_address converts a script object into the proper address type
@spec to_hex(t()) :: String.t()
to_hex returns the hex of a serialized script.
@spec to_list(t()) :: list()
to_list returns the script as a list of items
@spec to_p2sh(t()) :: {:ok, t()} | {:error, String.t()}

to_p2sh wraps any script in a p2sh by first hashing it (hash160) and then wrapping then script hash in a p2sh script.

@spec to_p2wsh(t()) :: {:ok, t()}

to_p2wsh converts any script into a p2wsh script by hashing it (SHA256) then wrapping the script hash as a p2wsh script.