BSV-ex v0.3.0 BSV.Script View Source

Module for the construction, parsing and serialization of transactions to and from binary data.

Examples

iex> %BSV.Script{}
...> |> BSV.Script.push(:OP_FALSE)
...> |> BSV.Script.push(:OP_RETURN)
...> |> BSV.Script.push("hello world")
...> |> BSV.Script.serialize(encoding: :hex)
"006a0b68656c6c6f20776f726c64"

iex> "006a0b68656c6c6f20776f726c64"
...> |> BSV.Script.parse(encoding: :hex)
%BSV.Script{
  chunks: [:OP_FALSE, :OP_RETURN, "hello world"]
}

Link to this section Summary

Types

t()

Bitcoin Script

Functions

Gets a coinbase script.

Gets whether this is a coinbase script.

Parses the given binary into a transaction script.

Pushes a chunk into the given transaction script. The chunk can be any binary value or OP code.

Serialises the given script into a binary.

Link to this section Types

Link to this type

t()

View Source
t() :: %BSV.Script{chunks: list(), coinbase: binary() | nil}

Bitcoin Script

Link to this section Functions

Link to this function

get_coinbase(data)

View Source
get_coinbase(binary()) :: t()

Gets a coinbase script.

Examples

iex> Script.get_coinbase("keep calm and BSV on") %Script{coinbase: "keep calm and BSV on", chunks: []}

Link to this function

is_coinbase(script)

View Source
is_coinbase(t()) :: boolean()

Gets whether this is a coinbase script.

Examples

iex> Script.get_coinbase("keep calm and BSV on") |> Script.is_coinbase() true

iex> %Script{chunks: [:OP_1]} |> Script.is_coinbase() false

Link to this function

parse(data, options \\ [])

View Source
parse(binary(), keyword()) :: t()

Parses the given binary into a transaction script.

Options

The accepted options are:

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

Examples

iex> "76a9146afc0d6bb578282ac0f6ad5c5af2294c1971210888ac"
...> |> BSV.Script.parse(encoding: :hex)
%BSV.Script{
  chunks: [
    :OP_DUP,
    :OP_HASH160,
    <<106, 252, 13, 107, 181, 120, 40, 42, 192, 246, 173, 92, 90, 242, 41, 76, 25, 113, 33, 8>>,
    :OP_EQUALVERIFY,
    :OP_CHECKSIG
  ]
}
Link to this function

push(script, data)

View Source
push(t(), binary() | atom()) :: t()

Pushes a chunk into the given transaction script. The chunk can be any binary value or OP code.

Examples

iex> %BSV.Script{}
...> |> BSV.Script.push(:OP_FALSE)
...> |> BSV.Script.push(:OP_RETURN)
...> |> BSV.Script.push("Hello world")
%BSV.Script{
  chunks: [
    :OP_FALSE,
    :OP_RETURN,
    "Hello world"
  ]
}
Link to this function

serialize(script, options \\ [])

View Source
serialize(t(), keyword()) :: binary()

Serialises the given script into a binary.

Options

The accepted options are:

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

Examples

iex> %BSV.Script{}
...> |> BSV.Script.push(:OP_DUP)
...> |> BSV.Script.push(:OP_HASH160)
...> |> BSV.Script.push(<<106, 252, 13, 107, 181, 120, 40, 42, 192, 246, 173, 92, 90, 242, 41, 76, 25, 113, 33, 8>>)
...> |> BSV.Script.push(:OP_EQUALVERIFY)
...> |> BSV.Script.push(:OP_CHECKSIG)
...> |> BSV.Script.serialize(encoding: :hex)
"76a9146afc0d6bb578282ac0f6ad5c5af2294c1971210888ac"