Repository navigation
Expand file tree
/
Copy pathnostr.go
More file actions
67 lines (55 loc) · 2.31 KB
/
Copy pathnostr.go
File metadata and controls
67 lines (55 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package wasmtest
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"ducat/crypto"
"ducat/shared"
)
// signNostrEvent signs a Nostr event per NIP-01.
// It sets event.ID to the SHA-256 hex of the NIP-01 serialization and sets event.Sig to the Schnorr signature produced from privKeyBytes.
// Returns an error if event is nil, if privKeyBytes is not 32 bytes, or if signing fails.
func signNostrEvent(event *shared.NostrEvent, privKeyBytes []byte) error {
if event == nil {
return fmt.Errorf("event cannot be nil")
}
if len(privKeyBytes) != 32 {
return fmt.Errorf("invalid private key length: expected 32 bytes, got %d", len(privKeyBytes))
}
// Serialize event according to NIP-01
serialized := serializeNostrEvent(event)
// Compute event ID (SHA256 of serialized event)
hash := sha256.Sum256([]byte(serialized))
event.ID = hex.EncodeToString(hash[:])
// Sign event ID with Schnorr
sig, err := crypto.SignSchnorr(privKeyBytes, event.ID)
if err != nil {
return fmt.Errorf("failed to sign event: %w", err)
}
event.Sig = sig
return nil
}
// serializeNostrEvent produces the NIP-01 serialization of the given event as a JSON-array string containing the event's pubkey, created_at, kind, tags, and content.
// The event's Tags field is JSON-marshaled and inserted verbatim; any marshal error is ignored.
func serializeNostrEvent(event *shared.NostrEvent) string {
tags, _ := json.Marshal(event.Tags)
return fmt.Sprintf("[0,%q,%d,%d,%s,%q]",
event.PubKey, event.CreatedAt, event.Kind, string(tags), event.Content)
}
// verifyNostrEvent verifies a Nostr event according to NIP-01 by checking that the SHA-256 hash of the canonical serialization matches event.ID and by validating the Schnorr signature.
// It returns an error if event is nil, if the recomputed ID does not match event.ID, or if signature verification fails.
func verifyNostrEvent(event *shared.NostrEvent) error {
if event == nil {
return fmt.Errorf("event cannot be nil")
}
// Recompute event ID
serialized := serializeNostrEvent(event)
hash := sha256.Sum256([]byte(serialized))
expectedID := hex.EncodeToString(hash[:])
if event.ID != expectedID {
return fmt.Errorf("event ID mismatch: expected %s, got %s", expectedID, event.ID)
}
// Verify Schnorr signature
return crypto.VerifySchnorrEventSignature(event.PubKey, event.ID, event.Sig)
}