github.com/maynardminer/ethereumprogpow@v1.8.23/swarm/api/config.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum 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 go-ethereum 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 go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package api
    18  
    19  import (
    20  	"crypto/ecdsa"
    21  	"fmt"
    22  	"os"
    23  	"path/filepath"
    24  	"time"
    25  
    26  	"github.com/ethereumprogpow/ethereumprogpow/common"
    27  	"github.com/ethereumprogpow/ethereumprogpow/contracts/ens"
    28  	"github.com/ethereumprogpow/ethereumprogpow/crypto"
    29  	"github.com/ethereumprogpow/ethereumprogpow/node"
    30  	"github.com/ethereumprogpow/ethereumprogpow/p2p/enode"
    31  	"github.com/ethereumprogpow/ethereumprogpow/swarm/log"
    32  	"github.com/ethereumprogpow/ethereumprogpow/swarm/network"
    33  	"github.com/ethereumprogpow/ethereumprogpow/swarm/pss"
    34  	"github.com/ethereumprogpow/ethereumprogpow/swarm/services/swap"
    35  	"github.com/ethereumprogpow/ethereumprogpow/swarm/storage"
    36  )
    37  
    38  const (
    39  	DefaultHTTPListenAddr = "127.0.0.1"
    40  	DefaultHTTPPort       = "8500"
    41  )
    42  
    43  // separate bzz directories
    44  // allow several bzz nodes running in parallel
    45  type Config struct {
    46  	// serialised/persisted fields
    47  	*storage.FileStoreParams
    48  	*storage.LocalStoreParams
    49  	*network.HiveParams
    50  	Swap *swap.LocalProfile
    51  	Pss  *pss.PssParams
    52  	//*network.SyncParams
    53  	Contract             common.Address
    54  	EnsRoot              common.Address
    55  	EnsAPIs              []string
    56  	Path                 string
    57  	ListenAddr           string
    58  	Port                 string
    59  	PublicKey            string
    60  	BzzKey               string
    61  	NodeID               string
    62  	NetworkID            uint64
    63  	SwapEnabled          bool
    64  	SyncEnabled          bool
    65  	SyncingSkipCheck     bool
    66  	DeliverySkipCheck    bool
    67  	MaxStreamPeerServers int
    68  	LightNodeEnabled     bool
    69  	SyncUpdateDelay      time.Duration
    70  	SwapAPI              string
    71  	Cors                 string
    72  	BzzAccount           string
    73  	privateKey           *ecdsa.PrivateKey
    74  }
    75  
    76  //create a default config with all parameters to set to defaults
    77  func NewConfig() (c *Config) {
    78  
    79  	c = &Config{
    80  		LocalStoreParams: storage.NewDefaultLocalStoreParams(),
    81  		FileStoreParams:  storage.NewFileStoreParams(),
    82  		HiveParams:       network.NewHiveParams(),
    83  		//SyncParams:    network.NewDefaultSyncParams(),
    84  		Swap:                 swap.NewDefaultSwapParams(),
    85  		Pss:                  pss.NewPssParams(),
    86  		ListenAddr:           DefaultHTTPListenAddr,
    87  		Port:                 DefaultHTTPPort,
    88  		Path:                 node.DefaultDataDir(),
    89  		EnsAPIs:              nil,
    90  		EnsRoot:              ens.TestNetAddress,
    91  		NetworkID:            network.DefaultNetworkID,
    92  		SwapEnabled:          false,
    93  		SyncEnabled:          true,
    94  		SyncingSkipCheck:     false,
    95  		MaxStreamPeerServers: 10000,
    96  		DeliverySkipCheck:    true,
    97  		SyncUpdateDelay:      15 * time.Second,
    98  		SwapAPI:              "",
    99  	}
   100  
   101  	return
   102  }
   103  
   104  //some config params need to be initialized after the complete
   105  //config building phase is completed (e.g. due to overriding flags)
   106  func (c *Config) Init(prvKey *ecdsa.PrivateKey) {
   107  
   108  	address := crypto.PubkeyToAddress(prvKey.PublicKey)
   109  	c.Path = filepath.Join(c.Path, "bzz-"+common.Bytes2Hex(address.Bytes()))
   110  	err := os.MkdirAll(c.Path, os.ModePerm)
   111  	if err != nil {
   112  		log.Error(fmt.Sprintf("Error creating root swarm data directory: %v", err))
   113  		return
   114  	}
   115  
   116  	pubkey := crypto.FromECDSAPub(&prvKey.PublicKey)
   117  	pubkeyhex := common.ToHex(pubkey)
   118  	keyhex := crypto.Keccak256Hash(pubkey).Hex()
   119  
   120  	c.PublicKey = pubkeyhex
   121  	c.BzzKey = keyhex
   122  	c.NodeID = enode.PubkeyToIDV4(&prvKey.PublicKey).String()
   123  
   124  	if c.SwapEnabled {
   125  		c.Swap.Init(c.Contract, prvKey)
   126  	}
   127  
   128  	c.privateKey = prvKey
   129  	c.LocalStoreParams.Init(c.Path)
   130  	c.LocalStoreParams.BaseKey = common.FromHex(keyhex)
   131  
   132  	c.Pss = c.Pss.WithPrivateKey(c.privateKey)
   133  }
   134  
   135  func (c *Config) ShiftPrivateKey() (privKey *ecdsa.PrivateKey) {
   136  	if c.privateKey != nil {
   137  		privKey = c.privateKey
   138  		c.privateKey = nil
   139  	}
   140  	return privKey
   141  }