wallet/pkg/rest/export_v1.go

145 lines
3.6 KiB
Go
Raw Normal View History

2024-08-31 14:46:20 +00:00
package rest
import (
"net/http"
hex "encoding/hex"
gin "github.com/gin-gonic/gin"
eth "custodial/pkg/eth"
tron "custodial/pkg/tron"
crypto "github.com/ethereum/go-ethereum/crypto"
)
type ExportV1Request struct {
Index int `json:"index"`
}
type ExportV1Response struct {
ETHAddress string `json:"eth_address"`
TRONAddress string `json:"tron_address"`
ETHPrivateKey string `json:"eth_private_key"`
TRONPrivateKey string `json:"tron_private_key"`
}
func GetPrivKeys(idx int) ExportV1Response {
eth.InitMasterFromPrivateSettings()
tron.InitMasterFromPrivateSettings()
ethAccount, _ := eth.DeriveAccount(idx)
tronAccount, _ := tron.DeriveAccount(idx)
ethPrivateKey := crypto.FromECDSA(ethAccount.PrivateKey())
tronPrivateKey := crypto.FromECDSA(tronAccount.PrivateKey().ToECDSA())
// PRINT PRIVATE KEYS FOR THE ACCOUNTS
// fmt.Println("")
// fmt.Println("ETH Private Key: ", hex.EncodeToString(ethPrivateKey), "Address:", ethAccount.Address())
// fmt.Println("TRON Private Key: ", hex.EncodeToString(tronPrivateKey), "Address:", tronAccount.Address())
// fmt.Println("")
return ExportV1Response{
ETHAddress: ethAccount.Address().String(),
TRONAddress: tronAccount.Address(),
ETHPrivateKey: hex.EncodeToString(ethPrivateKey),
TRONPrivateKey: hex.EncodeToString(tronPrivateKey),
}
}
// @BasePath /api/v1
//
// ExportV1 godoc
//
// @Summary Export private keys for the given index
// @Schemes
// @Description Export private keys for the given index (uint32)
// @Tags Utils
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param message body ExportV1Request true "Account Info"
// @Success 200 {object} ExportV1Response
// @Failure 400 {object} ErrorResponse
// @Router /export [post]
func ExportV1(c *gin.Context) {
var req ExportV1Request
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if req.Index < 2 {
c.JSON(http.StatusBadRequest, gin.H{
"error": "index is invalid: min index must is 2",
})
return
}
res := GetPrivKeys(req.Index)
c.JSON(http.StatusOK, res)
}
type ExportBatchV1Request struct {
Limit int `json:"limit"`
Offset int `json:"offset"`
}
// @BasePath /api/v1
//
// ExportV1 godoc
//
// @Summary Export private keys for the given range
// @Schemes
// @Description Export private keys for the given range (uint32)
// @Tags Utils
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param message body ExportBatchV1Request true "Paging Info"
// @Success 200 {object} []ExportV1Response
// @Failure 400 {object} ErrorResponse
// @Router /export-batch [post]
func ExportBatchV1(c *gin.Context) {
var req ExportBatchV1Request
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if req.Offset < 2 {
c.JSON(http.StatusBadRequest, gin.H{
"error": "index is invalid: min index must is 2",
})
return
}
var res []ExportV1Response
for i := req.Offset; i < req.Offset+req.Limit; i++ {
res = append(res, GetPrivKeys(i))
}
c.IndentedJSON(http.StatusOK, res)
}
// @BasePath /api/v1
//
// ExportSpenderKeysV1 godoc
//
// @Summary Export spender private keys
// @Schemes
// @Description Export spender private keys
// @Tags Utils
// @Accept json
// @Security ApiKeyAuth
// @Produce json
// @Success 200 {object} ExportV1Response
// @Failure 400 {object} ErrorResponse
// @Router /export-spender-keys [get]
func ExportSpenderKeysV1(c *gin.Context) {
keys := GetPrivKeys(eth.MAX_ACCOUNT_INDEX)
c.IndentedJSON(http.StatusOK, keys)
}