event

package
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2024 License: CC0-1.0 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

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

type B = []byte

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 E

type E = error

type Reader

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.

func NewReadBuffer

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

NewReadBuffer returns a new buffer containing the provided slice.

func (*Reader) ReadContent

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

func (*Reader) ReadCreatedAt

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

func (*Reader) ReadEvent

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

func (*Reader) ReadID

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

func (*Reader) ReadKind

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

func (*Reader) ReadPubKey

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

func (*Reader) ReadSignature

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

func (*Reader) ReadTags

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

type S

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(signer pkg.Signer, 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) 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

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

func (*T) MarshalJSON

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 pkg.Signer) (err error)

Sign the event using the pkg.Signer. 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 pkg.Signer and p256k.Signer / p256k.BTCECSigner implementations.

func (*T) String

func (ev *T) String() (r S)

func (*T) TagStrings

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

func (*T) ToCanonical

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

func (*T) UnmarshalBinary

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

func (*T) UnmarshalJSON

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

func (*T) UnmarshalJSONold

func (ev *T) UnmarshalJSONold(b B) (rem 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 Writer

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

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

func NewWriteBuffer

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

func (w *Writer) Bytes() B

func (*Writer) Len

func (w *Writer) Len() int

func (*Writer) WriteContent

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

func (*Writer) WriteCreatedAt

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

func (*Writer) WriteEvent

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

func (*Writer) WriteID

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

func (*Writer) WriteKind

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

func (*Writer) WritePubKey

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

func (*Writer) WriteSignature

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

func (*Writer) WriteTags

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