aboutsummaryrefslogtreecommitdiffstats
path: root/swarm/api/config.go
diff options
context:
space:
mode:
authorlash <nolash@users.noreply.github.com>2019-03-22 12:55:47 +0800
committerViktor TrĂ³n <viktor.tron@gmail.com>2019-03-22 12:55:47 +0800
commit09924cbcaab5106951fb67648315131bb4024ac5 (patch)
treeefeb2e96b97b6537ce5680a26934176567da00b3 /swarm/api/config.go
parent358535188852bd9d8e351ccbb0d1fa608284a77b (diff)
downloadgo-tangerine-09924cbcaab5106951fb67648315131bb4024ac5.tar
go-tangerine-09924cbcaab5106951fb67648315131bb4024ac5.tar.gz
go-tangerine-09924cbcaab5106951fb67648315131bb4024ac5.tar.bz2
go-tangerine-09924cbcaab5106951fb67648315131bb4024ac5.tar.lz
go-tangerine-09924cbcaab5106951fb67648315131bb4024ac5.tar.xz
go-tangerine-09924cbcaab5106951fb67648315131bb4024ac5.tar.zst
go-tangerine-09924cbcaab5106951fb67648315131bb4024ac5.zip
cmd/swarm, p2p, swarm: Enable ENR in binary/execadapter (#19309)
* cmd/swarm, p2p, swarm: Enable ENR in binary/execadapter * cmd/p2p/swarm: Remove comments + config.Enode nomarshal * p2p/simulations: Remove superfluous error check * p2p/simulation: Move init enode comment * swarm/api: Check error in config test * swarm, p2p/simulations, cmd/swarm: Use nodekey in binary record sign * cmd/swarm: Make nodekey available for swarm api config
Diffstat (limited to 'swarm/api/config.go')
-rw-r--r--swarm/api/config.go62
1 files changed, 44 insertions, 18 deletions
diff --git a/swarm/api/config.go b/swarm/api/config.go
index 1fa6c4fdf..0a7100c57 100644
--- a/swarm/api/config.go
+++ b/swarm/api/config.go
@@ -29,7 +29,6 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p/enode"
- "github.com/ethereum/go-ethereum/swarm/log"
"github.com/ethereum/go-ethereum/swarm/network"
"github.com/ethereum/go-ethereum/swarm/pss"
"github.com/ethereum/go-ethereum/swarm/services/swap"
@@ -58,7 +57,7 @@ type Config struct {
Port string
PublicKey string
BzzKey string
- NodeID string
+ Enode *enode.Node `toml:"-"`
NetworkID uint64
SwapEnabled bool
SyncEnabled bool
@@ -104,33 +103,38 @@ func NewConfig() (c *Config) {
//some config params need to be initialized after the complete
//config building phase is completed (e.g. due to overriding flags)
-func (c *Config) Init(prvKey *ecdsa.PrivateKey) {
+func (c *Config) Init(prvKey *ecdsa.PrivateKey, nodeKey *ecdsa.PrivateKey) error {
- address := crypto.PubkeyToAddress(prvKey.PublicKey)
- c.Path = filepath.Join(c.Path, "bzz-"+common.Bytes2Hex(address.Bytes()))
- err := os.MkdirAll(c.Path, os.ModePerm)
+ // create swarm dir and record key
+ err := c.createAndSetPath(c.Path, prvKey)
if err != nil {
- log.Error(fmt.Sprintf("Error creating root swarm data directory: %v", err))
- return
+ return fmt.Errorf("Error creating root swarm data directory: %v", err)
+ }
+ c.setKey(prvKey)
+
+ // create the new enode record
+ // signed with the ephemeral node key
+ enodeParams := &network.EnodeParams{
+ PrivateKey: prvKey,
+ EnodeKey: nodeKey,
+ Lightnode: c.LightNodeEnabled,
+ Bootnode: c.BootnodeMode,
+ }
+ c.Enode, err = network.NewEnode(enodeParams)
+ if err != nil {
+ return fmt.Errorf("Error creating enode: %v", err)
}
- pubkey := crypto.FromECDSAPub(&prvKey.PublicKey)
- pubkeyhex := common.ToHex(pubkey)
- keyhex := hexutil.Encode(network.PrivateKeyToBzzKey(prvKey))
-
- c.PublicKey = pubkeyhex
- c.BzzKey = keyhex
- c.NodeID = enode.PubkeyToIDV4(&prvKey.PublicKey).String()
-
+ // initialize components that depend on the swarm instance's private key
if c.SwapEnabled {
c.Swap.Init(c.Contract, prvKey)
}
- c.privateKey = prvKey
c.LocalStoreParams.Init(c.Path)
- c.LocalStoreParams.BaseKey = common.FromHex(keyhex)
+ c.LocalStoreParams.BaseKey = common.FromHex(c.BzzKey)
c.Pss = c.Pss.WithPrivateKey(c.privateKey)
+ return nil
}
func (c *Config) ShiftPrivateKey() (privKey *ecdsa.PrivateKey) {
@@ -140,3 +144,25 @@ func (c *Config) ShiftPrivateKey() (privKey *ecdsa.PrivateKey) {
}
return privKey
}
+
+func (c *Config) setKey(prvKey *ecdsa.PrivateKey) {
+ bzzkeybytes := network.PrivateKeyToBzzKey(prvKey)
+ pubkey := crypto.FromECDSAPub(&prvKey.PublicKey)
+ pubkeyhex := hexutil.Encode(pubkey)
+ keyhex := hexutil.Encode(bzzkeybytes)
+
+ c.privateKey = prvKey
+ c.PublicKey = pubkeyhex
+ c.BzzKey = keyhex
+}
+
+func (c *Config) createAndSetPath(datadirPath string, prvKey *ecdsa.PrivateKey) error {
+ address := crypto.PubkeyToAddress(prvKey.PublicKey)
+ bzzdirPath := filepath.Join(datadirPath, "bzz-"+common.Bytes2Hex(address.Bytes()))
+ err := os.MkdirAll(bzzdirPath, os.ModePerm)
+ if err != nil {
+ return err
+ }
+ c.Path = bzzdirPath
+ return nil
+}