bitcoin-elixir v0.0.2 Bitcoin.Script

Bitcoin Script.

Bitcoin uses a scripting system for transactions. Forth-like, Script is simple, stack-based and processed from left to right. It is purposefully not Turing-complete, with no loops.

This module contains functions to oparet on scripts. The actual implementation of the Script engine can be found in Bitcoin.Script.Interpreter

Common pattern of opts is passed to verification and run functions. They set up the context for script verification. Here are the opts used:

  • tx - Bitcoin.Protocol.Messages.Tx in which the script is present
  • input_number - input number of that transaction (needed for sighash)
  • sub_script - also used for sighash, (often equal to pk_script)
  • flags - script validation flags, see below

Flags are in the format of hash e.g. %{p2sh: true, dersig: true}. The reason for using that instead of a simple list is ability to do function matching.

Summary

Functions

Parse binary script into a form consumable by the interpreter (ops list). Parsed script looks like this

Parse script from the string in a format that is outputed by bitcoid

Parse script from a strig form familiar from test cases

Represent parsed script (list of :OP_CODES and binary data), in it’s original binary form

Returns string representation of the provided parsed script in the same form as bitcoind decodescript command

Functions

cast_to_bool(arg1)
exec(script, opts \\ %{})
exec(list | binary, map) :: list | {:error, term}

See Bitcoin.Script.Interpreter.exec/2.

exec(stack, script, opts)

See Bitcoin.Script.Interpreter.exec/3.

parse(binary)

Parse binary script into a form consumable by the interpreter (ops list). Parsed script looks like this:

[:OP_10, :OP_10, :OP_ADD, <<20>>, :OP_EQUAL]
parse_string(string)

Parse script from the string in a format that is outputed by bitcoid.

E.g. “2 OP_IF 0 OP_ELSE 1 OP_ENDIF”

parse_string2(string)

Parse script from a strig form familiar from test cases.

E.g. “128 SIZE 2 EQUAL”

Binaries appear in the 0x form or literaly in single quotes.

to_binary(script)

Represent parsed script (list of :OP_CODES and binary data), in it’s original binary form.

to_string(script)

Returns string representation of the provided parsed script in the same form as bitcoind decodescript command

verify(script, opts \\ %{})
verify_sig_pk(sig_bin, pk_bin)
verify_sig_pk(sig_bin, pk_bin, opts)