cic-custodial/internal/api/sign.go

68 lines
1.4 KiB
Go
Raw Normal View History

2023-02-09 08:42:15 +01:00
package api
import (
"encoding/json"
"net/http"
2023-02-12 14:53:53 +01:00
"github.com/google/uuid"
2023-02-09 08:42:15 +01:00
"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 {
2023-02-12 14:53:53 +01:00
trackingId := uuid.NewString()
2023-02-09 08:42:15 +01:00
var transferRequest struct {
2023-02-12 10:50:43 +01:00
From string `json:"from" validate:"required,eth_addr"`
2023-02-09 08:42:15 +01:00
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 {
2023-02-12 14:53:53 +01:00
return err
2023-02-09 08:42:15 +01:00
}
if err := c.Validate(transferRequest); err != nil {
return err
}
taskPayload, err := json.Marshal(transferRequest)
if err != nil {
2023-02-12 14:53:53 +01:00
return err
2023-02-09 08:42:15 +01:00
}
_, err = taskerClient.CreateTask(
2023-02-09 12:23:37 +01:00
tasker.SignTransferTask,
2023-02-09 08:42:15 +01:00
tasker.HighPriority,
&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-12 14:53:53 +01:00
return c.JSON(http.StatusOK, OkResp{
2023-02-09 08:42:15 +01:00
Ok: true,
2023-02-12 14:53:53 +01:00
Result: H{
"trackingId": trackingId,
},
2023-02-09 08:42:15 +01:00
})
}
}