config.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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/common/hexutil"
  25. "github.com/ethereum/go-ethereum/contracts/ens"
  26. "github.com/ethereum/go-ethereum/crypto"
  27. "github.com/ethereum/go-ethereum/node"
  28. "github.com/ethereum/go-ethereum/p2p/enode"
  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. // LocalStore
  44. ChunkDbPath string
  45. DbCapacity uint64
  46. CacheCapacity uint
  47. BaseKey []byte
  48. *network.HiveParams
  49. Swap *swap.LocalProfile
  50. Pss *pss.PssParams
  51. Contract common.Address
  52. EnsRoot common.Address
  53. EnsAPIs []string
  54. Path string
  55. ListenAddr string
  56. Port string
  57. PublicKey string
  58. BzzKey string
  59. Enode *enode.Node `toml:"-"`
  60. NetworkID uint64
  61. SwapEnabled bool
  62. SyncEnabled bool
  63. SyncingSkipCheck bool
  64. DeliverySkipCheck bool
  65. MaxStreamPeerServers int
  66. LightNodeEnabled bool
  67. BootnodeMode bool
  68. SyncUpdateDelay time.Duration
  69. SwapAPI string
  70. Cors string
  71. BzzAccount string
  72. GlobalStoreAPI string
  73. privateKey *ecdsa.PrivateKey
  74. }
  75. //create a default config with all parameters to set to defaults
  76. func NewConfig() (c *Config) {
  77. c = &Config{
  78. FileStoreParams: storage.NewFileStoreParams(),
  79. HiveParams: network.NewHiveParams(),
  80. Swap: swap.NewDefaultSwapParams(),
  81. Pss: pss.NewPssParams(),
  82. ListenAddr: DefaultHTTPListenAddr,
  83. Port: DefaultHTTPPort,
  84. Path: node.DefaultDataDir(),
  85. EnsAPIs: nil,
  86. EnsRoot: ens.TestNetAddress,
  87. NetworkID: network.DefaultNetworkID,
  88. SwapEnabled: false,
  89. SyncEnabled: true,
  90. SyncingSkipCheck: false,
  91. MaxStreamPeerServers: 10000,
  92. DeliverySkipCheck: true,
  93. SyncUpdateDelay: 15 * time.Second,
  94. SwapAPI: "",
  95. }
  96. return
  97. }
  98. //some config params need to be initialized after the complete
  99. //config building phase is completed (e.g. due to overriding flags)
  100. func (c *Config) Init(prvKey *ecdsa.PrivateKey, nodeKey *ecdsa.PrivateKey) error {
  101. // create swarm dir and record key
  102. err := c.createAndSetPath(c.Path, prvKey)
  103. if err != nil {
  104. return fmt.Errorf("Error creating root swarm data directory: %v", err)
  105. }
  106. c.setKey(prvKey)
  107. // create the new enode record
  108. // signed with the ephemeral node key
  109. enodeParams := &network.EnodeParams{
  110. PrivateKey: prvKey,
  111. EnodeKey: nodeKey,
  112. Lightnode: c.LightNodeEnabled,
  113. Bootnode: c.BootnodeMode,
  114. }
  115. c.Enode, err = network.NewEnode(enodeParams)
  116. if err != nil {
  117. return fmt.Errorf("Error creating enode: %v", err)
  118. }
  119. // initialize components that depend on the swarm instance's private key
  120. if c.SwapEnabled {
  121. c.Swap.Init(c.Contract, prvKey)
  122. }
  123. c.privateKey = prvKey
  124. c.ChunkDbPath = filepath.Join(c.Path, "chunks")
  125. c.BaseKey = common.FromHex(c.BzzKey)
  126. c.Pss = c.Pss.WithPrivateKey(c.privateKey)
  127. return nil
  128. }
  129. func (c *Config) ShiftPrivateKey() (privKey *ecdsa.PrivateKey) {
  130. if c.privateKey != nil {
  131. privKey = c.privateKey
  132. c.privateKey = nil
  133. }
  134. return privKey
  135. }
  136. func (c *Config) setKey(prvKey *ecdsa.PrivateKey) {
  137. bzzkeybytes := network.PrivateKeyToBzzKey(prvKey)
  138. pubkey := crypto.FromECDSAPub(&prvKey.PublicKey)
  139. pubkeyhex := hexutil.Encode(pubkey)
  140. keyhex := hexutil.Encode(bzzkeybytes)
  141. c.privateKey = prvKey
  142. c.PublicKey = pubkeyhex
  143. c.BzzKey = keyhex
  144. }
  145. func (c *Config) createAndSetPath(datadirPath string, prvKey *ecdsa.PrivateKey) error {
  146. address := crypto.PubkeyToAddress(prvKey.PublicKey)
  147. bzzdirPath := filepath.Join(datadirPath, "bzz-"+common.Bytes2Hex(address.Bytes()))
  148. err := os.MkdirAll(bzzdirPath, os.ModePerm)
  149. if err != nil {
  150. return err
  151. }
  152. c.Path = bzzdirPath
  153. return nil
  154. }