config.go 4.0 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/crypto"
  26. "github.com/ethereum/go-ethereum/swarm/network"
  27. "github.com/ethereum/go-ethereum/swarm/services/swap"
  28. "github.com/ethereum/go-ethereum/swarm/storage"
  29. )
  30. const (
  31. port = "8500"
  32. )
  33. var (
  34. ensRootAddress = common.HexToAddress("0x112234455c3a32fd11230c42e7bccd4a84e02010")
  35. )
  36. // separate bzz directories
  37. // allow several bzz nodes running in parallel
  38. type Config struct {
  39. // serialised/persisted fields
  40. *storage.StoreParams
  41. *storage.ChunkerParams
  42. *network.HiveParams
  43. Swap *swap.SwapParams
  44. *network.SyncParams
  45. Path 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. Port: port,
  72. Path: dirpath,
  73. Swap: swap.DefaultSwapParams(contract, prvKey),
  74. PublicKey: pubkeyhex,
  75. BzzKey: keyhex,
  76. EnsRoot: ensRootAddress,
  77. NetworkId: networkId,
  78. }
  79. data, err = ioutil.ReadFile(confpath)
  80. // if not set in function param, then set default for swarm network, will be overwritten by config file if present
  81. if networkId == 0 {
  82. self.NetworkId = network.NetworkId
  83. }
  84. if err != nil {
  85. if !os.IsNotExist(err) {
  86. return
  87. }
  88. // file does not exist
  89. // write out config file
  90. err = self.Save()
  91. if err != nil {
  92. err = fmt.Errorf("error writing config: %v", err)
  93. }
  94. return
  95. }
  96. // file exists, deserialise
  97. err = json.Unmarshal(data, self)
  98. if err != nil {
  99. return nil, fmt.Errorf("unable to parse config: %v", err)
  100. }
  101. // check public key
  102. if pubkeyhex != self.PublicKey {
  103. return nil, fmt.Errorf("public key does not match the one in the config file %v != %v", pubkeyhex, self.PublicKey)
  104. }
  105. if keyhex != self.BzzKey {
  106. return nil, fmt.Errorf("bzz key does not match the one in the config file %v != %v", keyhex, self.BzzKey)
  107. }
  108. // if set in function param, replace id set from config file
  109. if networkId != 0 {
  110. self.NetworkId = networkId
  111. }
  112. self.Swap.SetKey(prvKey)
  113. if (self.EnsRoot == common.Address{}) {
  114. self.EnsRoot = ensRootAddress
  115. }
  116. return
  117. }
  118. func (self *Config) Save() error {
  119. data, err := json.MarshalIndent(self, "", " ")
  120. if err != nil {
  121. return err
  122. }
  123. err = os.MkdirAll(self.Path, os.ModePerm)
  124. if err != nil {
  125. return err
  126. }
  127. confpath := filepath.Join(self.Path, "config.json")
  128. return ioutil.WriteFile(confpath, data, os.ModePerm)
  129. }