init: experimental custodial

This commit is contained in:
2022-10-22 12:36:22 +00:00
commit 827bdb2bd9
30 changed files with 1839 additions and 0 deletions

View 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
View 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
}