MCPcopy Create free account
hub / github.com/NoFxAiOS/nofx / handleUpdateExchangeConfigs

Method handleUpdateExchangeConfigs

api/handler_exchange.go:166–328  ·  view source on GitHub ↗

handleUpdateExchangeConfigs Update exchange configurations (supports both encrypted and plain text based on config)

(c *gin.Context)

Source from the content-addressed store, hash-verified

164
165// handleUpdateExchangeConfigs Update exchange configurations (supports both encrypted and plain text based on config)
166func (s *Server) handleUpdateExchangeConfigs(c *gin.Context) {
167 userID := c.GetString("user_id")
168 cfg := config.Get()
169
170 // Read raw request body
171 bodyBytes, err := c.GetRawData()
172 if err != nil {
173 c.JSON(http.StatusBadRequest, gin.H{"error": "Failed to read request body"})
174 return
175 }
176
177 var req UpdateExchangeConfigRequest
178
179 // Check if transport encryption is enabled
180 if !cfg.TransportEncryption {
181 // Transport encryption disabled, accept plain JSON
182 if err := json.Unmarshal(bodyBytes, &req); err != nil {
183 logger.Infof("❌ Failed to parse plain JSON request: %v", err)
184 c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request format"})
185 return
186 }
187 logger.Infof("📝 Received plain text exchange config (UserID: %s)", userID)
188 } else {
189 // Transport encryption enabled, require encrypted payload
190 var encryptedPayload crypto.EncryptedPayload
191 if err := json.Unmarshal(bodyBytes, &encryptedPayload); err != nil {
192 logger.Infof("❌ Failed to parse encrypted payload: %v", err)
193 c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request format, encrypted transmission required"})
194 return
195 }
196
197 // Verify encrypted data
198 if encryptedPayload.WrappedKey == "" {
199 logger.Infof("❌ Detected unencrypted request (UserID: %s)", userID)
200 c.JSON(http.StatusBadRequest, gin.H{
201 "error": "This endpoint only supports encrypted transmission, please use encrypted client",
202 "code": "ENCRYPTION_REQUIRED",
203 "message": "Encrypted transmission is required for security reasons",
204 })
205 return
206 }
207
208 // Decrypt data
209 decrypted, err := s.cryptoHandler.cryptoService.DecryptSensitiveData(&encryptedPayload)
210 if err != nil {
211 logger.Infof("❌ Failed to decrypt exchange config (UserID: %s): %v", userID, err)
212 c.JSON(http.StatusBadRequest, gin.H{"error": "Failed to decrypt data"})
213 return
214 }
215
216 // Parse decrypted data
217 if err := json.Unmarshal([]byte(decrypted), &req); err != nil {
218 logger.Infof("❌ Failed to parse decrypted data: %v", err)
219 c.JSON(http.StatusBadRequest, gin.H{"error": "Failed to parse decrypted data"})
220 return
221 }
222 logger.Infof("🔓 Decrypted exchange config data (UserID: %s)", userID)
223 }

Callers

nothing calls this directly

Calls 15

GetFunction · 0.92
InfofFunction · 0.92
SafeInternalErrorFunction · 0.85
DecryptSensitiveDataMethod · 0.80
ExchangeMethod · 0.80
ListByExchangeIDMethod · 0.80
TraderMethod · 0.80
InvalidateMethod · 0.80
RemoveTraderMethod · 0.80

Tested by

no test coverage detected