2023-02-15 08:05:43 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2023-02-20 10:56:30 +01:00
|
|
|
"github.com/grassrootseconomics/cic-custodial/internal/custodial"
|
2023-02-15 08:05:43 +01:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CreateAccountHandler route.
|
|
|
|
// POST: /api/account/create
|
|
|
|
// Returns the public key.
|
2023-03-28 08:39:44 +02:00
|
|
|
func HandleAccountCreate(cu *custodial.Custodial) func(echo.Context) error {
|
|
|
|
return func(c echo.Context) error {
|
|
|
|
generatedKeyPair, err := keypair.Generate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := cu.Keystore.WriteKeyPair(c.Request().Context(), generatedKeyPair)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
trackingId := uuid.NewString()
|
|
|
|
taskPayload, err := json.Marshal(task.AccountPayload{
|
|
|
|
PublicKey: generatedKeyPair.Public,
|
|
|
|
TrackingId: trackingId,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = cu.TaskerClient.CreateTask(
|
|
|
|
c.Request().Context(),
|
2023-03-29 18:10:58 +02:00
|
|
|
tasker.AccountRegisterTask,
|
2023-03-28 08:39:44 +02:00
|
|
|
tasker.DefaultPriority,
|
|
|
|
&tasker.Task{
|
|
|
|
Id: trackingId,
|
|
|
|
Payload: taskPayload,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, OkResp{
|
|
|
|
Ok: true,
|
|
|
|
Result: H{
|
|
|
|
"publicKey": generatedKeyPair.Public,
|
|
|
|
"custodialId": id,
|
|
|
|
"trackingId": trackingId,
|
|
|
|
},
|
|
|
|
})
|
2023-02-24 17:46:46 +01:00
|
|
|
}
|
2023-02-15 08:05:43 +01:00
|
|
|
}
|