cic-custodial/internal/api/account.go

65 lines
1.4 KiB
Go
Raw Permalink Normal View History

2023-02-02 13:29:43 +01:00
package api
import (
2023-02-09 08:42:15 +01:00
"encoding/json"
2023-02-02 13:29:43 +01:00
"net/http"
2023-02-12 14:53:53 +01:00
"github.com/google/uuid"
2023-02-02 13:29:43 +01:00
"github.com/grassrootseconomics/cic-custodial/internal/keystore"
"github.com/grassrootseconomics/cic-custodial/internal/tasker"
2023-02-09 08:42:15 +01:00
"github.com/grassrootseconomics/cic-custodial/internal/tasker/task"
2023-02-02 13:29:43 +01:00
"github.com/grassrootseconomics/cic-custodial/pkg/keypair"
"github.com/labstack/echo/v4"
)
// CreateAccountHandler route.
2023-02-09 08:42:15 +01:00
// POST: /api/account/create
// Returns the public key.
2023-02-02 13:29:43 +01:00
func CreateAccountHandler(
keystore keystore.Keystore,
2023-02-09 08:42:15 +01:00
taskerClient *tasker.TaskerClient,
2023-02-02 13:29:43 +01:00
) func(echo.Context) error {
return func(c echo.Context) error {
2023-02-12 14:53:53 +01:00
trackingId := uuid.NewString()
2023-02-09 08:42:15 +01:00
2023-02-02 13:29:43 +01:00
generatedKeyPair, err := keypair.Generate()
if err != nil {
2023-02-12 14:53:53 +01:00
return err
2023-02-02 13:29:43 +01:00
}
id, err := keystore.WriteKeyPair(c.Request().Context(), generatedKeyPair)
if err != nil {
2023-02-12 14:53:53 +01:00
return err
2023-02-02 13:29:43 +01:00
}
2023-02-09 08:42:15 +01:00
taskPayload, err := json.Marshal(task.AccountPayload{
PublicKey: generatedKeyPair.Public,
2023-02-12 14:53:53 +01:00
TrackingId: trackingId,
2023-02-02 13:29:43 +01:00
})
2023-02-09 08:42:15 +01:00
if err != nil {
2023-02-12 14:53:53 +01:00
return err
2023-02-09 08:42:15 +01:00
}
_, err = taskerClient.CreateTask(
tasker.PrepareAccountTask,
tasker.DefaultPriority,
&tasker.Task{
2023-02-12 14:53:53 +01:00
Id: trackingId,
2023-02-09 08:42:15 +01:00
Payload: taskPayload,
},
)
if err != nil {
2023-02-12 14:53:53 +01:00
return err
2023-02-09 08:42:15 +01:00
}
2023-02-02 13:29:43 +01:00
2023-02-12 14:53:53 +01:00
return c.JSON(http.StatusOK, OkResp{
2023-02-02 13:29:43 +01:00
Ok: true,
2023-02-09 08:42:15 +01:00
Result: H{
"publicKey": generatedKeyPair.Public,
"custodialId": id,
2023-02-12 14:53:53 +01:00
"trackingId": trackingId,
2023-02-09 08:42:15 +01:00
},
2023-02-02 13:29:43 +01:00
})
}
}