github.com/cuiweixie/go-ethereum@v1.8.2-0.20180303084001-66cd41af1e38/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 25 "github.com/ethereum/go-ethereum/common" 26 "github.com/ethereum/go-ethereum/contracts/ens" 27 "github.com/ethereum/go-ethereum/crypto" 28 "github.com/ethereum/go-ethereum/log" 29 "github.com/ethereum/go-ethereum/node" 30 "github.com/ethereum/go-ethereum/swarm/network" 31 "github.com/ethereum/go-ethereum/swarm/services/swap" 32 "github.com/ethereum/go-ethereum/swarm/storage" 33 ) 34 35 const ( 36 DefaultHTTPListenAddr = "127.0.0.1" 37 DefaultHTTPPort = "8500" 38 ) 39 40 // separate bzz directories 41 // allow several bzz nodes running in parallel 42 type Config struct { 43 // serialised/persisted fields 44 *storage.StoreParams 45 *storage.ChunkerParams 46 *network.HiveParams 47 Swap *swap.SwapParams 48 *network.SyncParams 49 Contract common.Address 50 EnsRoot common.Address 51 EnsAPIs []string 52 Path string 53 ListenAddr string 54 Port string 55 PublicKey string 56 BzzKey string 57 NetworkId uint64 58 SwapEnabled bool 59 SyncEnabled bool 60 SwapApi string 61 Cors string 62 BzzAccount string 63 BootNodes string 64 } 65 66 //create a default config with all parameters to set to defaults 67 func NewDefaultConfig() (self *Config) { 68 69 self = &Config{ 70 StoreParams: storage.NewDefaultStoreParams(), 71 ChunkerParams: storage.NewChunkerParams(), 72 HiveParams: network.NewDefaultHiveParams(), 73 SyncParams: network.NewDefaultSyncParams(), 74 Swap: swap.NewDefaultSwapParams(), 75 ListenAddr: DefaultHTTPListenAddr, 76 Port: DefaultHTTPPort, 77 Path: node.DefaultDataDir(), 78 EnsAPIs: nil, 79 EnsRoot: ens.TestNetAddress, 80 NetworkId: network.NetworkId, 81 SwapEnabled: false, 82 SyncEnabled: true, 83 SwapApi: "", 84 BootNodes: "", 85 } 86 87 return 88 } 89 90 //some config params need to be initialized after the complete 91 //config building phase is completed (e.g. due to overriding flags) 92 func (self *Config) Init(prvKey *ecdsa.PrivateKey) { 93 94 address := crypto.PubkeyToAddress(prvKey.PublicKey) 95 self.Path = filepath.Join(self.Path, "bzz-"+common.Bytes2Hex(address.Bytes())) 96 err := os.MkdirAll(self.Path, os.ModePerm) 97 if err != nil { 98 log.Error(fmt.Sprintf("Error creating root swarm data directory: %v", err)) 99 return 100 } 101 102 pubkey := crypto.FromECDSAPub(&prvKey.PublicKey) 103 pubkeyhex := common.ToHex(pubkey) 104 keyhex := crypto.Keccak256Hash(pubkey).Hex() 105 106 self.PublicKey = pubkeyhex 107 self.BzzKey = keyhex 108 109 self.Swap.Init(self.Contract, prvKey) 110 self.SyncParams.Init(self.Path) 111 self.HiveParams.Init(self.Path) 112 self.StoreParams.Init(self.Path) 113 }