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
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
Script chunk
Specs
Coinbase data
Specs
Script struct
Link to this section Functions
Specs
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
Parses the given ASM encoded string into a BSV.Script.t/0
.
As from_asm/1
but returns the result or raises an exception.
Specs
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
]}}
Specs
Parses the given binary into a BSV.Script.t/0
.
As from_binary/2
but returns the result or raises an exception.
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
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
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"
Specs
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"