2024-07-20 01:09:42 +00:00
|
|
|
package events
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-07-22 17:50:43 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
2024-07-20 01:09:42 +00:00
|
|
|
"fmt"
|
2024-07-22 18:53:59 +00:00
|
|
|
"log"
|
2024-07-20 01:09:42 +00:00
|
|
|
|
2024-07-22 17:50:43 +00:00
|
|
|
"github.com/btcsuite/btcd/btcec/v2"
|
|
|
|
"github.com/btcsuite/btcd/btcec/v2/schnorr"
|
2024-07-20 01:09:42 +00:00
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
2024-07-22 17:50:43 +00:00
|
|
|
"golang.org/x/net/websocket"
|
2024-07-20 01:09:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Event struct {
|
2024-07-22 17:50:43 +00:00
|
|
|
ID string `json:"id"`
|
|
|
|
PubKey string `json:"pubkey"`
|
|
|
|
CreatedAt int64 `json:"created_at"`
|
|
|
|
Kind int `json:"kind"`
|
|
|
|
Tags [][]string `json:"tags"`
|
|
|
|
Content string `json:"content"`
|
|
|
|
Sig string `json:"sig"`
|
2024-07-20 01:09:42 +00:00
|
|
|
}
|
|
|
|
|
2024-07-23 00:08:04 +00:00
|
|
|
var (
|
|
|
|
client *mongo.Client
|
|
|
|
collections = make(map[int]*mongo.Collection)
|
|
|
|
)
|
|
|
|
|
|
|
|
func SetClient(mongoClient *mongo.Client) {
|
|
|
|
client = mongoClient
|
2024-07-22 17:50:43 +00:00
|
|
|
}
|
2024-07-20 01:09:42 +00:00
|
|
|
|
2024-07-23 00:08:04 +00:00
|
|
|
func GetCollection(kind int) *mongo.Collection {
|
2024-07-22 17:50:43 +00:00
|
|
|
if collection, exists := collections[kind]; exists {
|
|
|
|
return collection
|
|
|
|
}
|
|
|
|
collectionName := fmt.Sprintf("event-kind%d", kind)
|
|
|
|
collection := client.Database("grain").Collection(collectionName)
|
|
|
|
collections[kind] = collection
|
2024-07-20 01:09:42 +00:00
|
|
|
indexModel := mongo.IndexModel{
|
|
|
|
Keys: bson.D{{Key: "id", Value: 1}},
|
|
|
|
Options: options.Index().SetUnique(true),
|
|
|
|
}
|
2024-07-22 17:50:43 +00:00
|
|
|
_, err := collection.Indexes().CreateOne(context.TODO(), indexModel)
|
2024-07-20 01:09:42 +00:00
|
|
|
if err != nil {
|
2024-07-22 17:50:43 +00:00
|
|
|
fmt.Printf("Failed to create index on %s: %v\n", collectionName, err)
|
2024-07-20 01:09:42 +00:00
|
|
|
}
|
2024-07-22 17:50:43 +00:00
|
|
|
return collection
|
2024-07-20 01:09:42 +00:00
|
|
|
}
|
|
|
|
|
2024-07-23 00:08:04 +00:00
|
|
|
func HandleEvent(ctx context.Context, evt Event, ws *websocket.Conn) {
|
|
|
|
if !CheckSignature(evt) {
|
2024-07-22 17:50:43 +00:00
|
|
|
sendOKResponse(ws, evt.ID, false, "invalid: signature verification failed")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-23 00:08:04 +00:00
|
|
|
collection := GetCollection(evt.Kind)
|
2024-07-22 17:50:43 +00:00
|
|
|
|
|
|
|
var err error
|
2024-07-20 01:09:42 +00:00
|
|
|
switch evt.Kind {
|
|
|
|
case 0:
|
2024-07-22 17:50:43 +00:00
|
|
|
err = HandleEventKind0(ctx, evt, collection)
|
2024-07-20 01:09:42 +00:00
|
|
|
case 1:
|
2024-07-22 17:50:43 +00:00
|
|
|
err = HandleEventKind1(ctx, evt, collection)
|
2024-07-20 01:09:42 +00:00
|
|
|
default:
|
2024-07-23 00:08:04 +00:00
|
|
|
err = HandleUnknownEvent(ctx, evt, collection)
|
2024-07-22 17:50:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
sendOKResponse(ws, evt.ID, false, fmt.Sprintf("error: %v", err))
|
|
|
|
return
|
2024-07-20 01:09:42 +00:00
|
|
|
}
|
2024-07-22 17:50:43 +00:00
|
|
|
|
|
|
|
sendOKResponse(ws, evt.ID, true, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendOKResponse(ws *websocket.Conn, eventID string, status bool, message string) {
|
|
|
|
response := []interface{}{"OK", eventID, status, message}
|
|
|
|
responseBytes, _ := json.Marshal(response)
|
|
|
|
websocket.Message.Send(ws, string(responseBytes))
|
2024-07-20 01:09:42 +00:00
|
|
|
}
|
|
|
|
|
2024-07-22 17:50:43 +00:00
|
|
|
func SerializeEvent(evt Event) []byte {
|
|
|
|
eventData := []interface{}{
|
|
|
|
0,
|
|
|
|
evt.PubKey,
|
|
|
|
evt.CreatedAt,
|
|
|
|
evt.Kind,
|
|
|
|
evt.Tags,
|
|
|
|
evt.Content,
|
|
|
|
}
|
|
|
|
serializedEvent, _ := json.Marshal(eventData)
|
|
|
|
return serializedEvent
|
|
|
|
}
|
|
|
|
|
2024-07-23 00:08:04 +00:00
|
|
|
func CheckSignature(evt Event) bool {
|
|
|
|
serializedEvent := SerializeEvent(evt)
|
|
|
|
hash := sha256.Sum256(serializedEvent)
|
|
|
|
eventID := hex.EncodeToString(hash[:])
|
|
|
|
if eventID != evt.ID {
|
|
|
|
log.Printf("Invalid ID: expected %s, got %s\n", eventID, evt.ID)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
sigBytes, err := hex.DecodeString(evt.Sig)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error decoding signature: %v\n", err)
|
|
|
|
return false
|
|
|
|
}
|
2024-07-22 18:53:59 +00:00
|
|
|
|
2024-07-23 00:08:04 +00:00
|
|
|
sig, err := schnorr.ParseSignature(sigBytes)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error parsing signature: %v\n", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
pubKeyBytes, err := hex.DecodeString(evt.PubKey)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error decoding public key: %v\n", err)
|
|
|
|
return false
|
|
|
|
}
|
2024-07-22 18:53:59 +00:00
|
|
|
|
2024-07-23 00:08:04 +00:00
|
|
|
var pubKey *btcec.PublicKey
|
|
|
|
if len(pubKeyBytes) == 32 {
|
|
|
|
// Handle 32-byte public key (x-coordinate only)
|
|
|
|
pubKey, err = btcec.ParsePubKey(append([]byte{0x02}, pubKeyBytes...))
|
|
|
|
} else {
|
|
|
|
// Handle standard compressed or uncompressed public key
|
|
|
|
pubKey, err = btcec.ParsePubKey(pubKeyBytes)
|
|
|
|
}
|
2024-07-22 17:50:43 +00:00
|
|
|
if err != nil {
|
2024-07-23 00:08:04 +00:00
|
|
|
log.Printf("Error parsing public key: %v\n", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
verified := sig.Verify(hash[:], pubKey)
|
|
|
|
if !verified {
|
|
|
|
log.Printf("Signature verification failed for event ID: %s\n", evt.ID)
|
2024-07-22 17:50:43 +00:00
|
|
|
}
|
|
|
|
|
2024-07-23 00:08:04 +00:00
|
|
|
return verified
|
2024-07-20 01:09:42 +00:00
|
|
|
}
|