2023-02-15 08:05:43 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2023-02-20 10:56:30 +01:00
|
|
|
"github.com/grassrootseconomics/cic-custodial/internal/custodial"
|
|
|
|
|
2023-02-15 08:05:43 +01:00
|
|
|
"github.com/grassrootseconomics/cic-custodial/internal/tasker"
|
2023-02-20 10:56:30 +01:00
|
|
|
"github.com/grassrootseconomics/cic-custodial/internal/tasker/task"
|
2023-02-15 08:05:43 +01:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SignTxHandler route.
|
|
|
|
// POST: /api/sign/transfer
|
|
|
|
// JSON Body:
|
|
|
|
// from -> ETH address
|
|
|
|
// to -> ETH address
|
|
|
|
// voucherAddress -> ETH address
|
|
|
|
// amount -> int (6 d.p. precision)
|
|
|
|
// e.g. 1000000 = 1 VOUCHER
|
|
|
|
// Returns the task id.
|
2023-02-20 10:56:30 +01:00
|
|
|
func SignTransferHandler(cu *custodial.Custodial) func(echo.Context) error {
|
2023-02-15 08:05:43 +01:00
|
|
|
return func(c echo.Context) error {
|
|
|
|
trackingId := uuid.NewString()
|
|
|
|
|
|
|
|
var transferRequest struct {
|
2023-02-20 10:56:30 +01:00
|
|
|
From string `json:"from" validate:"required,eth_checksum"`
|
|
|
|
To string `json:"to" validate:"required,eth_checksum"`
|
|
|
|
VoucherAddress string `json:"voucherAddress" validate:"required,eth_checksum"`
|
2023-02-21 18:34:22 +01:00
|
|
|
Amount uint64 `json:"amount" validate:"required,numeric"`
|
2023-02-15 08:05:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.Bind(&transferRequest); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.Validate(transferRequest); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-20 10:56:30 +01:00
|
|
|
// TODO: Checksum addresses
|
|
|
|
taskPayload, err := json.Marshal(task.TransferPayload{
|
|
|
|
TrackingId: trackingId,
|
|
|
|
From: transferRequest.From,
|
|
|
|
To: transferRequest.To,
|
|
|
|
VoucherAddress: transferRequest.VoucherAddress,
|
|
|
|
Amount: transferRequest.Amount,
|
|
|
|
})
|
2023-02-15 08:05:43 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-20 10:56:30 +01:00
|
|
|
_, err = cu.TaskerClient.CreateTask(
|
2023-02-15 08:05:43 +01:00
|
|
|
tasker.SignTransferTask,
|
|
|
|
tasker.HighPriority,
|
|
|
|
&tasker.Task{
|
|
|
|
Id: trackingId,
|
|
|
|
Payload: taskPayload,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, OkResp{
|
|
|
|
Ok: true,
|
|
|
|
Result: H{
|
|
|
|
"trackingId": trackingId,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|