github.com/janotchain/janota@v0.0.0-20220824112012-93ea4c5dee78/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/contracts/ens" 29 "github.com/ethereum/go-ethereum/crypto" 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 Path string 50 ListenAddr string 51 Port string 52 PublicKey string 53 BzzKey string 54 EnsRoot common.Address 55 NetworkId uint64 56 } 57 58 // config is agnostic to where private key is coming from 59 // so managing accounts is outside swarm and left to wrappers 60 func NewConfig(path string, contract common.Address, prvKey *ecdsa.PrivateKey, networkId uint64) (self *Config, err error) { 61 address := crypto.PubkeyToAddress(prvKey.PublicKey) // default beneficiary address 62 dirpath := filepath.Join(path, "bzz-"+common.Bytes2Hex(address.Bytes())) 63 err = os.MkdirAll(dirpath, os.ModePerm) 64 if err != nil { 65 return 66 } 67 confpath := filepath.Join(dirpath, "config.json") 68 var data []byte 69 pubkey := crypto.FromECDSAPub(&prvKey.PublicKey) 70 pubkeyhex := common.ToHex(pubkey) 71 keyhex := crypto.Keccak256Hash(pubkey).Hex() 72 73 self = &Config{ 74 SyncParams: network.NewSyncParams(dirpath), 75 HiveParams: network.NewHiveParams(dirpath), 76 ChunkerParams: storage.NewChunkerParams(), 77 StoreParams: storage.NewStoreParams(dirpath), 78 ListenAddr: DefaultHTTPListenAddr, 79 Port: DefaultHTTPPort, 80 Path: dirpath, 81 Swap: swap.DefaultSwapParams(contract, prvKey), 82 PublicKey: pubkeyhex, 83 BzzKey: keyhex, 84 EnsRoot: ens.TestNetAddress, 85 NetworkId: networkId, 86 } 87 data, err = ioutil.ReadFile(confpath) 88 89 // if not set in function param, then set default for swarm network, will be overwritten by config file if present 90 if networkId == 0 { 91 self.NetworkId = network.NetworkId 92 } 93 94 if err != nil { 95 if !os.IsNotExist(err) { 96 return 97 } 98 99 // file does not exist 100 // write out config file 101 err = self.Save() 102 if err != nil { 103 err = fmt.Errorf("error writing config: %v", err) 104 } 105 return 106 } 107 108 // file exists, deserialise 109 err = json.Unmarshal(data, self) 110 if err != nil { 111 return nil, fmt.Errorf("unable to parse config: %v", err) 112 } 113 // check public key 114 if pubkeyhex != self.PublicKey { 115 return nil, fmt.Errorf("public key does not match the one in the config file %v != %v", pubkeyhex, self.PublicKey) 116 } 117 if keyhex != self.BzzKey { 118 return nil, fmt.Errorf("bzz key does not match the one in the config file %v != %v", keyhex, self.BzzKey) 119 } 120 121 // if set in function param, replace id set from config file 122 if networkId != 0 { 123 self.NetworkId = networkId 124 } 125 126 self.Swap.SetKey(prvKey) 127 128 if (self.EnsRoot == common.Address{}) { 129 self.EnsRoot = ens.TestNetAddress 130 } 131 132 return 133 } 134 135 func (self *Config) Save() error { 136 data, err := json.MarshalIndent(self, "", " ") 137 if err != nil { 138 return err 139 } 140 err = os.MkdirAll(self.Path, os.ModePerm) 141 if err != nil { 142 return err 143 } 144 confpath := filepath.Join(self.Path, "config.json") 145 return ioutil.WriteFile(confpath, data, os.ModePerm) 146 }