github.com/CommerciumBlockchain/go-commercium@v0.0.0-20220709212705-b46438a77516/cmd/puppeth/wizard_faucet.go (about)

     1  // Copyright 2017 The go-ethereum Authors
     2  // This file is part of go-ethereum.
     3  //
     4  // go-ethereum is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // go-ethereum is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU General Public License
    15  // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package main
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  
    23  	"github.com/CommerciumBlockchain/go-commercium/accounts/keystore"
    24  	"github.com/CommerciumBlockchain/go-commercium/log"
    25  )
    26  
    27  // deployFaucet queries the user for various input on deploying a faucet, after
    28  // which it executes it.
    29  func (w *wizard) deployFaucet() {
    30  	// Select the server to interact with
    31  	server := w.selectServer()
    32  	if server == "" {
    33  		return
    34  	}
    35  	client := w.servers[server]
    36  
    37  	// Retrieve any active faucet configurations from the server
    38  	infos, err := checkFaucet(client, w.network)
    39  	if err != nil {
    40  		infos = &faucetInfos{
    41  			node:    &nodeInfos{port: 30303, peersTotal: 25},
    42  			port:    80,
    43  			host:    client.server,
    44  			amount:  1,
    45  			minutes: 1440,
    46  			tiers:   3,
    47  		}
    48  	}
    49  	existed := err == nil
    50  
    51  	infos.node.genesis, _ = json.MarshalIndent(w.conf.Genesis, "", "  ")
    52  	infos.node.network = w.conf.Genesis.Config.ChainID.Int64()
    53  
    54  	// Figure out which port to listen on
    55  	fmt.Println()
    56  	fmt.Printf("Which port should the faucet listen on? (default = %d)\n", infos.port)
    57  	infos.port = w.readDefaultInt(infos.port)
    58  
    59  	// Figure which virtual-host to deploy ethstats on
    60  	if infos.host, err = w.ensureVirtualHost(client, infos.port, infos.host); err != nil {
    61  		log.Error("Failed to decide on faucet host", "err", err)
    62  		return
    63  	}
    64  	// Port and proxy settings retrieved, figure out the funding amount per period configurations
    65  	fmt.Println()
    66  	fmt.Printf("How many Ethers to release per request? (default = %d)\n", infos.amount)
    67  	infos.amount = w.readDefaultInt(infos.amount)
    68  
    69  	fmt.Println()
    70  	fmt.Printf("How many minutes to enforce between requests? (default = %d)\n", infos.minutes)
    71  	infos.minutes = w.readDefaultInt(infos.minutes)
    72  
    73  	fmt.Println()
    74  	fmt.Printf("How many funding tiers to feature (x2.5 amounts, x3 timeout)? (default = %d)\n", infos.tiers)
    75  	infos.tiers = w.readDefaultInt(infos.tiers)
    76  	if infos.tiers == 0 {
    77  		log.Error("At least one funding tier must be set")
    78  		return
    79  	}
    80  	// Accessing the reCaptcha service requires API authorizations, request it
    81  	if infos.captchaToken != "" {
    82  		fmt.Println()
    83  		fmt.Println("Reuse previous reCaptcha API authorization (y/n)? (default = yes)")
    84  		if !w.readDefaultYesNo(true) {
    85  			infos.captchaToken, infos.captchaSecret = "", ""
    86  		}
    87  	}
    88  	if infos.captchaToken == "" {
    89  		// No previous authorization (or old one discarded)
    90  		fmt.Println()
    91  		fmt.Println("Enable reCaptcha protection against robots (y/n)? (default = no)")
    92  		if !w.readDefaultYesNo(false) {
    93  			log.Warn("Users will be able to requests funds via automated scripts")
    94  		} else {
    95  			// Captcha protection explicitly requested, read the site and secret keys
    96  			fmt.Println()
    97  			fmt.Printf("What is the reCaptcha site key to authenticate human users?\n")
    98  			infos.captchaToken = w.readString()
    99  
   100  			fmt.Println()
   101  			fmt.Printf("What is the reCaptcha secret key to verify authentications? (won't be echoed)\n")
   102  			infos.captchaSecret = w.readPassword()
   103  		}
   104  	}
   105  
   106  	// Accessing the twitter api requires a bearer token, request it
   107  	if infos.twitterToken != "" {
   108  		fmt.Println()
   109  		fmt.Println("Reuse previous twitter API Bearer token (y/n)? (default = yes)")
   110  		if !w.readDefaultYesNo(true) {
   111  			infos.twitterToken = ""
   112  		}
   113  	}
   114  	if infos.twitterToken == "" {
   115  		// No previous twitter token (or old one discarded)
   116  		fmt.Println()
   117  		fmt.Println("Enable twitter API (y/n)? (default = no)")
   118  		if !w.readDefaultYesNo(false) {
   119  			log.Warn("The faucet will fallback to using direct calls")
   120  		} else {
   121  			// Twitter api explicitly requested, read the bearer token
   122  			fmt.Println()
   123  			fmt.Printf("What is the twitter API Bearer token?\n")
   124  			infos.twitterToken = w.readString()
   125  		}
   126  	}
   127  
   128  	// Figure out where the user wants to store the persistent data
   129  	fmt.Println()
   130  	if infos.node.datadir == "" {
   131  		fmt.Printf("Where should data be stored on the remote machine?\n")
   132  		infos.node.datadir = w.readString()
   133  	} else {
   134  		fmt.Printf("Where should data be stored on the remote machine? (default = %s)\n", infos.node.datadir)
   135  		infos.node.datadir = w.readDefaultString(infos.node.datadir)
   136  	}
   137  	// Figure out which port to listen on
   138  	fmt.Println()
   139  	fmt.Printf("Which TCP/UDP port should the light client listen on? (default = %d)\n", infos.node.port)
   140  	infos.node.port = w.readDefaultInt(infos.node.port)
   141  
   142  	// Set a proper name to report on the stats page
   143  	fmt.Println()
   144  	if infos.node.ethstats == "" {
   145  		fmt.Printf("What should the node be called on the stats page?\n")
   146  		infos.node.ethstats = w.readString() + ":" + w.conf.ethstats
   147  	} else {
   148  		fmt.Printf("What should the node be called on the stats page? (default = %s)\n", infos.node.ethstats)
   149  		infos.node.ethstats = w.readDefaultString(infos.node.ethstats) + ":" + w.conf.ethstats
   150  	}
   151  	// Load up the credential needed to release funds
   152  	if infos.node.keyJSON != "" {
   153  		if key, err := keystore.DecryptKey([]byte(infos.node.keyJSON), infos.node.keyPass); err != nil {
   154  			infos.node.keyJSON, infos.node.keyPass = "", ""
   155  		} else {
   156  			fmt.Println()
   157  			fmt.Printf("Reuse previous (%s) funding account (y/n)? (default = yes)\n", key.Address.Hex())
   158  			if !w.readDefaultYesNo(true) {
   159  				infos.node.keyJSON, infos.node.keyPass = "", ""
   160  			}
   161  		}
   162  	}
   163  	for i := 0; i < 3 && infos.node.keyJSON == ""; i++ {
   164  		fmt.Println()
   165  		fmt.Println("Please paste the faucet's funding account key JSON:")
   166  		infos.node.keyJSON = w.readJSON()
   167  
   168  		fmt.Println()
   169  		fmt.Println("What's the unlock password for the account? (won't be echoed)")
   170  		infos.node.keyPass = w.readPassword()
   171  
   172  		if _, err := keystore.DecryptKey([]byte(infos.node.keyJSON), infos.node.keyPass); err != nil {
   173  			log.Error("Failed to decrypt key with given password")
   174  			infos.node.keyJSON = ""
   175  			infos.node.keyPass = ""
   176  		}
   177  	}
   178  	// Check if the user wants to run the faucet in debug mode (noauth)
   179  	noauth := "n"
   180  	if infos.noauth {
   181  		noauth = "y"
   182  	}
   183  	fmt.Println()
   184  	fmt.Printf("Permit non-authenticated funding requests (y/n)? (default = %v)\n", infos.noauth)
   185  	infos.noauth = w.readDefaultString(noauth) != "n"
   186  
   187  	// Try to deploy the faucet server on the host
   188  	nocache := false
   189  	if existed {
   190  		fmt.Println()
   191  		fmt.Printf("Should the faucet be built from scratch (y/n)? (default = no)\n")
   192  		nocache = w.readDefaultYesNo(false)
   193  	}
   194  	if out, err := deployFaucet(client, w.network, w.conf.bootnodes, infos, nocache); err != nil {
   195  		log.Error("Failed to deploy faucet container", "err", err)
   196  		if len(out) > 0 {
   197  			fmt.Printf("%s\n", out)
   198  		}
   199  		return
   200  	}
   201  	// All ok, run a network scan to pick any changes up
   202  	w.networkStats()
   203  }