Shapeshifter (Shapeshifter v0.2.0) View Source

Shapeshifter lets you quickly and simply switch between Bitcoin transaction formats

GitHub

Shapeshifter is an Elixir library for switching between Bitcoin transaction formats. Quickly and simply shift between raw tx, BSV Transaction, TXO and BOB transaction formats.

Installation

The package can be installed by adding shapeshifter to your list of dependencies in mix.exs:

def deps do
  [
    {:shapeshifter, "~> 0.2.0"}
  ]
end

Usage

Using Shapeshifter couldn't be simpler. Under the hood pattern matching is used to automatically determine the source format, so all you need to do is pass a transaction object of any format to the appropriate function of the format you want to convert to (from: to_raw/2, to_tx/1, to_txo/1 or to_bob/1).

# Convert to raw tx
iex> Shapeshifter.to_raw(tx)
<<1, 0, 0, 0, ...>>

# Convert to raw tx with hex encoding
iex> Shapeshifter.to_raw(tx, encoding: :hex)
"01000000..."

# Convert to BSV.Tx struct
iex> Shapeshifter.to_tx(tx)
%BSV.Tx{}

# Convert to TXO map
iex> Shapeshifter.to_txo(tx)
%{"in" => [...], "out" => [...], ...}

# Convert to BOB map
iex> Shapeshifter.to_bob(tx)
%{"in" => [...], "out" => [...], ...}

For more advanced use, Shapeshifter can also be used to convert individual inputs and outputs between the supported formats. Refer to Shapeshifter.TXO and Shapeshifter.BOB for more details.

Link to this section Summary

Types

Bitcoin OP_RETURN Bytecode format

t()

Shapeshifter struct

Source transaction

Transaction Object format

Functions

Creates a new Shapeshifter from the given transaction.

Converts the given transaction to the BOB transaction format.

Converts the given transaction to a raw tx binary, with or without hex encoding.

Converts the given transaction to a BSV Transaction struct.

Converts the given transaction to the TXO transaction format.

Link to this section Types

Specs

bob() :: %{required(String.t()) => String.t() | integer() | list()}

Bitcoin OP_RETURN Bytecode format

Tranaction objects as given by Bitbus or Bitsocket using the Bitcoin OP_RETURN Bytecode format.

Specs

t() :: %Shapeshifter{format: :tx | :txo | :bob, src: BSV.Tx.t() | txo() | bob()}

Shapeshifter struct

Specs

tx() :: binary() | BSV.Tx.t() | txo() | bob()

Source transaction

Shapeshifter accepts and effortlessly switches between the following transaction formats:

Specs

txo() :: %{required(String.t()) => String.t() | integer() | list()}

Transaction Object format

Tranaction objects as given by Bitbus or Bitsocket using the Transaction Object format.

Link to this section Functions

Specs

new(tx()) :: {:ok, t()} | {:error, Exception.t()}

Creates a new Shapeshifter from the given transaction.

Accepts either a raw tx binary (with or without hex encoding), BSV Transaction struct, or TXO or BOB formatted maps.

Returns the Shapeshifter struct in an :ok tuple pair, or returns an :error tuple pair if the given transaction format is not recognised.

Specs

to_bob(t() | tx()) :: {:ok, bob()} | {:error, Exception.t()}

Converts the given transaction to the BOB transaction format.

Accepts either a raw tx binary, BSV Transaction struct, or TXO formatted map.

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

Link to this function

to_raw(tx, options \\ [])

View Source

Specs

to_raw(t() | tx(), keyword()) :: {:ok, binary()} | {:error, Exception.t()}

Converts the given transaction to a raw tx binary, with or without hex encoding.

Accepts either a BSV Transaction struct, or TXO or BOB formatted maps.

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

Options

The accepted options are:

  • :encoding - Set :hex for hex encoding

Specs

to_tx(t() | tx()) :: {:ok, BSV.Tx.t()} | {:error, Exception.t()}

Converts the given transaction to a BSV Transaction struct.

Accepts either a raw tx binary, or TXO or BOB formatted maps.

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

Specs

to_txo(t() | tx()) :: {:ok, txo()} | {:error, Exception.t()}

Converts the given transaction to the TXO transaction format.

Accepts either a raw tx binary, BSV Transaction struct, or BOB formatted map.

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