BSV.Script (BSV v2.1.0) View Source

Module for parsing, serialising and building Scripts.

Script is the scripting language built into Bitcoin. Transaction outputs each contain a "locking script" which lock a number of satoshis. Transaction inputs contain an "unlocking script" which unlock the satoshis contained in a previous output. Both the unlocking script and previous locking script are concatenated in the following order:

unlocking_script <> locking_script

The entire script is evaluated and if it returns a truthy value, the output is unlocked and spent.

Link to this section Summary

Types

Script chunk

Coinbase data

t()

Script struct

Functions

Parses the given ASM encoded string into a BSV.Script.t/0.

Parses the given ASM encoded string into a BSV.Script.t/0.

Parses the given binary into a BSV.Script.t/0.

Parses the given binary into a BSV.Script.t/0.

Returns formatted coinbase data from the given Script.

Returns the size of the Script in bytes.

Pushes a chunk into the BSV.Script.t/0.

Serialises the given BSV.Script.t/0 into an ASM encoded string.

Serialises the given BSV.Script.t/0 into a binary.

Link to this section Types

Specs

chunk() :: atom() | binary()

Script chunk

Specs

coinbase_data() :: %{height: integer(), data: binary(), nonce: binary()}

Coinbase data

Specs

t() :: %BSV.Script{chunks: [chunk()], coinbase: nil | binary()}

Script struct

Link to this section Functions

Specs

from_asm(binary()) :: {:ok, t()} | {:error, term()}

Parses the given ASM encoded string into a BSV.Script.t/0.

Returns the result in an :ok / :error tuple pair.

Examples

iex> Script.from_asm("OP_DUP OP_HASH160 5ae866af9de106847de6111e5f1faa168b2be689 OP_EQUALVERIFY OP_CHECKSIG")
{:ok, %Script{chunks: [
  :OP_DUP,
  :OP_HASH160,
  <<90, 232, 102, 175, 157, 225, 6, 132, 125, 230, 17, 30, 95, 31, 170, 22, 139, 43, 230, 137>>,
  :OP_EQUALVERIFY,
  :OP_CHECKSIG
]}}

Specs

from_asm!(binary()) :: t()

Parses the given ASM encoded string into a BSV.Script.t/0.

As from_asm/1 but returns the result or raises an exception.

Link to this function

from_binary(data, opts \\ [])

View Source

Specs

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

Parses the given binary into a BSV.Script.t/0.

Returns the result in an :ok / :error tuple pair.

Options

The accepted options are:

  • :encoding - Optionally decode the binary with either the :base64 or :hex encoding scheme.

Examples

iex> Script.from_binary("76a9145ae866af9de106847de6111e5f1faa168b2be68988ac", encoding: :hex)
{:ok, %Script{chunks: [
  :OP_DUP,
  :OP_HASH160,
  <<90, 232, 102, 175, 157, 225, 6, 132, 125, 230, 17, 30, 95, 31, 170, 22, 139, 43, 230, 137>>,
  :OP_EQUALVERIFY,
  :OP_CHECKSIG
]}}
Link to this function

from_binary!(data, opts \\ [])

View Source

Specs

from_binary!(binary(), keyword()) :: t()

Parses the given binary into a BSV.Script.t/0.

As from_binary/2 but returns the result or raises an exception.

Link to this function

get_coinbase_data(script)

View Source

Specs

get_coinbase_data(t()) :: coinbase_data() | binary()

Returns formatted coinbase data from the given Script.

Specs

get_size(t()) :: non_neg_integer()

Returns the size of the Script in bytes.

Examples

iex> Script.get_size(@p2pkh_script)
25

Specs

push(t(), atom() | integer() | binary()) :: t()

Pushes a chunk into the BSV.Script.t/0.

The chunk can be any binary value, BSV.OpCode.t/0 or integer/0. Integer values will be encoded as a BSV.ScriptNum.t/0.

Examples

iex> %Script{}
...> |> Script.push(:OP_FALSE)
...> |> Script.push(:OP_RETURN)
...> |> Script.push("Hello world!")
...> |> Script.push(2021)
%Script{chunks: [
  :OP_FALSE,
  :OP_RETURN,
  "Hello world!",
  <<229, 7>>
]}

Specs

to_asm(t()) :: binary()

Serialises the given BSV.Script.t/0 into an ASM encoded string.

Examples

iex> Script.to_asm(@p2pkh_script)
"OP_DUP OP_HASH160 5ae866af9de106847de6111e5f1faa168b2be689 OP_EQUALVERIFY OP_CHECKSIG"
Link to this function

to_binary(script, opts \\ [])

View Source

Specs

to_binary(t(), keyword()) :: binary()

Serialises the given BSV.Script.t/0 into a binary.

Options

The accepted options are:

  • :encoding - Optionally encode the binary with either the :base64 or :hex encoding scheme.

Examples

iex> Script.to_binary(@p2pkh_script, encoding: :hex)
"76a9145ae866af9de106847de6111e5f1faa168b2be68988ac"