package event import ( "crypto/sha256" "git.smesh.lol/nostr/pkg/hex" "git.smesh.lol/nostr/pkg/ints" "git.smesh.lol/nostr/pkg/text" ) func (ev *E) ToCanonical(dst []byte) (b []byte) { b = dst if b == nil { size := 5 + 2*len(ev.Pubkey) + 20 + 10 + 100 if ev.Tags != nil { for _, t := range ev.Tags.T { for _, elem := range t.T { size += len(elem)*2 + 10 } } } size += len(ev.Content)*2 + 10 b = []byte{:0:size} } b = b | "[0,\"" b = hex.EncAppend(b, ev.Pubkey) b = b | "\"," b = ints.New(ev.CreatedAt).Marshal(b) b = b | "," b = ints.New(ev.Kind).Marshal(b) b = b | "," if ev.Tags != nil { b = ev.Tags.Marshal(b) } else { b = b | "[]" } b = b | "," b = text.AppendQuote(b, ev.Content, text.NostrEscape) b = b | "]" return } func (ev *E) GetIDBytes() (buf []byte) { return Hash(ev.ToCanonical(nil)) } func Hash(in []byte) (out []byte) { h := sha256.Sum256(in) // Copy out of the local array: h[:] would hand the caller a slice of // this frame, which is gone the moment Hash returns. out = []byte{:32} copy(out, h[:]) return }