github.com/letterj/go-ethereum@v1.8.22-0.20190204142846-520024dfd689/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/ethereum/go-ethereum/common"
    27  	"github.com/ethereum/go-ethereum/contracts/ens"
    28  	"github.com/ethereum/go-ethereum/crypto"
    29  	"github.com/ethereum/go-ethereum/node"
    30  	"github.com/ethereum/go-ethereum/p2p/enode"
    31  	"github.com/ethereum/go-ethereum/swarm/log"
    32  	"github.com/ethereum/go-ethereum/swarm/network"
    33  	"github.com/ethereum/go-ethereum/swarm/pss"
    34  	"github.com/ethereum/go-ethereum/swarm/services/swap"
    35  	"github.com/ethereum/go-ethereum/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  	BootnodeMode         bool
    70  	SyncUpdateDelay      time.Duration
    71  	SwapAPI              string
    72  	Cors                 string
    73  	BzzAccount           string
    74  	privateKey           *ecdsa.PrivateKey
    75  }
    76  
    77  //create a default config with all parameters to set to defaults
    78  func NewConfig() (c *Config) {
    79  
    80  	c = &Config{
    81  		LocalStoreParams: storage.NewDefaultLocalStoreParams(),
    82  		FileStoreParams:  storage.NewFileStoreParams(),
    83  		HiveParams:       network.NewHiveParams(),
    84  		//SyncParams:    network.NewDefaultSyncParams(),
    85  		Swap:                 swap.NewDefaultSwapParams(),
    86  		Pss:                  pss.NewPssParams(),
    87  		ListenAddr:           DefaultHTTPListenAddr,
    88  		Port:                 DefaultHTTPPort,
    89  		Path:                 node.DefaultDataDir(),
    90  		EnsAPIs:              nil,
    91  		EnsRoot:              ens.TestNetAddress,
    92  		NetworkID:            network.DefaultNetworkID,
    93  		SwapEnabled:          false,
    94  		SyncEnabled:          true,
    95  		SyncingSkipCheck:     false,
    96  		MaxStreamPeerServers: 10000,
    97  		DeliverySkipCheck:    true,
    98  		SyncUpdateDelay:      15 * time.Second,
    99  		SwapAPI:              "",
   100  	}
   101  
   102  	return
   103  }
   104  
   105  //some config params need to be initialized after the complete
   106  //config building phase is completed (e.g. due to overriding flags)
   107  func (c *Config) Init(prvKey *ecdsa.PrivateKey) {
   108  
   109  	address := crypto.PubkeyToAddress(prvKey.PublicKey)
   110  	c.Path = filepath.Join(c.Path, "bzz-"+common.Bytes2Hex(address.Bytes()))
   111  	err := os.MkdirAll(c.Path, os.ModePerm)
   112  	if err != nil {
   113  		log.Error(fmt.Sprintf("Error creating root swarm data directory: %v", err))
   114  		return
   115  	}
   116  
   117  	pubkey := crypto.FromECDSAPub(&prvKey.PublicKey)
   118  	pubkeyhex := common.ToHex(pubkey)
   119  	keyhex := crypto.Keccak256Hash(pubkey).Hex()
   120  
   121  	c.PublicKey = pubkeyhex
   122  	c.BzzKey = keyhex
   123  	c.NodeID = enode.PubkeyToIDV4(&prvKey.PublicKey).String()
   124  
   125  	if c.SwapEnabled {
   126  		c.Swap.Init(c.Contract, prvKey)
   127  	}
   128  
   129  	c.privateKey = prvKey
   130  	c.LocalStoreParams.Init(c.Path)
   131  	c.LocalStoreParams.BaseKey = common.FromHex(keyhex)
   132  
   133  	c.Pss = c.Pss.WithPrivateKey(c.privateKey)
   134  }
   135  
   136  func (c *Config) ShiftPrivateKey() (privKey *ecdsa.PrivateKey) {
   137  	if c.privateKey != nil {
   138  		privKey = c.privateKey
   139  		c.privateKey = nil
   140  	}
   141  	return privKey
   142  }