wallet/pkg/rest/account_v1_create.go
Anton Nesterov b19bf34e5f
[init]
2024-08-31 16:54:43 +02:00

124 lines
3.1 KiB
Go

package rest
import (
"fmt"
"net/http"
"sync"
eth "custodial/pkg/eth"
store "custodial/pkg/store"
trc "custodial/pkg/tron"
gin "github.com/gin-gonic/gin"
)
type CreateAccountRequest struct {
AccountID string `json:"account_id"`
Label string `json:"label"`
Index int `json:"index"`
}
type CreateAccountResponse struct {
AccountID string `json:"account_id"`
Label string `json:"label"`
Index int `json:"index"`
TronAddress string `json:"tron_address"`
EthAddress string `json:"eth_address"`
}
var amu sync.Mutex
// @BasePath /api/v1
//
// CreateAccount godoc
//
// @Summary Create a new user account
// @Schemes
// @Description Create a new user account identified by account_id and index (incremental number)
// @Tags Account
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param message body CreateAccountRequest true "Account Info"
// @Success 200 {object} CreateAccountResponse
// @Failure 400 {object} ErrorResponse
// @Router /account/create [post]
func CreateAccountV1(c *gin.Context) {
amu.Lock()
defer amu.Unlock()
var req CreateAccountRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
fmt.Println("CreateAccountV1", req.AccountID)
if req.AccountID == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "account_id is required"})
return
}
if req.Index < 2 || req.Index >= store.MAX_ACCOUNT_INDEX {
c.JSON(http.StatusBadRequest, gin.H{
"error": fmt.Sprintf("index is invalid: min index must is 2, max index is %s", store.MAX_ACCOUNT_INDEX-1),
})
return
}
exEth, _ := store.GetAccount(req.AccountID, store.Ethereum)
exTrc, _ := store.GetAccount(req.AccountID, store.Tron)
fmt.Println("exEth", exEth)
fmt.Println("exTrc", exTrc)
if exEth != nil && exTrc != nil {
if exEth.GetAddress() != "" || exTrc.GetAddress() != "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "account with this id already exists"})
return
}
}
nextHdIndex := int(req.Index)
ethAccount, err := eth.DeriveAccount(nextHdIndex)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
tronAccount, err := trc.DeriveAccount(nextHdIndex)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
dbEthAccount := store.EthereumAccount{
AccountID: req.AccountID,
Label: req.Label,
Address: ethAccount.Address().String(),
HDIndex: nextHdIndex,
}
err = store.CreateAccount(&dbEthAccount)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
dbTronAccount := store.TronAccount{
AccountID: req.AccountID,
Label: req.Label,
Address: tronAccount.Address(),
HDIndex: nextHdIndex,
}
err = store.CreateAccount(&dbTronAccount)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"account_id": req.AccountID,
"label": req.Label,
"index": nextHdIndex,
"tron_address": tronAccount.Address(),
"eth_address": ethAccount.Address().String(),
})
}