sign.go raw

   1  package event
   2  
   3  import (
   4  	"git.smesh.lol/orly/pkg/nostr/interfaces/signer"
   5  	"git.smesh.lol/orly/pkg/lol/chk"
   6  )
   7  
   8  // Sign the event using the signer.I. Uses github.com/bitcoin-core/secp256k1 if
   9  // available for much faster signatures.
  10  //
  11  // Note that this only populates the Pubkey, ID and Sig. The caller must
  12  // set the CreatedAt timestamp as intended.
  13  func (ev *E) Sign(keys signer.I) (err error) {
  14  	// Copy Pub() and Sign() results — signers may return internal buffers
  15  	// that are reused on subsequent calls (e.g. P256K1Signer.sigBuf).
  16  	pub := keys.Pub()
  17  	ev.Pubkey = make([]byte, len(pub))
  18  	copy(ev.Pubkey, pub)
  19  	ev.ID = ev.GetIDBytes()
  20  	var sig []byte
  21  	if sig, err = keys.Sign(ev.ID); chk.E(err) {
  22  		return
  23  	}
  24  	ev.Sig = make([]byte, len(sig))
  25  	copy(ev.Sig, sig)
  26  	return
  27  }
  28