mirror of
https://github.com/grassrootseconomics/cic-custodial.git
synced 2026-05-27 21:47:55 +02:00
init: experimental custodial
This commit is contained in:
40
internal/api/registration.go
Normal file
40
internal/api/registration.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/actions"
|
||||
tasker_client "github.com/grassrootseconomics/cic-custodial/internal/tasker/client"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
type registrationResponse struct {
|
||||
PublicKey string `json:"publicKey"`
|
||||
JobRef string `json:"jobRef"`
|
||||
}
|
||||
|
||||
func handleRegistration(c echo.Context) error {
|
||||
var (
|
||||
ap = c.Get("actions").(*actions.ActionsProvider)
|
||||
tc = c.Get("tasker_client").(*tasker_client.TaskerClient)
|
||||
)
|
||||
|
||||
generatedKeyPair, err := ap.CreateNewKeyPair(c.Request().Context())
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "ERR_GEN_KEYPAIR")
|
||||
}
|
||||
|
||||
job, err := tc.CreateRegistrationTask(tasker_client.RegistrationPayload{
|
||||
PublicKey: generatedKeyPair.Public,
|
||||
}, tasker_client.SetNewAccountNonceTask)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "ERR_START_TASK_CHAIN")
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, okResp{
|
||||
registrationResponse{
|
||||
PublicKey: generatedKeyPair.Public,
|
||||
JobRef: job.ID,
|
||||
},
|
||||
})
|
||||
}
|
||||
35
internal/api/server.go
Normal file
35
internal/api/server.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/actions"
|
||||
tasker_client "github.com/grassrootseconomics/cic-custodial/internal/tasker/client"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
type okResp struct {
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
type Opts struct {
|
||||
ActionsProvider *actions.ActionsProvider
|
||||
TaskerClient *tasker_client.TaskerClient
|
||||
}
|
||||
|
||||
func BootstrapHTTPServer(o Opts) *echo.Echo {
|
||||
server := echo.New()
|
||||
server.HideBanner = true
|
||||
server.HidePort = true
|
||||
|
||||
server.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
c.Set("actions", o.ActionsProvider)
|
||||
c.Set("tasker_client", o.TaskerClient)
|
||||
return next(c)
|
||||
}
|
||||
})
|
||||
|
||||
api := server.Group("/api")
|
||||
api.GET("/register", handleRegistration)
|
||||
|
||||
return server
|
||||
}
|
||||
Reference in New Issue
Block a user