mirror of
https://github.com/hyprspace/hyprspace.git
synced 2026-09-12 19:51:07 +05:00
cli: add keygen command (#118)
* cli: factor out key generation * cli: add keygen command
This commit is contained in:
parent
105806fa99
commit
9b2d97f96b
11
cli/init.go
11
cli/init.go
@ -8,8 +8,6 @@ import (
|
||||
|
||||
"github.com/DataDrake/cli-ng/v2/cmd"
|
||||
"github.com/hyprspace/hyprspace/schema"
|
||||
"github.com/libp2p/go-libp2p/core/crypto"
|
||||
"github.com/libp2p/go-libp2p/core/peer"
|
||||
"github.com/multiformats/go-multibase"
|
||||
)
|
||||
|
||||
@ -39,10 +37,7 @@ func InitRun(r *cmd.Root, c *cmd.Sub) {
|
||||
configPath = "/etc/hyprspace/" + ifName + ".json"
|
||||
}
|
||||
|
||||
privKey, _, err := crypto.GenerateKeyPair(crypto.Ed25519, 256)
|
||||
checkErr(err)
|
||||
|
||||
keyBytes, err := crypto.MarshalPrivateKey(privKey)
|
||||
keyBytes, peerId, err := GenerateKeyPair()
|
||||
checkErr(err)
|
||||
|
||||
// Setup an initial default config.
|
||||
@ -72,8 +67,7 @@ func InitRun(r *cmd.Root, c *cmd.Sub) {
|
||||
checkErr(err)
|
||||
|
||||
fmt.Printf("Initialized new config at %s\n", configPath)
|
||||
peerId, err := peer.IDFromPrivateKey(privKey)
|
||||
if err == nil {
|
||||
|
||||
fmt.Println("Add this entry to your other peers:")
|
||||
fmt.Println("{")
|
||||
name := c.Flags.(*InitFlags).Name
|
||||
@ -89,4 +83,3 @@ func InitRun(r *cmd.Root, c *cmd.Sub) {
|
||||
fmt.Printf(" \"id\": \"%s\"\n", peerId)
|
||||
fmt.Println("}")
|
||||
}
|
||||
}
|
||||
|
||||
50
cli/keygen.go
Normal file
50
cli/keygen.go
Normal file
@ -0,0 +1,50 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/DataDrake/cli-ng/v2/cmd"
|
||||
"github.com/multiformats/go-multibase"
|
||||
)
|
||||
|
||||
type KeygenFlags struct {
|
||||
PrivateKeyFile string `long:"private-key-file" desc:"Path to private key file."`
|
||||
PublicKeyFile string `long:"public-key-file" desc:"Path to public key file. If not specified, prints to stdout."`
|
||||
}
|
||||
|
||||
// Keygen creates a new keypair and writes each key to a file.
|
||||
var Keygen = cmd.Sub{
|
||||
Name: "keygen",
|
||||
Short: "Generate a new keypair",
|
||||
Flags: &KeygenFlags{},
|
||||
Run: KeygenRun,
|
||||
}
|
||||
|
||||
func KeygenRun(r *cmd.Root, c *cmd.Sub) {
|
||||
flags := c.Flags.(*KeygenFlags)
|
||||
|
||||
if flags.PrivateKeyFile == "" {
|
||||
log.Fatal("--private-key-file is required")
|
||||
}
|
||||
|
||||
keyBytes, peerId, err := GenerateKeyPair()
|
||||
checkErr(err)
|
||||
|
||||
encoded := multibase.MustNewEncoder(multibase.Base58BTC).Encode(keyBytes)
|
||||
|
||||
err = os.WriteFile(flags.PrivateKeyFile, []byte(encoded), 0600)
|
||||
checkErr(err)
|
||||
|
||||
fmt.Fprintf(os.Stderr, "Private key written to %s\n", flags.PrivateKeyFile)
|
||||
|
||||
if flags.PublicKeyFile != "" {
|
||||
err = os.WriteFile(flags.PublicKeyFile, []byte(peerId.String()), 0644)
|
||||
checkErr(err)
|
||||
|
||||
fmt.Fprintf(os.Stderr, "Public key written to %s\n", flags.PublicKeyFile)
|
||||
} else {
|
||||
fmt.Println(peerId.String())
|
||||
}
|
||||
}
|
||||
@ -30,6 +30,7 @@ func init() {
|
||||
|
||||
cmd.Register(&cmd.Help)
|
||||
cmd.Register(&Init)
|
||||
cmd.Register(&Keygen)
|
||||
cmd.Register(&Up)
|
||||
cmd.Register(&Status)
|
||||
cmd.Register(&Peers)
|
||||
|
||||
23
cli/util.go
23
cli/util.go
@ -4,6 +4,8 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/libp2p/go-libp2p/core/crypto"
|
||||
"github.com/libp2p/go-libp2p/core/peer"
|
||||
"github.com/mattn/go-isatty"
|
||||
)
|
||||
|
||||
@ -16,3 +18,24 @@ func printListF(strings []string, handler func(string) string) {
|
||||
func isTTY() bool {
|
||||
return isatty.IsTerminal(os.Stdout.Fd())
|
||||
}
|
||||
|
||||
func GenerateKeyPair() ([]byte, peer.ID, error) {
|
||||
var zeroID peer.ID
|
||||
|
||||
privKey, _, err := crypto.GenerateKeyPair(crypto.Ed25519, 256)
|
||||
if err != nil {
|
||||
return nil, zeroID, err
|
||||
}
|
||||
|
||||
keyBytes, err := crypto.MarshalPrivateKey(privKey)
|
||||
if err != nil {
|
||||
return nil, zeroID, err
|
||||
}
|
||||
|
||||
peerId, err := peer.IDFromPrivateKey(privKey)
|
||||
if err != nil {
|
||||
return nil, zeroID, err
|
||||
}
|
||||
|
||||
return keyBytes, peerId, nil
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user