config.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. "time"
  23. "github.com/ethereum/go-ethereum/common"
  24. "github.com/ethereum/go-ethereum/contracts/ens"
  25. "github.com/ethereum/go-ethereum/crypto"
  26. "github.com/ethereum/go-ethereum/node"
  27. "github.com/ethereum/go-ethereum/p2p/discover"
  28. "github.com/ethereum/go-ethereum/swarm/log"
  29. "github.com/ethereum/go-ethereum/swarm/network"
  30. "github.com/ethereum/go-ethereum/swarm/pss"
  31. "github.com/ethereum/go-ethereum/swarm/services/swap"
  32. "github.com/ethereum/go-ethereum/swarm/storage"
  33. )
  34. const (
  35. DefaultHTTPListenAddr = "127.0.0.1"
  36. DefaultHTTPPort = "8500"
  37. )
  38. // separate bzz directories
  39. // allow several bzz nodes running in parallel
  40. type Config struct {
  41. // serialised/persisted fields
  42. *storage.FileStoreParams
  43. *storage.LocalStoreParams
  44. *network.HiveParams
  45. Swap *swap.LocalProfile
  46. Pss *pss.PssParams
  47. //*network.SyncParams
  48. Contract common.Address
  49. EnsRoot common.Address
  50. EnsAPIs []string
  51. Path string
  52. ListenAddr string
  53. Port string
  54. PublicKey string
  55. BzzKey string
  56. NodeID string
  57. NetworkID uint64
  58. SwapEnabled bool
  59. SyncEnabled bool
  60. SyncingSkipCheck bool
  61. DeliverySkipCheck bool
  62. MaxStreamPeerServers int
  63. LightNodeEnabled bool
  64. SyncUpdateDelay time.Duration
  65. SwapAPI string
  66. Cors string
  67. BzzAccount string
  68. privateKey *ecdsa.PrivateKey
  69. }
  70. //create a default config with all parameters to set to defaults
  71. func NewConfig() (c *Config) {
  72. c = &Config{
  73. LocalStoreParams: storage.NewDefaultLocalStoreParams(),
  74. FileStoreParams: storage.NewFileStoreParams(),
  75. HiveParams: network.NewHiveParams(),
  76. //SyncParams: network.NewDefaultSyncParams(),
  77. Swap: swap.NewDefaultSwapParams(),
  78. Pss: pss.NewPssParams(),
  79. ListenAddr: DefaultHTTPListenAddr,
  80. Port: DefaultHTTPPort,
  81. Path: node.DefaultDataDir(),
  82. EnsAPIs: nil,
  83. EnsRoot: ens.TestNetAddress,
  84. NetworkID: network.DefaultNetworkID,
  85. SwapEnabled: false,
  86. SyncEnabled: true,
  87. SyncingSkipCheck: false,
  88. MaxStreamPeerServers: 10000,
  89. DeliverySkipCheck: true,
  90. SyncUpdateDelay: 15 * time.Second,
  91. SwapAPI: "",
  92. }
  93. return
  94. }
  95. //some config params need to be initialized after the complete
  96. //config building phase is completed (e.g. due to overriding flags)
  97. func (c *Config) Init(prvKey *ecdsa.PrivateKey) {
  98. address := crypto.PubkeyToAddress(prvKey.PublicKey)
  99. c.Path = filepath.Join(c.Path, "bzz-"+common.Bytes2Hex(address.Bytes()))
  100. err := os.MkdirAll(c.Path, os.ModePerm)
  101. if err != nil {
  102. log.Error(fmt.Sprintf("Error creating root swarm data directory: %v", err))
  103. return
  104. }
  105. pubkey := crypto.FromECDSAPub(&prvKey.PublicKey)
  106. pubkeyhex := common.ToHex(pubkey)
  107. keyhex := crypto.Keccak256Hash(pubkey).Hex()
  108. c.PublicKey = pubkeyhex
  109. c.BzzKey = keyhex
  110. c.NodeID = discover.PubkeyID(&prvKey.PublicKey).String()
  111. if c.SwapEnabled {
  112. c.Swap.Init(c.Contract, prvKey)
  113. }
  114. c.privateKey = prvKey
  115. c.LocalStoreParams.Init(c.Path)
  116. c.LocalStoreParams.BaseKey = common.FromHex(keyhex)
  117. c.Pss = c.Pss.WithPrivateKey(c.privateKey)
  118. }
  119. func (c *Config) ShiftPrivateKey() (privKey *ecdsa.PrivateKey) {
  120. if c.privateKey != nil {
  121. privKey = c.privateKey
  122. c.privateKey = nil
  123. }
  124. return privKey
  125. }