|
| 1 | +package handlers |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/base64" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + |
| 8 | + "github.com/dappnode/validator-monitoring/listener/internal/api/types" |
| 9 | + "github.com/dappnode/validator-monitoring/listener/internal/api/validation" |
| 10 | + "github.com/dappnode/validator-monitoring/listener/internal/logger" |
| 11 | + "go.mongodb.org/mongo-driver/bson" |
| 12 | + "go.mongodb.org/mongo-driver/mongo" |
| 13 | +) |
| 14 | + |
| 15 | +type signatureRequest struct { |
| 16 | + Payload string `json:"payload"` |
| 17 | + Signature string `json:"signature"` |
| 18 | + Network string `json:"network"` |
| 19 | + Label string `json:"label"` |
| 20 | +} |
| 21 | + |
| 22 | +func PostNewSignature(w http.ResponseWriter, r *http.Request, dbCollection *mongo.Collection) { |
| 23 | + logger.Debug("Received new POST '/newSignature' request") |
| 24 | + |
| 25 | + var sigs []signatureRequest |
| 26 | + err := json.NewDecoder(r.Body).Decode(&sigs) |
| 27 | + if err != nil { |
| 28 | + logger.Error("Failed to decode request body: " + err.Error()) |
| 29 | + respondError(w, http.StatusBadRequest, "Invalid request format") |
| 30 | + return |
| 31 | + } |
| 32 | + |
| 33 | + var validPubkeys []string // Needed to store valid pubkeys for bulk validation later |
| 34 | + |
| 35 | + // For each element of the request slice, we validate the element format and decode its payload |
| 36 | + for _, req := range sigs { |
| 37 | + if req.Network == "" || req.Label == "" || req.Signature == "" || req.Payload == "" { |
| 38 | + logger.Debug("Skipping invalid signature from request, missing fields") |
| 39 | + continue // Skipping invalid elements |
| 40 | + } |
| 41 | + if !validation.ValidateSignature(req.Signature) { |
| 42 | + logger.Debug("Skipping invalid signature from request, invalid signature format: " + req.Signature) |
| 43 | + continue // Skipping invalid signatures |
| 44 | + } |
| 45 | + decodedBytes, err := base64.StdEncoding.DecodeString(req.Payload) |
| 46 | + if err != nil { |
| 47 | + logger.Error("Failed to decode BASE64 payload from request: " + err.Error()) |
| 48 | + continue // Skipping payloads that can't be decoded from BASE64 |
| 49 | + } |
| 50 | + var decodedPayload types.DecodedPayload |
| 51 | + if err := json.Unmarshal(decodedBytes, &decodedPayload); err != nil { |
| 52 | + logger.Error("Failed to decode JSON payload from request: " + err.Error()) |
| 53 | + continue // Skipping payloads that can't be decoded from JSON |
| 54 | + } |
| 55 | + // If the payload is valid, we append the pubkey to the validPubkeys slice. Else, we skip it |
| 56 | + if decodedPayload.Platform == "dappnode" && decodedPayload.Timestamp != "" && decodedPayload.Pubkey != "" { |
| 57 | + validPubkeys = append(validPubkeys, decodedPayload.Pubkey) // Collecting valid pubkeys |
| 58 | + } else { |
| 59 | + logger.Debug("Skipping invalid signature from request, invalid payload format") |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // Make a single API call to validate pubkeys in bulk |
| 64 | + validatedPubkeys := validation.ValidatePubkeysWithConsensusClient(validPubkeys) |
| 65 | + if len(validatedPubkeys) == 0 { |
| 66 | + respondError(w, http.StatusInternalServerError, "Failed to validate pubkeys with consensus client") |
| 67 | + return |
| 68 | + } |
| 69 | + |
| 70 | + // Now, iterate over the originally valid requests, check if the pubkey was validated, then verify signature and insert into DB |
| 71 | + // This means going over the requests again! TODO: find a better way? |
| 72 | + for _, req := range sigs { |
| 73 | + decodedBytes, _ := base64.StdEncoding.DecodeString(req.Payload) |
| 74 | + var decodedPayload types.DecodedPayload |
| 75 | + json.Unmarshal(decodedBytes, &decodedPayload) |
| 76 | + |
| 77 | + // Only try to validate message signatures if the pubkey is validated |
| 78 | + if _, exists := validatedPubkeys[decodedPayload.Pubkey]; exists { |
| 79 | + // If the pubkey is validated, we can proceed to validate the signature |
| 80 | + if validation.ValidateSignatureAgainstPayload(req.Signature, decodedPayload) { |
| 81 | + // Insert into MongoDB |
| 82 | + _, err := dbCollection.InsertOne(r.Context(), bson.M{ |
| 83 | + "platform": decodedPayload.Platform, |
| 84 | + "timestamp": decodedPayload.Timestamp, |
| 85 | + "pubkey": decodedPayload.Pubkey, |
| 86 | + "signature": req.Signature, |
| 87 | + "network": req.Network, |
| 88 | + "label": req.Label, |
| 89 | + }) |
| 90 | + if err != nil { |
| 91 | + continue // TODO: Log error or handle as needed |
| 92 | + } else { |
| 93 | + logger.Info("New Signature " + req.Signature + " inserted into MongoDB") |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + respondOK(w, "Finished processing signatures") |
| 100 | +} |
0 commit comments