github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/swarm/api/config.go (about)

     1  // This file is part of the go-sberex library. The go-sberex library is 
     2  // free software: you can redistribute it and/or modify it under the terms 
     3  // of the GNU Lesser General Public License as published by the Free 
     4  // Software Foundation, either version 3 of the License, or (at your option)
     5  // any later version.
     6  //
     7  // The go-sberex library is distributed in the hope that it will be useful, 
     8  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
    10  // General Public License <http://www.gnu.org/licenses/> for more details.
    11  
    12  package api
    13  
    14  import (
    15  	"crypto/ecdsa"
    16  	"fmt"
    17  	"os"
    18  	"path/filepath"
    19  
    20  	"github.com/Sberex/go-sberex/common"
    21  	"github.com/Sberex/go-sberex/contracts/ens"
    22  	"github.com/Sberex/go-sberex/crypto"
    23  	"github.com/Sberex/go-sberex/log"
    24  	"github.com/Sberex/go-sberex/node"
    25  	"github.com/Sberex/go-sberex/swarm/network"
    26  	"github.com/Sberex/go-sberex/swarm/services/swap"
    27  	"github.com/Sberex/go-sberex/swarm/storage"
    28  )
    29  
    30  const (
    31  	DefaultHTTPListenAddr = "127.0.0.1"
    32  	DefaultHTTPPort       = "8500"
    33  )
    34  
    35  // separate bzz directories
    36  // allow several bzz nodes running in parallel
    37  type Config struct {
    38  	// serialised/persisted fields
    39  	*storage.StoreParams
    40  	*storage.ChunkerParams
    41  	*network.HiveParams
    42  	Swap *swap.SwapParams
    43  	*network.SyncParams
    44  	Contract    common.Address
    45  	EnsRoot     common.Address
    46  	EnsAPIs     []string
    47  	Path        string
    48  	ListenAddr  string
    49  	Port        string
    50  	PublicKey   string
    51  	BzzKey      string
    52  	NetworkId   uint64
    53  	SwapEnabled bool
    54  	SyncEnabled bool
    55  	SwapApi     string
    56  	Cors        string
    57  	BzzAccount  string
    58  	BootNodes   string
    59  }
    60  
    61  //create a default config with all parameters to set to defaults
    62  func NewDefaultConfig() (self *Config) {
    63  
    64  	self = &Config{
    65  		StoreParams:   storage.NewDefaultStoreParams(),
    66  		ChunkerParams: storage.NewChunkerParams(),
    67  		HiveParams:    network.NewDefaultHiveParams(),
    68  		SyncParams:    network.NewDefaultSyncParams(),
    69  		Swap:          swap.NewDefaultSwapParams(),
    70  		ListenAddr:    DefaultHTTPListenAddr,
    71  		Port:          DefaultHTTPPort,
    72  		Path:          node.DefaultDataDir(),
    73  		EnsAPIs:       nil,
    74  		EnsRoot:       ens.TestNetAddress,
    75  		NetworkId:     network.NetworkId,
    76  		SwapEnabled:   false,
    77  		SyncEnabled:   true,
    78  		SwapApi:       "",
    79  		BootNodes:     "",
    80  	}
    81  
    82  	return
    83  }
    84  
    85  //some config params need to be initialized after the complete
    86  //config building phase is completed (e.g. due to overriding flags)
    87  func (self *Config) Init(prvKey *ecdsa.PrivateKey) {
    88  
    89  	address := crypto.PubkeyToAddress(prvKey.PublicKey)
    90  	self.Path = filepath.Join(self.Path, "bzz-"+common.Bytes2Hex(address.Bytes()))
    91  	err := os.MkdirAll(self.Path, os.ModePerm)
    92  	if err != nil {
    93  		log.Error(fmt.Sprintf("Error creating root swarm data directory: %v", err))
    94  		return
    95  	}
    96  
    97  	pubkey := crypto.FromECDSAPub(&prvKey.PublicKey)
    98  	pubkeyhex := common.ToHex(pubkey)
    99  	keyhex := crypto.Keccak256Hash(pubkey).Hex()
   100  
   101  	self.PublicKey = pubkeyhex
   102  	self.BzzKey = keyhex
   103  
   104  	self.Swap.Init(self.Contract, prvKey)
   105  	self.SyncParams.Init(self.Path)
   106  	self.HiveParams.Init(self.Path)
   107  	self.StoreParams.Init(self.Path)
   108  }