diff --git a/plugins/webapi/drng/collectiveBeacon/handler.go b/plugins/webapi/drng/collectiveBeacon/handler.go new file mode 100644 index 0000000000000000000000000000000000000000..6180a02a6f85858f3a06a011abc54d37134e1d69 --- /dev/null +++ b/plugins/webapi/drng/collectiveBeacon/handler.go @@ -0,0 +1,44 @@ +package collectiveBeacon + +import ( + "net/http" + + cb "github.com/iotaledger/goshimmer/packages/binary/drng/subtypes/collectiveBeacon/payload" + generic "github.com/iotaledger/goshimmer/packages/binary/messagelayer/payload" + "github.com/iotaledger/goshimmer/plugins/messagelayer" + "github.com/iotaledger/hive.go/marshalutil" + "github.com/labstack/echo" + "github.com/labstack/gommon/log" +) + +// Handler creates a message of the given payload and +// broadcasts it to the node's neighbors. It returns the message ID if successful. +func Handler(c echo.Context) error { + var request Request + if err := c.Bind(&request); err != nil { + log.Info(err.Error()) + return c.JSON(http.StatusBadRequest, Response{Error: err.Error()}) + } + + //TODO: to check max payload size allowed, if exceeding return an error + + marshalUtil := marshalutil.New(request.Payload) + cbPayload, err := cb.Parse(marshalUtil) + + parsedPayload, err := generic.Parse(marshalUtil) + if err != nil { + return c.JSON(http.StatusBadRequest, Response{Error: "Not a valid Payload"}) + } + tx := messagelayer.MessageFactory.IssuePayload(parsedPayload) + + return c.JSON(http.StatusOK, Response{Id: tx.Id().String()}) +} + +type Response struct { + Id string `json:"id,omitempty"` + Error string `json:"error,omitempty"` +} + +type Request struct { + Payload []byte `json:"payload"` +} diff --git a/plugins/webapi/drng/plugin.go b/plugins/webapi/drng/plugin.go new file mode 100644 index 0000000000000000000000000000000000000000..9b9649d297c6e41e23052e74d03768550f0ec325 --- /dev/null +++ b/plugins/webapi/drng/plugin.go @@ -0,0 +1,16 @@ +package drng + +import ( + "github.com/iotaledger/goshimmer/plugins/webapi" + "github.com/iotaledger/goshimmer/plugins/webapi/drng/collectiveBeacon" + "github.com/iotaledger/hive.go/logger" + "github.com/iotaledger/hive.go/node" +) + +var PLUGIN = node.NewPlugin("WebAPI dRNG Endpoint", node.Enabled, configure) +var log *logger.Logger + +func configure(plugin *node.Plugin) { + log = logger.NewLogger("API-dRNG") + webapi.Server.POST("drng/collectiveBeacon", collectiveBeacon.Handler) +}