wip: refactor taskers

This commit is contained in:
2023-02-09 10:42:15 +03:00
parent 8676450122
commit 8a0880fcfc
18 changed files with 755 additions and 62 deletions

View File

@@ -1,22 +1,41 @@
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"
)
// CreateAccountHandler route.
// POST: /api/account/create.
// Returns the public key and tasker account prep receipt.
// POST: /api/account/create
// JSON Body:
// trackingId -> Unique string
// Returns the public key.
func CreateAccountHandler(
taskerClient *tasker.TaskerClient,
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
}
generatedKeyPair, err := keypair.Generate()
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, errResp{
@@ -33,24 +52,38 @@ func CreateAccountHandler(
})
}
taskPayload, err := json.Marshal(task.AccountPayload{
PublicKey: generatedKeyPair.Public,
TrackingId: accountRequest.TrackingId,
})
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, errResp{
Ok: false,
Code: INTERNAL_ERROR,
})
}
_, err = taskerClient.CreateTask(
tasker.PrepareAccountTask,
tasker.DefaultPriority,
&tasker.Task{
Id: accountRequest.TrackingId,
Payload: taskPayload,
},
)
if 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,
"keyId": id,
"publicKey": generatedKeyPair.Public,
"custodialId": id,
},
})
}
}
// AccountStatusHandler route.
// GET: /api/account/status.
// Check if an account is ready to be used.
// Returns the status as a bool.
func AccountStatusHandler() func(echo.Context) error {
return func(c echo.Context) error {
return c.JSON(http.StatusOK, okResp{
Ok: true,
})
}
}

71
internal/api/sign.go Normal file
View File

@@ -0,0 +1,71 @@
package api
import (
"encoding/json"
"net/http"
"github.com/grassrootseconomics/cic-custodial/internal/tasker"
"github.com/labstack/echo/v4"
)
// SignTxHandler route.
// POST: /api/sign/transfer
// JSON Body:
// trackingId -> Unique string
// from -> ETH address
// to -> ETH address
// voucherAddress -> ETH address
// amount -> int (6 d.p. precision)
// e.g. 1000000 = 1 VOUCHER
// Returns the task id.
func SignTransferHandler(
taskerClient *tasker.TaskerClient,
) func(echo.Context) error {
return func(c echo.Context) error {
var transferRequest struct {
TrackingId string `json:"trackingId" validate:"required"`
From string `json:"from" validate:"required,eth_address"`
To string `json:"to" validate:"required,eth_addr"`
VoucherAddress string `json:"voucherAddress" validate:"required,eth_addr"`
Amount int64 `json:"amount" validate:"required,numeric"`
}
if err := c.Bind(&transferRequest); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, errResp{
Ok: false,
Code: INTERNAL_ERROR,
})
}
if err := c.Validate(transferRequest); err != nil {
return err
}
taskPayload, err := json.Marshal(transferRequest)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, errResp{
Ok: false,
Code: INTERNAL_ERROR,
})
}
_, err = taskerClient.CreateTask(
tasker.TransferTokenTask,
tasker.HighPriority,
&tasker.Task{
Id: transferRequest.TrackingId,
Payload: taskPayload,
},
)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, errResp{
Ok: false,
Code: INTERNAL_ERROR,
})
}
return c.JSON(http.StatusOK, okResp{
Ok: true,
})
}
}