fixed.go raw

   1  package event
   2  
   3  import (
   4  	"next.orly.dev/pkg/nostr/types"
   5  )
   6  
   7  // Fixed-type accessor methods for Event.
   8  // These methods provide type-safe access to event fields using fixed-size
   9  // array types that are stack-allocated and copied on assignment.
  10  //
  11  // Migration guide:
  12  //   - Old: ev.ID ([]byte, mutable, may escape to heap)
  13  //   - New: ev.IDFixed() (types.EventID, immutable copy, stays on stack)
  14  //
  15  // The slice fields (ID, Pubkey, Sig) are retained for backward compatibility.
  16  // New code should prefer the Fixed methods for type safety and performance.
  17  
  18  // IDFixed returns the event ID as a fixed-size array.
  19  // The returned value is a copy - modifications do not affect the event.
  20  func (ev *E) IDFixed() types.EventID {
  21  	return types.EventIDFromBytes(ev.ID)
  22  }
  23  
  24  // SetIDFixed sets the event ID from a fixed-size array.
  25  // This updates the underlying slice field for backward compatibility.
  26  func (ev *E) SetIDFixed(id types.EventID) {
  27  	if ev.ID == nil {
  28  		ev.ID = make([]byte, types.EventIDSize)
  29  	}
  30  	copy(ev.ID, id[:])
  31  }
  32  
  33  // PubkeyFixed returns the event pubkey as a fixed-size array.
  34  // The returned value is a copy - modifications do not affect the event.
  35  func (ev *E) PubkeyFixed() types.Pubkey {
  36  	return types.PubkeyFromBytes(ev.Pubkey)
  37  }
  38  
  39  // SetPubkeyFixed sets the event pubkey from a fixed-size array.
  40  // This updates the underlying slice field for backward compatibility.
  41  func (ev *E) SetPubkeyFixed(pk types.Pubkey) {
  42  	if ev.Pubkey == nil {
  43  		ev.Pubkey = make([]byte, types.PubkeySize)
  44  	}
  45  	copy(ev.Pubkey, pk[:])
  46  }
  47  
  48  // SigFixed returns the event signature as a fixed-size array.
  49  // The returned value is a copy - modifications do not affect the event.
  50  func (ev *E) SigFixed() types.Signature {
  51  	return types.SignatureFromBytes(ev.Sig)
  52  }
  53  
  54  // SetSigFixed sets the event signature from a fixed-size array.
  55  // This updates the underlying slice field for backward compatibility.
  56  func (ev *E) SetSigFixed(sig types.Signature) {
  57  	if ev.Sig == nil {
  58  		ev.Sig = make([]byte, types.SignatureSize)
  59  	}
  60  	copy(ev.Sig, sig[:])
  61  }
  62  
  63  // IDHex returns the event ID as a lowercase hex string.
  64  // Convenience method equivalent to hex.EncodeToString(ev.ID).
  65  func (ev *E) IDHex() string {
  66  	return types.EventIDFromBytes(ev.ID).Hex()
  67  }
  68  
  69  // PubkeyHex returns the event pubkey as a lowercase hex string.
  70  // Convenience method equivalent to hex.EncodeToString(ev.Pubkey).
  71  func (ev *E) PubkeyHex() string {
  72  	return types.PubkeyFromBytes(ev.Pubkey).Hex()
  73  }
  74  
  75  // SigHex returns the event signature as a lowercase hex string.
  76  // Convenience method equivalent to hex.EncodeToString(ev.Sig).
  77  func (ev *E) SigHex() string {
  78  	return types.SignatureFromBytes(ev.Sig).Hex()
  79  }
  80