working sign/verify on events

This commit is contained in:
2025-05-22 13:08:25 +01:00
parent 4ce418c1a1
commit 2386edb169
2 changed files with 19 additions and 5 deletions

View File

@@ -31,9 +31,13 @@ type E struct {
// structure so that one can just find the `"sig":` text and cut the text out from the initial
// parenthesis until this object key. For this reason, ensure that your JSON encoder produces
// consistent ordering of the fields as shown below.
//
// For future expansion and to enable bridging to Nostr relays this will possibly also contain a
// second event type based on the nostr canonical format which can be parsed back into the nostr
// wire format to send to legacy nostr clients.
type Signed struct {
*E `json:"event" doc:"embedded event"`
Signature *signature.S `json:"sig" doc:"ed25519 signature on event"`
Signature *signature.S `json:"sig" doc:"signature on event"`
}
func (e *E) GetId() (i []byte) {
@@ -48,17 +52,23 @@ func (e *E) GetId() (i []byte) {
}
func (e *E) Sign(sign signer.I) (s *Signed, err error) {
e.Pubkey = pubkey.New(sign.Pub())
i := e.GetId()
var sig []byte
if sig, err = sign.Sign(i); chk.E(err) {
return
}
e.Pubkey = pubkey.New(sign.Pub())
s = &Signed{e, signature.New(sig)}
return
}
func (e *E) Verify() (valid bool) {
func (s *Signed) Verify(sign signer.I) (valid bool) {
// encode the event as json to derive the event I d
var err error
eid := s.E.GetId()
if valid, err = sign.Verify(eid, s.Signature.Bytes); chk.E(err) {
return
}
return
}

View File

@@ -76,8 +76,12 @@ func TestSign(t *testing.T) {
if signed, err = e.Sign(signer); chk.E(err) {
return
}
sb := signed.Serialize()
log.I.F("%s", sb)
// sb := signed.Serialize()
// log.I.F("%s", sb)
valid := signed.Verify(signer)
if !valid {
t.Fatal("failed to verify signature")
}
}
func generatePubkey() (p *pubkey.P) {