ExPlasma.Output behaviour (ExPlasma v0.2.0)
An Output.
output_id
- The identifier scheme for the Output. We currently have two: Position and Id.
output_type
- An integer value of what type of output data is associated.
output_data
- The main data for the output. This can be decode by the different output types.
Link to this section Summary
Functions
Decode RLP data into an Output.
Throwing version of decode/1
Decode RLP input position into an Output.
Throwing version of decode_id/1
Examples
# Encode as an Output
iex> output = %ExPlasma.Output{
...> output_data: %{
...> amount: 1000000000000000000,
...> output_guard: <<205, 193, 229, 59, 220, 116, 187, 245, 181, 247, 21, 214, 50, 125, 202, 87, 133, 226, 40, 180>>,
...> token: <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>>},
...> output_id: nil,
...> output_type: 1
...> }
iex> ExPlasma.Output.encode(output)
{:ok, <<245, 1, 243, 148, 205, 193, 229, 59, 220, 116, 187, 245, 181, 247, 21, 214,
50, 125, 202, 87, 133, 226, 40, 180, 148, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 136, 13, 224, 182, 179, 167, 100, 0, 0>>}
# Encode as an Input
iex> output = %ExPlasma.Output{
...> output_data: nil,
...> output_id: %{
...> blknum: 1,
...> oindex: 0,
...> position: 1000000000,
...> txindex: 0
...> },
...> output_type: nil
...> }
iex> ExPlasma.Output.encode(output, as: :input)
{:ok, <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 59, 154, 202, 0>>}
Maps the given RLP list into an output.
Maps the given integer position into an output.
Encode an Output into RLP bytes
Transforms an Output identifer into an RLP encoded position. This is to generate
the inputs
in a Transaction.
Validates the Output
Link to this section Types
decoding_error()
Specs
decoding_error() :: :malformed_output_rlp | mapping_error()
input_position()
Specs
input_position() :: %ExPlasma.Output{ output_data: nil, output_id: ExPlasma.Output.Position.with_position(), output_type: nil }
mapping_error()
Specs
mapping_error() :: :malformed_outputs | :unrecognized_output_type | atom()
output()
Specs
output() :: %ExPlasma.Output{ output_data: output_data(), output_id: nil, output_type: output_type() }
output_data()
Specs
output_data() :: map() | nil
output_id()
Specs
output_id() :: map() | nil
output_type()
Specs
output_type() :: non_neg_integer() | nil
rlp()
Specs
rlp() :: [output_type() | output_data()]
Specs
t() :: %ExPlasma.Output{ output_data: output_data(), output_id: output_id(), output_type: output_type() }
validation_errors()
Specs
validation_responses()
Specs
validation_responses() :: :ok | validation_errors()
Link to this section Functions
decode(data)
Specs
decode(binary()) :: {:ok, output()} | {:error, decoding_error()}
Decode RLP data into an Output.
Examples
# Generate an Output from an RLP list
iex> encoded = <<245, 1, 243, 148, 205, 193, 229, 59, 220, 116, 187, 245, 181, 247, 21, 214,
...> 50, 125, 202, 87, 133, 226, 40, 180, 148, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
...> 0, 0, 0, 0, 0, 0, 0, 0, 136, 13, 224, 182, 179, 167, 100, 0, 0>>
iex> ExPlasma.Output.decode(encoded)
{:ok, %ExPlasma.Output{
output_data: %{
amount: 1000000000000000000,
output_guard: <<205, 193, 229, 59, 220, 116, 187, 245, 181, 247, 21, 214, 50, 125, 202, 87, 133, 226, 40, 180>>,
token: <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>>},
output_id: nil,
output_type: 1
}}
decode!(data)
Specs
Throwing version of decode/1
decode_id(data)
Specs
decode_id(binary()) :: {:ok, input_position()} | {:error, :malformed_input_position_rlp}
Decode RLP input position into an Output.
Example
iex> encoded_position = <<59, 154, 202, 0>>
iex> ExPlasma.Output.decode_id(encoded_position)
{:ok, %ExPlasma.Output{
output_data: nil,
output_id: %{
blknum: 1,
oindex: 0,
position: 1000000000,
txindex: 0
},
output_type: nil
}}
decode_id!(data)
Specs
decode_id!(binary()) :: input_position() | no_return()
Throwing version of decode_id/1
encode(output)
Specs
encode(output, list)
Examples
# Encode as an Output
iex> output = %ExPlasma.Output{
...> output_data: %{
...> amount: 1000000000000000000,
...> output_guard: <<205, 193, 229, 59, 220, 116, 187, 245, 181, 247, 21, 214, 50, 125, 202, 87, 133, 226, 40, 180>>,
...> token: <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>>},
...> output_id: nil,
...> output_type: 1
...> }
iex> ExPlasma.Output.encode(output)
{:ok, <<245, 1, 243, 148, 205, 193, 229, 59, 220, 116, 187, 245, 181, 247, 21, 214,
50, 125, 202, 87, 133, 226, 40, 180, 148, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 136, 13, 224, 182, 179, 167, 100, 0, 0>>}
# Encode as an Input
iex> output = %ExPlasma.Output{
...> output_data: nil,
...> output_id: %{
...> blknum: 1,
...> oindex: 0,
...> position: 1000000000,
...> txindex: 0
...> },
...> output_type: nil
...> }
iex> ExPlasma.Output.encode(output, as: :input)
{:ok, <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 59, 154, 202, 0>>}
to_map(rlp)
Specs
to_map(list()) :: {:ok, output()} | {:error, mapping_error()}
Maps the given RLP list into an output.
The RLP list must start with the output type and follow with its data.
Only validates that the RLP is structurally correct. Does not perform any other kind of validation, use validate/1 for that.
Examples
iex> rlp = [
...> <<1>>,
...> [
...> <<11, 246, 22, 41, 33, 46, 44, 159, 55, 132, 157, 153, 217, 206, 65, 226, 241, 55, 0, 110>>,
...> <<46, 38, 45, 41, 28, 46, 150, 159, 176, 132, 157, 153, 217, 206, 65, 226, 241, 55, 0, 110>>,
...> <<1>>
...> ]
...>]
iex> ExPlasma.Output.to_map(rlp)
{:ok,
%ExPlasma.Output{
output_data: %{
amount: 1,
output_guard: <<11, 246, 22, 41, 33, 46, 44, 159, 55, 132, 157, 153, 217, 206, 65, 226, 241, 55, 0, 110>>,
token: <<46, 38, 45, 41, 28, 46, 150, 159, 176, 132, 157, 153, 217, 206, 65, 226, 241, 55, 0, 110>>
},
output_id: nil,
output_type: 1}
}
to_map_id(position)
Specs
to_map_id(ExPlasma.Output.Position.position()) :: {:ok, input_position()} | {:error, :malformed_output_position}
Maps the given integer position into an output.
Only validates that the RLP is structurally correct. Does not perform any other kind of validation, use validate/1 for that.
Examples
iex> pos = 1_000_000_000
iex> ExPlasma.Output.to_map_id(pos)
{:ok, %ExPlasma.Output{output_id: %{position: 1_000_000_000, blknum: 1, txindex: 0, oindex: 0}}}
to_rlp(output)
Specs
Encode an Output into RLP bytes
Example
iex> output = %ExPlasma.Output{
...> output_id: nil,
...> output_type: 1,
...> output_data: %{output_guard: <<1::160>>, token: <<0::160>>, amount: 1}
...> }
iex> ExPlasma.Output.to_rlp(output)
{:ok, [<<1>>, [<<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1>>, <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>>, <<1>>]]}
to_rlp_id(arg1)
Specs
to_rlp_id(input_position()) :: {:ok, binary()} | {:error, :invalid_output_id}
Transforms an Output identifer into an RLP encoded position. This is to generate
the inputs
in a Transaction.
Example
iex> output = %ExPlasma.Output{
...> output_data: nil,
...> output_id: %{
...> blknum: 1,
...> oindex: 0,
...> position: 1000000000,
...> txindex: 0
...> },
...> output_type: nil
...> }
iex> ExPlasma.Output.to_rlp_id(output)
{:ok, <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 59, 154, 202, 0>>}
validate(output)
Specs
validate(t()) :: validation_responses()
Validates the Output
Example
# Validate a Payment v1 Output
iex> encoded = <<245, 1, 243, 148, 205, 193, 229, 59, 220, 116, 187, 245, 181, 247, 21, 214,
...> 50, 125, 202, 87, 133, 226, 40, 180, 148, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
...> 0, 0, 0, 0, 0, 0, 0, 0, 136, 13, 224, 182, 179, 167, 100, 0, 0>>
iex> encoded |> ExPlasma.Output.decode!() |> ExPlasma.Output.validate()
:ok
# Validate a Output position
iex> encoded_position = <<59, 154, 202, 0>>
iex> encoded_position |> ExPlasma.Output.decode_id!() |> ExPlasma.Output.validate()
:ok
Link to this section Callbacks
to_map(any)
Specs
to_rlp(map)
Specs
validate(map)
Specs
validate(map()) :: validation_responses()