https://github.com/alephao/swift-rlp
Recursive Length Prefix encoding written in Swift
https://github.com/alephao/swift-rlp
encoding ethereum ios macos rlp swift
Last synced: 6 months ago
JSON representation
Recursive Length Prefix encoding written in Swift
- Host: GitHub
- URL: https://github.com/alephao/swift-rlp
- Owner: alephao
- License: mit
- Created: 2018-01-31T10:57:46.000Z (over 8 years ago)
- Default Branch: main
- Last Pushed: 2024-01-29T16:23:40.000Z (over 2 years ago)
- Last Synced: 2025-04-16T07:10:59.656Z (about 1 year ago)
- Topics: encoding, ethereum, ios, macos, rlp, swift
- Language: Swift
- Homepage:
- Size: 79.1 KB
- Stars: 24
- Watchers: 3
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# RLP
[](https://developer.apple.com/swift/)
[](https://github.com/alephao/swift-rlp/releases/tag/v1.0.0)
This is a simple, pure Swift implementation of Recursive Length Prefix Encoding, a serialisation method for encoding arbitrarily structured binary data (byte arrays).
RLP Encoding is used in Ethereum. You can read more about it here:
* [Ethereum Wiki - RLP](https://github.com/ethereum/wiki/wiki/RLP)
* [Ethereum Yellowpaper](https://ethereum.github.io/yellowpaper/paper.pdf) (Appendix B)
## Library Usage
RLP is available through [Swift Package Manager](https://swift.org/package-manager/).
Adding RLP as a dependency is as easy as adding it to the `dependencies` value of your `Package.swift`.
```swift
dependencies: [
.package(url: "https://github.com/alephao/swift-rlp.git", from: "1.0.0")
],
targets: [
.target(
name: "MyTarget",
dependencies: [
.product(name: "RLP", package: "swift-rlp")
]
),
]
```
### Encoding
```swift
import RLP
let encoder = RLPEncoder()
// String Encoding
try encoder.encode(.string("dog"))
try encoder.encode(string: "dog")
// Array Encoding
try encoder.encode(.array(["d", "o", "g"]))
```
### Decoding
```swift
import RLP
let encodedData = try RLPEncoder().encode(string: "dog")
let decoder = RLPDecoder()
try decoder.decode(from: encodedData) // RLPValue.string("dog")
```
## CLI
Currently, the CLI is only available via source code. To use it, clone this repo and run:
### Encoding
```bash
$ swift run cli encode dog
> 0x83646F67
```
### Decoding
```bash
$ swift run cli decode 0x83646F67
> string("dog")
```
## License
RLP is released under an [MIT](https://tldrlegal.com/license/mit-license) license. See [LICENSE](LICENSE) for more information.