Skip to content

Commit

Permalink
setup init: add key-file flag
Browse files Browse the repository at this point in the history
  • Loading branch information
jchappelow committed Jan 31, 2025
1 parent 55fb4ff commit 635b7b7
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions app/setup/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ kwild setup init --allocs "0xc89D42189f0450C2b2c3c61f58Ec5d628176A1E7:1000000000
)

func InitCmd() *cobra.Command {
var genesisPath, genesisState string
var genesisPath, keyFile string
var genFlags genesisFlagConfig

cmd := &cobra.Command{
Expand Down Expand Up @@ -113,7 +113,7 @@ func InitCmd() *cobra.Command {
}

if cmd.Flags().Changed(genesisSnapshotFlag) {
genesisState = genFlags.genesisState
genesisState := genFlags.genesisState
genesisState, err = node.ExpandPath(genesisState)
if err != nil {
return display.PrintErr(cmd, err)
Expand All @@ -127,14 +127,29 @@ func InitCmd() *cobra.Command {
cfg.GenesisState = stateFile
}

// Generate and save the node key to the root directory
privKey, err := crypto.GeneratePrivateKey(crypto.KeyTypeSecp256k1)
if err != nil {
return display.PrintErr(cmd, err)
}
var privKey crypto.PrivateKey
if cmd.Flags().Changed("key-file") {
if keyFile, err = node.ExpandPath(keyFile); err != nil {
return display.PrintErr(cmd, err)
}
privKey, err = key.LoadNodeKey(keyFile)
if err != nil {
return display.PrintErr(cmd, err)
}
err = utils.CopyFile(keyFile, config.NodeKeyFilePath(outDir))
if err != nil {
return display.PrintErr(cmd, err)
}
} else {
// Generate and save the node key to the root directory
privKey, err = crypto.GeneratePrivateKey(crypto.KeyTypeSecp256k1)
if err != nil {
return display.PrintErr(cmd, err)
}

if err := key.SaveNodeKey(config.NodeKeyFilePath(rootDir), privKey); err != nil {
return display.PrintErr(cmd, err)
if err := key.SaveNodeKey(config.NodeKeyFilePath(rootDir), privKey); err != nil {
return display.PrintErr(cmd, err)
}
}

genFile := config.GenesisFilePath(outDir)
Expand Down Expand Up @@ -212,6 +227,8 @@ func InitCmd() *cobra.Command {
// genesis config flags
cmd.Flags().StringVarP(&genesisPath, "genesis", "g", "", "path to genesis file")

cmd.Flags().StringVarP(&keyFile, "key-file", "k", "", "path to node key file")

return cmd
}

Expand Down

0 comments on commit 635b7b7

Please sign in to comment.