@@ -107,18 +107,24 @@ func (s *EspressoReaderService) requestNonce(w http.ResponseWriter, r *http.Requ
107107 w .Header ().Set ("Access-Control-Allow-Origin" , "*" )
108108 w .Header ().Set ("Access-Control-Allow-Headers" , "Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers" )
109109 if r .Method != http .MethodPost {
110+ w .WriteHeader (http .StatusMethodNotAllowed )
111+ json .NewEncoder (w ).Encode (map [string ]string {"error" : "Only POST method is allowed" })
110112 return
111113 }
112114
113115 body , err := io .ReadAll (r .Body )
114116 if err != nil {
115- slog .Error ("could not read body" , "err" , err )
116- // TODO: error?
117+ slog .Error ("could not read request body" , "error" , err )
118+ http .Error (w , "Failed to read request body" , http .StatusBadRequest )
119+ return
117120 }
121+ defer r .Body .Close ()
122+
118123 nonceRequest := & NonceRequest {}
119124 if err := json .Unmarshal (body , nonceRequest ); err != nil {
120- slog .Error ("could not unmarshal" , "err" , err )
121- // ?
125+ slog .Error ("could not unmarshal request body" , "error" , err , "body" , string (body ))
126+ http .Error (w , "Invalid request body" , http .StatusBadRequest )
127+ return
122128 }
123129
124130 senderAddress := common .HexToAddress (nonceRequest .MsgSender )
@@ -128,9 +134,15 @@ func (s *EspressoReaderService) requestNonce(w http.ResponseWriter, r *http.Requ
128134 if nonceCache [applicationAddress ] == nil {
129135 nonceCache [applicationAddress ] = make (map [common.Address ]uint64 )
130136 }
131- if nonceCache [applicationAddress ][senderAddress ] == 0 {
137+ _ , exists := nonceCache [applicationAddress ][senderAddress ]
138+ if ! exists {
132139 ctx := r .Context ()
133- nonce = s .queryNonceFromDb (ctx , senderAddress , applicationAddress )
140+ nonce , err = s .queryNonceFromDb (ctx , senderAddress , applicationAddress )
141+ if err != nil {
142+ slog .Error ("failed to query nonce from database" , "error" , err , "senderAddress" , senderAddress , "applicationAddress" , applicationAddress )
143+ http .Error (w , "Failed to retrieve nonce" , http .StatusInternalServerError )
144+ return
145+ }
134146 nonceCache [applicationAddress ][senderAddress ] = nonce
135147 } else {
136148 nonce = nonceCache [applicationAddress ][senderAddress ]
@@ -139,10 +151,6 @@ func (s *EspressoReaderService) requestNonce(w http.ResponseWriter, r *http.Requ
139151 slog .Debug ("got nonce request" , "senderAddress" , senderAddress , "applicationAddress" , applicationAddress )
140152
141153 nonceResponse := NonceResponse {Nonce : nonce }
142- if err != nil {
143- slog .Error ("error json marshal nonce response" , "err" , err )
144- // ?
145- }
146154
147155 err = json .NewEncoder (w ).Encode (nonceResponse )
148156 if err != nil {
@@ -156,14 +164,14 @@ func (s *EspressoReaderService) requestNonce(w http.ResponseWriter, r *http.Requ
156164func (s * EspressoReaderService ) queryNonceFromDb (
157165 ctx context.Context ,
158166 senderAddress common.Address ,
159- applicationAddress common.Address ) uint64 {
167+ applicationAddress common.Address ) ( uint64 , error ) {
160168 nonce , err := s .database .GetEspressoNonce (ctx , senderAddress .Hex (), applicationAddress .Hex ())
161169 if err != nil {
162170 slog .Error ("failed to get espresso nonce" , "error" , err )
163- // TODO: error?
171+ return 0 , err
164172 }
165173
166- return nonce
174+ return nonce , nil
167175}
168176
169177type SubmitResponse struct {
@@ -175,20 +183,23 @@ func (s *EspressoReaderService) submit(w http.ResponseWriter, r *http.Request) {
175183 w .Header ().Set ("Access-Control-Allow-Origin" , "*" )
176184 w .Header ().Set ("Access-Control-Allow-Headers" , "Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers" )
177185 if r .Method != http .MethodPost {
178- // TODO: error?
186+ w .WriteHeader (http .StatusMethodNotAllowed )
187+ json .NewEncoder (w ).Encode (map [string ]string {"error" : "Only POST method is allowed" })
179188 return
180189 }
181190
182191 body , err := io .ReadAll (r .Body )
183192 if err != nil {
184- slog .Error ("could not read body" , "err" , err )
193+ slog .Error ("could not read request body" , "error" , err )
194+ http .Error (w , "Failed to read request body" , http .StatusBadRequest )
195+ return
185196 }
186197 slog .Debug ("got submit request" , "request body" , string (body ))
187198
188199 msgSender , typedData , sigHash , err := ExtractSigAndData (string (body ))
189200 if err != nil {
190201 slog .Error ("transaction not correctly formatted" , "error" , err )
191- // TODO: error?
202+ http . Error ( w , "Invalid transaction format" , http . StatusBadRequest )
192203 return
193204 }
194205 submitResponse := SubmitResponse {Id : sigHash }
@@ -198,43 +209,62 @@ func (s *EspressoReaderService) submit(w http.ResponseWriter, r *http.Request) {
198209 client := client .NewClient (s .EspressoBaseUrl )
199210 ctx := r .Context ()
200211 _ , namespace , err := getEspressoConfig (ctx , appAddress , s .database , s .blockchainHttpEndpoint )
212+ if err != nil {
213+ slog .Error ("failed to get espresso config" , "error" , err , "appAddress" , appAddress )
214+ http .Error (w , "Failed to get application configuration" , http .StatusInternalServerError )
215+ return
216+ }
201217 var tx types.Transaction
202218 tx .Namespace = namespace
203219 tx .Payload = body
204220 _ , err = client .SubmitTransaction (ctx , tx )
205221 if err != nil {
206- slog .Error ("espresso tx submit error" , "err " , err )
207- // TODO: error?
222+ slog .Error ("espresso tx submit error" , "error " , err )
223+ http . Error ( w , "Failed to submit transaction to Espresso" , http . StatusInternalServerError )
208224 return
209225 }
210226
211- err = json .NewEncoder (w ).Encode (submitResponse )
212- if err != nil {
213- slog .Info ("Internal server error" ,
214- "service" , "espresso submit endpoint" ,
215- "err" , err )
216- http .Error (w , err .Error (), http .StatusInternalServerError )
217- // TODO: error?
227+ if err := json .NewEncoder (w ).Encode (submitResponse ); err != nil {
228+ slog .Error ("failed to encode submit response" , "error" , err , "submitResponse" , submitResponse )
229+ http .Error (w , "Internal server error" , http .StatusInternalServerError )
218230 return
219231 }
220232
221233 // update nonce cache
222234 if nonceCache [appAddress ] == nil {
223- slog .Error ("Should query nonce before submit" )
224- // TODO: error?
235+ slog .Error ("Should query nonce before submit" , "appAddress" , appAddress )
236+ http .Error (w , "Nonce not initialized for this application. Please request a nonce first." , http .StatusBadRequest )
237+ return
238+ }
239+ nonceInRequestFloat , ok := typedData .Message ["nonce" ].(float64 )
240+ if ! ok {
241+ slog .Error ("Nonce not found or invalid type in request" , "typedData" , typedData )
242+ http .Error (w , "Invalid or missing nonce in request" , http .StatusBadRequest )
225243 return
226244 }
227- nonceInRequest := uint64 (typedData .Message ["nonce" ].(float64 ))
228- if nonceCache [appAddress ][msgSender ] == 0 {
245+ nonceInRequest := uint64 (nonceInRequestFloat )
246+
247+ cachedNonce , exists := nonceCache [appAddress ][msgSender ]
248+ if ! exists {
229249 ctx := r .Context ()
230- nonceInDb := s .queryNonceFromDb (ctx , msgSender , appAddress )
250+ nonceInDb , err := s .queryNonceFromDb (ctx , msgSender , appAddress )
251+ if err != nil {
252+ slog .Error ("failed to query nonce from database during submit" , "error" , err , "senderAddress" , msgSender , "applicationAddress" , appAddress )
253+ http .Error (w , "Failed to retrieve nonce for validation" , http .StatusInternalServerError )
254+ return
255+ }
231256 if nonceInRequest != nonceInDb {
232- slog .Error ("Nonce in request is incorrect" )
233- // TODO: error?
257+ slog .Error ("Nonce in request is incorrect" , "nonceInRequest" , nonceInRequest , "nonceInDb" , nonceInDb , "senderAddress" , msgSender , "applicationAddress" , appAddress )
258+ http . Error ( w , "Incorrect nonce" , http . StatusBadRequest )
234259 return
235260 }
236261 nonceCache [appAddress ][msgSender ] = nonceInDb + 1
237262 } else {
263+ if nonceInRequest != cachedNonce {
264+ slog .Error ("Nonce in request is incorrect" , "requestNonce" , nonceInRequest , "cachedNonce" , cachedNonce , "senderAddress" , msgSender , "applicationAddress" , appAddress )
265+ http .Error (w , "Incorrect nonce" , http .StatusBadRequest )
266+ return
267+ }
238268 nonceCache [appAddress ][msgSender ]++
239269 }
240270}
0 commit comments