config.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. "fmt"
  20. "os"
  21. "path/filepath"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/contracts/ens"
  24. "github.com/ethereum/go-ethereum/crypto"
  25. "github.com/ethereum/go-ethereum/log"
  26. "github.com/ethereum/go-ethereum/node"
  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. Contract common.Address
  45. EnsRoot common.Address
  46. EnsAPIs []string
  47. Path string
  48. ListenAddr string
  49. Port string
  50. PublicKey string
  51. BzzKey string
  52. NetworkId uint64
  53. SwapEnabled bool
  54. SyncEnabled bool
  55. SwapApi string
  56. Cors string
  57. BzzAccount string
  58. BootNodes string
  59. }
  60. //create a default config with all parameters to set to defaults
  61. func NewDefaultConfig() (self *Config) {
  62. self = &Config{
  63. StoreParams: storage.NewDefaultStoreParams(),
  64. ChunkerParams: storage.NewChunkerParams(),
  65. HiveParams: network.NewDefaultHiveParams(),
  66. SyncParams: network.NewDefaultSyncParams(),
  67. Swap: swap.NewDefaultSwapParams(),
  68. ListenAddr: DefaultHTTPListenAddr,
  69. Port: DefaultHTTPPort,
  70. Path: node.DefaultDataDir(),
  71. EnsAPIs: nil,
  72. EnsRoot: ens.TestNetAddress,
  73. NetworkId: network.NetworkId,
  74. SwapEnabled: false,
  75. SyncEnabled: true,
  76. SwapApi: "",
  77. BootNodes: "",
  78. }
  79. return
  80. }
  81. //some config params need to be initialized after the complete
  82. //config building phase is completed (e.g. due to overriding flags)
  83. func (self *Config) Init(prvKey *ecdsa.PrivateKey) {
  84. address := crypto.PubkeyToAddress(prvKey.PublicKey)
  85. self.Path = filepath.Join(self.Path, "bzz-"+common.Bytes2Hex(address.Bytes()))
  86. err := os.MkdirAll(self.Path, os.ModePerm)
  87. if err != nil {
  88. log.Error(fmt.Sprintf("Error creating root swarm data directory: %v", err))
  89. return
  90. }
  91. pubkey := crypto.FromECDSAPub(&prvKey.PublicKey)
  92. pubkeyhex := common.ToHex(pubkey)
  93. keyhex := crypto.Keccak256Hash(pubkey).Hex()
  94. self.PublicKey = pubkeyhex
  95. self.BzzKey = keyhex
  96. self.Swap.Init(self.Contract, prvKey)
  97. self.SyncParams.Init(self.Path)
  98. self.HiveParams.Init(self.Path)
  99. self.StoreParams.Init(self.Path)
  100. }