-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathnode_key_config.go
99 lines (87 loc) · 2.73 KB
/
node_key_config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright 2024 The Erigon Authors
// This file is part of Erigon.
//
// Erigon is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Erigon is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Erigon. If not, see <http://www.gnu.org/licenses/>.
package p2p
import (
"crypto/ecdsa"
"errors"
"fmt"
"os"
"path"
"github.com/erigontech/erigon-lib/crypto"
)
type NodeKeyConfig struct {
}
func (config NodeKeyConfig) DefaultPath(datadir string) string {
return path.Join(datadir, "nodekey")
}
func (config NodeKeyConfig) generateKey() (*ecdsa.PrivateKey, error) {
key, err := crypto.GenerateKey()
if err != nil {
err = fmt.Errorf("failed to generate node key: %w", err)
}
return key, err
}
func (config NodeKeyConfig) parseHex(hex string) (*ecdsa.PrivateKey, error) {
key, err := crypto.HexToECDSA(hex)
if err != nil {
err = fmt.Errorf("failed to parse node key from %s: %w", hex, err)
}
return key, err
}
func (config NodeKeyConfig) load(keyfile string) (*ecdsa.PrivateKey, error) {
key, err := crypto.LoadECDSA(keyfile)
if err != nil {
err = fmt.Errorf("failed to load node key from %s: %w", keyfile, err)
}
return key, err
}
func (config NodeKeyConfig) save(keyfile string, key *ecdsa.PrivateKey) error {
err := os.MkdirAll(path.Dir(keyfile), 0755)
if err == nil {
err = crypto.SaveECDSA(keyfile, key)
}
if err != nil {
return fmt.Errorf("failed to save node key to %s: %w", keyfile, err)
}
return nil
}
func (config NodeKeyConfig) LoadOrGenerateAndSave(keyfile string) (*ecdsa.PrivateKey, error) {
// If file exists, try to load it.
if _, err := os.Stat(keyfile); err == nil {
return config.load(keyfile)
}
// No persistent key found, generate and store a new one.
key, err := config.generateKey()
if err != nil {
return nil, err
}
if err := config.save(keyfile, key); err != nil {
return nil, err
}
return key, nil
}
func (config NodeKeyConfig) LoadOrParseOrGenerateAndSave(file, hex, datadir string) (*ecdsa.PrivateKey, error) {
switch {
case file != "" && hex != "":
return nil, errors.New("P2P node key is set as both file and hex string - these options are mutually exclusive")
case file != "":
return config.load(file)
case hex != "":
return config.parseHex(hex)
default:
return config.LoadOrGenerateAndSave(config.DefaultPath(datadir))
}
}