Bitcoinex.Script (bitcoinex v0.1.6) View Source

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

Link to this section Types

Specs

script_type() ::
  :p2pk | :p2pkh | :p2sh | :p2wpkh | :p2wsh | :p2tr | :multi | :non_standard

Specs

t() :: %Bitcoinex.Script{items: list()}

Link to this section Functions

Specs

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

Specs

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.

Specs

create_p2pk(binary()) :: {:ok, t()} | {:error, String.t()}
create_p2pk creates a p2pk script using the passed public key

Specs

create_p2pkh(binary()) :: {:ok, t()} | {:error, String.t()}
create_p2pkh creates a p2pkh script using the passed 20-byte public key hash

Specs

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

Specs

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

Specs

create_p2sh_p2wpkh(binary()) :: {:ok, t(), t()}
create_p2sh_p2wpkh creates a p2wsh script using the passed 20-byte public key hash

Specs

create_p2tr(binary()) :: {:ok, t()}
create_p2tr creates a p2tr script using the passed 32-byte public key

Specs

create_p2wpkh(binary()) :: {:ok, t()}
create_p2wpkh creates a p2wpkh script using the passed 20-byte public key hash

Specs

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

Specs

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(witver, witness_program)

View Source

Specs

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. 

Specs

display_script(t()) :: String.t()
display_script returns a human readable string of the script, with
op_codes shown by name rather than number.

Specs

empty?(t()) :: bool()
empty? returns true for empty scripts, false otherwise.
Link to this function

extract_multi_policy(script)

View Source

Specs

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.

Specs

from_address(String.t()) ::
  {:error, String.t()} | {:ok, t(), Bitcoinex.Network.network_name()}
from_address produces the scriptpubkey from an address.

Specs

get_op_atom(non_neg_integer()) :: non_neg_integer() | {:ok, atom()}
get_op_atom returns the atom associated with the passed opcode integer.

Specs

get_op_num(atom()) :: {:ok, non_neg_integer()} | :error
get_op_num returns the integer associated with the passed opcode atom.

Specs

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

Specs

hash160(t()) :: binary()

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

Specs

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

Specs

is_p2pk?(t()) :: boolean()
is_p2pk? returns whether a given script is of the p2pk format:
<33-byte or 65-byte pubkey> OP_CHECKSIG

Specs

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

Specs

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

Specs

is_p2tr?(t()) :: boolean()
is_p2tr? returns whether a given script is of the p2tr format:
OP_1 OP_PUSHBYTES_32 <32-byte hash>

Specs

is_p2wpkh?(t()) :: boolean()
is_p2wpkh? returns whether a given script is of the p2wpkh format:
OP_0 OP_PUSHBYTES_20 <20-byte hash>

Specs

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)

Specs

new() :: t()
new returns an empty script object.
Link to this function

parse_script(script_str)

View Source

Specs

parse_script(binary()) :: {:ok, t()} | {:error, String.t()}
parse_script parses a binary or hex string into a script.

Specs

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

Specs

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.

Specs

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

Specs

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.

Specs

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.

Specs

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.

Specs

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

Specs

raw_combine(t(), t()) :: t()
raw_combine directly concatenates two scripts with no checks.

Specs

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

Specs

serialize_script(t()) :: binary()
serialize_script serializes the script into binary 
according to Bitcoin's standard.

Specs

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

Specs

to_address(t(), Bitcoinex.Network.network_name()) ::
  {:ok, String.t()} | {:error, String.t()}
to_address converts a script object into the proper address type

Specs

to_hex(t()) :: String.t()
to_hex returns the hex of a serialized script. 

Specs

to_list(t()) :: list()
to_list returns the script as a list of items