refactor: cmd/service/* and api

This commit is contained in:
2023-02-02 12:29:43 +00:00
parent 2a5c87b22c
commit b4c09cd11a
19 changed files with 448 additions and 500 deletions

47
internal/api/account.go Normal file
View File

@@ -0,0 +1,47 @@
package api
import (
"net/http"
"github.com/grassrootseconomics/cic-custodial/internal/keystore"
"github.com/grassrootseconomics/cic-custodial/internal/tasker"
"github.com/grassrootseconomics/cic-custodial/pkg/keypair"
"github.com/labstack/echo/v4"
)
func CreateAccountHandler(
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,
Code: INTERNAL_ERROR,
})
}
if err := keystore.WriteKeyPair(c.Request().Context(), generatedKeyPair); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, errResp{
Ok: false,
Code: INTERNAL_ERROR,
})
}
return c.JSON(http.StatusOK, okResp{
Ok: true,
Result: H{
"publicKey": generatedKeyPair.Public,
},
})
}
}
func AccountStatusHandler() func(echo.Context) error {
return func(c echo.Context) error {
return c.JSON(http.StatusOK, okResp{
Ok: true,
})
}
}

View File

@@ -1,71 +0,0 @@
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"`
TaskRef string `json:"taskRef"`
}
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,
Error: INTERNAL_ERROR,
})
}
taskPayload, err := json.Marshal(task.SystemPayload{
PublicKey: generatedKeyPair.Public,
})
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, errResp{
Ok: false,
Error: JSON_MARSHAL_ERROR,
})
}
task, err := taskerClient.CreateTask(
tasker.PrepareAccountTask,
tasker.DefaultPriority,
&tasker.Task{
Payload: taskPayload,
},
)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, errResp{
Ok: false,
Error: TASK_CHAIN_ERROR,
})
}
return c.JSON(http.StatusOK, okResp{
Ok: true,
Data: registrationResponse{
PublicKey: generatedKeyPair.Public,
TaskRef: task.ID,
},
})
}
}

View File

@@ -1,69 +0,0 @@
package api
import (
"encoding/json"
"net/http"
"github.com/grassrootseconomics/cic-custodial/internal/tasker"
"github.com/labstack/echo/v4"
)
type (
transferRequest struct {
From string `json:"from" validate:"required,eth_addr"`
To string `json:"to" validate:"required,eth_addr"`
VoucherAddress string `json:"voucherAddress" validate:"required,eth_addr"`
Amount string `json:"amount" validate:"required,numeric"`
}
transferResponse struct {
TaskRef string `json:"taskRef"`
}
)
func TransferHandler(
taskerClient *tasker.TaskerClient,
) func(echo.Context) error {
return func(c echo.Context) error {
transferPayload := new(transferRequest)
if err := c.Bind(transferPayload); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, errResp{
Ok: false,
Error: BIND_ERROR,
})
}
if err := c.Validate(transferPayload); err != nil {
return err
}
taskPayload, err := json.Marshal(transferPayload)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, errResp{
Ok: false,
Error: JSON_MARSHAL_ERROR,
})
}
task, err := taskerClient.CreateTask(
tasker.TransferTokenTask,
tasker.DefaultPriority,
&tasker.Task{
Payload: taskPayload,
},
)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, errResp{
Ok: false,
Error: TASK_CHAIN_ERROR,
})
}
return c.JSON(http.StatusOK, okResp{
Ok: true,
Data: transferResponse{
TaskRef: task.ID,
},
})
}
}

View File

@@ -1,20 +1,18 @@
package api
const (
INTERNAL_ERROR = "ERR_INTERNAL"
KEYPAIR_ERROR = "ERR_GEN_KEYPAIR"
JSON_MARSHAL_ERROR = "ERR_PAYLOAD_SERIALIZATION"
TASK_CHAIN_ERROR = "ERR_START_TASK_CHAIN"
VALIDATION_ERROR = "ERR_VALIDATE"
BIND_ERROR = "ERR_BIND"
INTERNAL_ERROR = "ERR_INTERNAL"
VALIDATION_ERROR = "ERR_VALIDATE"
)
type H map[string]any
type okResp struct {
Ok bool `json:"ok"`
Data interface{} `json:"data"`
Ok bool `json:"ok"`
Result H `json:"result"`
}
type errResp struct {
Ok bool `json:"ok"`
Error string `json:"error"`
Ok bool `json:"ok"`
Code string `json:"errorCode"`
}

View File

@@ -1,23 +1,21 @@
package api
import (
"fmt"
"net/http"
"github.com/go-playground/validator"
"github.com/labstack/echo/v4"
)
type CustomValidator struct {
Validator *validator.Validate
type Validator struct {
ValidatorProvider *validator.Validate
}
func (cv *CustomValidator) Validate(i interface{}) error {
if err := cv.Validator.Struct(i); err != nil {
fmt.Println(err)
func (v *Validator) Validate(i interface{}) error {
if err := v.ValidatorProvider.Struct(i); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, errResp{
Ok: false,
Error: VALIDATION_ERROR,
Ok: false,
Code: VALIDATION_ERROR,
})
}
return nil