verify.go raw

   1  //go:build !js && !wasm
   2  
   3  package event
   4  
   5  import (
   6  	"git.smesh.lol/orly/pkg/nostr/interfaces/signer/p8k"
   7  	"git.smesh.lol/orly/pkg/nostr/utils"
   8  	"git.smesh.lol/orly/pkg/lol/chk"
   9  	"git.smesh.lol/orly/pkg/lol/errorf"
  10  	"git.smesh.lol/orly/pkg/lol/log"
  11  )
  12  
  13  // Verify an event is signed by the pubkey it contains. Uses
  14  // github.com/bitcoin-core/secp256k1 if available for faster verification.
  15  func (ev *E) Verify() (valid bool, err error) {
  16  	var keys *p8k.Signer
  17  	if keys, err = p8k.New(); chk.E(err) {
  18  		return
  19  	}
  20  	if err = keys.InitPub(ev.Pubkey); chk.E(err) {
  21  		return
  22  	}
  23  	if valid, err = keys.Verify(ev.ID, ev.Sig); chk.T(err) {
  24  		// check that this isn't because of a bogus ID
  25  		id := ev.GetIDBytes()
  26  		if !utils.FastEqual(id, ev.ID) {
  27  			log.E.Ln("event Subscription incorrect")
  28  			ev.ID = id
  29  			err = nil
  30  			if valid, err = keys.Verify(ev.ID, ev.Sig); chk.E(err) {
  31  				return
  32  			}
  33  			err = errorf.W("event Subscription incorrect but signature is valid on correct Subscription")
  34  		}
  35  		return
  36  	}
  37  	return
  38  }
  39