github.com/divan/go-ethereum@v1.8.14-0.20180820134928-1de9ada4016d/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 LightNodeEnabled bool 67 SyncUpdateDelay time.Duration 68 SwapAPI string 69 Cors string 70 BzzAccount 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 } 96 97 return 98 } 99 100 //some config params need to be initialized after the complete 101 //config building phase is completed (e.g. due to overriding flags) 102 func (c *Config) Init(prvKey *ecdsa.PrivateKey) { 103 104 address := crypto.PubkeyToAddress(prvKey.PublicKey) 105 c.Path = filepath.Join(c.Path, "bzz-"+common.Bytes2Hex(address.Bytes())) 106 err := os.MkdirAll(c.Path, os.ModePerm) 107 if err != nil { 108 log.Error(fmt.Sprintf("Error creating root swarm data directory: %v", err)) 109 return 110 } 111 112 pubkey := crypto.FromECDSAPub(&prvKey.PublicKey) 113 pubkeyhex := common.ToHex(pubkey) 114 keyhex := crypto.Keccak256Hash(pubkey).Hex() 115 116 c.PublicKey = pubkeyhex 117 c.BzzKey = keyhex 118 c.NodeID = discover.PubkeyID(&prvKey.PublicKey).String() 119 120 if c.SwapEnabled { 121 c.Swap.Init(c.Contract, prvKey) 122 } 123 124 c.privateKey = prvKey 125 c.LocalStoreParams.Init(c.Path) 126 c.LocalStoreParams.BaseKey = common.FromHex(keyhex) 127 128 c.Pss = c.Pss.WithPrivateKey(c.privateKey) 129 } 130 131 func (c *Config) ShiftPrivateKey() (privKey *ecdsa.PrivateKey) { 132 if c.privateKey != nil { 133 privKey = c.privateKey 134 c.privateKey = nil 135 } 136 return privKey 137 }