2019-02-19 17:32:14 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-03-07 16:14:57 +00:00
|
|
|
"crypto/ecdsa"
|
2019-02-19 17:32:14 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-03-07 16:14:57 +00:00
|
|
|
"log"
|
|
|
|
"math/big"
|
|
|
|
"time"
|
2019-02-19 17:32:14 +00:00
|
|
|
|
2019-03-07 16:14:57 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/ethclient"
|
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2019-02-19 17:32:14 +00:00
|
|
|
recaptcha "github.com/prestonvanloon/go-recaptcha"
|
|
|
|
faucetpb "github.com/prysmaticlabs/prysm/proto/faucet"
|
|
|
|
"google.golang.org/grpc/peer"
|
|
|
|
)
|
|
|
|
|
2019-03-07 16:14:57 +00:00
|
|
|
const minScore = 0.8
|
|
|
|
|
|
|
|
var fundingAmount = big.NewInt(0.5 * params.Ether)
|
2019-02-19 17:32:14 +00:00
|
|
|
|
|
|
|
type faucetServer struct {
|
2019-03-07 16:14:57 +00:00
|
|
|
r recaptcha.Recaptcha
|
|
|
|
client *ethclient.Client
|
|
|
|
funder common.Address
|
|
|
|
pk *ecdsa.PrivateKey
|
2019-02-19 17:32:14 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 16:14:57 +00:00
|
|
|
func newFaucetServer(
|
|
|
|
r recaptcha.Recaptcha,
|
|
|
|
rpcPath string,
|
|
|
|
funderPrivateKey string,
|
|
|
|
) *faucetServer {
|
|
|
|
client, err := ethclient.DialContext(context.Background(), rpcPath)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pk, err := crypto.HexToECDSA(funderPrivateKey)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
funder := crypto.PubkeyToAddress(pk.PublicKey)
|
|
|
|
|
|
|
|
bal, err := client.BalanceAt(context.Background(), funder, nil)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Funder is %s\n", funder.Hex())
|
|
|
|
fmt.Printf("Funder has %d\n", bal)
|
|
|
|
|
|
|
|
return &faucetServer{
|
|
|
|
r: r,
|
|
|
|
client: client,
|
|
|
|
funder: funder,
|
|
|
|
pk: pk,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *faucetServer) verifyRecaptcha(ctx context.Context, req *faucetpb.FundingRequest) error {
|
2019-02-19 17:32:14 +00:00
|
|
|
p, ok := peer.FromContext(ctx)
|
|
|
|
if !ok {
|
2019-03-07 16:14:57 +00:00
|
|
|
return errors.New("peer from ctx not ok")
|
2019-02-19 17:32:14 +00:00
|
|
|
}
|
|
|
|
fmt.Printf("Sending captcha request for peer %s\n", p.Addr.String())
|
|
|
|
|
|
|
|
rr, err := s.r.Check(p.Addr.String(), req.RecaptchaResponse)
|
|
|
|
if err != nil {
|
2019-03-07 16:14:57 +00:00
|
|
|
return err
|
2019-02-19 17:32:14 +00:00
|
|
|
}
|
|
|
|
if !rr.Success {
|
|
|
|
fmt.Printf("Unsuccessful recaptcha request. Error codes: %+v\n", rr.ErrorCodes)
|
2019-03-07 16:14:57 +00:00
|
|
|
return errors.New("failed")
|
2019-02-19 17:32:14 +00:00
|
|
|
}
|
|
|
|
if rr.Score < minScore {
|
2019-03-07 16:14:57 +00:00
|
|
|
return errors.New("recaptcha score too low")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RequestFunds from the ethereum 1.x faucet. Requires a valid captcha
|
|
|
|
// response.
|
|
|
|
func (s *faucetServer) RequestFunds(ctx context.Context, req *faucetpb.FundingRequest) (*faucetpb.FundingResponse, error) {
|
|
|
|
|
|
|
|
if err := s.verifyRecaptcha(ctx, req); err != nil {
|
|
|
|
return &faucetpb.FundingResponse{Error: fmt.Sprintf("Recaptcha failure: %v", err)}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
txHash, err := s.fundAndWait(common.HexToAddress(req.WalletAddress))
|
|
|
|
if err != nil {
|
|
|
|
return &faucetpb.FundingResponse{Error: fmt.Sprintf("Failed to send transaction %v", err)}, nil
|
2019-02-19 17:32:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &faucetpb.FundingResponse{
|
2019-03-07 16:14:57 +00:00
|
|
|
Amount: fundingAmount.String(),
|
|
|
|
TransactionHash: txHash,
|
2019-02-19 17:32:14 +00:00
|
|
|
}, nil
|
|
|
|
}
|
2019-03-07 16:14:57 +00:00
|
|
|
|
|
|
|
func (s *faucetServer) fundAndWait(to common.Address) (string, error) {
|
|
|
|
nonce := uint64(0)
|
|
|
|
nonce, err := s.client.PendingNonceAt(context.Background(), s.funder)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
tx := types.NewTransaction(nonce, to, fundingAmount, 40000, big.NewInt(1*params.GWei), nil /*data*/)
|
|
|
|
|
|
|
|
tx, err = types.SignTx(tx, types.NewEIP155Signer(big.NewInt(5)), s.pk)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.client.SendTransaction(context.Background(), tx); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for contract to mine
|
|
|
|
for pending := true; pending; _, pending, err = s.client.TransactionByHash(context.Background(), tx.Hash()) {
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx.Hash().Hex(), nil
|
|
|
|
}
|