ex_rlp v0.2.1 ExRLP
ExRLP 
Elixir implementation of Ethereum’s RLP (Recursive Length Prefix) encoding
The encoding’s specification can be found in the yellow paper or in the ethereum wiki
Installation
The easiest way to add ExRLP to your project is by using Mix.
Add :ex_rlp
as a dependency to your project’s mix.exs
:
defp deps do
[
{:ex_rlp, "~> 0.1.0"}
]
end
And run:
$ mix deps.get
Basic Usage
Use ExRLP.encode/1 method to encode an item to RLP representation. An item can be nonnegative integer, binary or list. List can contain integers, binaries or lists.
## Examples
iex(1)> "dog" |> ExRLP.encode(encoding: :hex)
"83646f67"
iex(2)> "dog" |> ExRLP.encode(encoding: :binary)
<<0x83, 0x64, 0x6f, 0x67>>
iex(3)> 1000 |> ExRLP.encode(encoding: :hex)
"8203e8"
# Default encoding is binary
iex(3)> 1000 |> ExRLP.encode
<<0x82, 0x03, 0xe8>>
iex(4)> [ [ [], [] ], [] ] |> ExRLP.encode(encoding: :hex)
"c4c2c0c0c0"
Use ExRLP.decode/1 method to decode a rlp encoded data. All items except lists are decoded as binaries so additional deserialization is needed if initially an item of another type was encoded.
## Examples
iex(1)> "83646f67" |> ExRLP.decode(:binary, encoding: :hex)
"dog"
iex(2)> "8203e8" |> ExRLP.decode(:binary, encoding: :hex) |> :binary.decode_unsigned
1000
iex(3)> "c4c2c0c0c0" |> ExRLP.decode(:binary, encoding: :hex)
[[[], []], []]
More examples can be found in test files.
Protocols
You can define protocols for encoding/decoding custom data types.
Custom protocols for Map have already been implemented in ExRLP:
defimpl ExRLP.Encoder, for: Map do
alias ExRLP.Encode
def encode(map, _) do
map
|> Map.keys
|> Enum.reduce([], fn(key, acc) ->
value = Map.get(map, key)
acc ++ [value]
end)
|> Encode.encode
end
end
defimpl ExRLP.Decoder, for: BitString do
alias ExRLP.Decode
def decode(value, :map, options) do
keys =
options
|> Keyword.fetch!(:keys)
|> Enum.sort
value
|> Decode.decode
|> Enum.with_index
|> Enum.reduce(%{}, fn({value, index}, acc) ->
key = keys |> Enum.at(index)
acc |> Map.put(key, value)
end)
...
end
end
So now it’s possible to encode/decode maps:
iex(1)> %{name: "Vitalik", surname: "Buterin"} |> ExRLP.encode(encoding: :hex)
"d087566974616c696b874275746572696e"
iex(2)> "d087566974616c696b874275746572696e" |> ExRLP.decode(:map, keys: [:surname, :name], encoding: :hex)
%{name: "Vitalik", surname: "Buterin"}
Contributing
- Fork it!
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
Author
Ayrat Badykov (@ayrat555)
License
ExRLP is released under the MIT License. See the LICENSE file for further details.
Link to this section Summary
Functions
Given an RLP-encoded string, returns a decoded RPL structure (which is an array of RLP structures or binaries)
Given an RLP structure, returns the encoding as a string
Link to this section Types
Link to this section Functions
decode(binary, atom, keyword) :: t
Given an RLP-encoded string, returns a decoded RPL structure (which is an array of RLP structures or binaries).
Examples
iex> ExRLP.decode(<<>>) nil
iex> ExRLP.decode(<<0x83, ?d, ?o, ?g>>) “dog”
iex> ExRLP.decode(“83646f67”, :binary, encoding: :hex) “dog”
iex> ExRLP.decode(<<184, 60, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65>>) Enum.join(for _ <- 1..60, do: “A”)
iex> ExRLP.decode(<<0xc8, 0x83, ?c, ?a, ?t, 0x83, ?d, ?o, ?g>>) [“cat”, “dog”]
iex> ExRLP.decode(<<198, 51, 132, 99, 111, 111, 108>>) [“3”, “cool”]
iex> ExRLP.decode(<<0x80>>) “”
iex> ExRLP.decode(<<0xc0>>) []
iex> ExRLP.decode(<<0x0f>>) <<0x0f>>
iex> ExRLP.decode(<<0x82, 0x04, 0x00>>) “”
iex> ExRLP.decode(<<0xc7, 0xc0, 0xc1, 0xc0, 0xc3, 0xc0, 0xc1, 0xc0>>) [[],[[]],[[],[[]]]]
iex> ExRLP.decode(<<248, 60, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192>>) for _ <- 1..60, do: []
iex> ExRLP.decode(<<143, 2, 227, 142, 158, 4, 75, 160, 83, 84, 85, 150, 0, 0, 0, 0>>) |> :binary.decode_unsigned 15_000_000_000_000_000_000_000_000_000_000_000
Given an RLP structure, returns the encoding as a string.
Examples
iex> ExRLP.encode(“dog”) <<0x83, ?d, ?o, ?g>>
iex> ExRLP.encode(“dog”, encoding: :hex) “83646f67”
iex> ExRLP.encode(Enum.join(for _ <- 1..60, do: “A”)) <<184, 60, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65>>
iex> ExRLP.encode([“cat”, “dog”]) <<0xc8, 0x83, ?c, ?a, ?t, 0x83, ?d, ?o, ?g>>
iex> ExRLP.encode(“”) <<0x80>>
iex> ExRLP.encode([]) <<0xc0>>
iex> ExRLP.encode(“”) <<0x0f>>
iex> ExRLP.encode(15) <<0x0f>>
iex> ExRLP.encode(15_000_000_000_000_000_000_000_000_000_000_000) <<143, 2, 227, 142, 158, 4, 75, 160, 83, 84, 85, 150, 0, 0, 0, 0>>
iex> ExRLP.encode(1024) <<0x82, 0x04, 0x00>>
iex> ExRLP.encode(“”) <<0x82, 0x04, 0x00>>
iex> ExRLP.encode([[],[[]],[[],[[]]]]) <<0xc7, 0xc0, 0xc1, 0xc0, 0xc3, 0xc0, 0xc1, 0xc0>>
iex> ExRLP.encode(for _ <- 1..60, do: []) <<248, 60, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192>>