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