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