|
| 1 | +package cctp |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "sync" |
| 8 | + |
| 9 | + "github.com/gin-gonic/gin" |
| 10 | + |
| 11 | + cctpclient "github.com/smartcontractkit/chainlink-ccv/verifier/token/cctp" |
| 12 | + "github.com/smartcontractkit/chainlink-testing-framework/framework/components/fake" |
| 13 | +) |
| 14 | + |
| 15 | +type RegisterAttestationRequest struct { |
| 16 | + SourceDomain string `json:"sourceDomain" binding:"required"` |
| 17 | + MessageID string `json:"messageID" binding:"required"` |
| 18 | + Status string `json:"status" binding:"required"` |
| 19 | + MessageSender string `json:"messageSender" binding:"required"` |
| 20 | + Message string `json:"message"` |
| 21 | + Attestation string `json:"attestation"` |
| 22 | +} |
| 23 | + |
| 24 | +type AttestationAPI struct { |
| 25 | + mu sync.RWMutex |
| 26 | + responses map[string]cctpclient.Message |
| 27 | +} |
| 28 | + |
| 29 | +func NewAttestationAPI() *AttestationAPI { |
| 30 | + return &AttestationAPI{ |
| 31 | + responses: make(map[string]cctpclient.Message), |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +// RegisterAttestation registers a new attestation response for a given sourceDomain. |
| 36 | +func (a *AttestationAPI) RegisterAttestation(sourceDomain, messageID, status, message, attestation, messageSender string) cctpclient.Message { |
| 37 | + a.mu.Lock() |
| 38 | + defer a.mu.Unlock() |
| 39 | + |
| 40 | + // Construct hookData as 0x8e1d1a9d + messageID (without 0x prefix) |
| 41 | + // Strip 0x prefix from messageID if present |
| 42 | + cleanMessageID := messageID |
| 43 | + if len(messageID) > 2 && messageID[:2] == "0x" { |
| 44 | + cleanMessageID = messageID[2:] |
| 45 | + } |
| 46 | + hookData := "0x8e1d1a9d" + cleanMessageID |
| 47 | + |
| 48 | + // Use provided values or defaults |
| 49 | + if message == "" { |
| 50 | + message = "0xbbbbbbbb" |
| 51 | + } |
| 52 | + if attestation == "" { |
| 53 | + attestation = "0xaaaaaaaa" |
| 54 | + } |
| 55 | + |
| 56 | + // Create a response based on the example attestation but with the provided sourceDomain, hookData and status |
| 57 | + response := cctpclient.Message{ |
| 58 | + Message: message, |
| 59 | + EventNonce: "9682", |
| 60 | + Attestation: attestation, |
| 61 | + DecodedMessage: cctpclient.DecodedMessage{ |
| 62 | + SourceDomain: sourceDomain, |
| 63 | + DestinationDomain: "5", |
| 64 | + Nonce: "569", |
| 65 | + Sender: "0xthis_is_ignored_for_simplicity", |
| 66 | + Recipient: "0xthis_is_ignored_for_simplicity", |
| 67 | + DestinationCaller: "0xthis_is_ignored_for_simplicity", |
| 68 | + MessageBody: "0xthis_is_ignored_for_simplicity", |
| 69 | + DecodedMessageBody: cctpclient.DecodedMessageBody{ |
| 70 | + BurnToken: "0xthis_is_ignored_for_simplicity", |
| 71 | + MintRecipient: "0xthis_is_ignored_for_simplicity", |
| 72 | + Amount: "1000", |
| 73 | + MessageSender: messageSender, |
| 74 | + HookData: hookData, |
| 75 | + }, |
| 76 | + }, |
| 77 | + CCTPVersion: "2", |
| 78 | + Status: cctpclient.AttestationStatus(status), |
| 79 | + } |
| 80 | + |
| 81 | + a.responses[sourceDomain] = response |
| 82 | + return response |
| 83 | +} |
| 84 | + |
| 85 | +func (a *AttestationAPI) Register() error { |
| 86 | + // POST endpoint to register attestation responses |
| 87 | + err := fake.Func("POST", "/cctp/v2/attestations", func(ctx *gin.Context) { |
| 88 | + var req RegisterAttestationRequest |
| 89 | + if err := ctx.ShouldBindJSON(&req); err != nil { |
| 90 | + ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) |
| 91 | + return |
| 92 | + } |
| 93 | + |
| 94 | + response := a.RegisterAttestation(req.SourceDomain, req.MessageID, req.Status, req.Message, req.Attestation, req.MessageSender) |
| 95 | + ctx.JSON(http.StatusOK, response) |
| 96 | + }) |
| 97 | + if err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + |
| 101 | + // GET endpoint to retrieve attestation by sourceDomain |
| 102 | + err = fake.Func("GET", "/cctp/v2/messages/:sourceDomain", func(ctx *gin.Context) { |
| 103 | + sourceDomain := ctx.Param("sourceDomain") |
| 104 | + |
| 105 | + a.mu.RLock() |
| 106 | + response, exists := a.responses[sourceDomain] |
| 107 | + a.mu.RUnlock() |
| 108 | + |
| 109 | + if !exists { |
| 110 | + ctx.JSON(http.StatusNotFound, gin.H{"error": fmt.Sprintf("no attestation found for sourceDomain: %s", sourceDomain)}) |
| 111 | + return |
| 112 | + } |
| 113 | + |
| 114 | + // Return wrapped in "messages" array to match the expected format |
| 115 | + wrappedResponse := cctpclient.Messages{ |
| 116 | + Messages: []cctpclient.Message{response}, |
| 117 | + } |
| 118 | + responseBytes, err := json.Marshal(wrappedResponse) |
| 119 | + if err != nil { |
| 120 | + ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) |
| 121 | + return |
| 122 | + } |
| 123 | + |
| 124 | + ctx.Data(http.StatusOK, "application/json", responseBytes) |
| 125 | + }) |
| 126 | + if err != nil { |
| 127 | + return err |
| 128 | + } |
| 129 | + |
| 130 | + return nil |
| 131 | +} |
0 commit comments