snapshot: 12-ebening

This commit is contained in:
2023-02-12 16:53:53 +03:00
parent 4f7909e4ee
commit f2ba079232
6 changed files with 58 additions and 60 deletions

View File

@@ -4,6 +4,7 @@ import (
"encoding/json"
"net/http"
"github.com/google/uuid"
"github.com/grassrootseconomics/cic-custodial/internal/keystore"
"github.com/grassrootseconomics/cic-custodial/internal/tasker"
"github.com/grassrootseconomics/cic-custodial/internal/tasker/task"
@@ -13,76 +14,50 @@ import (
// CreateAccountHandler route.
// POST: /api/account/create
// JSON Body:
// trackingId -> Unique string
// Returns the public key.
func CreateAccountHandler(
keystore keystore.Keystore,
taskerClient *tasker.TaskerClient,
) func(echo.Context) error {
return func(c echo.Context) error {
var accountRequest struct {
TrackingId string `json:"trackingId" validate:"required"`
}
if err := c.Bind(&accountRequest); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, errResp{
Ok: false,
Code: INTERNAL_ERROR,
})
}
if err := c.Validate(accountRequest); err != nil {
return err
}
trackingId := uuid.NewString()
generatedKeyPair, err := keypair.Generate()
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, errResp{
Ok: false,
Code: INTERNAL_ERROR,
})
return err
}
id, err := keystore.WriteKeyPair(c.Request().Context(), generatedKeyPair)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, errResp{
Ok: false,
Code: INTERNAL_ERROR,
})
return err
}
taskPayload, err := json.Marshal(task.AccountPayload{
PublicKey: generatedKeyPair.Public,
TrackingId: accountRequest.TrackingId,
TrackingId: trackingId,
})
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, errResp{
Ok: false,
Code: INTERNAL_ERROR,
})
return err
}
_, err = taskerClient.CreateTask(
tasker.PrepareAccountTask,
tasker.DefaultPriority,
&tasker.Task{
Id: accountRequest.TrackingId,
Id: trackingId,
Payload: taskPayload,
},
)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, errResp{
Ok: false,
Code: INTERNAL_ERROR,
})
return err
}
return c.JSON(http.StatusOK, okResp{
return c.JSON(http.StatusOK, OkResp{
Ok: true,
Result: H{
"publicKey": generatedKeyPair.Public,
"custodialId": id,
"trackingId": trackingId,
},
})
}

View File

@@ -4,6 +4,7 @@ import (
"encoding/json"
"net/http"
"github.com/google/uuid"
"github.com/grassrootseconomics/cic-custodial/internal/tasker"
"github.com/labstack/echo/v4"
)
@@ -22,8 +23,9 @@ func SignTransferHandler(
taskerClient *tasker.TaskerClient,
) func(echo.Context) error {
return func(c echo.Context) error {
trackingId := uuid.NewString()
var transferRequest struct {
TrackingId string `json:"trackingId" validate:"required"`
From string `json:"from" validate:"required,eth_addr"`
To string `json:"to" validate:"required,eth_addr"`
VoucherAddress string `json:"voucherAddress" validate:"required,eth_addr"`
@@ -31,10 +33,7 @@ func SignTransferHandler(
}
if err := c.Bind(&transferRequest); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, errResp{
Ok: false,
Code: INTERNAL_ERROR,
})
return err
}
if err := c.Validate(transferRequest); err != nil {
@@ -43,29 +42,26 @@ func SignTransferHandler(
taskPayload, err := json.Marshal(transferRequest)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, errResp{
Ok: false,
Code: INTERNAL_ERROR,
})
return err
}
_, err = taskerClient.CreateTask(
tasker.SignTransferTask,
tasker.HighPriority,
&tasker.Task{
Id: transferRequest.TrackingId,
Id: trackingId,
Payload: taskPayload,
},
)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, errResp{
Ok: false,
Code: INTERNAL_ERROR,
})
return err
}
return c.JSON(http.StatusOK, okResp{
return c.JSON(http.StatusOK, OkResp{
Ok: true,
Result: H{
"trackingId": trackingId,
},
})
}
}

View File

@@ -3,16 +3,18 @@ package api
const (
INTERNAL_ERROR = "ERR_INTERNAL"
VALIDATION_ERROR = "ERR_VALIDATE"
DUPLICATE_ERROR = "ERR_DUPLICATE"
)
type H map[string]any
type okResp struct {
type OkResp struct {
Ok bool `json:"ok"`
Result H `json:"result"`
}
type errResp struct {
Ok bool `json:"ok"`
Code string `json:"errorCode"`
type ErrResp struct {
Ok bool `json:"ok"`
Code string `json:"errorCode"`
Message string `json:"message"`
}

View File

@@ -13,7 +13,7 @@ type Validator struct {
func (v *Validator) Validate(i interface{}) error {
if err := v.ValidatorProvider.Struct(i); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, errResp{
return echo.NewHTTPError(http.StatusBadRequest, ErrResp{
Ok: false,
Code: VALIDATION_ERROR,
})