config.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. DeliverySkipCheck bool
  61. LightNodeEnabled bool
  62. SyncUpdateDelay time.Duration
  63. SwapAPI string
  64. Cors string
  65. BzzAccount string
  66. privateKey *ecdsa.PrivateKey
  67. }
  68. //create a default config with all parameters to set to defaults
  69. func NewConfig() (c *Config) {
  70. c = &Config{
  71. LocalStoreParams: storage.NewDefaultLocalStoreParams(),
  72. FileStoreParams: storage.NewFileStoreParams(),
  73. HiveParams: network.NewHiveParams(),
  74. //SyncParams: network.NewDefaultSyncParams(),
  75. Swap: swap.NewDefaultSwapParams(),
  76. Pss: pss.NewPssParams(),
  77. ListenAddr: DefaultHTTPListenAddr,
  78. Port: DefaultHTTPPort,
  79. Path: node.DefaultDataDir(),
  80. EnsAPIs: nil,
  81. EnsRoot: ens.TestNetAddress,
  82. NetworkID: network.DefaultNetworkID,
  83. SwapEnabled: false,
  84. SyncEnabled: true,
  85. DeliverySkipCheck: false,
  86. SyncUpdateDelay: 15 * time.Second,
  87. SwapAPI: "",
  88. }
  89. return
  90. }
  91. //some config params need to be initialized after the complete
  92. //config building phase is completed (e.g. due to overriding flags)
  93. func (c *Config) Init(prvKey *ecdsa.PrivateKey) {
  94. address := crypto.PubkeyToAddress(prvKey.PublicKey)
  95. c.Path = filepath.Join(c.Path, "bzz-"+common.Bytes2Hex(address.Bytes()))
  96. err := os.MkdirAll(c.Path, os.ModePerm)
  97. if err != nil {
  98. log.Error(fmt.Sprintf("Error creating root swarm data directory: %v", err))
  99. return
  100. }
  101. pubkey := crypto.FromECDSAPub(&prvKey.PublicKey)
  102. pubkeyhex := common.ToHex(pubkey)
  103. keyhex := crypto.Keccak256Hash(pubkey).Hex()
  104. c.PublicKey = pubkeyhex
  105. c.BzzKey = keyhex
  106. c.NodeID = discover.PubkeyID(&prvKey.PublicKey).String()
  107. if c.SwapEnabled {
  108. c.Swap.Init(c.Contract, prvKey)
  109. }
  110. c.privateKey = prvKey
  111. c.LocalStoreParams.Init(c.Path)
  112. c.LocalStoreParams.BaseKey = common.FromHex(keyhex)
  113. c.Pss = c.Pss.WithPrivateKey(c.privateKey)
  114. }
  115. func (c *Config) ShiftPrivateKey() (privKey *ecdsa.PrivateKey) {
  116. if c.privateKey != nil {
  117. privKey = c.privateKey
  118. c.privateKey = nil
  119. }
  120. return privKey
  121. }