Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd: Add goal node subcommand to generate peer private key #6078

Merged
merged 6 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/goal/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
72 changes: 72 additions & 0 deletions cmd/goal/p2pid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// 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 <https://www.gnu.org/licenses/>.

// 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) {
anyError := false
datadir.OnDataDirs(func(dataDir string) {
jasonpaulos marked this conversation as resolved.
Show resolved Hide resolved
exist := false
privKeyPath := path.Join(dataDir, p2p.DefaultPrivKeyPath)
if util.FileExists(privKeyPath) {
exist = true

Check warning on line 45 in cmd/goal/p2pid.go

View check run for this annotation

Codecov / codecov/patch

cmd/goal/p2pid.go#L39-L45

Added lines #L39 - L45 were not covered by tests
}

peerKey, err := p2p.GetPrivKey(config.Local{P2PPersistPeerID: true}, dataDir)
if err != nil {
fmt.Fprintf(os.Stderr, "Error obtaining private key: %v\n", err)
anyError = true
return

Check warning on line 52 in cmd/goal/p2pid.go

View check run for this annotation

Codecov / codecov/patch

cmd/goal/p2pid.go#L48-L52

Added lines #L48 - L52 were not covered by tests
}
peerID, err := peer.IDFromPublicKey(peerKey.GetPublic())
if err != nil {
fmt.Fprintf(os.Stderr, "Error obtaining peerID from a key: %v\n", err)
anyError = true
return

Check warning on line 58 in cmd/goal/p2pid.go

View check run for this annotation

Codecov / codecov/patch

cmd/goal/p2pid.go#L54-L58

Added lines #L54 - L58 were not covered by tests
jasonpaulos marked this conversation as resolved.
Show resolved Hide resolved
}

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)

Check warning on line 65 in cmd/goal/p2pid.go

View check run for this annotation

Codecov / codecov/patch

cmd/goal/p2pid.go#L61-L65

Added lines #L61 - L65 were not covered by tests
}
})
if anyError {
os.Exit(1)

Check warning on line 69 in cmd/goal/p2pid.go

View check run for this annotation

Codecov / codecov/patch

cmd/goal/p2pid.go#L68-L69

Added lines #L68 - L69 were not covered by tests
}
},
}
Loading