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

CBOR Envelope module, implements BRFC 5b82a2ed7b16 (CBOR Tx Envelope).

BRFC 5b82a2ed7b16 defines a standard for serializing a CBOR payload in order to have consistnency when signing the payload with a ECDSA keypair.

The :payload attribute is a CBOR encoded binary Payload.

The :pubkey and :signature attributes are optional binaries.

Examples

Example envelope with an unsigned payload.

%Txpost.Envelope{
  payload: <<161, 100, 100, 97, 116, 97, 161, 101, 114, 97, 119, 116, 120, 106, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0>>
}

Example envelope with an signed payload.

%Txpost.Envelope{
  payload: <<161, 100, 100, 97, 116, 97, 161, 101, 114, 97, 119, 116, 120, 106, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0>>,
  pubkey: <<2, 170, 75, 142, 232, 142, 111, 76, 138, 31, 212, 197, 4, 20, 227, 157, 8, 252, 150, 79, 61, 83, 205, 99, 54, 225, 193, 254, 122, 200, 147, 51, 180>>,
  signature: <<48, 68, 2, 32, 24, 134, 241, 47, 243, 122, 86, 199, 199, 220, 173, 209, 38, 189, 238, 84, 197, 20, 218, 193, 190, 35, 88, 95, 214, 137, 204, 206, 156, 21, 223, 5, 2, 32, 67, 243, 10, 255, 17, 52, 68, 176, 250, 253, 199, 208, 16, 167, 132, 183, 206, 49, 147, 241, 61, 117, 231, 254, 197, 52, 109, 45, 247, 78, 210, 62>>
}

Link to this section Summary

Types

t()

CBOR Envelope

Functions

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

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

Decodes the payload of the given Envelope struct and returns a Payload struct or returns a validation error message.

Encodes the given Envelope struct and returns a CBOR binary.

Signs the Envelope payload with the given ECDSA private key.

Returns the given Envelope struct as a map with stringified keys. The pubkey and signature attributes are removed if they are nil.

Verifies the Envelope signature against its payload and public key, returning a boolean.

Link to this section Types

Specs

t() :: %Txpost.Envelope{
  payload: binary(),
  pubkey: binary() | nil,
  signature: binary() | nil
}

CBOR Envelope

Link to this section Functions

Specs

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

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

Parameters can be passed as either a map or keyword list. The payload attribute can be an already encoded CBOR binary or a Payload struct.

Examples

iex> Txpost.Envelope.build(%{
...>   payload: %Txpost.Payload{data: %{"rawtx" => <<1, 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 >>}}
...> })
{:ok, %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>>,
  pubkey: nil,
  signature: nil
}}

Returns an error when given invalid params.

iex> Txpost.Envelope.build(payload: ["not a valid payload"])
{:error, "Invalid param: payload"}

Specs

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

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

Examples

iex> Txpost.Envelope.decode(<<161, 103, 112, 97, 121, 108, 111, 97, 100, 120, 24, 161, 100, 100, 97, 116, 97, 161, 101, 114, 97, 119, 116, 120, 106, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0>>)
{:ok, %Txpost.Envelope{
  payload: <<161, 100, 100, 97, 116, 97, 161, 101, 114, 97, 119, 116, 120, 106, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0>>,
  pubkey: nil,
  signature: nil
}}

Returns an error when given invalid binary.

iex> Txpost.Envelope.decode(<<0,1,2,3>>)
{:error, "Invalid payload binary"}
Link to this function

decode_payload(envelope)

View Source

Specs

decode_payload(t()) :: {:ok, Txpost.Payload.t()} | {:error, any()}

Decodes the payload of the given Envelope struct and returns a Payload struct or returns a validation error message.

Examples

iex> Txpost.Envelope.decode_payload(%Txpost.Envelope{
...>   payload: <<161, 100, 100, 97, 116, 97, 161, 101, 114, 97, 119, 116, 120, 106, 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: %{}
}}

Specs

encode(t()) :: binary()

Encodes the given Envelope struct and returns a CBOR binary.

Examples

iex> Txpost.Envelope.encode(%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>>
...> })
<<161, 103, 112, 97, 121, 108, 111, 97, 100, 88, 24, 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

sign(t(), binary()) :: t()

Signs the Envelope payload with the given ECDSA private key.

NOT YET IMPLEMENTED

Specs

to_map(t()) :: map()

Returns the given Envelope struct as a map with stringified keys. The pubkey and signature attributes are removed if they are nil.

Examples

iex> Txpost.Envelope.to_map(%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>>
...> })
%{
  "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

verify(t()) :: boolean()

Verifies the Envelope signature against its payload and public key, returning a boolean.

If no signature or public key is present, returns false.

Examples

iex> Txpost.Envelope.verify(%Txpost.Envelope{
...>   payload: <<161, 100, 100, 97, 116, 97, 161, 101, 114, 97, 119, 116, 120, 106, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0>>,
...>   pubkey: <<2, 170, 75, 142, 232, 142, 111, 76, 138, 31, 212, 197, 4, 20, 227, 157, 8, 252, 150, 79, 61, 83, 205, 99, 54, 225, 193, 254, 122, 200, 147, 51, 180>>,
...>   signature: <<48, 68, 2, 32, 24, 134, 241, 47, 243, 122, 86, 199, 199, 220, 173, 209, 38, 189, 238, 84, 197, 20, 218, 193, 190, 35, 88, 95, 214, 137, 204, 206, 156, 21, 223, 5, 2, 32, 67, 243, 10, 255, 17, 52, 68, 176, 250, 253, 199, 208, 16, 167, 132, 183, 206, 49, 147, 241, 61, 117, 231, 254, 197, 52, 109, 45, 247, 78, 210, 62>>
...> })
true