From dc8b3e0b25f5c8033b989a8cafcc2e29e1e7959b Mon Sep 17 00:00:00 2001 From: Pavel Zbitskiy Date: Mon, 29 Jul 2024 13:41:01 -0400 Subject: [PATCH] move to `goal node generate-p2pid` subcommand --- cmd/goal/node.go | 1 + cmd/goal/p2pid.go | 66 +++++++++++++++++++++++++++++++++++++++++++++ cmd/p2pid/main.go | 69 ----------------------------------------------- 3 files changed, 67 insertions(+), 69 deletions(-) create mode 100644 cmd/goal/p2pid.go delete mode 100644 cmd/p2pid/main.go diff --git a/cmd/goal/node.go b/cmd/goal/node.go index 2db08fd4e5..8bb103115b 100644 --- a/cmd/goal/node.go +++ b/cmd/goal/node.go @@ -79,6 +79,7 @@ func init() { nodeCmd.AddCommand(catchupCmd) // Once the server-side implementation of the shutdown command is ready, we should enable this one. //nodeCmd.AddCommand(shutdownCmd) + nodeCmd.AddCommand(p2pID) startCmd.Flags().StringVarP(&peerDial, "peer", "p", "", "Peer address to dial for initial connection") startCmd.Flags().StringVarP(&listenIP, "listen", "l", "", "Endpoint / REST address to listen on") diff --git a/cmd/goal/p2pid.go b/cmd/goal/p2pid.go new file mode 100644 index 0000000000..48914bde91 --- /dev/null +++ b/cmd/goal/p2pid.go @@ -0,0 +1,66 @@ +// Copyright (C) 2019-2024 Algorand, Inc. +// This file is part of go-algorand +// +// go-algorand is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// go-algorand 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with go-algorand. If not, see . + +// generate a new p2p private key and print out peerID to stdout + +package main + +import ( + "fmt" + "os" + "path" + + "github.com/algorand/go-algorand/cmd/util/datadir" + "github.com/algorand/go-algorand/config" + "github.com/algorand/go-algorand/network/p2p" + "github.com/algorand/go-algorand/util" + "github.com/libp2p/go-libp2p/core/peer" + "github.com/spf13/cobra" +) + +var p2pID = &cobra.Command{ + Use: "generate-p2pid", + Short: "Generate a new p2p private key", + Long: "Generate a new p2p private key (saved to " + p2p.DefaultPrivKeyPath + ") and print out peerID to stdout", + Args: validateNoPosArgsFn, + Run: func(cmd *cobra.Command, args []string) { + exist := false + datadir.OnDataDirs(func(dataDir string) { + privKeyPath := path.Join(dataDir, p2p.DefaultPrivKeyPath) + if util.FileExists(privKeyPath) { + exist = true + } + + peerKey, err := p2p.GetPrivKey(config.Local{P2PPersistPeerID: true}, dataDir) + if err != nil { + fmt.Fprintf(os.Stderr, "Error obtaining private key: %v\n", err) + os.Exit(1) + } + peerID, err := peer.IDFromPublicKey(peerKey.GetPublic()) + if err != nil { + fmt.Fprintf(os.Stderr, "Error obtaining peerID from a key: %v\n", err) + os.Exit(1) + } + + fmt.Printf("PeerID: %s\n", peerID.String()) + if !exist { + fmt.Printf("Private key saved to %s\n", privKeyPath) + } else { + fmt.Printf("Used existing key %s\n", privKeyPath) + } + }) + }, +} diff --git a/cmd/p2pid/main.go b/cmd/p2pid/main.go deleted file mode 100644 index 1bd6ab01f2..0000000000 --- a/cmd/p2pid/main.go +++ /dev/null @@ -1,69 +0,0 @@ -// Copyright (C) 2019-2024 Algorand, Inc. -// This file is part of go-algorand -// -// go-algorand is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// go-algorand 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with go-algorand. If not, see . - -// generate a new p2p private key and print out peerID to stdout - -package main - -import ( - "flag" - "fmt" - "os" - "path" - - "github.com/algorand/go-algorand/config" - "github.com/algorand/go-algorand/network/p2p" - "github.com/algorand/go-algorand/util" - "github.com/libp2p/go-libp2p/core/peer" -) - -var dataDirectory = flag.String( - "d", "", - "Optional root Algorand data path or a path to a directory where the private key will be stored.\n"+ - "Default directory is the current directory. Private key name is '"+p2p.DefaultPrivKeyPath+"'", -) - -func main() { - flag.Parse() - dataDir := *dataDirectory - if dataDir == "" { - dataDir = "." - } - - exist := false - privKeyPath := path.Join(dataDir, p2p.DefaultPrivKeyPath) - if util.FileExists(privKeyPath) { - exist = true - } - - peerKey, err := p2p.GetPrivKey(config.Local{P2PPersistPeerID: true}, dataDir) - if err != nil { - fmt.Fprintf(os.Stderr, "Error obtaining private key: %v\n", err) - os.Exit(1) - } - peerID, err := peer.IDFromPublicKey(peerKey.GetPublic()) - if err != nil { - fmt.Fprintf(os.Stderr, "Error obtaining peerID from a key: %v\n", err) - os.Exit(1) - } - - fmt.Printf("PeerID: %s\n", peerID.String()) - if !exist { - fmt.Printf("Private key saved to %s\n", privKeyPath) - } else { - fmt.Printf("Used existing key %s\n", privKeyPath) - } -}