-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Endpoint for Arweave transactions in the form of JSON
- Loading branch information
1 parent
3284ca1
commit bf376d4
Showing
2 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/cosmos/cosmos-sdk/client" | ||
"github.com/gorilla/mux" | ||
"github.com/warp-contracts/sequencer/x/sequencer/types" | ||
) | ||
|
||
type arweaveHandler struct { | ||
ctx client.Context | ||
} | ||
|
||
// The endpoint that accepts the Arweave transaction in the form of JSON, | ||
// wraps it with a Cosmos transaction and broadcasts it to the network. | ||
func RegisterArweaveAPIRoute(clientCtx client.Context, router *mux.Router) { | ||
router.Handle("/arweave", arweaveHandler{ctx: clientCtx}).Methods("POST") | ||
} | ||
|
||
func (h arweaveHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
// parse JSON | ||
var msg types.MsgArweave | ||
decoder := json.NewDecoder(r.Body) | ||
decoder.DisallowUnknownFields() | ||
err := decoder.Decode(&msg) | ||
if err != nil { | ||
badRequest(w, err.Error()) | ||
return | ||
} | ||
|
||
// wrap Arweave message with Cosmos transaction | ||
txBytes, err := createTxWithArweaveMsg(h.ctx, msg) | ||
if err != nil { | ||
badRequest(w, err.Error()) | ||
return | ||
} | ||
|
||
// broadcast transaction | ||
response, err := h.ctx.BroadcastTxSync(txBytes) | ||
if err != nil { | ||
badRequest(w, err.Error()) | ||
return | ||
} | ||
if response.Code != 0 { | ||
badRequest(w, response.RawLog) | ||
return | ||
} | ||
|
||
w.Write([]byte(response.TxHash)) | ||
} | ||
|
||
func badRequest(w http.ResponseWriter, errorMessage string) { | ||
http.Error(w, errorMessage, http.StatusBadRequest) | ||
} | ||
|
||
func createTxWithArweaveMsg(ctx client.Context, msg types.MsgArweave) ([]byte, error) { | ||
txBuilder := ctx.TxConfig.NewTxBuilder() | ||
txBuilder.SetMsgs(&msg) | ||
return ctx.TxConfig.TxEncoder()(txBuilder.GetTx()) | ||
} |