BSV.Script (bsv_sdk v1.1.0)

Copy Markdown View Source

Bitcoin Script type — a sequence of opcodes and data pushes.

Scripts are used in transaction inputs (unlocking) and outputs (locking) to define spending conditions.

Summary

Functions

Parse a Bitcoin ASM string into a Script.

Parse a binary into a Script struct.

Parse a hex string into a Script.

Extract the public key hash from a P2PKH script.

Check if a script is OP_RETURN data.

Check if a script is P2PKH.

Check if a script is P2SH (OP_HASH160 <20 bytes> OP_EQUAL).

Create a new empty script.

Build an OP_RETURN data script (OP_FALSE OP_RETURN <data>...).

Build a P2PKH locking script from a 20-byte public key hash.

Build a P2PKH unlocking script from a signature and public key.

Convert a script to its ASM string representation.

Serialize a script to raw bytes.

Serialize a script to a hex string.

Types

chunk()

@type chunk() :: {:op, byte()} | {:data, binary()}

t()

@type t() :: %BSV.Script{chunks: [chunk()]}

Functions

from_asm(asm)

@spec from_asm(String.t()) :: {:ok, t()} | {:error, term()}

Parse a Bitcoin ASM string into a Script.

from_binary(bin)

@spec from_binary(binary()) :: {:ok, t()} | {:error, term()}

Parse a binary into a Script struct.

from_hex(hex)

@spec from_hex(String.t()) :: {:ok, t()} | {:error, term()}

Parse a hex string into a Script.

get_pubkey_hash(script)

@spec get_pubkey_hash(t()) :: {:ok, <<_::160>>} | :error

Extract the public key hash from a P2PKH script.

is_op_return?(arg1)

@spec is_op_return?(t()) :: boolean()

Check if a script is OP_RETURN data.

is_p2pkh?(script)

@spec is_p2pkh?(t()) :: boolean()

Check if a script is P2PKH.

is_p2sh?(script)

@spec is_p2sh?(t()) :: boolean()

Check if a script is P2SH (OP_HASH160 <20 bytes> OP_EQUAL).

new()

@spec new() :: t()

Create a new empty script.

op_return(data_list)

@spec op_return([binary()]) :: t()

Build an OP_RETURN data script (OP_FALSE OP_RETURN <data>...).

p2pkh_lock(arg)

@spec p2pkh_lock(<<_::160>>) :: t()

Build a P2PKH locking script from a 20-byte public key hash.

p2pkh_unlock(signature, pubkey)

@spec p2pkh_unlock(binary(), binary()) :: t()

Build a P2PKH unlocking script from a signature and public key.

to_asm(script)

@spec to_asm(t()) :: String.t()

Convert a script to its ASM string representation.

to_binary(script)

@spec to_binary(t()) :: binary()

Serialize a script to raw bytes.

to_hex(script)

@spec to_hex(t()) :: String.t()

Serialize a script to a hex string.