binary

package
v1.0.8 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2024 License: CC0-1.0, MIT Imports: 10 Imported by: 0

README

The simplest binary encoding for Nostr events

Some benchmarks:

goos: linux
goarch: amd64
pkg: github.com/nbd-wtf/go-nostr/binary
cpu: AMD Ryzen 3 3200G with Radeon Vega Graphics
BenchmarkBinaryEncoding/easyjson.Marshal-4                 24488             53274 ns/op           35191 B/op        102 allocs/op
BenchmarkBinaryEncoding/binary.Marshal-4                    5066            218284 ns/op         1282116 B/op         88 allocs/op
BenchmarkBinaryEncoding/binary.MarshalBinary-4              5743            191603 ns/op         1277763 B/op         37 allocs/op
BenchmarkBinaryDecoding/easyjson.Unmarshal-4               32701             38647 ns/op           45832 B/op        124 allocs/op
BenchmarkBinaryDecoding/binary.Unmarshal-4                 85705             14249 ns/op           25488 B/op        141 allocs/op
BenchmarkBinaryDecoding/binary.UnmarshalBinary-4          213438              5451 ns/op           16784 B/op         39 allocs/op
BenchmarkBinaryDecoding/easyjson.Unmarshal+sig-4             307           3971993 ns/op          131639 B/op        404 allocs/op
BenchmarkBinaryDecoding/binary.Unmarshal+sig-4               310           3924042 ns/op          111277 B/op        421 allocs/op
PASS
ok      github.com/nbd-wtf/go-nostr/binary      11.444s

This is 2~5x faster than NSON decoding, which means 8x faster than default easyjson decoding, but, just like NSON, the performance gains from this encoding is negligible when you add the cost of signature verification. Which means this encoding must only be used in internal processes.

Documentation

Index

Constants

View Source
const (
	MaxKind         = math.MaxUint16
	MaxCreatedAt    = math.MaxUint32
	MaxContentSize  = math.MaxUint16
	MaxTagCount     = math.MaxUint16
	MaxTagItemCount = math.MaxUint8
	MaxTagItemSize  = math.MaxUint16
)

Variables

This section is empty.

Functions

func EventEligibleForBinaryEncoding

func EventEligibleForBinaryEncoding(event *nostr.Event) bool

func Marshal

func Marshal(evt *nip1.Event) ([]byte, error)

func MarshalBinary

func MarshalBinary(evt *Event) []byte

func Unmarshal

func Unmarshal(data []byte, evt *nip1.Event) (err error)

func UnmarshalBinary

func UnmarshalBinary(data []byte, evt *Event) (err error)

Types

type Event

type Event struct {
	PubKey    [32]byte
	Sig       [64]byte
	ID        [32]byte
	Kind      uint16
	CreatedAt timestamp.T
	Content   string
	Tags      tags.T
}

func BinaryEvent

func BinaryEvent(evt *nip1.Event) (be *Event)

func (*Event) ToNormalEvent

func (be *Event) ToNormalEvent() *nip1.Event

Jump to

Keyboard shortcuts

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