github.com/daeglee/go-ethereum@v0.0.0-20190504220456-cad3e8d18e9b/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/common/hexutil" 28 "github.com/ethereum/go-ethereum/contracts/ens" 29 "github.com/ethereum/go-ethereum/crypto" 30 "github.com/ethereum/go-ethereum/node" 31 "github.com/ethereum/go-ethereum/p2p/enode" 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 Contract common.Address 53 EnsRoot common.Address 54 EnsAPIs []string 55 Path string 56 ListenAddr string 57 Port string 58 PublicKey string 59 BzzKey string 60 Enode *enode.Node `toml:"-"` 61 NetworkID uint64 62 SwapEnabled bool 63 SyncEnabled bool 64 SyncingSkipCheck bool 65 DeliverySkipCheck bool 66 MaxStreamPeerServers int 67 LightNodeEnabled bool 68 BootnodeMode bool 69 SyncUpdateDelay time.Duration 70 SwapAPI string 71 Cors string 72 BzzAccount string 73 GlobalStoreAPI string 74 privateKey *ecdsa.PrivateKey 75 } 76 77 //create a default config with all parameters to set to defaults 78 func NewConfig() (c *Config) { 79 80 c = &Config{ 81 LocalStoreParams: storage.NewDefaultLocalStoreParams(), 82 FileStoreParams: storage.NewFileStoreParams(), 83 HiveParams: network.NewHiveParams(), 84 Swap: swap.NewDefaultSwapParams(), 85 Pss: pss.NewPssParams(), 86 ListenAddr: DefaultHTTPListenAddr, 87 Port: DefaultHTTPPort, 88 Path: node.DefaultDataDir(), 89 EnsAPIs: nil, 90 EnsRoot: ens.TestNetAddress, 91 NetworkID: network.DefaultNetworkID, 92 SwapEnabled: false, 93 SyncEnabled: true, 94 SyncingSkipCheck: false, 95 MaxStreamPeerServers: 10000, 96 DeliverySkipCheck: true, 97 SyncUpdateDelay: 15 * time.Second, 98 SwapAPI: "", 99 } 100 101 return 102 } 103 104 //some config params need to be initialized after the complete 105 //config building phase is completed (e.g. due to overriding flags) 106 func (c *Config) Init(prvKey *ecdsa.PrivateKey, nodeKey *ecdsa.PrivateKey) error { 107 108 // create swarm dir and record key 109 err := c.createAndSetPath(c.Path, prvKey) 110 if err != nil { 111 return fmt.Errorf("Error creating root swarm data directory: %v", err) 112 } 113 c.setKey(prvKey) 114 115 // create the new enode record 116 // signed with the ephemeral node key 117 enodeParams := &network.EnodeParams{ 118 PrivateKey: prvKey, 119 EnodeKey: nodeKey, 120 Lightnode: c.LightNodeEnabled, 121 Bootnode: c.BootnodeMode, 122 } 123 c.Enode, err = network.NewEnode(enodeParams) 124 if err != nil { 125 return fmt.Errorf("Error creating enode: %v", err) 126 } 127 128 // initialize components that depend on the swarm instance's private key 129 if c.SwapEnabled { 130 c.Swap.Init(c.Contract, prvKey) 131 } 132 133 c.LocalStoreParams.Init(c.Path) 134 c.LocalStoreParams.BaseKey = common.FromHex(c.BzzKey) 135 136 c.Pss = c.Pss.WithPrivateKey(c.privateKey) 137 return nil 138 } 139 140 func (c *Config) ShiftPrivateKey() (privKey *ecdsa.PrivateKey) { 141 if c.privateKey != nil { 142 privKey = c.privateKey 143 c.privateKey = nil 144 } 145 return privKey 146 } 147 148 func (c *Config) setKey(prvKey *ecdsa.PrivateKey) { 149 bzzkeybytes := network.PrivateKeyToBzzKey(prvKey) 150 pubkey := crypto.FromECDSAPub(&prvKey.PublicKey) 151 pubkeyhex := hexutil.Encode(pubkey) 152 keyhex := hexutil.Encode(bzzkeybytes) 153 154 c.privateKey = prvKey 155 c.PublicKey = pubkeyhex 156 c.BzzKey = keyhex 157 } 158 159 func (c *Config) createAndSetPath(datadirPath string, prvKey *ecdsa.PrivateKey) error { 160 address := crypto.PubkeyToAddress(prvKey.PublicKey) 161 bzzdirPath := filepath.Join(datadirPath, "bzz-"+common.Bytes2Hex(address.Bytes())) 162 err := os.MkdirAll(bzzdirPath, os.ModePerm) 163 if err != nil { 164 return err 165 } 166 c.Path = bzzdirPath 167 return nil 168 }