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