rlp

package module
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 23, 2023 License: MIT Imports: 4 Imported by: 2

README

go-rlp

The go-rlp package provides an implementation of RLP serialization format.

Installation

go get github.com/ethereum/go-rlp

Usage

Encoding data
package main

import (
	"fmt"
	"math/big"

	"github.com/defiweb/go-rlp"
)

func main() {
	list := rlp.NewList()
	list.Append(rlp.NewString("foo"))
	list.Append(rlp.NewString("bar"))
	list.Append(rlp.NewBigInt(big.NewInt(42)))

	enc, err := rlp.Encode(list)
	if err != nil {
		panic(err)
	}

	fmt.Printf("%x", enc)
}
Decoding data (method 1)
package main

import (
	"fmt"
	"encoding/hex"

	"github.com/defiweb/go-rlp"
)

func main() {
	data, _ := hex.DecodeString("c983666f6f836261722a")

	items := rlp.ListItem{
		&rlp.StringItem{},
		&rlp.StringItem{},
		&rlp.BigIntItem{},
	}

	// Decode the data
	_, err := rlp.DecodeTo(data, &items)
	if err != nil {
		panic(err)
	}

	// Print the decoded data
	fmt.Println(items[0].(*rlp, StringItem).String())
	fmt.Println(items[1].(*rlp, StringItem).String())
	fmt.Println(items[2].(*rlp, BigIntItem).X)
}
Decoding data (method 2)

This method does not require a prior definition of the data structure, hence it can be useful when the data structure is not known in advance.

package main

import (
	"fmt"
	"encoding/hex"

	"github.com/defiweb/go-rlp"
)

func main() {
	data, _ := hex.DecodeString("c983666f6f836261722a")

	// Decode the data
	dec, _, err := rlp.Decode(data)
	if err != nil {
		panic(err)
	}

	// Check if the decoded data is a list
	list, err := dec.GetList()
	if err != nil {
		panic(err)
	}
	if len(list) != 3 {
		panic("invalid list length")
	}

	// Check if list items are strings
	if !list[0].IsString() || list[1].IsString() || list[2].IsString() {
		panic("expected strings")
	}

	// Decode items
	foo, _ := list[0].GetString()
	bar, _ := list[1].GetString()
	num, _ := list[2].GetBigInt()

	fmt.Println(foo, bar, num)
}

Documentation

https://pkg.go.dev/github.com/defiweb/go-rlp

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrTooLarge = errors.New("rlp: value too large")
View Source
var ErrUnexpectedEndOfData = errors.New("rlp: unexpected end of data")
View Source
var ErrUnsupportedType = errors.New("rlp: unsupported type")

Functions

func DecodeTo

func DecodeTo(data []byte, dest Item) (int, error)

DecodeTo decode RLP data and stores the result in the value pointed to by dest. It returns the number of bytes read and an error, if any.

func Encode

func Encode(data Item) ([]byte, error)

Encode encodes the given item into RLP.

Types

type BigIntItem

type BigIntItem struct{ X *big.Int }

BigIntItem is a RLP encoded big integer.

func NewBigInt

func NewBigInt(x *big.Int) *BigIntItem

NewBigInt creates a new BigInt item.

func (*BigIntItem) DecodeRLP

func (b *BigIntItem) DecodeRLP(d []byte) (int, error)

func (BigIntItem) EncodeRLP

func (b BigIntItem) EncodeRLP() ([]byte, error)

type BinaryMarshaler

type BinaryMarshaler interface {
	encoding.BinaryMarshaler
	encoding.BinaryUnmarshaler
}

type BinaryMarshalerItem

type BinaryMarshalerItem struct {
	X BinaryMarshaler
}

BinaryMarshalerItem is a RLP encoded struct that implements the encoding.BinaryMarshaler and encoding.BinaryUnmarshaler interfaces.

func NewBinaryMarshaler

func NewBinaryMarshaler(x BinaryMarshaler) *BinaryMarshalerItem

NewBinaryMarshaler creates a new BinaryMarshaler item.

func (*BinaryMarshalerItem) DecodeRLP

func (m *BinaryMarshalerItem) DecodeRLP(d []byte) (int, error)

func (BinaryMarshalerItem) EncodeRLP

func (m BinaryMarshalerItem) EncodeRLP() ([]byte, error)

type Item

type Item interface {
	// EncodeRLP returns the RLP encoding of the item.
	EncodeRLP() ([]byte, error)

	// DecodeRLP decodes the given RLP data and returns the number of bytes
	// read. The given data may be longer than the encoded item, in which case
	// the remaining data is ignored.
	DecodeRLP([]byte) (int, error)
}

Item represents an item that can be encoded and decoded using RLP.

https://ethereum.org/en/developers/docs/data-structures-and-encoding/rlp/

type ListItem

type ListItem []Item

ListItem is a RLP encoded list. During decoding, the data is decoded into existing items if possible. If there are more items in the list than existing items, new items are created.

func NewList

func NewList(items ...Item) *ListItem

NewList creates a new List item.

func (*ListItem) Append

func (l *ListItem) Append(items ...Item)

Append appends an item to the list.

func (*ListItem) DecodeRLP

func (l *ListItem) DecodeRLP(data []byte) (int, error)

func (ListItem) EncodeRLP

func (l ListItem) EncodeRLP() ([]byte, error)

func (*ListItem) Items

func (l *ListItem) Items() []Item

Items returns a slice of items in the list.

type RLP

type RLP []byte

RLP is a raw RLP encoded data that can be decoded into any other type later.

func Decode

func Decode(data []byte) (*RLP, int, error)

Decode performs lazy decoding of RLP encoded data. It returns an RLP type that provides methods for further decoding, the number of bytes read and an error, if any.

Because decoding is performed lazily, the Encode function may not return an error if the data is invalid.

func (RLP) Bytes

func (r RLP) Bytes() []byte

Bytes returns raw RLP encoded data. To get the decoded data use one of the Get* methods.

func (*RLP) DecodeRLP

func (r *RLP) DecodeRLP(data []byte) (int, error)

func (RLP) DecodeTo

func (r RLP) DecodeTo(dest Item) error

DecodeTo decodes RLP encoded data into the given item.

func (RLP) EncodeRLP

func (r RLP) EncodeRLP() ([]byte, error)

func (RLP) Get

func (r RLP) Get(i Item, fn func(Item)) error

Get decodes RLP encoded data into the given item and if decoding was successful it invokes the fn callback.

func (RLP) GetBigInt

func (r RLP) GetBigInt() (*big.Int, error)

GetBigInt tries to decode itself as a big.Int. If the decoding was successful it returns the decoded big.Int.

func (RLP) GetBigIntItem

func (r RLP) GetBigIntItem() (*BigIntItem, error)

GetBigIntItem tries to decode itself as a big integer. If the decoding was successful it returns the decoded BigIntItem.

func (RLP) GetBinaryMarshalerItem

func (r RLP) GetBinaryMarshalerItem() (*BinaryMarshalerItem, error)

GetBinaryMarshalerItem tries to decode itself as a BinaryMarshaler. If the decoding was successful it returns the decoded BinaryMarshalerItem.

func (RLP) GetBytes

func (r RLP) GetBytes() ([]byte, error)

GetBytes tries to decode itself as a byte slice. If the decoding was successful it returns the decoded byte slice.

func (RLP) GetList

func (r RLP) GetList() ([]*RLP, error)

GetList tries to decode itself as a slice of RLP items. If the decoding was successful it returns the decoded slice.

func (RLP) GetListItem

func (r RLP) GetListItem() (*ListItem, error)

GetListItem tries to decode itself as a list. If the decoding was successful it returns the decoded ListItem.

func (RLP) GetString

func (r RLP) GetString() (string, error)

GetString tries to decode itself as a string. If the decoding was successful it returns the decoded string.

func (RLP) GetStringItem

func (r RLP) GetStringItem() (*StringItem, error)

GetStringItem tries to decode itself as a string. If the decoding was successful it returns the decoded StringItem.

func (RLP) GetUint

func (r RLP) GetUint() (uint64, error)

GetUint tries to decode itself as an uint64. If the decoding was successful it returns the decoded uint64.

func (RLP) GetUintItem

func (r RLP) GetUintItem() (*UintItem, error)

GetUintItem tries to decode itself as an integer. If the decoding was successful it returns the decoded UintItem.

func (RLP) IsList

func (r RLP) IsList() bool

IsList returns true if the encoded data is a list. If the data is empty, it returns false.

func (RLP) IsString

func (r RLP) IsString() bool

IsString returns true if the encoded data is a string. If the data is empty, it returns false.

func (RLP) Length

func (r RLP) Length() uint64

Length returns the length of the string or list. If item is invalid it returns 0.

type StringItem

type StringItem []byte

StringItem is a RLP encoded string.

func NewBytes

func NewBytes(data []byte) *StringItem

NewBytes creates a new String item from byte slice.

func NewString

func NewString(data string) *StringItem

NewString creates a new String item.

func (StringItem) Bytes

func (s StringItem) Bytes() []byte

Bytes returns the byte slice representation of the string type.

func (*StringItem) DecodeRLP

func (s *StringItem) DecodeRLP(data []byte) (int, error)

func (StringItem) EncodeRLP

func (s StringItem) EncodeRLP() ([]byte, error)

func (StringItem) String

func (s StringItem) String() string

String returns the string representation of the string type.

type UintItem

type UintItem struct{ X uint64 }

UintItem is a RLP encoded unsigned integer.

func NewUint

func NewUint(x uint64) *UintItem

NewUint creates a new Uint item.

func (*UintItem) DecodeRLP

func (u *UintItem) DecodeRLP(d []byte) (int, error)

func (UintItem) EncodeRLP

func (u UintItem) EncodeRLP() ([]byte, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL