event

package
v0.0.6 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	ID = iota
	PubKey
	CreatedAt
	Kind
	Tags
	Content
	Signature
)

Variables

View Source
var DecimalHexInSecond = []byte{'a'}

DecimalHexInSecond is the list of first tag fields that have "decimal:hex:"

View Source
var EOF = errors.New("truncated buffer")
View Source
var HexInSecond = []byte{'e', 'p'}

HexInSecond is the list of first tag fields that the second is pure hex

Functions

func EstimateSize

func EstimateSize(ev *T) (size int)

func EventToBinary

func EventToBinary(ev *T) (b []byte, err error)

func Hash

func Hash(in []byte) (out []byte)

Types

type Ascending

type Ascending []*T

Ascending is a slice of events that sorts in ascending chronological order

func (Ascending) Len

func (ev Ascending) Len() int

func (Ascending) Less

func (ev Ascending) Less(i, j int) bool

func (Ascending) Swap

func (ev Ascending) Swap(i, j int)

type C

type C chan *T

type Descending

type Descending []*T

Descending sorts a slice of events in reverse chronological order (newest first)

func (Descending) Len

func (e Descending) Len() int

func (Descending) Less

func (e Descending) Less(i, j int) bool

func (Descending) Swap

func (e Descending) Swap(i, j int)

type ReadBuffer

type ReadBuffer struct {
	Pos int
	Buf []byte
}

ReadBuffer is a control structure for reading and writing buffers.

It keeps track of the current cursor position, and each read function increments it to reflect the position of the next field in the data.

All strings extracted from a ReadBuffer will be directly converted to strings using unsafe.String and will be garbage collected only once these strings fall out of scope.

Thus the buffers cannot effectively be reused, the memory can only be reused via GC processing. This avoids data copy as the content fields are the biggest in the event.T structure and dominate the size of the whole event anyway, so either way this is done there is a tradeoff. This can be mitigated by changing the event.T to be a []byte instead.

func NewReadBuffer

func NewReadBuffer(b []byte) (buf *ReadBuffer)

NewReadBuffer returns a new buffer containing the provided slice.

func (*ReadBuffer) Bytes

func (r *ReadBuffer) Bytes() []byte

func (*ReadBuffer) ReadContent

func (r *ReadBuffer) ReadContent() (s string, err error)

func (*ReadBuffer) ReadCreatedAt

func (r *ReadBuffer) ReadCreatedAt() (t timestamp.T, err error)

func (*ReadBuffer) ReadEvent

func (r *ReadBuffer) ReadEvent() (ev *T, err error)

func (*ReadBuffer) ReadID

func (r *ReadBuffer) ReadID() (id *eventid.T, err error)

func (*ReadBuffer) ReadKind

func (r *ReadBuffer) ReadKind() (k kind.T, err error)

func (*ReadBuffer) ReadPubKey

func (r *ReadBuffer) ReadPubKey() (pk *pubkey.T, err error)

func (*ReadBuffer) ReadSignature

func (r *ReadBuffer) ReadSignature() (sig *signature.T, err error)

func (*ReadBuffer) ReadTags

func (r *ReadBuffer) ReadTags() (t tags.T, err error)

type T

type T struct {
	// ID is the SHA256 hash of the canonical encoding of the event
	ID *eventid.T `json:"id"`
	// PubKey is the public key of the event creator in *hexadecimal* format
	PubKey *pubkey.T `json:"pubkey"`
	// CreatedAt is the UNIX timestamp of the event according to the event
	// creator (never trust a timestamp!)
	CreatedAt timestamp.T `json:"created_at"`
	// Kind is the nostr protocol code for the type of event. See kind.T
	Kind kind.T `json:"kind"`
	// Tags are a list of tags, which are a list of strings usually structured
	// as a 3 layer scheme indicating specific features of an event.
	Tags tags.T `json:"tags"`
	// Content is an arbitrary string that can contain anything, but usually
	// conforming to a specification relating to the Kind and the Tags.
	Content string `json:"content"`
	// Sig is the signature on the ID hash that validates as coming from the
	// Pubkey.
	Sig *signature.T `json:"sig"`
}

T is the primary datatype of nostr. This is the form of the structure that defines its JSON string based format.

func BinaryToEvent

func BinaryToEvent(b []byte) (ev *T, err error)

func (*T) CheckSignature

func (ev *T) CheckSignature() (valid bool, err error)

CheckSignature checks if the signature is valid for the id (which is a hash of the serialized event content). returns an error if the signature itself is invalid.

func (*T) GetIDBytes

func (ev *T) GetIDBytes() []byte

GetIDBytes returns the raw SHA256 hash of the canonical form of an T.

func (*T) MarshalJSON

func (ev *T) MarshalJSON() (bytes []byte, err error)

func (*T) Serialize

func (ev *T) Serialize() []byte

func (*T) Sign

func (ev *T) Sign(skStr string, so ...schnorr.SignOption) (err error)

Sign signs an event with a given Secret Key encoded in hexadecimal.

func (*T) SignWithSecKey

func (ev *T) SignWithSecKey(sk *secp256k1.SecretKey,
	so ...schnorr.SignOption) (err error)

SignWithSecKey signs an event with a given *secp256xk1.SecretKey.

func (*T) ToCanonical

func (ev *T) ToCanonical() (o array.T)

ToCanonical returns a structure that provides a byte stringer that generates the canonical form used to generate the ID hash that can be signed.

func (*T) ToObject

func (ev *T) ToObject() (o object.T)

type WriteBuffer

type WriteBuffer struct {
	Buf []byte
}

WriteBuffer the buffer should have its capacity pre-allocated but its length initialized to zero, and the length of the buffer acts as its effective cursor position.

func NewBufForEvent

func NewBufForEvent(ev *T) (buf *WriteBuffer)

func NewWriteBuffer

func NewWriteBuffer(l int) (buf *WriteBuffer)

NewWriteBuffer allocates a slice with zero length and capacity at the given length. Use with EstimateSize to get a buffer that will not require a secondary allocation step.

func (*WriteBuffer) Bytes

func (w *WriteBuffer) Bytes() []byte

func (*WriteBuffer) Len

func (w *WriteBuffer) Len() int

func (*WriteBuffer) WriteContent

func (w *WriteBuffer) WriteContent(s string) (err error)

func (*WriteBuffer) WriteCreatedAt

func (w *WriteBuffer) WriteCreatedAt(t timestamp.T) (err error)

func (*WriteBuffer) WriteEvent

func (w *WriteBuffer) WriteEvent(ev *T) (err error)

func (*WriteBuffer) WriteID

func (w *WriteBuffer) WriteID(id *eventid.T) (err error)

func (*WriteBuffer) WriteKind

func (w *WriteBuffer) WriteKind(k kind.T) (err error)

func (*WriteBuffer) WritePubKey

func (w *WriteBuffer) WritePubKey(pk *pubkey.T) (err error)

func (*WriteBuffer) WriteSignature

func (w *WriteBuffer) WriteSignature(sig *signature.T) (err error)

func (*WriteBuffer) WriteTags

func (w *WriteBuffer) WriteTags(t tags.T) (err error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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