|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/hex" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + |
| 9 | + "github.com/spf13/cobra" |
| 10 | + |
| 11 | + rollconf "github.com/rollkit/rollkit/pkg/config" |
| 12 | + "github.com/rollkit/rollkit/pkg/signer/file" |
| 13 | +) |
| 14 | + |
| 15 | +// KeysCmd returns a command for managing keys. |
| 16 | +func KeysCmd() *cobra.Command { |
| 17 | + cmd := &cobra.Command{ |
| 18 | + Use: "keys", |
| 19 | + Short: "Manage signing keys", |
| 20 | + } |
| 21 | + |
| 22 | + cmd.AddCommand(exportKeyCmd()) |
| 23 | + cmd.AddCommand(importKeyCmd()) |
| 24 | + |
| 25 | + return cmd |
| 26 | +} |
| 27 | + |
| 28 | +func exportKeyCmd() *cobra.Command { |
| 29 | + cmd := &cobra.Command{ |
| 30 | + Use: "export", |
| 31 | + Short: "Export the private key to plain text", |
| 32 | + Long: `Export the locally saved signing key to plain text. |
| 33 | +This allows the key to be stored in a password manager or other secure storage. |
| 34 | +
|
| 35 | +WARNING: The exported key is not encrypted. Handle it with extreme care. |
| 36 | +Anyone with access to the exported key can sign messages on your behalf. |
| 37 | +`, |
| 38 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 39 | + nodeConfig, err := rollconf.Load(cmd) |
| 40 | + if err != nil { |
| 41 | + return fmt.Errorf("failed to load node config: %w", err) |
| 42 | + } |
| 43 | + |
| 44 | + passphrase, err := cmd.Flags().GetString(rollconf.FlagSignerPassphrase) |
| 45 | + if err != nil { |
| 46 | + return err |
| 47 | + } |
| 48 | + if passphrase == "" { |
| 49 | + return fmt.Errorf("passphrase is required. Please provide it using the --%s flag", rollconf.FlagSignerPassphrase) |
| 50 | + } |
| 51 | + |
| 52 | + keyPath := filepath.Join(nodeConfig.RootDir, "config") |
| 53 | + |
| 54 | + // Print a strong warning to stderr |
| 55 | + cmd.PrintErrln("WARNING: EXPORTING PRIVATE KEY. HANDLE WITH EXTREME CARE.") |
| 56 | + cmd.PrintErrln("ANYONE WITH ACCESS TO THIS KEY CAN SIGN MESSAGES ON YOUR BEHALF.") |
| 57 | + |
| 58 | + privKeyBytes, err := file.ExportPrivateKey(keyPath, []byte(passphrase)) |
| 59 | + if err != nil { |
| 60 | + return fmt.Errorf("failed to export private key: %w", err) |
| 61 | + } |
| 62 | + |
| 63 | + hexKey := hex.EncodeToString(privKeyBytes) |
| 64 | + cmd.Println(hexKey) |
| 65 | + |
| 66 | + return nil |
| 67 | + }, |
| 68 | + } |
| 69 | + cmd.Flags().String(rollconf.FlagSignerPassphrase, "", "Passphrase for the signer key") |
| 70 | + return cmd |
| 71 | +} |
| 72 | + |
| 73 | +func importKeyCmd() *cobra.Command { |
| 74 | + cmd := &cobra.Command{ |
| 75 | + Use: "import [hex-private-key]", |
| 76 | + Short: "Import a private key from plain text", |
| 77 | + Long: `Import a decrypted private key and generate the encrypted file locally. |
| 78 | +This allows the system to use it for signing. |
| 79 | +
|
| 80 | +The private key should be provided as a hex-encoded string. |
| 81 | +
|
| 82 | +WARNING: This command will overwrite any existing key. |
| 83 | +If a 'signer.json' file exists in your home directory, you must use the --force flag. |
| 84 | +`, |
| 85 | + Args: cobra.ExactArgs(1), |
| 86 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 87 | + nodeConfig, err := rollconf.Load(cmd) |
| 88 | + if err != nil { |
| 89 | + return fmt.Errorf("failed to load node config: %w", err) |
| 90 | + } |
| 91 | + |
| 92 | + passphrase, err := cmd.Flags().GetString(rollconf.FlagSignerPassphrase) |
| 93 | + if err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + if passphrase == "" { |
| 97 | + return fmt.Errorf("passphrase is required to encrypt the imported key. Please provide it using the --%s flag", rollconf.FlagSignerPassphrase) |
| 98 | + } |
| 99 | + |
| 100 | + hexKey := args[0] |
| 101 | + privKeyBytes, err := hex.DecodeString(hexKey) |
| 102 | + if err != nil { |
| 103 | + return fmt.Errorf("failed to decode hex private key: %w", err) |
| 104 | + } |
| 105 | + |
| 106 | + keyPath := filepath.Join(nodeConfig.RootDir, "config") |
| 107 | + filePath := filepath.Join(keyPath, "signer.json") |
| 108 | + |
| 109 | + // Check if file exists and if --force is used |
| 110 | + force, _ := cmd.Flags().GetBool("force") |
| 111 | + if _, err := os.Stat(filePath); err == nil && !force { |
| 112 | + return fmt.Errorf("key file already exists at %s. Use --force to overwrite", filePath) |
| 113 | + } |
| 114 | + |
| 115 | + if err := file.ImportPrivateKey(keyPath, privKeyBytes, []byte(passphrase)); err != nil { |
| 116 | + return fmt.Errorf("failed to import private key: %w", err) |
| 117 | + } |
| 118 | + |
| 119 | + cmd.Printf("Successfully imported key and saved to %s\n", filePath) |
| 120 | + return nil |
| 121 | + }, |
| 122 | + } |
| 123 | + |
| 124 | + cmd.Flags().Bool("force", false, "Overwrite existing key file if it exists") |
| 125 | + cmd.Flags().String(rollconf.FlagSignerPassphrase, "", "Passphrase to encrypt the imported key") |
| 126 | + return cmd |
| 127 | +} |
0 commit comments