diff --git a/client/message.go b/client/message.go index 267dbb343a8bea0f7c9601a3e2823babc82b697d..a920c336d9acba56b8bf967eff2c2af993f85923 100644 --- a/client/message.go +++ b/client/message.go @@ -7,7 +7,8 @@ import ( ) const ( - routeFindByID = "message/findById" + routeFindByID = "message/findById" + routeSendPayload = "message/sendPayload" ) // FindMessageByID finds messages by the given base58 encoded IDs. The messages are returned in the same order as @@ -26,3 +27,14 @@ func (api *GoShimmerAPI) FindMessageByID(base58EncodedIDs []string) (*webapi_mes return res, nil } + +// SendPayload send a message with the given payload. +func (api *GoShimmerAPI) SendPayload(payload []byte) (string, error) { + res := &webapi_message.MsgResponse{} + if err := api.do(http.MethodPost, routeSendPayload, + &webapi_message.MsgRequest{Payload: payload}, res); err != nil { + return "", err + } + + return res.ID, nil +} diff --git a/plugins/webapi/message/plugin.go b/plugins/webapi/message/plugin.go index 1ebbf8b7fb31be52efa6c59984a81e56c8218ca2..bdba4f3740deaf17d000969693b531de05c9cd5e 100644 --- a/plugins/webapi/message/plugin.go +++ b/plugins/webapi/message/plugin.go @@ -23,6 +23,7 @@ var ( func configure(plugin *node.Plugin) { log = logger.NewLogger(PluginName) webapi.Server.POST("message/findById", findMessageByID) + webapi.Server.POST("message/sendPayload", sendPayload) } // findMessageByID returns the array of messages for the diff --git a/plugins/webapi/message/sendPayload.go b/plugins/webapi/message/sendPayload.go new file mode 100644 index 0000000000000000000000000000000000000000..159ffe16f0cc7cd0dcc015e39b09cd005fe5955b --- /dev/null +++ b/plugins/webapi/message/sendPayload.go @@ -0,0 +1,44 @@ +package message + +import ( + "net/http" + + "github.com/iotaledger/goshimmer/packages/binary/messagelayer/payload" + "github.com/iotaledger/goshimmer/plugins/issuer" + "github.com/labstack/echo" +) + +// sendPayload creates a message of the given payload and +// broadcasts it to the node's neighbors. It returns the message ID if successful. +func sendPayload(c echo.Context) error { + var request MsgRequest + if err := c.Bind(&request); err != nil { + log.Info(err.Error()) + return c.JSON(http.StatusBadRequest, MsgResponse{Error: err.Error()}) + } + + //TODO: to check max payload size allowed, if exceeding return an error + + parsedPayload, _, err := payload.FromBytes(request.Payload) + if err != nil { + return c.JSON(http.StatusBadRequest, MsgResponse{Error: "not a valid payload"}) + } + + msg, err := issuer.IssuePayload(parsedPayload) + if err != nil { + return c.JSON(http.StatusBadRequest, MsgResponse{Error: err.Error()}) + } + + return c.JSON(http.StatusOK, MsgResponse{ID: msg.Id().String()}) +} + +// MsgResponse contains the ID of the message sent. +type MsgResponse struct { + ID string `json:"id,omitempty"` + Error string `json:"error,omitempty"` +} + +// MsgRequest contains the message to send. +type MsgRequest struct { + Payload []byte `json:"payload"` +}