event

package
v1.0.12 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2024 License: Unlicense Imports: 18 Imported by: 0

Documentation

Index

Constants

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

Variables

View Source
var DecimalHexInSecond = B{'a'}

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

View Source
var HexInSecond = B{'e', 'p'}

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

Functions

func EstimateSize added in v1.0.0

func EstimateSize(ev *T) (size int)

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 B added in v1.0.0

type B = []byte

type C

type C chan *T

type Ctx added in v1.0.0

type Ctx = context.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 E added in v1.0.0

type E = error

type N added in v1.0.0

type N = int

type Reader added in v1.0.0

type Reader struct {
	Pos int
	Buf B
}

Reader 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 Reader 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. Or alternatively, copy the binary buffer out and the database can manage recycling this buffer.

func NewReadBuffer added in v1.0.0

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

NewReadBuffer returns a new buffer containing the provided slice.

func (*Reader) ReadContent added in v1.0.0

func (r *Reader) ReadContent() (s B, err error)

func (*Reader) ReadCreatedAt added in v1.0.0

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

func (*Reader) ReadEvent added in v1.0.0

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

func (*Reader) ReadID added in v1.0.0

func (r *Reader) ReadID() (id B, err E)

func (*Reader) ReadKind added in v1.0.0

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

func (*Reader) ReadPubKey added in v1.0.0

func (r *Reader) ReadPubKey() (pk B, err E)

func (*Reader) ReadSignature added in v1.0.0

func (r *Reader) ReadSignature() (sig B, err error)

func (*Reader) ReadTags added in v1.0.0

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

type S added in v1.0.0

type S = string

type T

type T struct {
	// ID is the SHA256 hash of the canonical encoding of the event
	ID B `json:"id"`
	// PubKey is the public key of the event creator
	PubKey B `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 B `json:"content"`
	// Sig is the signature on the ID hash that validates as coming from the
	// Pubkey.
	Sig B `json:"sig"`
}

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

func GenerateRandomTextNoteEvent

func GenerateRandomTextNoteEvent(sign signer.I, maxSize int) (ev *T,
	err error)

func New

func New() (ev *T)

func (*T) CheckSignature deprecated

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

CheckSignature returns whether an event signature is authentic and matches the event ID and Pubkey.

Deprecated: use Verify

func (*T) ContentString

func (ev *T) ContentString() (s S)

func (*T) EventID

func (ev *T) EventID() (eid *eventid.T)

func (*T) GetIDBytes

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

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

func (*T) IDString

func (ev *T) IDString() (s S)

func (*T) MarshalBinary added in v1.0.0

func (ev *T) MarshalBinary(dst B) (b B, err E)

func (*T) MarshalJSON added in v1.0.0

func (ev *T) MarshalJSON(dst B) (b B, err error)

func (*T) PubKeyString

func (ev *T) PubKeyString() (s S)

func (*T) Serialize

func (ev *T) Serialize() (b B)

func (*T) SigString

func (ev *T) SigString() (s S)

func (*T) Sign

func (ev *T) Sign(keys signer.I) (err error)

Sign the event using the signer.I. Uses github.com/bitcoin-core/secp256k1 if available for much faster signatures.

func (*T) SignWithSecKey deprecated

func (ev *T) SignWithSecKey(sk *k1.SecretKey,
	so ...sch.SignOption) (err error)

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

Deprecated: use Sign and nostr.I and p256k.Signer / p256k.BTCECSigner implementations.

func (*T) TagStrings

func (ev *T) TagStrings() (s [][]S)

func (*T) ToCanonical

func (ev *T) ToCanonical() (b B)

func (*T) UnmarshalBinary added in v1.0.0

func (ev *T) UnmarshalBinary(b B) (r B, err E)

func (*T) UnmarshalJSON added in v1.0.0

func (ev *T) UnmarshalJSON(b B) (r B, err error)

func (*T) Verify

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

Verify an event is signed by the pubkey it contains. Uses github.com/bitcoin-core/secp256k1 if available for faster verification.

type Ts

type Ts []*T

Ts is an array of T that sorts in reverse chronological order.

func (Ts) Len

func (ev Ts) Len() int

func (Ts) Less

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

func (Ts) Swap

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

type Writer added in v1.0.0

type Writer struct {
	Buf B
}

Writer 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 added in v1.0.0

func NewBufForEvent(dst B, ev *T) (buf *Writer)

func NewWriteBuffer added in v1.0.0

func NewWriteBuffer(dst B, l int) (buf *Writer)

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 (*Writer) Bytes added in v1.0.0

func (w *Writer) Bytes() B

func (*Writer) Len added in v1.0.0

func (w *Writer) Len() int

func (*Writer) WriteContent added in v1.0.0

func (w *Writer) WriteContent(s B) (err error)

func (*Writer) WriteCreatedAt added in v1.0.0

func (w *Writer) WriteCreatedAt(t *timestamp.T) (err E)

func (*Writer) WriteEvent added in v1.0.0

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

func (*Writer) WriteID added in v1.0.0

func (w *Writer) WriteID(id B) (err E)

func (*Writer) WriteKind added in v1.0.0

func (w *Writer) WriteKind(k *kind.T) (err E)

func (*Writer) WritePubKey added in v1.0.0

func (w *Writer) WritePubKey(pk B) (err E)

func (*Writer) WriteSignature added in v1.0.0

func (w *Writer) WriteSignature(sig B) (err error)

func (*Writer) WriteTags added in v1.0.0

func (w *Writer) WriteTags(t *tags.T) (err E)

WriteTags encodes tags into binary form, including special handling for protocol defined a, e and p tags.

todo: currently logging of incorrect a tag second section hex encoding as an

event ID is disabled because of a wrong a tag in the test events cache.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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