@@ -29,7 +29,6 @@ import (
2929 "github.com/ethereum/go-ethereum/crypto"
3030 "github.com/ethereum/go-ethereum/node"
3131 "github.com/ethereum/go-ethereum/p2p/enode"
32- "github.com/ethereum/go-ethereum/swarm/log"
3332 "github.com/ethereum/go-ethereum/swarm/network"
3433 "github.com/ethereum/go-ethereum/swarm/pss"
3534 "github.com/ethereum/go-ethereum/swarm/services/swap"
@@ -58,7 +57,7 @@ type Config struct {
5857 Port string
5958 PublicKey string
6059 BzzKey string
61- NodeID string
60+ Enode * enode. Node `toml:"-"`
6261 NetworkID uint64
6362 SwapEnabled bool
6463 SyncEnabled bool
@@ -104,33 +103,38 @@ func NewConfig() (c *Config) {
104103
105104//some config params need to be initialized after the complete
106105//config building phase is completed (e.g. due to overriding flags)
107- func (c * Config ) Init (prvKey * ecdsa.PrivateKey ) {
106+ func (c * Config ) Init (prvKey * ecdsa.PrivateKey , nodeKey * ecdsa. PrivateKey ) error {
108107
109- address := crypto .PubkeyToAddress (prvKey .PublicKey )
110- c .Path = filepath .Join (c .Path , "bzz-" + common .Bytes2Hex (address .Bytes ()))
111- err := os .MkdirAll (c .Path , os .ModePerm )
108+ // create swarm dir and record key
109+ err := c .createAndSetPath (c .Path , prvKey )
112110 if err != nil {
113- log .Error (fmt .Sprintf ("Error creating root swarm data directory: %v" , err ))
114- return
111+ return fmt .Errorf ("Error creating root swarm data directory: %v" , err )
112+ }
113+ c .setKey (prvKey )
114+
115+ // create the new enode record
116+ // signed with the ephemeral node key
117+ enodeParams := & network.EnodeParams {
118+ PrivateKey : prvKey ,
119+ EnodeKey : nodeKey ,
120+ Lightnode : c .LightNodeEnabled ,
121+ Bootnode : c .BootnodeMode ,
122+ }
123+ c .Enode , err = network .NewEnode (enodeParams )
124+ if err != nil {
125+ return fmt .Errorf ("Error creating enode: %v" , err )
115126 }
116127
117- pubkey := crypto .FromECDSAPub (& prvKey .PublicKey )
118- pubkeyhex := common .ToHex (pubkey )
119- keyhex := hexutil .Encode (network .PrivateKeyToBzzKey (prvKey ))
120-
121- c .PublicKey = pubkeyhex
122- c .BzzKey = keyhex
123- c .NodeID = enode .PubkeyToIDV4 (& prvKey .PublicKey ).String ()
124-
128+ // initialize components that depend on the swarm instance's private key
125129 if c .SwapEnabled {
126130 c .Swap .Init (c .Contract , prvKey )
127131 }
128132
129- c .privateKey = prvKey
130133 c .LocalStoreParams .Init (c .Path )
131- c .LocalStoreParams .BaseKey = common .FromHex (keyhex )
134+ c .LocalStoreParams .BaseKey = common .FromHex (c . BzzKey )
132135
133136 c .Pss = c .Pss .WithPrivateKey (c .privateKey )
137+ return nil
134138}
135139
136140func (c * Config ) ShiftPrivateKey () (privKey * ecdsa.PrivateKey ) {
@@ -140,3 +144,25 @@ func (c *Config) ShiftPrivateKey() (privKey *ecdsa.PrivateKey) {
140144 }
141145 return privKey
142146}
147+
148+ func (c * Config ) setKey (prvKey * ecdsa.PrivateKey ) {
149+ bzzkeybytes := network .PrivateKeyToBzzKey (prvKey )
150+ pubkey := crypto .FromECDSAPub (& prvKey .PublicKey )
151+ pubkeyhex := hexutil .Encode (pubkey )
152+ keyhex := hexutil .Encode (bzzkeybytes )
153+
154+ c .privateKey = prvKey
155+ c .PublicKey = pubkeyhex
156+ c .BzzKey = keyhex
157+ }
158+
159+ func (c * Config ) createAndSetPath (datadirPath string , prvKey * ecdsa.PrivateKey ) error {
160+ address := crypto .PubkeyToAddress (prvKey .PublicKey )
161+ bzzdirPath := filepath .Join (datadirPath , "bzz-" + common .Bytes2Hex (address .Bytes ()))
162+ err := os .MkdirAll (bzzdirPath , os .ModePerm )
163+ if err != nil {
164+ return err
165+ }
166+ c .Path = bzzdirPath
167+ return nil
168+ }
0 commit comments