-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: store private key in keyring or keyfile (#64)
* store private key in os keyring * --insecure flag for createaccount * improve secure flag * createaccount support keyfile * cleanup * working transfer with privkey, keyring, and keyfile * handle error * test file decryption * remove quotes that implied we aren't secure. was quoting because we're in the if branch that is ran when no `--insecure` flag exists, which means we're "secure" * capture all args for nicer just run usage * update just command comment * use astria-dusk-5 as chain id so remote will work * can only pass in one type of key flag
- Loading branch information
1 parent
89d32e3
commit 2c0f7ee
Showing
22 changed files
with
750 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package cmd | ||
|
||
import ( | ||
"os" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
// CreateDirOrPanic creates a directory with the given name with 0755 permissions. | ||
// If the directory can't be created, it will panic. | ||
func CreateDirOrPanic(dirName string) { | ||
err := os.MkdirAll(dirName, 0755) | ||
if err != nil { | ||
log.WithError(err).Error("Error creating data directory") | ||
panic(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package sequencer | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/astria/astria-cli-go/internal/keys" | ||
"github.com/astria/astria-cli-go/internal/sequencer" | ||
"github.com/pterm/pterm" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// GetPrivateKeyFromFlags retrieves the private key from the command flags. | ||
// If the 'privkey' flag is set, it returns the value of that flag. | ||
// If the 'keyring-address' flag is set, it calls the 'PrivateKeyFromKeyringAddress' function | ||
// to retrieve the private key from the keyring. | ||
// If the 'keyfile' flag is set, it calls the 'PrivateKeyFromKeyfile' function | ||
// to retrieve the private key from the keyfile. | ||
// If none of the flags are set or if the value of 'keyfile' is empty, it returns an error. | ||
// NOTE - this requires the flags `keyfile`, `keyring-address`, and `privkey` | ||
func GetPrivateKeyFromFlags(c *cobra.Command) (string, error) { | ||
keyfile := c.Flag("keyfile").Value.String() | ||
keyringAddress := c.Flag("keyring-address").Value.String() | ||
priv := c.Flag("privkey").Value.String() | ||
|
||
// NOTE - this isn't very secure but we still support it | ||
if priv != "" { | ||
return priv, nil | ||
} | ||
|
||
// NOTE - this should trigger user's os keyring password prompt | ||
if keyringAddress != "" { | ||
return PrivateKeyFromKeyringAddress(keyringAddress) | ||
} | ||
|
||
if keyfile != "" { | ||
return PrivateKeyFromKeyfile(keyfile) | ||
} | ||
|
||
return "", fmt.Errorf("no private key specified") | ||
} | ||
|
||
// PrivateKeyFromKeyfile retrieves the private key from the specified keyfile. | ||
func PrivateKeyFromKeyfile(keyfile string) (string, error) { | ||
kf, err := keys.ResolveKeyfilePath(keyfile) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
pwIn := pterm.DefaultInteractiveTextInput.WithMask("*") | ||
pw, _ := pwIn.Show("Account password:") | ||
|
||
privkey, err := keys.DecryptKeyfile(kf, pw) | ||
if err != nil { | ||
log.WithError(err).Error("Error decrypting keyfile") | ||
return "", err | ||
} | ||
account := sequencer.NewAccountFromPrivKey(privkey) | ||
return account.PrivateKeyString(), nil | ||
} | ||
|
||
// PrivateKeyFromKeyringAddress retrieves the private key from the keyring for a given keyring address. | ||
func PrivateKeyFromKeyringAddress(keyringAddress string) (string, error) { | ||
key, err := keys.GetKeyring(keyringAddress) | ||
if err != nil { | ||
log.WithError(err).Error("Error getting private key from keyring") | ||
return "", err | ||
} | ||
return key, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package sequencer | ||
|
||
import ( | ||
"github.com/astria/astria-cli-go/cmd" | ||
"github.com/astria/astria-cli-go/internal/keys" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var setKeyCmd = &cobra.Command{ | ||
Use: "setkey [address] [private key]", | ||
Short: "Set private key for an address in system keyring.", | ||
Args: cobra.ExactArgs(2), | ||
PreRun: cmd.SetLogLevel, | ||
Run: setKeyCmdHandler, | ||
} | ||
|
||
func setKeyCmdHandler(cmd *cobra.Command, args []string) { | ||
key := args[0] | ||
val := args[1] | ||
|
||
err := keys.StoreKeyring(key, val) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
var getKeyCmd = &cobra.Command{ | ||
Use: "getkey [address]", | ||
Short: "Get private key for an address in system keyring.", | ||
Args: cobra.ExactArgs(1), | ||
PreRun: cmd.SetLogLevel, | ||
Run: getKeyCmdHandler, | ||
} | ||
|
||
func getKeyCmdHandler(cmd *cobra.Command, args []string) { | ||
key := args[0] | ||
|
||
val, err := keys.GetKeyring(key) | ||
if err != nil { | ||
panic(err) | ||
} | ||
log.Infof("value: %s", val) | ||
} | ||
|
||
func init() { | ||
sequencerCmd.AddCommand(setKeyCmd) | ||
sequencerCmd.AddCommand(getKeyCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.