Merge pull request #10784 from filecoin-project/maciej/fountain

lotus-fountain: make compatible with 0x addresses #10560
This commit is contained in:
Aayush Rajasekaran 2023-05-12 12:17:56 -04:00 committed by GitHub
commit b3d0b1803f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,6 +7,7 @@ import (
"net" "net"
"net/http" "net/http"
"os" "os"
"strings"
"time" "time"
rice "github.com/GeertJohan/go.rice" rice "github.com/GeertJohan/go.rice"
@ -19,6 +20,7 @@ import (
"github.com/filecoin-project/lotus/api/v0api" "github.com/filecoin-project/lotus/api/v0api"
"github.com/filecoin-project/lotus/build" "github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/types/ethtypes"
lcli "github.com/filecoin-project/lotus/cli" lcli "github.com/filecoin-project/lotus/cli"
) )
@ -193,18 +195,34 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return return
} }
to, err := address.NewFromString(r.FormValue("address")) addressInput := r.FormValue("address")
if err != nil {
var filecoinAddress address.Address
var decodeError error
if strings.HasPrefix(addressInput, "0x") {
ethAddress, err := ethtypes.ParseEthAddress(addressInput)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
filecoinAddress, decodeError = ethAddress.ToFilecoinAddress()
} else {
filecoinAddress, decodeError = address.NewFromString(addressInput)
}
if decodeError != nil {
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest)
return return
} }
if to == address.Undef { if filecoinAddress == address.Undef {
http.Error(w, "empty address", http.StatusBadRequest) http.Error(w, "empty address", http.StatusBadRequest)
return return
} }
// Limit based on wallet address // Limit based on wallet address
limiter := h.limiter.GetWalletLimiter(to.String()) limiter := h.limiter.GetWalletLimiter(filecoinAddress.String())
if !limiter.Allow() { if !limiter.Allow() {
http.Error(w, http.StatusText(http.StatusTooManyRequests)+": wallet limit", http.StatusTooManyRequests) http.Error(w, http.StatusText(http.StatusTooManyRequests)+": wallet limit", http.StatusTooManyRequests)
return return
@ -230,7 +248,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
smsg, err := h.api.MpoolPushMessage(h.ctx, &types.Message{ smsg, err := h.api.MpoolPushMessage(h.ctx, &types.Message{
Value: types.BigInt(h.sendPerRequest), Value: types.BigInt(h.sendPerRequest),
From: h.from, From: h.from,
To: to, To: filecoinAddress,
}, nil) }, nil)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest)