Skip to content

alephao/swift-rlp

Repository files navigation

RLP

Swift 6.1 Version

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:

Library Usage

RLP is available through Swift Package Manager.

Adding RLP as a dependency is as easy as adding it to the dependencies value of your Package.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

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

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

$ swift run cli encode dog
> 0x83646F67

Decoding

$ swift run cli decode 0x83646F67
> string("dog")

License

RLP is released under an MIT license. See LICENSE for more information.

About

Recursive Length Prefix encoding written in Swift

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •