config.go 4.1 KB

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