BSV.Contract.Helpers (BSV v2.1.0) View Source
Base helper module containing helper functions for use in BSV.Contract
modules.
Using BSV.Contract.Helpers
will import itself and all related helper modules
into your context.
use BSV.Contract.Helpers
Alternative, helper modules can be imported individually.
import BSV.Contract.Helpers
import BSV.Contract.OpCodeHelpers
import BSV.Contract.VarIntHelpers
Link to this section Summary
Functions
Assuming the top stack element is an unsigned integer, casts it to a
BSV.ScriptNum.t()
encoded number.
Iterates over the given enumerable, invoking the handle_each
function on
each.
Pushes the given data onto the script. If a list of data elements is given, each will be pushed to the script as seperate pushdata elements.
Iterates the given number of times, invoking the handle_each
function on
each iteration.
Reverses the top item on the stack.
Signs the transaction context
and pushes the
signature onto the script.
Extracts the bytes from top item on the stack, starting on the given start
index for length
bytes. The stack item is replaced with the sliced value.
Trims the given number of leading or trailing bytes from the top item on the stack. The stack item is replaced with the trimmed value.
Link to this section Functions
Specs
decode_uint(BSV.Contract.t(), atom()) :: BSV.Contract.t()
Assuming the top stack element is an unsigned integer, casts it to a
BSV.ScriptNum.t()
encoded number.
Specs
each( BSV.Contract.t(), Enum.t(), (Enum.element(), BSV.Contract.t() -> BSV.Contract.t()) ) :: BSV.Contract.t()
Iterates over the given enumerable, invoking the handle_each
function on
each.
Example
contract
|> each(["foo", "bar", "baz"], fn el, c ->
c
|> push(el)
|> op_cat()
end)
Specs
push( BSV.Contract.t(), atom() | binary() | integer() | [atom() | binary() | integer()] ) :: BSV.Contract.t()
Pushes the given data onto the script. If a list of data elements is given, each will be pushed to the script as seperate pushdata elements.
Specs
repeat( BSV.Contract.t(), non_neg_integer(), (non_neg_integer(), BSV.Contract.t() -> BSV.Contract.t()) ) :: BSV.Contract.t()
Iterates the given number of times, invoking the handle_each
function on
each iteration.
Example
contract
|> repeat(5, fn _i, c ->
c
|> op_5()
|> op_add()
end)
Specs
reverse(BSV.Contract.t(), integer()) :: BSV.Contract.t()
Reverses the top item on the stack.
This helper function pushes op codes on to the script that will reverse a binary of the given length.
Specs
sig(BSV.Contract.t(), BSV.PrivKey.t() | [BSV.PrivKey.t()]) :: BSV.Contract.t()
Signs the transaction context
and pushes the
signature onto the script.
A list of private keys can be given, in which case each is used to sign and multiple signatures are added.
If no context is available in the contract
, then
71 bytes of zeros are pushed onto the script for each private key.
Specs
slice(BSV.Contract.t(), integer(), non_neg_integer()) :: BSV.Contract.t()
Extracts the bytes from top item on the stack, starting on the given start
index for length
bytes. The stack item is replaced with the sliced value.
Binaries are zero indexed. If start
is a negative integer, then the start
index is counted from the end.
Specs
trim(BSV.Contract.t(), integer()) :: BSV.Contract.t()
Trims the given number of leading or trailing bytes from the top item on the stack. The stack item is replaced with the trimmed value.
When the given length
is a positive integer, leading bytes are trimmed. When
a negative integer is given, trailing bytes are trimmed.