https://github.com/mana-ethereum/ex_rlp
Elixir implementation of Ethereum's RLP (Recursive Length Prefix) encoding
https://github.com/mana-ethereum/ex_rlp
elixir encoding ethereum
Last synced: 19 days ago
JSON representation
Elixir implementation of Ethereum's RLP (Recursive Length Prefix) encoding
- Host: GitHub
- URL: https://github.com/mana-ethereum/ex_rlp
- Owner: mana-ethereum
- License: mit
- Created: 2017-06-10T04:49:59.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2023-12-29T03:35:31.000Z (over 1 year ago)
- Last Synced: 2025-03-03T15:49:26.941Z (about 2 months ago)
- Topics: elixir, encoding, ethereum
- Language: Elixir
- Size: 160 KB
- Stars: 31
- Watchers: 5
- Forks: 10
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
- freaking_awesome_elixir - Elixir - Elixir implementation of Ethereum's RLP (Recursive Length Prefix) encoding. (Encoding and Compression)
README
# ExRLP
[](https://circleci.com/gh/mana-ethereum/ex_rlp)
[](https://hex.pm/packages/ex_rlp)
[](https://hexdocs.pm/ex_rlp/)
[](https://hex.pm/packages/ex_rlp)
[](https://github.com/mana-ethereum/ex_rlp/blob/master/LICENSE.md)
[](https://github.com/mana-ethereum/ex_rlp/commits/master)Elixir implementation of Ethereum's RLP (Recursive Length Prefix) encoding.
The encoding's specification can be found in [the yellow paper](http://yellowpaper.io/) or in the [ethereum wiki](https://github.com/ethereum/wiki/wiki/RLP).
## Installation
The easiest way to add ExRLP to your project is by [using Mix](http://elixir-lang.org/getting-started/mix-otp/introduction-to-mix.html).
Add `:ex_rlp` as a dependency to your project's `mix.exs`:
```elixir
defp deps do
[
{:ex_rlp, "~> 0.6.0"}
]
end
```And run:
```bash
$ mix deps.get
```## Basic Usage
Use `ExRLP.encode/1` method to encode an item to RLP representation. An item can be non-negative integer, binary or list. List can contain integers, binaries or lists.
```elixir
iex> "dog" |> ExRLP.encode(encoding: :hex)
"83646f67"iex> "dog" |> ExRLP.encode(encoding: :binary)
<<0x83, 0x64, 0x6f, 0x67>>iex> 1000 |> ExRLP.encode(encoding: :hex)
"8203e8"# Default encoding is binary
iex> 1000 |> ExRLP.encode
<<0x82, 0x03, 0xe8>>iex> [ [ [], [] ], [] ] |> 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.
```elixir
iex> "83646f67" |> ExRLP.decode(encoding: :hex)
"dog"iex> "8203e8" |> ExRLP.decode(encoding: :hex) |> :binary.decode_unsigned
1000iex> "c4c2c0c0c0" |> ExRLP.decode(encoding: :hex)
[[[], []], []]
```More examples can be found in test files.
## Protocols
You can define protocols for encoding/decoding custom data types.
```elixir
defmodule ExRLP.LogEntry do
defstruct address: nil, topics: [], data: nil@type t :: %__MODULE__{
address: EVM.address(),
topics: [integer()],
data: binary()
}@spec new(binary, [integer()], binary()) :: t()
def new(address, topics, data) do
%__MODULE__{
address: address,
topics: topics,
data: data
}
enddef to_list(log) do
[log.address, log.topics, log.data]
end
enddefimpl ExRLP.Encode, for: ExRLP.LogEntry do
alias ExRLP.{Encode, LogEntry}@spec encode(LogEntry.t(), keyword()) :: binary()
def encode(log, options \\ []) do
log
|> LogEntry.to_list()
|> Encode.encode(options)
end
end
```## Contributing
1. [Fork it!](https://github.com/exthereum/ex_rlp/fork)
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request## Author
Ayrat Badykov [(@ayrat555)](https://github.com/ayrat555)
## Copyright and License
Copyright (c) 2017-present Geoffrey Hayes, Ayrat Badykov, Mason Forest
ExRLP is released under the MIT License. See the [LICENSE.md](./LICENSE.md) file
for further details.