github.com/amazechain/amc@v0.1.3/conf/node_config.go (about)

     1  // Copyright 2022 The AmazeChain Authors
     2  // This file is part of the AmazeChain library.
     3  //
     4  // The AmazeChain library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser 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  // The AmazeChain library 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 Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the AmazeChain library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package conf
    18  
    19  import (
    20  	"os"
    21  	"path/filepath"
    22  )
    23  
    24  const (
    25  	datadirDefaultKeyStore = "keystore" // Path within the datadir to the keystore
    26  )
    27  
    28  type NodeConfig struct {
    29  	NodePrivate string `json:"private" yaml:"private"`
    30  	HTTP        bool   `json:"http" yaml:"http" `
    31  	HTTPHost    string `json:"http_host" yaml:"http_host" `
    32  	HTTPPort    string `json:"http_port" yaml:"http_port"`
    33  	HTTPApi     string `json:"http_api" yaml:"http_api"`
    34  	// HTTPCors is the Cross-Origin Resource Sharing header to send to requesting
    35  	// clients. Please be aware that CORS is a browser enforced security, it's fully
    36  	// useless for custom HTTP clients.
    37  	HTTPCors string `json:"http_cors" yaml:"http_cors"`
    38  
    39  	WS     bool   `json:"ws" yaml:"ws" `
    40  	WSHost string `json:"ws_host" yaml:"ws_host" `
    41  	WSPort string `json:"ws_port" yaml:"ws_port"`
    42  	WSApi  string `json:"ws_api" yaml:"ws_api"`
    43  	// WSOrigins is the list of domain to accept websocket requests from. Please be
    44  	// aware that the server can only act upon the HTTP request the client sends and
    45  	// cannot verify the validity of the request header.
    46  	WSOrigins        string `toml:",omitempty"`
    47  	IPCPath          string `json:"ipc_path" yaml:"ipc_path"`
    48  	DataDir          string `json:"data_dir" yaml:"data_dir"`
    49  	MinFreeDiskSpace int    `json:"min_free_disk_space" yaml:"min_free_disk_space"`
    50  	Chain            string `json:"chain" yaml:"chain"`
    51  	Miner            bool   `json:"miner" yaml:"miner"`
    52  
    53  	AuthRPC bool `json:"auth_rpc" yaml:"auth_rpc"`
    54  	// AuthAddr is the listening address on which authenticated APIs are provided.
    55  	AuthAddr string `json:"auth_addr" yaml:"auth_addr"`
    56  
    57  	// AuthPort is the port number on which authenticated APIs are provided.
    58  	AuthPort int `json:"auth_port" yaml:"auth_port"`
    59  
    60  	// AuthVirtualHosts is the list of virtual hostnames which are allowed on incoming requests
    61  	// for the authenticated api. This is by default {'localhost'}.
    62  	AuthVirtualHosts []string `json:"auth_virtual_hosts" yaml:"auth_virtual_hosts"`
    63  
    64  	// JWTSecret is the path to the hex-encoded jwt secret.
    65  	JWTSecret string `json:"jwt_secret" yaml:"jwt_secret"`
    66  
    67  	// KeyStoreDir is the file system folder that contains private keys. The directory can
    68  	// be specified as a relative path, in which case it is resolved relative to the
    69  	// current directory.
    70  	//
    71  	// If KeyStoreDir is empty, the default location is the "keystore" subdirectory of
    72  	// DataDir. If DataDir is unspecified and KeyStoreDir is empty, an ephemeral directory
    73  	// is created by New and destroyed when the node is stopped.
    74  	KeyStoreDir string `json:"key_store_dir" yaml:"key_store_dir"`
    75  
    76  	// ExternalSigner specifies an external URI for a clef-type signer
    77  	ExternalSigner string `json:"external_signer" yaml:"external_signer"`
    78  
    79  	// UseLightweightKDF lowers the memory and CPU requirements of the key store
    80  	// scrypt KDF at the expense of security.
    81  	UseLightweightKDF bool `json:"use_lightweight_kdf" yaml:"use_lightweight_kdf"`
    82  
    83  	// InsecureUnlockAllowed allows user to unlock accounts in unsafe http environment.
    84  	InsecureUnlockAllowed bool `json:"insecure_unlock_allowed" yaml:"insecure_unlock_allowed"`
    85  
    86  	PasswordFile string `json:"password_file" yaml:"password_file"`
    87  }
    88  
    89  // KeyDirConfig determines the settings for keydirectory
    90  func (c *NodeConfig) KeyDirConfig() (string, error) {
    91  	var (
    92  		keydir string
    93  		err    error
    94  	)
    95  	switch {
    96  	case filepath.IsAbs(c.KeyStoreDir):
    97  		keydir = c.KeyStoreDir
    98  	case c.DataDir != "":
    99  		if c.KeyStoreDir == "" {
   100  			keydir = filepath.Join(c.DataDir, datadirDefaultKeyStore)
   101  		} else {
   102  			keydir, err = filepath.Abs(c.KeyStoreDir)
   103  		}
   104  	case c.KeyStoreDir != "":
   105  		keydir, err = filepath.Abs(c.KeyStoreDir)
   106  	}
   107  	return keydir, err
   108  }
   109  
   110  // getKeyStoreDir retrieves the key directory and will create
   111  // and ephemeral one if necessary.
   112  func getKeyStoreDir(conf *NodeConfig) (string, bool, error) {
   113  	keydir, err := conf.KeyDirConfig()
   114  	if err != nil {
   115  		return "", false, err
   116  	}
   117  	isEphemeral := false
   118  	if keydir == "" {
   119  		// There is no datadir.
   120  		keydir, err = os.MkdirTemp("", "amazechain-keystore")
   121  		isEphemeral = true
   122  	}
   123  
   124  	if err != nil {
   125  		return "", false, err
   126  	}
   127  	if err := os.MkdirAll(keydir, 0700); err != nil {
   128  		return "", false, err
   129  	}
   130  
   131  	return keydir, isEphemeral, nil
   132  }
   133  
   134  // ExtRPCEnabled returns the indicator whether node enables the external
   135  // RPC(http, ws or graphql).
   136  func (c *NodeConfig) ExtRPCEnabled() bool {
   137  	return c.HTTPHost != "" || c.WSHost != ""
   138  }