@@ -29,7 +29,6 @@ import (
29
29
"github.com/ethereum/go-ethereum/crypto"
30
30
"github.com/ethereum/go-ethereum/node"
31
31
"github.com/ethereum/go-ethereum/p2p/enode"
32
- "github.com/ethereum/go-ethereum/swarm/log"
33
32
"github.com/ethereum/go-ethereum/swarm/network"
34
33
"github.com/ethereum/go-ethereum/swarm/pss"
35
34
"github.com/ethereum/go-ethereum/swarm/services/swap"
@@ -58,7 +57,7 @@ type Config struct {
58
57
Port string
59
58
PublicKey string
60
59
BzzKey string
61
- NodeID string
60
+ Enode * enode. Node `toml:"-"`
62
61
NetworkID uint64
63
62
SwapEnabled bool
64
63
SyncEnabled bool
@@ -104,33 +103,38 @@ func NewConfig() (c *Config) {
104
103
105
104
//some config params need to be initialized after the complete
106
105
//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 {
108
107
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 )
112
110
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 )
115
126
}
116
127
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
125
129
if c .SwapEnabled {
126
130
c .Swap .Init (c .Contract , prvKey )
127
131
}
128
132
129
- c .privateKey = prvKey
130
133
c .LocalStoreParams .Init (c .Path )
131
- c .LocalStoreParams .BaseKey = common .FromHex (keyhex )
134
+ c .LocalStoreParams .BaseKey = common .FromHex (c . BzzKey )
132
135
133
136
c .Pss = c .Pss .WithPrivateKey (c .privateKey )
137
+ return nil
134
138
}
135
139
136
140
func (c * Config ) ShiftPrivateKey () (privKey * ecdsa.PrivateKey ) {
@@ -140,3 +144,25 @@ func (c *Config) ShiftPrivateKey() (privKey *ecdsa.PrivateKey) {
140
144
}
141
145
return privKey
142
146
}
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