github.com/alanchchen/go-ethereum@v1.6.6-0.20170601190819-6171d01b1195/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 "encoding/json" 22 "fmt" 23 "io/ioutil" 24 "os" 25 "path/filepath" 26 27 "github.com/ethereum/go-ethereum/common" 28 "github.com/ethereum/go-ethereum/crypto" 29 "github.com/ethereum/go-ethereum/swarm/network" 30 "github.com/ethereum/go-ethereum/swarm/services/swap" 31 "github.com/ethereum/go-ethereum/swarm/storage" 32 ) 33 34 const ( 35 DefaultHTTPListenAddr = "127.0.0.1" 36 DefaultHTTPPort = "8500" 37 ) 38 39 var ( 40 ensRootAddress = common.HexToAddress("0x112234455c3a32fd11230c42e7bccd4a84e02010") 41 ) 42 43 // separate bzz directories 44 // allow several bzz nodes running in parallel 45 type Config struct { 46 // serialised/persisted fields 47 *storage.StoreParams 48 *storage.ChunkerParams 49 *network.HiveParams 50 Swap *swap.SwapParams 51 *network.SyncParams 52 Path string 53 ListenAddr string 54 Port string 55 PublicKey string 56 BzzKey string 57 EnsRoot common.Address 58 NetworkId uint64 59 } 60 61 // config is agnostic to where private key is coming from 62 // so managing accounts is outside swarm and left to wrappers 63 func NewConfig(path string, contract common.Address, prvKey *ecdsa.PrivateKey, networkId uint64) (self *Config, err error) { 64 address := crypto.PubkeyToAddress(prvKey.PublicKey) // default beneficiary address 65 dirpath := filepath.Join(path, "bzz-"+common.Bytes2Hex(address.Bytes())) 66 err = os.MkdirAll(dirpath, os.ModePerm) 67 if err != nil { 68 return 69 } 70 confpath := filepath.Join(dirpath, "config.json") 71 var data []byte 72 pubkey := crypto.FromECDSAPub(&prvKey.PublicKey) 73 pubkeyhex := common.ToHex(pubkey) 74 keyhex := crypto.Keccak256Hash(pubkey).Hex() 75 76 self = &Config{ 77 SyncParams: network.NewSyncParams(dirpath), 78 HiveParams: network.NewHiveParams(dirpath), 79 ChunkerParams: storage.NewChunkerParams(), 80 StoreParams: storage.NewStoreParams(dirpath), 81 ListenAddr: DefaultHTTPListenAddr, 82 Port: DefaultHTTPPort, 83 Path: dirpath, 84 Swap: swap.DefaultSwapParams(contract, prvKey), 85 PublicKey: pubkeyhex, 86 BzzKey: keyhex, 87 EnsRoot: ensRootAddress, 88 NetworkId: networkId, 89 } 90 data, err = ioutil.ReadFile(confpath) 91 92 // if not set in function param, then set default for swarm network, will be overwritten by config file if present 93 if networkId == 0 { 94 self.NetworkId = network.NetworkId 95 } 96 97 if err != nil { 98 if !os.IsNotExist(err) { 99 return 100 } 101 102 // file does not exist 103 // write out config file 104 err = self.Save() 105 if err != nil { 106 err = fmt.Errorf("error writing config: %v", err) 107 } 108 return 109 } 110 111 // file exists, deserialise 112 err = json.Unmarshal(data, self) 113 if err != nil { 114 return nil, fmt.Errorf("unable to parse config: %v", err) 115 } 116 // check public key 117 if pubkeyhex != self.PublicKey { 118 return nil, fmt.Errorf("public key does not match the one in the config file %v != %v", pubkeyhex, self.PublicKey) 119 } 120 if keyhex != self.BzzKey { 121 return nil, fmt.Errorf("bzz key does not match the one in the config file %v != %v", keyhex, self.BzzKey) 122 } 123 124 // if set in function param, replace id set from config file 125 if networkId != 0 { 126 self.NetworkId = networkId 127 } 128 129 self.Swap.SetKey(prvKey) 130 131 if (self.EnsRoot == common.Address{}) { 132 self.EnsRoot = ensRootAddress 133 } 134 135 return 136 } 137 138 func (self *Config) Save() error { 139 data, err := json.MarshalIndent(self, "", " ") 140 if err != nil { 141 return err 142 } 143 err = os.MkdirAll(self.Path, os.ModePerm) 144 if err != nil { 145 return err 146 } 147 confpath := filepath.Join(self.Path, "config.json") 148 return ioutil.WriteFile(confpath, data, os.ModePerm) 149 }