config.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.Sha3Hash(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 err != nil {
  81. if !os.IsNotExist(err) {
  82. return
  83. }
  84. // file does not exist
  85. // write out config file
  86. err = self.Save()
  87. if err != nil {
  88. err = fmt.Errorf("error writing config: %v", err)
  89. }
  90. return
  91. }
  92. // file exists, deserialise
  93. err = json.Unmarshal(data, self)
  94. if err != nil {
  95. return nil, fmt.Errorf("unable to parse config: %v", err)
  96. }
  97. // check public key
  98. if pubkeyhex != self.PublicKey {
  99. return nil, fmt.Errorf("public key does not match the one in the config file %v != %v", pubkeyhex, self.PublicKey)
  100. }
  101. if keyhex != self.BzzKey {
  102. return nil, fmt.Errorf("bzz key does not match the one in the config file %v != %v", keyhex, self.BzzKey)
  103. }
  104. self.Swap.SetKey(prvKey)
  105. if (self.EnsRoot == common.Address{}) {
  106. self.EnsRoot = ensRootAddress
  107. }
  108. return
  109. }
  110. func (self *Config) Save() error {
  111. data, err := json.MarshalIndent(self, "", " ")
  112. if err != nil {
  113. return err
  114. }
  115. err = os.MkdirAll(self.Path, os.ModePerm)
  116. if err != nil {
  117. return err
  118. }
  119. confpath := filepath.Join(self.Path, "config.json")
  120. return ioutil.WriteFile(confpath, data, os.ModePerm)
  121. }