Commit eb086344 authored by 贾浩@五瓣科技's avatar 贾浩@五瓣科技

update

parent db3fa0df
.vscode .vscode
.idea .idea
build build
\ No newline at end of file
...@@ -5,4 +5,4 @@ GOBIN = $(shell pwd)/build ...@@ -5,4 +5,4 @@ GOBIN = $(shell pwd)/build
default: api default: api
api: api:
go build $(BUILD_FLAGS) -v -o=${GOBIN}/$@ ./cmd/api go build $(BUILD_FLAGS) -v -o=${GOBIN}/$@ ./cmd/gateway
\ No newline at end of file \ No newline at end of file
package apigateway
import (
"context"
"encoding/json"
"fmt"
"math/big"
"net"
"net/http"
"strings"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log"
nebulav1 "github.com/exchain/go-exchain/exchain/protocol/gen/go/nebula/v1"
orderbookv1 "github.com/exchain/go-exchain/exchain/protocol/gen/go/orderbook/v1"
"github.com/exchain/process/engine"
"github.com/exchain/process/orderbook"
"github.com/exchain/process/types"
"github.com/exchain/process/utils"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/holiman/uint256"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
type server struct {
eg *engine.Engine
orderbookv1.UnimplementedOrderbookServer
}
func (s *server) PlaceLimitOrder(ctx context.Context, req *orderbookv1.PlaceLimitOrderRequest) (resp *orderbookv1.Response, err error) {
resp = new(orderbookv1.Response)
side := orderbook.Buy
tx := req.Transaction.Tx.(*nebulav1.Transaction_LimitTx)
if tx.LimitTx.Side == nebulav1.OrderSide_SELL {
side = orderbook.Sell
}
coins := strings.Split(tx.LimitTx.Pair, "-")
baseCoin, quoteCoin := coins[0], coins[1]
quantity := new(uint256.Int).SetBytes(tx.LimitTx.Quantity)
price := new(uint256.Int).SetBytes(tx.LimitTx.Price)
nonce := new(big.Int).SetBytes(req.Transaction.Nonce)
orderID := utils.GenOrderID()
_, res, err := s.eg.ProcessLimitOrder(orderID,
types.Coin(baseCoin),
types.Coin(quoteCoin),
common.HexToAddress(req.Transaction.User),
side,
price,
quantity,
uint64(nonce.Int64()),
req.Transaction.Proxy,
utils.CombineRSV(req.Transaction.Signature.R, req.Transaction.Signature.S, uint8(req.Transaction.Signature.V)))
if err != nil {
return
}
data, _ := json.Marshal(res)
return &orderbookv1.Response{Data: data}, nil
}
func (s *server) PlaceMarketOrder(ctx context.Context, req *orderbookv1.PlaceMarketOrderRequest) (resp *orderbookv1.Response, err error) {
resp = new(orderbookv1.Response)
side := orderbook.Buy
tx := req.Transaction.Tx.(*nebulav1.Transaction_MarketTx)
if tx.MarketTx.Side == nebulav1.OrderSide_SELL {
side = orderbook.Sell
}
coins := strings.Split(tx.MarketTx.Pair, "-")
baseCoin, quoteCoin := coins[0], coins[1]
quantity := new(uint256.Int).SetBytes(tx.MarketTx.Quantity)
nonce := new(big.Int).SetBytes(req.Transaction.Nonce)
orderID := utils.GenOrderID()
_, res, err := s.eg.ProcessMarketOrder(orderID,
types.Coin(baseCoin),
types.Coin(quoteCoin),
common.HexToAddress(req.Transaction.User),
side,
quantity,
uint64(nonce.Int64()),
req.Transaction.Proxy,
utils.CombineRSV(req.Transaction.Signature.R, req.Transaction.Signature.S, uint8(req.Transaction.Signature.V)))
if err != nil {
return
}
data, _ := json.Marshal(res)
return &orderbookv1.Response{Data: data}, nil
}
func (s *server) CancelOrder(ctx context.Context, req *orderbookv1.CancelOrderRequest) (resp *orderbookv1.Response, err error) {
tx := req.Transaction.Tx.(*nebulav1.Transaction_CancelTx)
orderID := tx.CancelTx.OrderId
coins := strings.Split(tx.CancelTx.Pair, "-")
baseCoin, quoteCoin := coins[0], coins[1]
nonce := new(big.Int).SetBytes(req.Transaction.Nonce)
err = s.eg.ProcessCancelOrder(orderID,
types.Coin(baseCoin),
types.Coin(quoteCoin),
common.HexToAddress(req.Transaction.User),
nonce.Uint64(),
req.Transaction.Proxy,
utils.CombineRSV(req.Transaction.Signature.R, req.Transaction.Signature.S, uint8(req.Transaction.Signature.V)))
return &orderbookv1.Response{}, err
}
func (s *server) SignProxy(ctx context.Context, req *orderbookv1.SignProxyRequest) (resp *orderbookv1.Response, err error) {
tx := req.Transaction.Tx.(*nebulav1.Transaction_SignProxyTx)
nonce := new(big.Int).SetBytes(req.Transaction.Nonce)
pubkey, err := crypto.UnmarshalPubkey(tx.SignProxyTx.SignerProxy)
if err != nil {
return
}
err = s.eg.PorcessSignProxy(common.HexToAddress(req.Transaction.User),
crypto.PubkeyToAddress(*pubkey),
nonce.Uint64(),
utils.CombineRSV(req.Transaction.Signature.R, req.Transaction.Signature.S, uint8(req.Transaction.Signature.V)))
return &orderbookv1.Response{}, err
}
func StartServer(grpcPort int, gwPort int, eg *engine.Engine) error {
defer log.Info("grpc and gateway server stopped")
log.Info("starting grpc and gateway server", "grpcPort", grpcPort, "gwPort", gwPort)
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
gServer := &server{
eg: eg,
}
listener, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", grpcPort))
if err != nil {
log.Error("failed to listen", "err", err)
return err
}
errCh := make(chan error)
grpcServer := grpc.NewServer()
orderbookv1.RegisterOrderbookServer(grpcServer, gServer)
go func(ch chan error) {
var e = grpcServer.Serve(listener)
ch <- e
}(errCh)
mux := runtime.NewServeMux()
opts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}
err = orderbookv1.RegisterOrderbookHandlerFromEndpoint(ctx, mux, fmt.Sprintf("127.0.0.1:%d", grpcPort), opts)
if err != nil {
return err
}
go func(ch chan error) {
var e = http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", gwPort), mux)
ch <- e
}(errCh)
e := <-errCh
return e
}
package handlers
import (
"net/http"
"github.com/ethereum/go-ethereum/common"
"github.com/holiman/uint256"
"github.com/gin-gonic/gin"
apiTypes "github.com/exchain/process/api/types"
"github.com/exchain/process/engine"
"github.com/exchain/process/orderbook"
"github.com/exchain/process/types"
"github.com/exchain/process/utils"
)
type OrderHandler struct {
eg *engine.Engine
}
func NewOrderHandler(eg *engine.Engine) *OrderHandler {
return &OrderHandler{
eg: eg,
}
}
// PlaceLimitOrder 处理限价单请求
func (h *OrderHandler) PlaceLimitOrder(c *gin.Context) {
var req apiTypes.PlaceLimitOrderRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
ok, agent := req.Recover()
if !ok {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid signature"})
return
}
side := orderbook.Buy
if req.Side == "sell" {
side = orderbook.Sell
}
quantity, err := uint256.FromDecimal(req.Quantity)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid quantity"})
return
}
price, err := uint256.FromDecimal(req.Price)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid price"})
return
}
orderID := utils.GenOrderID()
_, resp, err := h.eg.ProcessLimitOrder(orderID, types.Coin(req.BaseToken), types.Coin(req.QuoteToken), agent, side, price, quantity, req.Nonce)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, resp)
}
// PlaceMarketOrder 处理市价单请求
func (h *OrderHandler) PlaceMarketOrder(c *gin.Context) {
var req apiTypes.PlaceMarketOrderRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
ok, agent := req.Recover()
if !ok {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid signature"})
return
}
side := orderbook.Sell
if req.Side == "buy" {
side = orderbook.Buy
}
quantity, err := uint256.FromDecimal(req.Quantity)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid quantity"})
return
}
orderID := utils.GenOrderID()
_, resp, err := h.eg.ProcessMarketOrder(orderID,
types.Coin(req.BaseToken),
types.Coin(req.QuoteToken),
agent,
side, quantity, req.Nonce)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, resp)
}
// GetOrderBook 获取订单簿深度
func (h *OrderHandler) GetOrderBookDepth(c *gin.Context) {
var req apiTypes.DepthRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
asks, bids, err := h.eg.Depth(types.Coin(req.BaseToken), types.Coin(req.QuoteToken))
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"asks": asks,
"bids": bids,
})
}
// CancelOrder 取消订单
func (h *OrderHandler) CancelOrder(c *gin.Context) {
var req apiTypes.CancelOrderRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
ok, agent := req.Recover()
if !ok {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid signature"})
return
}
err := h.eg.ProcessCancelOrder(req.OrderId, types.Coin(req.BaseToken), types.Coin(req.QuoteToken), agent, req.Nonce)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"message": "success",
})
}
func (h *OrderHandler) SignProxy(c *gin.Context) {
var req apiTypes.SignProxyRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
ok, user := req.Recover()
if !ok {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid signature"})
return
}
err := h.eg.PorcessSignProxy(user, common.HexToAddress(req.Agent), req.Nonce)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"message": "success",
})
}
package router
import (
"github.com/exchain/process/api/handlers"
"github.com/gin-gonic/gin"
)
// SetupRouter 配置API路由
func SetupRouter(orderHandler *handlers.OrderHandler) *gin.Engine {
r := gin.Default()
// 订单相关路由
orderGroup := r.Group("/api/v1/orders")
{
orderGroup.POST("/limit", orderHandler.PlaceLimitOrder) // 限价单
orderGroup.POST("/market", orderHandler.PlaceMarketOrder) // 市价单
orderGroup.POST("/cancel", orderHandler.CancelOrder) // 取消订单
}
// 订单簿相关路由
r.GET("/api/v1/orderbook", orderHandler.GetOrderBookDepth) // 获取订单簿深度
return r
}
...@@ -17,14 +17,16 @@ type PlaceLimitOrderRequest struct { ...@@ -17,14 +17,16 @@ type PlaceLimitOrderRequest struct {
Quantity string `json:"quantity" binding:"required"` Quantity string `json:"quantity" binding:"required"`
Price string `json:"price" binding:"required"` Price string `json:"price" binding:"required"`
Nonce uint64 `json:"nonce" binding:"required"` Nonce uint64 `json:"nonce" binding:"required"`
V int32 `json:"v" binding:"required"` Proxy *bool `json:"proxy" binding:"required"`
V uint8 `json:"v" binding:"required"`
R hexutil.Bytes `json:"r" binding:"required"` R hexutil.Bytes `json:"r" binding:"required"`
S hexutil.Bytes `json:"s" binding:"required"` S hexutil.Bytes `json:"s" binding:"required"`
} }
func (r *PlaceLimitOrderRequest) Recover() (ok bool, address common.Address) { func (r *PlaceLimitOrderRequest) Recover() (ok bool, address common.Address) {
data := fmt.Sprintf("baseToken=%s&quoteToken=%s&side=%s&quantity=%s&price=%s&nonce=%d", r.BaseToken, r.QuoteToken, r.Side, r.Quantity, r.Price, r.Nonce) data := fmt.Sprintf("baseToken=%s&quoteToken=%s&side=%s&quantity=%s&price=%s&nonce=%d", r.BaseToken, r.QuoteToken, r.Side, r.Quantity, r.Price, r.Nonce)
hashsum := crypto.Keccak256Hash([]byte(data)) prefixedMessage := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(data), data)
hashsum := crypto.Keccak256Hash([]byte(prefixedMessage))
// combile sig // combile sig
sig := make([]byte, 65) sig := make([]byte, 65)
copy(sig, r.R) copy(sig, r.R)
...@@ -45,14 +47,16 @@ type PlaceMarketOrderRequest struct { ...@@ -45,14 +47,16 @@ type PlaceMarketOrderRequest struct {
Side string `json:"side" binding:"required,oneof=buy sell"` Side string `json:"side" binding:"required,oneof=buy sell"`
Quantity string `json:"quantity" binding:"required"` Quantity string `json:"quantity" binding:"required"`
Nonce uint64 `json:"nonce" binding:"required"` Nonce uint64 `json:"nonce" binding:"required"`
V int32 `json:"v" binding:"required"` Proxy *bool `json:"proxy" binding:"required"`
V uint8 `json:"v" binding:"required"`
R hexutil.Bytes `json:"r" binding:"required"` R hexutil.Bytes `json:"r" binding:"required"`
S hexutil.Bytes `json:"s" binding:"required"` S hexutil.Bytes `json:"s" binding:"required"`
} }
func (r *PlaceMarketOrderRequest) Recover() (ok bool, address common.Address) { func (r *PlaceMarketOrderRequest) Recover() (ok bool, address common.Address) {
data := fmt.Sprintf("baseToken=%s&quoteToken=%s&side=%s&quantity=%s&nonce=%d", r.BaseToken, r.QuoteToken, r.Side, r.Quantity, r.Nonce) data := fmt.Sprintf("baseToken=%s&quoteToken=%s&side=%s&quantity=%s&nonce=%d", r.BaseToken, r.QuoteToken, r.Side, r.Quantity, r.Nonce)
hashsum := crypto.Keccak256Hash([]byte(data)) prefixedMessage := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(data), data)
hashsum := crypto.Keccak256Hash([]byte(prefixedMessage))
// combile sig // combile sig
sig := make([]byte, 65) sig := make([]byte, 65)
copy(sig, r.R) copy(sig, r.R)
...@@ -75,14 +79,16 @@ type CancelOrderRequest struct { ...@@ -75,14 +79,16 @@ type CancelOrderRequest struct {
QuoteToken string `json:"quoteToken" binding:"required"` QuoteToken string `json:"quoteToken" binding:"required"`
OrderId string `json:"orderId" binding:"required"` OrderId string `json:"orderId" binding:"required"`
Nonce uint64 `json:"nonce" binding:"required"` Nonce uint64 `json:"nonce" binding:"required"`
V int32 `json:"v" binding:"required"` Proxy *bool `json:"proxy" binding:"required"`
V uint8 `json:"v" binding:"required"`
R hexutil.Bytes `json:"r" binding:"required"` R hexutil.Bytes `json:"r" binding:"required"`
S hexutil.Bytes `json:"s" binding:"required"` S hexutil.Bytes `json:"s" binding:"required"`
} }
func (r *CancelOrderRequest) Recover() (ok bool, address common.Address) { func (r *CancelOrderRequest) Recover() (ok bool, address common.Address) {
data := fmt.Sprintf("baseToken=%s&quoteToken=%s&orderId=%s&nonce=%d", r.BaseToken, r.QuoteToken, r.OrderId, r.Nonce) data := fmt.Sprintf("baseToken=%s&quoteToken=%s&orderId=%s&nonce=%d", r.BaseToken, r.QuoteToken, r.OrderId, r.Nonce)
hashsum := crypto.Keccak256Hash([]byte(data)) prefixedMessage := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(data), data)
hashsum := crypto.Keccak256Hash([]byte(prefixedMessage))
// combile sig // combile sig
sig := make([]byte, 65) sig := make([]byte, 65)
copy(sig, r.R) copy(sig, r.R)
...@@ -104,8 +110,9 @@ type SignProxyRequest struct { ...@@ -104,8 +110,9 @@ type SignProxyRequest struct {
} }
func (r *SignProxyRequest) Recover() (ok bool, address common.Address) { func (r *SignProxyRequest) Recover() (ok bool, address common.Address) {
data := fmt.Sprintf("s&agent=%s&nonce=%d", r.Agent, r.Nonce) data := fmt.Sprintf("agent=%s&nonce=%d", r.Agent, r.Nonce)
hashsum := crypto.Keccak256Hash([]byte(data)) prefixedMessage := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(data), data)
hashsum := crypto.Keccak256Hash([]byte(prefixedMessage))
// combile sig // combile sig
sig := make([]byte, 65) sig := make([]byte, 65)
copy(sig, r.R) copy(sig, r.R)
......
package main package main
import ( import (
"log" apigateway "github.com/exchain/process/api/gateway"
"github.com/exchain/process/api/handlers"
"github.com/exchain/process/api/router"
"github.com/exchain/process/engine" "github.com/exchain/process/engine"
) )
func main() { func main() {
eg := engine.GetEngine() eg := engine.GetEngine()
// 创建订单处理器 // 创建订单处理器
orderHandler := handlers.NewOrderHandler(eg) apigateway.StartServer(8111, 8112, eg)
// 设置路由
r := router.SetupRouter(orderHandler)
// 启动服务器
if err := r.Run(":8080"); err != nil {
log.Fatalf("Failed to start server: %v", err)
}
} }
...@@ -2,14 +2,29 @@ package engine ...@@ -2,14 +2,29 @@ package engine
import ( import (
"errors" "errors"
"fmt"
"github.com/exchain/process/types" "github.com/exchain/process/types"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/holiman/uint256" "github.com/holiman/uint256"
) )
func (e *Engine) PorcessSignProxy(user common.Address, proxy common.Address, nonce uint64) (err error) { func (e *Engine) PorcessSignProxy(user common.Address, proxy common.Address, nonce uint64, signature []byte) (err error) {
data := fmt.Sprintf("agent=%s&nonce=%d", proxy.Hex(), nonce)
prefixedMessage := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(data), data)
hashsum := crypto.Keccak256Hash([]byte(prefixedMessage))
pubkey, err := crypto.SigToPub(hashsum.Bytes(), signature)
if err != nil {
return
}
recovered := crypto.PubkeyToAddress(*pubkey)
if recovered.Hex() != user.Hex() {
err = errors.New("invalid signature")
return
}
userObject, err := e.db.GetOrNewAccountObject(user) userObject, err := e.db.GetOrNewAccountObject(user)
if err != nil { if err != nil {
return return
......
...@@ -125,11 +125,8 @@ func (e *Engine) NewPayload(params exchain.PayloadParams) (exchain.ExecutionResu ...@@ -125,11 +125,8 @@ func (e *Engine) NewPayload(params exchain.PayloadParams) (exchain.ExecutionResu
return result, nil return result, nil
} }
func (e *Engine) ProcessPayload(block *nebulav1.Block) (exchain.ExecutionResult, error) {
panic("not implemented")
}
func (e *Engine) AddToQueue(tx types.Transaction) { func (e *Engine) AddToQueue(tx types.Transaction) {
r, s, v := tx.GetRSV()
switch t := tx.(type) { switch t := tx.(type) {
case *types.PlaceOrderTx: case *types.PlaceOrderTx:
txType := nebulav1.TxType_MarketTx txType := nebulav1.TxType_MarketTx
...@@ -144,6 +141,12 @@ func (e *Engine) AddToQueue(tx types.Transaction) { ...@@ -144,6 +141,12 @@ func (e *Engine) AddToQueue(tx types.Transaction) {
TxType: txType, TxType: txType,
User: t.User.Hex(), User: t.User.Hex(),
Nonce: t.GetNonce(), Nonce: t.GetNonce(),
Proxy: t.Proxy,
Signature: &nebulav1.Signature{
V: int32(v),
R: r,
S: s,
},
Tx: &nebulav1.Transaction_LimitTx{ Tx: &nebulav1.Transaction_LimitTx{
LimitTx: &nebulav1.LimitOrderTransaction{ LimitTx: &nebulav1.LimitOrderTransaction{
Pair: t.GetPair(), Pair: t.GetPair(),
...@@ -159,6 +162,12 @@ func (e *Engine) AddToQueue(tx types.Transaction) { ...@@ -159,6 +162,12 @@ func (e *Engine) AddToQueue(tx types.Transaction) {
TxType: nebulav1.TxType_CancelTx, TxType: nebulav1.TxType_CancelTx,
User: t.User.Hex(), User: t.User.Hex(),
Nonce: t.GetNonce(), Nonce: t.GetNonce(),
Proxy: t.Proxy,
Signature: &nebulav1.Signature{
V: int32(v),
R: r,
S: s,
},
Tx: &nebulav1.Transaction_CancelTx{ Tx: &nebulav1.Transaction_CancelTx{
CancelTx: &nebulav1.CancelOrderTransaction{ CancelTx: &nebulav1.CancelOrderTransaction{
OrderId: t.GetOrderID(), OrderId: t.GetOrderID(),
...@@ -166,12 +175,17 @@ func (e *Engine) AddToQueue(tx types.Transaction) { ...@@ -166,12 +175,17 @@ func (e *Engine) AddToQueue(tx types.Transaction) {
}, },
} }
e.txQueue <- ptx e.txQueue <- ptx
case *types.SignProxyTx: case *types.SignProxyTx:
pTx := &nebulav1.Transaction{ pTx := &nebulav1.Transaction{
TxType: nebulav1.TxType_SignProxyTx, TxType: nebulav1.TxType_SignProxyTx,
User: t.User.Hex(), User: t.User.Hex(),
Nonce: t.GetNonce(), Nonce: t.GetNonce(),
Proxy: t.Proxy,
Signature: &nebulav1.Signature{
V: int32(v),
R: r,
S: s,
},
Tx: &nebulav1.Transaction_SignProxyTx{ Tx: &nebulav1.Transaction_SignProxyTx{
SignProxyTx: &nebulav1.SignProxyTransaction{ SignProxyTx: &nebulav1.SignProxyTransaction{
SignerProxy: t.GetProxyAddress().Bytes(), SignerProxy: t.GetProxyAddress().Bytes(),
......
...@@ -2,17 +2,32 @@ package engine ...@@ -2,17 +2,32 @@ package engine
import ( import (
"errors" "errors"
"fmt"
apiTypes "github.com/exchain/process/api/types" apiTypes "github.com/exchain/process/api/types"
"github.com/exchain/process/orderbook" "github.com/exchain/process/orderbook"
"github.com/exchain/process/types" "github.com/exchain/process/types"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/holiman/uint256" "github.com/holiman/uint256"
) )
func (e *Engine) ProcessMarketOrder(orderId string, baseToken, quoteToken types.Coin, agent common.Address, side orderbook.Side, quantity *uint256.Int, nonce uint64) (tx *types.PlaceOrderTx, response *apiTypes.OrderResponse, err error) { func (e *Engine) ProcessMarketOrder(orderId string, baseToken, quoteToken types.Coin, agent common.Address, side orderbook.Side, quantity *uint256.Int, nonce uint64, proxy bool, signature []byte) (tx *types.PlaceOrderTx, response *apiTypes.OrderResponse, err error) {
response = new(apiTypes.OrderResponse) response = new(apiTypes.OrderResponse)
data := fmt.Sprintf("baseToken=%s&quoteToken=%s&side=%s&quantity=%s&nonce=%d", baseToken, quoteToken, side, quantity.String(), nonce)
prefixedMessage := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(data), data)
hashsum := crypto.Keccak256Hash([]byte(prefixedMessage))
pubkey, err := crypto.SigToPub(hashsum.Bytes(), signature)
if err != nil {
return
}
recovered := crypto.PubkeyToAddress(*pubkey)
if !proxy && (recovered.Hex() != agent.Hex()) {
err = errors.New("invalid signature")
return
}
e.Lock() e.Lock()
ob, ok := e.orderbooks[string(baseToken+quoteToken)] ob, ok := e.orderbooks[string(baseToken+quoteToken)]
e.Unlock() e.Unlock()
...@@ -21,7 +36,7 @@ func (e *Engine) ProcessMarketOrder(orderId string, baseToken, quoteToken types. ...@@ -21,7 +36,7 @@ func (e *Engine) ProcessMarketOrder(orderId string, baseToken, quoteToken types.
return return
} }
makerObj, err := e.db.GetOrNewAcountObjectByAgent(agent) makerObj, err := e.db.GetOrNewAccountObject(agent)
if err != nil { if err != nil {
return return
} }
...@@ -31,6 +46,11 @@ func (e *Engine) ProcessMarketOrder(orderId string, baseToken, quoteToken types. ...@@ -31,6 +46,11 @@ func (e *Engine) ProcessMarketOrder(orderId string, baseToken, quoteToken types.
return return
} }
if proxy && (recovered.Hex() != makerObj.Proxy.Hex()) {
err = errors.New("invalid signature")
return
}
tx = new(types.PlaceOrderTx) tx = new(types.PlaceOrderTx)
tx.OrderID = orderId tx.OrderID = orderId
tx.Nonce = nonce tx.Nonce = nonce
...@@ -39,6 +59,8 @@ func (e *Engine) ProcessMarketOrder(orderId string, baseToken, quoteToken types. ...@@ -39,6 +59,8 @@ func (e *Engine) ProcessMarketOrder(orderId string, baseToken, quoteToken types.
tx.BaseCoin = baseToken tx.BaseCoin = baseToken
tx.QuoteCoin = quoteToken tx.QuoteCoin = quoteToken
tx.Type = types.PlaceOrder tx.Type = types.PlaceOrder
tx.Signature = signature
tx.Proxy = proxy
if side == orderbook.Sell { if side == orderbook.Sell {
tx.Action = types.OrderActionSell tx.Action = types.OrderActionSell
} else { } else {
...@@ -147,8 +169,21 @@ func (e *Engine) ProcessMarketOrder(orderId string, baseToken, quoteToken types. ...@@ -147,8 +169,21 @@ func (e *Engine) ProcessMarketOrder(orderId string, baseToken, quoteToken types.
return tx, response, nil return tx, response, nil
} }
func (e *Engine) ProcessLimitOrder(orderId string, baseToken, quoteToken types.Coin, agent common.Address, side orderbook.Side, price, quantity *uint256.Int, nonce uint64) (tx *types.PlaceOrderTx, response *apiTypes.OrderResponse, err error) { func (e *Engine) ProcessLimitOrder(orderId string, baseToken, quoteToken types.Coin, agent common.Address, side orderbook.Side, price, quantity *uint256.Int, nonce uint64, proxy bool, signature []byte) (tx *types.PlaceOrderTx, response *apiTypes.OrderResponse, err error) {
response = new(apiTypes.OrderResponse) response = new(apiTypes.OrderResponse)
data := fmt.Sprintf("baseToken=%s&quoteToken=%s&side=%s&quantity=%s&price=%s&nonce=%d", baseToken, quoteToken, side, quantity.String(), price.String(), nonce)
prefixedMessage := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(data), data)
hashsum := crypto.Keccak256Hash([]byte(prefixedMessage))
pubkey, err := crypto.SigToPub(hashsum.Bytes(), signature)
if err != nil {
return
}
recovered := crypto.PubkeyToAddress(*pubkey)
if !proxy && (recovered.Hex() != agent.Hex()) {
err = errors.New("invalid signature")
return
}
e.Lock() e.Lock()
ob, ok := e.orderbooks[string(baseToken+quoteToken)] ob, ok := e.orderbooks[string(baseToken+quoteToken)]
e.Unlock() e.Unlock()
...@@ -157,15 +192,21 @@ func (e *Engine) ProcessLimitOrder(orderId string, baseToken, quoteToken types.C ...@@ -157,15 +192,21 @@ func (e *Engine) ProcessLimitOrder(orderId string, baseToken, quoteToken types.C
return return
} }
makerObj, err := e.db.GetOrNewAcountObjectByAgent(agent) makerObj, err := e.db.GetOrNewAccountObject(agent)
if err != nil { if err != nil {
return return
} }
if makerObj == nil { if makerObj == nil {
err = errors.New("invalid agent") err = errors.New("invalid agent")
return return
} }
if proxy && (recovered.Hex() != makerObj.Proxy.Hex()) {
err = errors.New("invalid signature")
return
}
tx = new(types.PlaceOrderTx) tx = new(types.PlaceOrderTx)
tx.OrderID = orderId tx.OrderID = orderId
tx.Nonce = nonce tx.Nonce = nonce
...@@ -174,6 +215,8 @@ func (e *Engine) ProcessLimitOrder(orderId string, baseToken, quoteToken types.C ...@@ -174,6 +215,8 @@ func (e *Engine) ProcessLimitOrder(orderId string, baseToken, quoteToken types.C
tx.BaseCoin = baseToken tx.BaseCoin = baseToken
tx.QuoteCoin = quoteToken tx.QuoteCoin = quoteToken
tx.Type = types.PlaceOrder tx.Type = types.PlaceOrder
tx.Signature = signature
tx.Proxy = proxy
if side == orderbook.Sell { if side == orderbook.Sell {
tx.Action = types.OrderActionSell tx.Action = types.OrderActionSell
} else { } else {
...@@ -285,7 +328,20 @@ func (e *Engine) ProcessLimitOrder(orderId string, baseToken, quoteToken types.C ...@@ -285,7 +328,20 @@ func (e *Engine) ProcessLimitOrder(orderId string, baseToken, quoteToken types.C
return tx, response, nil return tx, response, nil
} }
func (e *Engine) ProcessCancelOrder(orderId string, baseToken, quoteToken types.Coin, agent common.Address, nonce uint64) (err error) { func (e *Engine) ProcessCancelOrder(orderId string, baseToken, quoteToken types.Coin, agent common.Address, nonce uint64, proxy bool, signature []byte) (err error) {
data := fmt.Sprintf("baseToken=%s&quoteToken=%s&orderId=%s&nonce=%d", baseToken, quoteToken, orderId, nonce)
prefixedMessage := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(data), data)
hashsum := crypto.Keccak256Hash([]byte(prefixedMessage))
pubkey, err := crypto.SigToPub(hashsum.Bytes(), signature)
if err != nil {
return
}
recovered := crypto.PubkeyToAddress(*pubkey)
if !proxy && (recovered.Hex() != agent.Hex()) {
err = errors.New("invalid signature")
return
}
e.Lock() e.Lock()
ob, ok := e.orderbooks[string(baseToken+quoteToken)] ob, ok := e.orderbooks[string(baseToken+quoteToken)]
e.Unlock() e.Unlock()
...@@ -293,7 +349,7 @@ func (e *Engine) ProcessCancelOrder(orderId string, baseToken, quoteToken types. ...@@ -293,7 +349,7 @@ func (e *Engine) ProcessCancelOrder(orderId string, baseToken, quoteToken types.
return return
} }
makerObj, err := e.db.GetOrNewAcountObjectByAgent(agent) makerObj, err := e.db.GetOrNewAccountObject(agent)
if err != nil { if err != nil {
return return
} }
...@@ -301,6 +357,11 @@ func (e *Engine) ProcessCancelOrder(orderId string, baseToken, quoteToken types. ...@@ -301,6 +357,11 @@ func (e *Engine) ProcessCancelOrder(orderId string, baseToken, quoteToken types.
return errors.New("invalid agent") return errors.New("invalid agent")
} }
if proxy && (recovered.Hex() != makerObj.Proxy.Hex()) {
err = errors.New("invalid signature")
return
}
order := ob.Order(orderId) order := ob.Order(orderId)
if order == nil { if order == nil {
return errors.New("invalid order id") return errors.New("invalid order id")
...@@ -317,11 +378,16 @@ func (e *Engine) ProcessCancelOrder(orderId string, baseToken, quoteToken types. ...@@ -317,11 +378,16 @@ func (e *Engine) ProcessCancelOrder(orderId string, baseToken, quoteToken types.
tx := &types.CancelOrderTx{ tx := &types.CancelOrderTx{
Tx: types.Tx{ Tx: types.Tx{
OrderID: orderId, OrderID: orderId,
Time: nonce, Time: nonce,
User: makerObj.Address, User: makerObj.Address,
Action: types.OrderActionCancel, BaseCoin: baseToken,
Nonce: nonce, QuoteCoin: quoteToken,
Type: types.CancelOrder,
Action: types.OrderActionCancel,
Nonce: nonce,
Proxy: proxy,
Signature: signature,
}, },
} }
......
package engine package engine
import ( import (
"fmt"
"math/big"
"strings"
"github.com/exchain/process/orderbook"
"github.com/exchain/process/types" "github.com/exchain/process/types"
"github.com/exchain/process/utils"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/exchain/go-exchain/exchain"
"github.com/exchain/go-exchain/exchain/chaindb"
nebulav1 "github.com/exchain/go-exchain/exchain/protocol/gen/go/nebula/v1" nebulav1 "github.com/exchain/go-exchain/exchain/protocol/gen/go/nebula/v1"
"github.com/exchain/go-exchain/exchain/wrapper" "github.com/exchain/go-exchain/exchain/wrapper"
"github.com/holiman/uint256" "github.com/holiman/uint256"
) )
func (e *Engine) ProcessPayload(block *nebulav1.Block) (exchain.ExecutionResult, error) {
genesis := block.Header.Height == 0
if !genesis {
parent, err := e.chainDB.GetBlockByLabel(chaindb.ExChainBlockLatest)
if err != nil {
return exchain.ExecutionResult{}, err
}
if parent.Header.Height+1 != block.Header.Height {
return exchain.ExecutionResult{}, fmt.Errorf("invalid block height")
}
}
receipts, err := e.ProcessTx(block.Header, block.Transactions)
if err != nil {
return exchain.ExecutionResult{}, err
}
return exchain.ExecutionResult{
Payload: block,
Receipts: receipts,
}, nil
}
func (e *Engine) ProcessOrders(header *nebulav1.BlockHeader) (txs *nebulav1.TransactionList, receipts *nebulav1.TransactionReceiptList, err error) { func (e *Engine) ProcessOrders(header *nebulav1.BlockHeader) (txs *nebulav1.TransactionList, receipts *nebulav1.TransactionReceiptList, err error) {
txs = &nebulav1.TransactionList{} txs = &nebulav1.TransactionList{}
txs.Txs = make([]*nebulav1.Transaction, 0) txs.Txs = make([]*nebulav1.Transaction, 0)
...@@ -131,6 +161,77 @@ func (e *Engine) ProcessTx(header *nebulav1.BlockHeader, txs *nebulav1.Transacti ...@@ -131,6 +161,77 @@ func (e *Engine) ProcessTx(header *nebulav1.BlockHeader, txs *nebulav1.Transacti
receipt.Content = &nebulav1.TransactionReceipt_DepositR{ receipt.Content = &nebulav1.TransactionReceipt_DepositR{
DepositR: &nebulav1.DepositReceipt{}, DepositR: &nebulav1.DepositReceipt{},
} }
case nebulav1.TxType_CreatePairTx:
err := e.ProcessCreatePair(tx.GetCreatePairTx())
if err != nil {
receipt.Success = false
}
receipt.Content = &nebulav1.TransactionReceipt_CreatePairR{
CreatePairR: &nebulav1.CreatePairReceipt{},
}
case nebulav1.TxType_DisablePairTx:
err := e.ProcessDisablePair(tx.GetDisablePairTx())
if err != nil {
receipt.Success = false
}
receipt.Content = &nebulav1.TransactionReceipt_DisablePairR{
DisablePairR: &nebulav1.DisablePairReceipt{},
}
case nebulav1.TxType_MarketTx:
coins := strings.Split(tx.GetMarketTx().Pair, "-")
baseCoin := types.Coin(coins[0])
quoteCoin := types.Coin(coins[1])
user := common.HexToAddress(tx.User)
side := orderbook.Buy
if tx.GetMarketTx().Side == nebulav1.OrderSide_SELL {
side = orderbook.Sell
}
quantity := new(uint256.Int).SetBytes(tx.GetMarketTx().Quantity)
nonce := new(big.Int).SetBytes(tx.Nonce)
_, _, err := e.ProcessMarketOrder(tx.GetMarketTx().OrderId,
baseCoin, quoteCoin, user, side, quantity, nonce.Uint64(),
tx.Proxy,
utils.CombineRSV(tx.Signature.R, tx.Signature.S, uint8(tx.Signature.V)))
if err != nil {
receipt.Success = false
}
receipt.Content = &nebulav1.TransactionReceipt_MarketR{
MarketR: &nebulav1.MarketOrderReceipt{},
}
case nebulav1.TxType_LimitTx:
coins := strings.Split(tx.GetLimitTx().Pair, "-")
baseCoin := types.Coin(coins[0])
quoteCoin := types.Coin(coins[1])
user := common.HexToAddress(tx.User)
side := orderbook.Buy
if tx.GetLimitTx().Side == nebulav1.OrderSide_SELL {
side = orderbook.Sell
}
quantity := new(uint256.Int).SetBytes(tx.GetLimitTx().Quantity)
price := new(uint256.Int).SetBytes(tx.GetLimitTx().Price)
nonce := new(big.Int).SetBytes(tx.Nonce)
_, _, err := e.ProcessLimitOrder(tx.GetLimitTx().OrderId,
baseCoin, quoteCoin, user, side, quantity, price, nonce.Uint64(),
tx.Proxy,
utils.CombineRSV(tx.Signature.R, tx.Signature.S, uint8(tx.Signature.V)))
if err != nil {
receipt.Success = false
}
receipt.Content = &nebulav1.TransactionReceipt_LimitR{
LimitR: &nebulav1.LimitOrderReceipt{},
}
case nebulav1.TxType_CancelTx:
coins := strings.Split(tx.GetCancelTx().Pair, "-")
baseCoin := types.Coin(coins[0])
quoteCoin := types.Coin(coins[1])
user := common.HexToAddress(tx.User)
err := e.ProcessCancelOrder(tx.GetCancelTx().OrderId, baseCoin, quoteCoin, user, new(big.Int).SetBytes(tx.Nonce).Uint64(), tx.Proxy, utils.CombineRSV(tx.Signature.R, tx.Signature.S, uint8(tx.Signature.V)))
if err != nil {
receipt.Success = false
}
receipt.Content = &nebulav1.TransactionReceipt_CancelR{
CancelR: &nebulav1.CancelOrderReceipt{},
}
default: default:
} }
receipts.Receipts = append(receipts.Receipts, receipt) receipts.Receipts = append(receipts.Receipts, receipt)
......
...@@ -5,55 +5,59 @@ go 1.23.3 ...@@ -5,55 +5,59 @@ go 1.23.3
require ( require (
github.com/bwmarrin/snowflake v0.3.0 github.com/bwmarrin/snowflake v0.3.0
github.com/emirpasic/gods v1.18.1 github.com/emirpasic/gods v1.18.1
github.com/ethereum/go-ethereum v1.15.0 github.com/ethereum/go-ethereum v1.15.7
github.com/exchain/go-exchain v0.0.1 github.com/exchain/go-exchain v0.0.1
github.com/gin-gonic/gin v1.10.0 github.com/gin-gonic/gin v1.10.0
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/holiman/uint256 v1.3.2 github.com/holiman/uint256 v1.3.2
github.com/syndtr/goleveldb v1.0.1-0.20220614013038-64ee5596c38a github.com/syndtr/goleveldb v1.0.1-0.20220614013038-64ee5596c38a
google.golang.org/grpc v1.57.1
) )
require ( require (
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect github.com/decred/dcrd/dcrec/secp256k1/v4 v4.4.0 // indirect
github.com/golang/protobuf v1.5.4 // indirect github.com/golang/protobuf v1.5.4 // indirect
github.com/google/uuid v1.6.0 // indirect github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/golang-lru v0.5.0 // indirect github.com/hashicorp/golang-lru v1.0.2 // indirect
github.com/oklog/ulid v1.3.1 // indirect github.com/oklog/ulid v1.3.1 // indirect
github.com/oklog/ulid/v2 v2.1.0 // indirect github.com/oklog/ulid/v2 v2.1.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect github.com/yusufpapurcu/wmi v1.2.4 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
) )
require ( require (
github.com/bytedance/sonic v1.11.6 // indirect github.com/bytedance/sonic v1.13.2 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect github.com/bytedance/sonic/loader v0.2.4 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect github.com/cloudwego/base64x v0.1.5 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect github.com/gabriel-vasile/mimetype v1.4.8 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect github.com/gin-contrib/sse v1.0.0 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect github.com/go-ole/go-ole v1.3.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.20.0 // indirect github.com/go-playground/validator/v10 v10.26.0 // indirect
github.com/goccy/go-json v0.10.3 // indirect github.com/goccy/go-json v0.10.5 // indirect
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect github.com/golang/snappy v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.8 // indirect github.com/klauspost/cpuid/v2 v2.2.10 // indirect
github.com/leodido/go-urn v1.4.0 // indirect github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/shirou/gopsutil v3.21.11+incompatible // indirect github.com/shirou/gopsutil v3.21.11+incompatible // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect github.com/tklauser/go-sysconf v0.3.15 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect github.com/tklauser/numcpus v0.10.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect github.com/ugorji/go/codec v1.2.12 // indirect
golang.org/x/arch v0.8.0 // indirect golang.org/x/arch v0.16.0 // indirect
golang.org/x/crypto v0.32.0 // indirect golang.org/x/crypto v0.36.0 // indirect
golang.org/x/net v0.34.0 // indirect golang.org/x/net v0.38.0 // indirect
golang.org/x/sys v0.29.0 // indirect golang.org/x/sys v0.32.0 // indirect
golang.org/x/text v0.21.0 // indirect golang.org/x/text v0.24.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect google.golang.org/genproto v0.0.0-20230726155614-23370e0ffb3e // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230711160842-782d3b101e98 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20250115164207-1a7da9e5054f // indirect
google.golang.org/protobuf v1.36.6 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect
) )
......
This diff is collapsed.
...@@ -5,6 +5,7 @@ import ( ...@@ -5,6 +5,7 @@ import (
"encoding/gob" "encoding/gob"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/exchain/process/utils"
"github.com/holiman/uint256" "github.com/holiman/uint256"
) )
...@@ -35,6 +36,7 @@ type Transaction interface { ...@@ -35,6 +36,7 @@ type Transaction interface {
GetOrderID() string GetOrderID() string
GetNonce() []byte GetNonce() []byte
GetProxyAddress() common.Address GetProxyAddress() common.Address
GetRSV() (r, s []byte, v uint8)
} }
type Tx struct { type Tx struct {
...@@ -46,6 +48,8 @@ type Tx struct { ...@@ -46,6 +48,8 @@ type Tx struct {
Type OrderType Type OrderType
Action OrderAction Action OrderAction
Nonce uint64 Nonce uint64
Proxy bool
Signature []byte
} }
type CancelOrderTx struct { type CancelOrderTx struct {
...@@ -71,6 +75,11 @@ func (tx *CancelOrderTx) GetUser() common.Address { ...@@ -71,6 +75,11 @@ func (tx *CancelOrderTx) GetUser() common.Address {
return tx.User return tx.User
} }
func (tx *CancelOrderTx) GetRSV() (r, s []byte, v uint8) {
r, s, v = utils.SplitRSV(tx.Signature)
return
}
func (tx *CancelOrderTx) GetLimitPrice() *uint256.Int { func (tx *CancelOrderTx) GetLimitPrice() *uint256.Int {
return nil return nil
} }
...@@ -152,6 +161,11 @@ func (tx *PlaceOrderTx) GetProxyAddress() common.Address { ...@@ -152,6 +161,11 @@ func (tx *PlaceOrderTx) GetProxyAddress() common.Address {
return common.Address{} return common.Address{}
} }
func (tx *PlaceOrderTx) GetRSV() (r, s []byte, v uint8) {
r, s, v = utils.SplitRSV(tx.Signature)
return
}
type SignProxyTx struct { type SignProxyTx struct {
Tx Tx
ProxyAddress common.Address ProxyAddress common.Address
...@@ -203,3 +217,8 @@ func (tx *SignProxyTx) GetNonce() []byte { ...@@ -203,3 +217,8 @@ func (tx *SignProxyTx) GetNonce() []byte {
func (tx *SignProxyTx) GetProxyAddress() common.Address { func (tx *SignProxyTx) GetProxyAddress() common.Address {
return tx.ProxyAddress return tx.ProxyAddress
} }
func (tx *SignProxyTx) GetRSV() (r, s []byte, v uint8) {
r, s, v = utils.SplitRSV(tx.Signature)
return
}
package utils
func SplitRSV(signature []byte) (r, s []byte, v uint8) {
r = signature[:32]
s = signature[32:64]
v = signature[64]
return
}
func CombineRSV(r, s []byte, v uint8) []byte {
return append(append(r, s...), v)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment