2022-11-30 10:51:24 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/grassrootseconomics/cic-custodial/internal/keystore"
|
|
|
|
"github.com/grassrootseconomics/cic-custodial/internal/tasker"
|
|
|
|
"github.com/grassrootseconomics/cic-custodial/internal/tasker/task"
|
|
|
|
"github.com/grassrootseconomics/cic-custodial/pkg/keypair"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
|
|
|
|
type registrationResponse struct {
|
|
|
|
PublicKey string `json:"publicKey"`
|
2022-11-30 16:18:01 +01:00
|
|
|
TaskRef string `json:"taskRef"`
|
2022-11-30 10:51:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func RegistrationHandler(
|
|
|
|
taskerClient *tasker.TaskerClient,
|
|
|
|
keystore keystore.Keystore,
|
|
|
|
) func(echo.Context) error {
|
|
|
|
return func(c echo.Context) error {
|
|
|
|
generatedKeyPair, err := keypair.Generate()
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, errResp{
|
|
|
|
Ok: false,
|
|
|
|
Error: KEYPAIR_ERROR,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := keystore.WriteKeyPair(c.Request().Context(), generatedKeyPair); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, errResp{
|
|
|
|
Ok: false,
|
2022-11-30 16:18:01 +01:00
|
|
|
Error: INTERNAL_ERROR,
|
2022-11-30 10:51:24 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-11-30 16:18:01 +01:00
|
|
|
taskPayload, err := json.Marshal(task.SystemPayload{
|
2022-11-30 10:51:24 +01:00
|
|
|
PublicKey: generatedKeyPair.Public,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, errResp{
|
|
|
|
Ok: false,
|
2022-11-30 16:18:01 +01:00
|
|
|
Error: JSON_MARSHAL_ERROR,
|
2022-11-30 10:51:24 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-11-30 16:18:01 +01:00
|
|
|
task, err := taskerClient.CreateTask(
|
2022-11-30 10:51:24 +01:00
|
|
|
tasker.PrepareAccountTask,
|
|
|
|
tasker.DefaultPriority,
|
|
|
|
&tasker.Task{
|
2022-11-30 16:18:01 +01:00
|
|
|
Payload: taskPayload,
|
2022-11-30 10:51:24 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, errResp{
|
|
|
|
Ok: false,
|
2022-11-30 16:18:01 +01:00
|
|
|
Error: TASK_CHAIN_ERROR,
|
2022-11-30 10:51:24 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, okResp{
|
|
|
|
Ok: true,
|
|
|
|
Data: registrationResponse{
|
|
|
|
PublicKey: generatedKeyPair.Public,
|
2022-11-30 16:18:01 +01:00
|
|
|
TaskRef: task.ID,
|
2022-11-30 10:51:24 +01:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|