|
15 | 15 | package api
|
16 | 16 |
|
17 | 17 | import (
|
| 18 | + "context" |
18 | 19 | "encoding/hex"
|
19 | 20 | "encoding/json"
|
| 21 | + "fmt" |
| 22 | + "io" |
20 | 23 | "log/slog"
|
21 | 24 | "net/http"
|
22 | 25 |
|
@@ -88,3 +91,49 @@ func (a *Api) handleTxSignup(w http.ResponseWriter, r *http.Request) {
|
88 | 91 | resp, _ := json.Marshal(tmpResp)
|
89 | 92 | _, _ = w.Write(resp)
|
90 | 93 | }
|
| 94 | + |
| 95 | +// handleTxSubmit godoc |
| 96 | +// |
| 97 | +// @Summary TxSubmit |
| 98 | +// @Description Submit a signed transaction to the blockchain |
| 99 | +// @Produce json |
| 100 | +// @Accept application/cbor |
| 101 | +// @Param Content-Type header string true "Content type" Enums(application/cbor) |
| 102 | +// @Success 200 {object} string "Ok" |
| 103 | +// @Failure 400 {object} string "Bad Request" |
| 104 | +// @Failure 405 {object} string "Method Not Allowed" |
| 105 | +// @Failure 415 {object} string "Unsupported Media Type" |
| 106 | +// @Failure 500 {object} string "Server Error" |
| 107 | +// @Router /api/tx/submit [post] |
| 108 | +func (a *Api) handleTxSubmit(w http.ResponseWriter, r *http.Request) { |
| 109 | + if r.Method != http.MethodPost { |
| 110 | + http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) |
| 111 | + return |
| 112 | + } |
| 113 | + |
| 114 | + if r.Header.Get("Content-Type") != "application/cbor" { |
| 115 | + http.Error(w, "Unsupported Media Type", http.StatusUnsupportedMediaType) |
| 116 | + return |
| 117 | + } |
| 118 | + |
| 119 | + // Read raw transaction bytes from the request body and store in a byte array |
| 120 | + txRawBytes, err := io.ReadAll(r.Body) |
| 121 | + if err != nil { |
| 122 | + http.Error(w, "Server Error", http.StatusInternalServerError) |
| 123 | + return |
| 124 | + } |
| 125 | + defer r.Body.Close() //nolint:errcheck |
| 126 | + |
| 127 | + submit, err := txbuilder.OgmiosClient().SubmitTx(context.Background(), hex.EncodeToString(txRawBytes)) |
| 128 | + if err != nil { |
| 129 | + http.Error(w, fmt.Sprintf("%s", err), http.StatusBadRequest) |
| 130 | + return |
| 131 | + } |
| 132 | + if submit.Error != nil { |
| 133 | + http.Error(w, submit.Error.Message, submit.Error.Code) |
| 134 | + return |
| 135 | + } |
| 136 | + w.Header().Set("Content-Type", "application/json") |
| 137 | + resp, _ := json.Marshal(submit.ID) |
| 138 | + _, _ = w.Write(resp) |
| 139 | +} |
0 commit comments