Txpost.Payload (Txpost v0.1.0-beta.3) View Source

Request payload module, implements BRFC c9a2975b3d19 (CBOR Tx Payload).

BRFC c9a2975b3d19 defines a simple structure for encoding a raw Bitcoin transaction alongside arbitrary data attributes and meta data in a CBOR encoded binary.

The :data attribute is either be a map with a single raw transaction alongside any other attributes, or alternatively it can be a list of maps containing multiple sets of raw transactions with additional attributes. This allows multiple transactions to be encoded in a single payload.

The :meta attribute is a map which can contain any other arbitrary infomation which can be used to help handle the request.

Examples

Example payload containing a single transaction.

%Txpost.Payload{
  data: %{
    "rawtx" => <<1, 0 ,0 ,0, ...>>,
    "type" => "article"
  },
  meta: %{
    "path" => "/posts"
  }
}

Example payload containing a list of transactions.

%Txpost.Payload{
  data: [%{
    "rawtx" => <<1, 0 ,0 ,0, ...>>,
    "type" => "article"
  }, %{
    "rawtx" => <<1, 0 ,0 ,0, ...>>,
    "type" => "article"
  }],
  meta: %{
    "path" => "/posts"
  }
}

Link to this section Summary

Types

t()

CBOR Request Payload

Functions

Validates the given parameters and returns a Payload struct or returns a validation error message.

Decodes the given CBOR binary and returns a Payload struct or returns a validation error message.

Encodes the given Payload struct and returns a CBOR binary.

Encodes the given Payload struct as a CBOR binary and wraps it within an Envelope struct.

Returns the given Payload struct as a map with stringified keys. The meta attribute is removed if it is an empty map.

Link to this section Types

Specs

t() :: %Txpost.Payload{data: map() | [map()], meta: map()}

CBOR Request Payload

Link to this section Functions

Specs

build(map() | keyword()) :: {:ok, t()} | {:error, String.t()}

Validates the given parameters and returns a Payload struct or returns a validation error message.

Parameters can be passed as either a map or keyword list.

Examples

iex> Txpost.Payload.build(data: %{"rawtx" => <<1, 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 >>})
{:ok, %Txpost.Payload{
  data: %{"rawtx" => <<1, 0, 0, 0, 0, 0, 0, 0, 0, 0>>},
  meta: %{}
}}

Returns an error when given invalid params.

iex> Txpost.Payload.build(data: "not a map")
{:error, "Invalid param: data"}

Specs

decode(binary()) :: {:ok, t()} | {:error, any()}

Decodes the given CBOR binary and returns a Payload struct or returns a validation error message.

Examples

iex> Txpost.Payload.decode(<<161, 100, 100, 97, 116, 97, 161, 101, 114, 97, 119, 116, 120, 74, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0>>)
{:ok, %Txpost.Payload{
  data: %{"rawtx" => <<1, 0, 0, 0, 0, 0, 0, 0, 0, 0>>},
  meta: %{}
}}

Returns an error when given invalid binary.

iex> Txpost.Payload.decode(<<0,1,2,3>>)
{:error, "Invalid payload binary"}

Specs

encode(t()) :: binary()

Encodes the given Payload struct and returns a CBOR binary.

Examples

iex> Txpost.Payload.encode(%Txpost.Payload{
...>   data: %{"rawtx" => <<1, 0, 0, 0, 0, 0, 0, 0, 0, 0>>}
...> })
<<161, 100, 100, 97, 116, 97, 161, 101, 114, 97, 119, 116, 120, 74, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0>>
Link to this function

encode_envelope(payload)

View Source

Specs

encode_envelope(t()) :: {:ok, Txpost.Envelope.t()}

Encodes the given Payload struct as a CBOR binary and wraps it within an Envelope struct.

Examples

iex> Txpost.Payload.encode_envelope(%Txpost.Payload{
...>   data: %{"rawtx" => <<1, 0, 0, 0, 0, 0, 0, 0, 0, 0>>}
...> })
%Txpost.Envelope{
  payload: <<161, 100, 100, 97, 116, 97, 161, 101, 114, 97, 119, 116, 120, 74, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0>>
}

Specs

to_map(t()) :: map()

Returns the given Payload struct as a map with stringified keys. The meta attribute is removed if it is an empty map.

Examples

iex> Txpost.Payload.to_map(%Txpost.Payload{
...>   data: %{"rawtx" => <<1, 0, 0, 0, 0, 0, 0, 0, 0, 0>>}
...> })
%{
  "data" => %{"rawtx" => <<1, 0, 0, 0, 0, 0, 0, 0, 0, 0>>}
}