From a183cfea216171f240cb97c15ccb20b1e211cbc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BF=97=E5=AE=87?= Date: Fri, 27 Mar 2020 18:19:56 +1300 Subject: [PATCH] Added --secret-key,-s flag to gen-config. #262 --- cmd/skywire-cli/commands/visor/gen-config.go | 8 +++++++- pkg/visor/config.go | 9 +++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/cmd/skywire-cli/commands/visor/gen-config.go b/cmd/skywire-cli/commands/visor/gen-config.go index 7c3d435d71..24c84b2539 100644 --- a/cmd/skywire-cli/commands/visor/gen-config.go +++ b/cmd/skywire-cli/commands/visor/gen-config.go @@ -24,6 +24,7 @@ func init() { } var ( + sk cipher.SecKey output string replace bool retainKeys bool @@ -32,6 +33,7 @@ var ( ) func init() { + genConfigCmd.Flags().VarP(&sk, "secret-key", "s", "if unspecified, a random key pair will be generated.") genConfigCmd.Flags().StringVarP(&output, "output", "o", "", "path of output config file. Uses default of 'type' flag if unspecified.") genConfigCmd.Flags().BoolVarP(&replace, "replace", "r", false, "whether to allow rewrite of a file that already exists.") genConfigCmd.Flags().BoolVar(&retainKeys, "retain-keys", false, "retain current keys") @@ -107,7 +109,11 @@ func localConfig() *visor.Config { func defaultConfig() *visor.Config { conf := &visor.Config{} - conf.KeyPair = visor.NewKeyPair() + if sk.Null() { + conf.KeyPair = visor.NewKeyPair() + } else { + conf.KeyPair = visor.RestoreKeyPair(sk) + } stcp, err := visor.DefaultSTCPConfig() if err != nil { diff --git a/pkg/visor/config.go b/pkg/visor/config.go index 743453f128..27e2fdd0d7 100644 --- a/pkg/visor/config.go +++ b/pkg/visor/config.go @@ -351,6 +351,15 @@ func NewKeyPair() *KeyPair { } } +// RestoreKeyPair generates a key pair using just the secret key. +func RestoreKeyPair(sk cipher.SecKey) *KeyPair { + pk, err := sk.PubKey() + if err != nil { + panic(fmt.Errorf("failed to restore key pair: %v", err)) + } + return &KeyPair{PubKey: pk, SecKey: sk} +} + // DefaultSTCPConfig returns default STCP config. func DefaultSTCPConfig() (*snet.STCPConfig, error) { lIPaddr, err := getLocalIPAddress()