diff --git a/pkg/api/router.go b/pkg/api/router.go index 3e4574ed896..f6f75e348ea 100644 --- a/pkg/api/router.go +++ b/pkg/api/router.go @@ -491,6 +491,12 @@ func (s *Service) mountBusinessDebug(restricted bool) { handle("/wallet", jsonhttp.MethodHandler{ "GET": http.HandlerFunc(s.walletHandler), }) + handle("/wallet/withdraw/{coin}/{addr}", jsonhttp.MethodHandler{ + "POST": web.ChainHandlers( + s.gasConfigMiddleware("wallet withdraw"), + web.FinalHandlerFunc(s.walletWithdrawHandler), + ), + }) } } diff --git a/pkg/api/wallet.go b/pkg/api/wallet.go index c98e83b5987..6413c44b09f 100644 --- a/pkg/api/wallet.go +++ b/pkg/api/wallet.go @@ -5,11 +5,13 @@ package api import ( + "math/big" "net/http" "github.com/ethereum/go-ethereum/common" "github.com/ethersphere/bee/pkg/bigint" "github.com/ethersphere/bee/pkg/jsonhttp" + "github.com/gorilla/mux" ) type walletResponse struct { @@ -20,6 +22,10 @@ type walletResponse struct { WalletAddress common.Address `json:"walletAddress"` // the address of the bee wallet } +type walletTxResponse struct { + TransactionHash common.Hash `json:"transactionHash"` +} + func (s *Service) walletHandler(w http.ResponseWriter, r *http.Request) { logger := s.logger.WithName("get_wallet").Build() @@ -47,3 +53,36 @@ func (s *Service) walletHandler(w http.ResponseWriter, r *http.Request) { WalletAddress: s.ethereumAddress, }) } + +func (s *Service) walletWithdrawHandler(w http.ResponseWriter, r *http.Request) { + logger := s.logger.WithName("post_wallet_withdraw").Build() + + queries := struct { + Amount *big.Int `map:"amount" validate:"required"` + }{} + + if response := s.mapStructure(r.URL.Query(), &queries); response != nil { + response("invalid query params", logger, w) + return + } + + path := struct { + Address *common.Address `map:"address" validate:"required"` + }{} + + if response := s.mapStructure(mux.Vars(r), &path); response != nil { + response("invalid query params", logger, w) + return + } + + // check whitelisted + + txHash, err := s.erc20Service.Transfer(r.Context(), *path.Address, queries.Amount) + if err != nil { + logger.Error(err, "unable to transfer") + jsonhttp.InternalServerError(w, "unable to transfer amount") + return + } + + jsonhttp.OK(w, walletTxResponse{TransactionHash: txHash}) +}