Skip to content

Commit

Permalink
feat: get wallet info from command flags (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
iczc authored Mar 22, 2022
1 parent 5ca746c commit 8f628b6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 36 deletions.
53 changes: 35 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,38 +35,55 @@ go build -o eth-faucet

## Usage

1. Set up Web3 Provider and Private Key
* Use private key as funder
```bash
export WEB3_PROVIDER="rpc endpoint"
export PRIVATE_KEY="hex private key"
./eth-faucet -httpport 8080 -wallet.provider http://localhost:8545 -wallet.privkey privkey
```

2. Run the eth faucet application
* Use keystore as funder
```bash
./eth-faucet -httpport 8080
./eth-faucet -httpport 8080 -wallet.provider http://localhost:8545 -wallet.keyjson `pwd`/keystore -wallet.keypass password.txt
```

**Optional Flags**
### Parameters

| Flag | Description | Default Value
| -------------- | ------------------------------------------------ | -------------
| -httpport | Listener port to serve HTTP connection | 8080
| -proxycount | Count of reverse proxies in front of the server | 0
| -queuecap | Maximum transactions waiting to be sent | 100
| -faucet.amount | Number of Ethers to transfer per user request | 1
| -faucet.minutes| Number of minutes to wait between funding rounds | 1440
| -faucet.name | Network name to display on the frontend | testnet
The following are the available parameters to the faucet app:

**Basic Flags**

| Flag | Description | Default Value
| ---------------- | ------------------------------------------------ | -------------
| -httpport | Listener port to serve HTTP connection | 8080
| -proxycount | Count of reverse proxies in front of the server | 0
| -queuecap | Maximum transactions waiting to be sent | 100

**Faucet Flags**

| Flag | Description | Default Value
| ---------------- | ------------------------------------------------ | -------------
| -faucet.amount | Number of Ethers to transfer per user request | 1
| -faucet.minutes | Number of minutes to wait between funding rounds | 1440
| -faucet.name | Network name to display on the frontend | testnet

**Wallet Flags**

| Flag | Description | Default Value
| ---------------- | ------------------------------------------------ | -------------
| -wallet.provider | Endpoint for Ethereum JSON-RPC connection | $WEB3_PROVIDER
| -wallet.privkey | Private key hex to fund user requests with | $PRIVATE_KEY
| -wallet.keyjson | Keystore file to fund user requests with | $KEYSTORE
| -wallet.keypass | Passphrase text file to decrypt keystore | password.txt

### Docker deployment

* Use private key as sender
* Use private key as funder
```bash
docker run -d -p 8080:8080 -e WEB3_PROVIDER="rpc endpoint" -e PRIVATE_KEY="hex private key" chainflag/eth-faucet:1.0.0
docker run -d -p 8080:8080 -e WEB3_PROVIDER=rpc endpoint -e PRIVATE_KEY=hex private key chainflag/eth-faucet:1.0.0
```

* Use keystore file as sender
* Use keystore as funder
```bash
docker run -d -p 8080:8080 -e WEB3_PROVIDER="rpc endpoint" -e KEYSTORE="keystore path" -v `pwd`/keystore:/app/keystore -v `pwd`/password.txt:/app/password.txt chainflag/eth-faucet:1.0.0
docker run -d -p 8080:8080 -e WEB3_PROVIDER=rpc endpoint -e KEYSTORE=keystore path -v `pwd`/keystore:/app/keystore -v `pwd`/password.txt:/app/password.txt chainflag/eth-faucet:1.0.0
```

### Heroku deployment
Expand Down
35 changes: 17 additions & 18 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"crypto/ecdsa"
"errors"
"flag"
"fmt"
"math/big"
Expand All @@ -15,10 +16,6 @@ import (
"github.com/chainflag/eth-faucet/internal/server"
)

const (
DefaultKeyAuth = "password.txt"
)

var (
appVersion = "v1.0.0"
chainIDMap = map[string]int{"ropsten": 3, "rinkeby": 4, "goerli": 5, "kovan": 42}
Expand All @@ -31,6 +28,11 @@ var (
payoutFlag = flag.Int("faucet.amount", 1, "Number of Ethers to transfer per user request")
intervalFlag = flag.Int("faucet.minutes", 1440, "Number of minutes to wait between funding rounds")
netnameFlag = flag.String("faucet.name", "testnet", "Network name to display on the frontend")

keyJSONFlag = flag.String("wallet.keyjson", os.Getenv("KEYSTORE"), "Keystore file to fund user requests with")
keyPassFlag = flag.String("wallet.keypass", "password.txt", "Passphrase text file to decrypt keystore")
privKeyFlag = flag.String("wallet.privkey", os.Getenv("PRIVATE_KEY"), "Private key hex to fund user requests with")
providerFlag = flag.String("wallet.provider", os.Getenv("WEB3_PROVIDER"), "Endpoint for Ethereum JSON-RPC connection")
)

func init() {
Expand All @@ -42,7 +44,7 @@ func init() {
}

func Execute() {
privateKey, err := getPrivateKeyFromEnv()
privateKey, err := getPrivateKeyFromFlags()
if err != nil {
panic(fmt.Errorf("failed to read private key: %w", err))
}
Expand All @@ -51,7 +53,7 @@ func Execute() {
chainID = big.NewInt(int64(value))
}

txBuilder, err := chain.NewTxBuilder(os.Getenv("WEB3_PROVIDER"), privateKey, chainID)
txBuilder, err := chain.NewTxBuilder(*providerFlag, privateKey, chainID)
if err != nil {
panic(fmt.Errorf("cannot connect to web3 provider: %w", err))
}
Expand All @@ -63,23 +65,20 @@ func Execute() {
<-c
}

func getPrivateKeyFromEnv() (*ecdsa.PrivateKey, error) {
if value, ok := os.LookupEnv("PRIVATE_KEY"); ok {
return crypto.HexToECDSA(value)
}
keydir, ok := os.LookupEnv("KEYSTORE")
if !ok {
fmt.Println("Please set the environment variable for private key or keystore")
os.Exit(1)
func getPrivateKeyFromFlags() (*ecdsa.PrivateKey, error) {
if *privKeyFlag != "" {
return crypto.HexToECDSA(*privKeyFlag)
} else if *keyJSONFlag == "" {
return nil, errors.New("missing private key or keystore")
}

keyfile, err := chain.ResolveKeyfilePath(keydir)
keyfile, err := chain.ResolveKeyfilePath(*keyJSONFlag)
if err != nil {
panic(err)
return nil, err
}
password, err := os.ReadFile(DefaultKeyAuth)
password, err := os.ReadFile(*keyPassFlag)
if err != nil {
panic(fmt.Errorf("failed to read password from %v", DefaultKeyAuth))
return nil, err
}

return chain.DecryptKeyfile(keyfile, strings.TrimRight(string(password), "\r\n"))
Expand Down

0 comments on commit 8f628b6

Please sign in to comment.