rlp

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2024 License: MIT Imports: 4 Imported by: 2

README

go-rlp

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

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

Installation

go get github.com/ethereum/go-rlp

Usage

Encoding data
package main

import (
	"fmt"

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

func main() {
	list := rlp.List{
		rlp.String("foo"),
		rlp.String("bar"),
		rlp.Uint(42),
	}

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

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

import (
	"encoding/hex"
	"fmt"

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

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

	// Define the data structure
	var item1 rlp.String
	var item2 rlp.String
	var item3 rlp.Uint
	list := rlp.List{&item1, &item2, &item3}

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

	// Print the decoded data
	fmt.Println(item1.Get())
	fmt.Println(item2.Get())
	fmt.Println(item3.Get())
}
Decoding data (method 2)

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

package main

import (
	"encoding/hex"
	"fmt"

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

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

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

	// Check if the decoded data is a list
	list, err := dec.List()
	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("unexpected types")
	}

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

	// Print the decoded data
	fmt.Println(foo.Get())
	fmt.Println(bar.Get())
	fmt.Println(num.Get())
}

Documentation

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

Documentation

Index

Constants

This section is empty.

Variables

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

Functions

func Decode

func Decode(src []byte, dst Decoder) (int, error)

Decode decodes RLP item and stores the result in the value pointed to by dst. It returns the number of bytes read and an error, if any.

func Encode

func Encode(src Encoder) ([]byte, error)

Encode encodes the given value into RLP item.

Types

type BigInt added in v0.4.0

type BigInt big.Int

BigInt is a big.Int type that can be encoded and decoded to/from RLP.

func (*BigInt) DecodeRLP added in v0.4.0

func (b *BigInt) DecodeRLP(data []byte) (int, error)

DecodeRLP implements the Decoder interface.

func (BigInt) EncodeRLP added in v0.4.0

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

EncodeRLP implements the Encoder interface.

func (BigInt) Get added in v0.4.0

func (b BigInt) Get() *big.Int

Get returns the big.Int value.

func (*BigInt) Ptr added in v0.4.0

func (b *BigInt) Ptr() *big.Int

Ptr returns a pointer to the big.Int value.

func (*BigInt) Set added in v0.4.0

func (b *BigInt) Set(value *big.Int)

Set sets the big.Int value.

type Bytes added in v0.4.0

type Bytes []byte

Bytes is a byte slice type that can be encoded and decoded to/from RLP.

func (*Bytes) DecodeRLP added in v0.4.0

func (b *Bytes) DecodeRLP(data []byte) (int, error)

DecodeRLP implements the Decoder interface.

func (Bytes) EncodeRLP added in v0.4.0

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

EncodeRLP implements the Encoder interface.

func (Bytes) Get added in v0.4.0

func (b Bytes) Get() []byte

Get returns the byte slice value.

func (*Bytes) Ptr added in v0.4.0

func (b *Bytes) Ptr() *[]byte

Ptr returns a pointer to the byte slice value.

func (*Bytes) Set added in v0.4.0

func (b *Bytes) Set(value []byte)

Set sets the byte slice value.

type Decoder added in v0.4.0

type Decoder interface {
	// DecodeRLP decodes the RLP data and stores the result in the value, and
	// returns the number of bytes read. The data may be longer than the encoded
	// value, in which case the remaining data is ignored.
	DecodeRLP([]byte) (int, error)
}

Decoder is the interface implemented by types that can unmarshal themselves from RLP.

type Encoder added in v0.4.0

type Encoder interface {
	// EncodeRLP returns the RLP encoding of the value.
	EncodeRLP() ([]byte, error)
}

Encoder is the interface implemented by types that can marshal themselves into RLP.

type List added in v0.4.0

type List []any

List represents a list of RLP items.

List items must implement the Encoder interface if the list is being encoded, or the Decoder interface if the list is being decoded. Otherwise, the encoding or decoding will fail.

During decoding, the data is decoded into existing items if they are already in the list. Otherwise, the items are decoded into RLP types.

func (*List) Add added in v0.4.0

func (l *List) Add(items ...any)

Add appends the given items to the list.

func (*List) DecodeRLP added in v0.4.0

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

DecodeRLP implements the Decoder interface.

func (List) EncodeRLP added in v0.4.0

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

EncodeRLP implements the Encoder interface.

func (List) Get added in v0.4.0

func (l List) Get() []any

Get returns the slice of items.

func (*List) Ptr added in v0.4.0

func (l *List) Ptr() *[]any

Ptr returns a pointer to the slice of items.

func (*List) Set added in v0.4.0

func (l *List) Set(items ...any)

Set sets the slice of items.

type RLP

type RLP []byte

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

func DecodeLazy added in v0.4.0

func DecodeLazy(src []byte) (r RLP, n int, err error)

DecodeLazy 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.

This method may be useful when the exact format of the data is not known.

func (RLP) BigInt added in v0.4.0

func (r RLP) BigInt() (v *BigInt, err error)

BigInt attempts to decode itself as a big.Int. If the decoding is successful, it returns the decoded big.Int.

func (RLP) Bytes

func (r RLP) Bytes() (v Bytes, err error)

Bytes attempts to decode itself as a byte slice. If the decoding is successful, it returns the decoded byte slice.

func (RLP) Decode added in v0.4.0

func (r RLP) Decode(dst Decoder) error

Decode decodes RLP encoded data into the given value.

func (*RLP) DecodeRLP

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

DecodeRLP implements the Decoder interface.

func (RLP) EncodeRLP

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

EncodeRLP implements the Encoder interface.

func (RLP) IsList

func (r RLP) IsList() bool

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

func (RLP) IsString

func (r RLP) IsString() bool

IsString returns true if the encoded data is an RLP string. Do not confuse this with the Go string type; an RLP string could be decoded as a string, byte slice, uint64, or big.Int. If the RLP data is empty, it returns false.

func (RLP) Length

func (r RLP) Length() int

Length returns the length of the string or number of items in the list. If the item is invalid, it returns 0.

func (RLP) List added in v0.4.0

func (r RLP) List() (l TypedList[RLP], err error)

List attempts to decode itself as a list. If the decoding is successful, it returns the decoded list.

func (RLP) String added in v0.4.0

func (r RLP) String() (v String, err error)

String attempts to decode itself as a string. If the decoding is successful, it returns the decoded string.

func (RLP) Uint added in v0.4.0

func (r RLP) Uint() (v Uint, err error)

Uint attempts to decode itself as an uint64. If the decoding is successful, it returns the decoded uint64.

type String added in v0.4.0

type String string

String is a string type that can be encoded and decoded to/from RLP.

func (*String) DecodeRLP added in v0.4.0

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

DecodeRLP implements the Decoder interface.

func (String) EncodeRLP added in v0.4.0

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

EncodeRLP implements the Encoder interface.

func (String) Get added in v0.4.0

func (s String) Get() string

Get returns the string value.

func (*String) Set added in v0.4.0

func (s *String) Set(value string)

Set sets the string value.

type TypedList added in v0.4.0

type TypedList[T any] []*T

TypedList represents a RLP list of a specific type.

The T type must implement the Encoder interface if the list is being encoded, or the Decoder interface if the list is being decoded. Otherwise, the encoding or decoding will fail.

During decoding, the data is decoded into existing items if they are already in the list. If the list is shorter than the data, new items are appended to the list.

func (*TypedList[T]) Add added in v0.4.0

func (l *TypedList[T]) Add(items ...*T)

Add appends the given items to the list.

func (*TypedList[T]) DecodeRLP added in v0.4.0

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

DecodeRLP implements the Decoder interface.

func (TypedList[T]) EncodeRLP added in v0.4.0

func (l TypedList[T]) EncodeRLP() ([]byte, error)

EncodeRLP implements the Encoder interface.

func (TypedList[T]) Get added in v0.4.0

func (l TypedList[T]) Get() []*T

Get returns the slice of items.

func (*TypedList[T]) Ptr added in v0.4.0

func (l *TypedList[T]) Ptr() *[]*T

Ptr returns a pointer to the slice of items.

func (*TypedList[T]) Set added in v0.4.0

func (l *TypedList[T]) Set(items ...*T)

Set sets the slice of items.

type Uint added in v0.4.0

type Uint uint64

Uint is an uint64 type that can be encoded and decoded to/from RLP.

func (*Uint) DecodeRLP added in v0.4.0

func (u *Uint) DecodeRLP(data []byte) (int, error)

DecodeRLP implements the Decoder interface.

func (Uint) EncodeRLP added in v0.4.0

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

EncodeRLP implements the Encoder interface.

func (Uint) Get added in v0.4.0

func (u Uint) Get() uint64

Get returns the uint64 value.

func (*Uint) Ptr added in v0.4.0

func (u *Uint) Ptr() *uint64

Ptr returns a pointer to the uint64 value.

func (*Uint) Set added in v0.4.0

func (u *Uint) Set(value uint64)

Set sets the uint64 value.

Directories

Path Synopsis
examples
decode1 command
decode2 command
encode command

Jump to

Keyboard shortcuts

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