-
Notifications
You must be signed in to change notification settings - Fork 166
/
key.go
186 lines (155 loc) · 5.82 KB
/
key.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package cmd
import (
"fmt"
"github.com/onflow/crypto"
"github.com/spf13/cobra"
"github.com/onflow/flow-go/cmd"
"github.com/onflow/flow-go/cmd/bootstrap/utils"
"github.com/onflow/flow-go/cmd/util/cmd/common"
model "github.com/onflow/flow-go/model/bootstrap"
"github.com/onflow/flow-go/model/flow"
)
var (
flagRole string
flagAddress string
// seed flags
flagNetworkSeed []byte
flagStakingSeed []byte
flagMachineSeed []byte
)
// keyCmd represents the key command
var keyCmd = &cobra.Command{
Use: "key",
Short: "Generate networking, staking, and machine account keys for a partner node and write them to files.",
Run: keyCmdRun,
}
func init() {
rootCmd.AddCommand(keyCmd)
// required flags
keyCmd.Flags().StringVar(&flagRole, "role", "", "node role (can be \"collection\", \"consensus\", \"execution\", \"verification\" or \"access\")")
cmd.MarkFlagRequired(keyCmd, "role")
keyCmd.Flags().StringVar(&flagAddress, "address", "", "network address")
cmd.MarkFlagRequired(keyCmd, "address")
keyCmd.Flags().BytesHexVar(
&flagNetworkSeed,
"networking-seed",
[]byte{},
fmt.Sprintf("hex encoded networking seed (min %d bytes)", crypto.KeyGenSeedMinLen))
keyCmd.Flags().BytesHexVar(
&flagStakingSeed,
"staking-seed",
[]byte{},
fmt.Sprintf("hex encoded staking seed (min %d bytes)", crypto.KeyGenSeedMinLen))
keyCmd.Flags().BytesHexVar(
&flagMachineSeed,
"machine-seed",
[]byte{},
fmt.Sprintf("hex encoded machine account seed (min %d bytes)", crypto.KeyGenSeedMinLen))
}
// keyCmdRun generate the node staking key, networking key and node information
func keyCmdRun(_ *cobra.Command, _ []string) {
// generate private key seeds if not specified via flag
if len(flagNetworkSeed) == 0 {
flagNetworkSeed = GenerateRandomSeed(crypto.KeyGenSeedMinLen)
}
if len(flagStakingSeed) == 0 {
flagStakingSeed = GenerateRandomSeed(crypto.KeyGenSeedMinLen)
}
if len(flagMachineSeed) == 0 {
flagMachineSeed = GenerateRandomSeed(crypto.KeyGenSeedMinLen)
}
// validate inputs
role := validateRole(flagRole)
common.ValidateAddressFormat(log, flagAddress)
// generate staking and network keys
networkKey, stakingKey, secretsDBKey, err := generateKeys()
if err != nil {
log.Fatal().Err(err).Msg("could not generate staking or network keys")
}
log.Debug().Str("address", flagAddress).Msg("assembling node information")
conf := model.NodeConfig{
Role: role,
Address: flagAddress,
Weight: 0,
}
nodeInfo := assembleNodeInfo(conf, networkKey, stakingKey)
private, err := nodeInfo.Private()
if err != nil {
log.Fatal().Err(err).Msg("could not access private keys")
}
// write files
err = common.WriteText(model.PathNodeID, flagOutdir, []byte(nodeInfo.NodeID.String()))
if err != nil {
log.Fatal().Err(err).Msg("failed to write file")
}
log.Info().Msgf("wrote file %s/%s", flagOutdir, model.PathNodeID)
err = common.WriteJSON(fmt.Sprintf(model.PathNodeInfoPriv, nodeInfo.NodeID), flagOutdir, private)
if err != nil {
log.Fatal().Err(err).Msg("failed to write json")
}
log.Info().Msgf("wrote file %s/%s", flagOutdir, model.PathNodeInfoPriv)
err = common.WriteText(fmt.Sprintf(model.PathSecretsEncryptionKey, nodeInfo.NodeID), flagOutdir, secretsDBKey)
if err != nil {
log.Fatal().Err(err).Msg("failed to write file")
}
log.Info().Msgf("wrote file %s/%s", flagOutdir, model.PathSecretsEncryptionKey)
err = common.WriteJSON(fmt.Sprintf(model.PathNodeInfoPub, nodeInfo.NodeID), flagOutdir, nodeInfo.Public())
if err != nil {
log.Fatal().Err(err).Msg("failed to write json")
}
log.Info().Msgf("wrote file %s/%s", flagOutdir, model.PathNodeInfoPub)
// write machine account info
if role == flow.RoleCollection || role == flow.RoleConsensus {
// generate machine account priv key
machineKey, err := generateMachineAccountKey()
if err != nil {
log.Fatal().Err(err).Msg("could not generate machine account key")
}
log.Debug().Str("address", flagAddress).Msg("assembling machine account information")
// write the public key to terminal for entry in Flow Port
machineAccountPriv := assembleNodeMachineAccountKey(machineKey)
err = common.WriteJSON(fmt.Sprintf(model.PathNodeMachineAccountPrivateKey, nodeInfo.NodeID), flagOutdir, machineAccountPriv)
if err != nil {
log.Fatal().Err(err).Msg("failed to write json")
}
log.Info().Msgf("wrote file %s/%s", flagOutdir, model.PathNodeMachineAccountPrivateKey)
}
}
func generateKeys() (crypto.PrivateKey, crypto.PrivateKey, []byte, error) {
log.Debug().Msg("will generate networking key")
networkKey, err := utils.GenerateNetworkingKey(flagNetworkSeed)
if err != nil {
return nil, nil, nil, fmt.Errorf("could not generate networking key: %w", err)
}
log.Info().Msg("generated networking key")
log.Debug().Msg("will generate staking key")
stakingKey, err := utils.GenerateStakingKey(flagStakingSeed)
if err != nil {
return nil, nil, nil, fmt.Errorf("could not generate staking key: %w", err)
}
log.Info().Msg("generated staking key")
log.Debug().Msg("will generate db encryption key")
secretsDBKey, err := utils.GenerateSecretsDBEncryptionKey()
if err != nil {
return nil, nil, nil, fmt.Errorf("could not generate secrets db encryption key: %w", err)
}
log.Info().Msg("generated db encryption key")
return networkKey, stakingKey, secretsDBKey, nil
}
func generateMachineAccountKey() (crypto.PrivateKey, error) {
log.Debug().Msg("will generate machine account key")
machineKey, err := utils.GenerateMachineAccountKey(flagMachineSeed)
if err != nil {
return nil, fmt.Errorf("could not generate machine key: %w", err)
}
log.Info().Msg("generated machine account key")
return machineKey, nil
}
func validateRole(role string) flow.Role {
parsed, err := flow.ParseRole(role)
if err != nil {
log.Fatal().Err(err).Msg("unsupported role, allowed values: \"collection\", \"consensus\", \"execution\", " +
"\"verification\" or \"access\"")
}
return parsed
}