config.go 3.7 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. // by default ens root is north internal
  34. var (
  35. toyNetEnsRoot = common.HexToAddress("0xd344889e0be3e9ef6c26b0f60ef66a32e83c1b69")
  36. )
  37. // separate bzz directories
  38. // allow several bzz nodes running in parallel
  39. type Config struct {
  40. // serialised/persisted fields
  41. *storage.StoreParams
  42. *storage.ChunkerParams
  43. *network.HiveParams
  44. Swap *swap.SwapParams
  45. *network.SyncParams
  46. Path string
  47. Port string
  48. PublicKey string
  49. BzzKey string
  50. EnsRoot common.Address
  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) (self *Config, err error) {
  55. address := crypto.PubkeyToAddress(prvKey.PublicKey) // default beneficiary address
  56. dirpath := filepath.Join(path, 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: toyNetEnsRoot,
  77. }
  78. data, err = ioutil.ReadFile(confpath)
  79. if err != nil {
  80. if !os.IsNotExist(err) {
  81. return
  82. }
  83. // file does not exist
  84. // write out config file
  85. err = self.Save()
  86. if err != nil {
  87. err = fmt.Errorf("error writing config: %v", err)
  88. }
  89. return
  90. }
  91. // file exists, deserialise
  92. err = json.Unmarshal(data, self)
  93. if err != nil {
  94. return nil, fmt.Errorf("unable to parse config: %v", err)
  95. }
  96. // check public key
  97. if pubkeyhex != self.PublicKey {
  98. return nil, fmt.Errorf("public key does not match the one in the config file %v != %v", pubkeyhex, self.PublicKey)
  99. }
  100. if keyhex != self.BzzKey {
  101. return nil, fmt.Errorf("bzz key does not match the one in the config file %v != %v", keyhex, self.BzzKey)
  102. }
  103. self.Swap.SetKey(prvKey)
  104. if (self.EnsRoot == common.Address{}) {
  105. self.EnsRoot = toyNetEnsRoot
  106. }
  107. return
  108. }
  109. func (self *Config) Save() error {
  110. data, err := json.MarshalIndent(self, "", " ")
  111. if err != nil {
  112. return err
  113. }
  114. err = os.MkdirAll(self.Path, os.ModePerm)
  115. if err != nil {
  116. return err
  117. }
  118. confpath := filepath.Join(self.Path, "config.json")
  119. return ioutil.WriteFile(confpath, data, os.ModePerm)
  120. }