2024-07-23 14:53:01 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
2024-09-05 15:58:18 +00:00
|
|
|
"fmt"
|
2024-07-23 14:53:01 +00:00
|
|
|
"log"
|
2024-09-05 15:58:18 +00:00
|
|
|
"strings"
|
2024-07-23 14:53:01 +00:00
|
|
|
|
2024-07-31 18:12:33 +00:00
|
|
|
relay "grain/server/types"
|
2024-07-23 14:53:01 +00:00
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/btcec/v2"
|
|
|
|
"github.com/btcsuite/btcd/btcec/v2/schnorr"
|
|
|
|
)
|
2024-09-05 15:58:18 +00:00
|
|
|
|
2024-09-17 13:24:35 +00:00
|
|
|
// EscapeSpecialChars escapes special characters in the content according to NIP-01
|
|
|
|
func EscapeSpecialChars(content string) string {
|
|
|
|
content = strings.ReplaceAll(content, "\\", "\\\\")
|
|
|
|
content = strings.ReplaceAll(content, "\"", "\\\"")
|
|
|
|
content = strings.ReplaceAll(content, "\n", "\\n")
|
|
|
|
content = strings.ReplaceAll(content, "\r", "\\r")
|
|
|
|
content = strings.ReplaceAll(content, "\t", "\\t")
|
|
|
|
content = strings.ReplaceAll(content, "\b", "\\b")
|
|
|
|
content = strings.ReplaceAll(content, "\f", "\\f")
|
|
|
|
return content
|
|
|
|
}
|
|
|
|
|
|
|
|
// SerializeEvent manually constructs the JSON string for event serialization according to NIP-01
|
2024-09-05 15:58:18 +00:00
|
|
|
func SerializeEvent(evt relay.Event) string {
|
2024-09-17 13:24:35 +00:00
|
|
|
// Escape special characters in the content
|
|
|
|
escapedContent := EscapeSpecialChars(evt.Content)
|
|
|
|
|
2024-09-05 15:58:18 +00:00
|
|
|
// Manually construct the event data as a JSON array string
|
|
|
|
return fmt.Sprintf(
|
|
|
|
`[0,"%s",%d,%d,%s,"%s"]`,
|
2024-07-23 14:53:01 +00:00
|
|
|
evt.PubKey,
|
|
|
|
evt.CreatedAt,
|
|
|
|
evt.Kind,
|
2024-09-05 15:58:18 +00:00
|
|
|
serializeTags(evt.Tags),
|
2024-09-17 13:24:35 +00:00
|
|
|
escapedContent, // Special characters are escaped
|
2024-09-05 15:58:18 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper function to serialize the tags array
|
|
|
|
func serializeTags(tags [][]string) string {
|
|
|
|
var tagStrings []string
|
|
|
|
for _, tag := range tags {
|
|
|
|
tagStrings = append(tagStrings, fmt.Sprintf(`["%s"]`, strings.Join(tag, `","`)))
|
2024-07-23 14:53:01 +00:00
|
|
|
}
|
2024-09-05 15:58:18 +00:00
|
|
|
return "[" + strings.Join(tagStrings, ",") + "]"
|
2024-07-23 14:53:01 +00:00
|
|
|
}
|
|
|
|
|
2024-09-05 15:58:18 +00:00
|
|
|
// CheckSignature verifies the event's signature and ID
|
2024-07-23 17:30:29 +00:00
|
|
|
func CheckSignature(evt relay.Event) bool {
|
2024-09-05 15:58:18 +00:00
|
|
|
// Manually serialize the event
|
2024-07-23 14:53:01 +00:00
|
|
|
serializedEvent := SerializeEvent(evt)
|
2024-09-05 15:58:18 +00:00
|
|
|
|
|
|
|
// Compute the SHA-256 hash of the serialized event
|
|
|
|
hash := sha256.Sum256([]byte(serializedEvent))
|
2024-07-23 14:53:01 +00:00
|
|
|
eventID := hex.EncodeToString(hash[:])
|
2024-09-05 15:58:18 +00:00
|
|
|
|
|
|
|
// Log the generated and provided IDs
|
|
|
|
log.Printf("Generated event ID: %s, Provided event ID: %s", eventID, evt.ID)
|
|
|
|
|
|
|
|
// Compare the computed event ID with the one provided by the client
|
2024-07-23 14:53:01 +00:00
|
|
|
if eventID != evt.ID {
|
2024-09-05 15:58:18 +00:00
|
|
|
log.Printf("Invalid ID: expected %s, got %s", eventID, evt.ID)
|
2024-07-23 14:53:01 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-09-05 15:58:18 +00:00
|
|
|
// Decode the signature from hex
|
2024-07-23 14:53:01 +00:00
|
|
|
sigBytes, err := hex.DecodeString(evt.Sig)
|
|
|
|
if err != nil {
|
2024-09-05 15:58:18 +00:00
|
|
|
log.Printf("Error decoding signature: %v", err)
|
2024-07-23 14:53:01 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-09-05 15:58:18 +00:00
|
|
|
// Parse the Schnorr signature
|
2024-07-23 14:53:01 +00:00
|
|
|
sig, err := schnorr.ParseSignature(sigBytes)
|
|
|
|
if err != nil {
|
2024-09-05 15:58:18 +00:00
|
|
|
log.Printf("Error parsing signature: %v", err)
|
2024-07-23 14:53:01 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-09-05 15:58:18 +00:00
|
|
|
// Decode the public key from hex
|
2024-07-23 14:53:01 +00:00
|
|
|
pubKeyBytes, err := hex.DecodeString(evt.PubKey)
|
|
|
|
if err != nil {
|
2024-09-05 15:58:18 +00:00
|
|
|
log.Printf("Error decoding public key: %v", err)
|
2024-07-23 14:53:01 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-09-17 13:24:35 +00:00
|
|
|
// Since the public key is 32 bytes, prepend 0x02 (assuming y-coordinate is even)
|
2024-07-23 14:53:01 +00:00
|
|
|
if len(pubKeyBytes) == 32 {
|
2024-09-17 13:24:35 +00:00
|
|
|
pubKeyBytes = append([]byte{0x02}, pubKeyBytes...)
|
2024-09-05 15:58:18 +00:00
|
|
|
} else {
|
|
|
|
log.Printf("Malformed public key: invalid length: %d", len(pubKeyBytes))
|
|
|
|
return false
|
2024-07-23 14:53:01 +00:00
|
|
|
}
|
2024-09-05 15:58:18 +00:00
|
|
|
|
2024-09-17 13:24:35 +00:00
|
|
|
// Parse the public key
|
|
|
|
pubKey, err := btcec.ParsePubKey(pubKeyBytes)
|
2024-07-23 14:53:01 +00:00
|
|
|
if err != nil {
|
2024-09-05 15:58:18 +00:00
|
|
|
log.Printf("Error parsing public key: %v", err)
|
2024-07-23 14:53:01 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-09-05 15:58:18 +00:00
|
|
|
// Verify the signature using the event's hash and public key
|
2024-07-23 14:53:01 +00:00
|
|
|
verified := sig.Verify(hash[:], pubKey)
|
|
|
|
if !verified {
|
2024-09-05 15:58:18 +00:00
|
|
|
log.Printf("Signature verification failed for event ID: %s", evt.ID)
|
2024-07-23 14:53:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return verified
|
2024-09-17 13:24:35 +00:00
|
|
|
}
|