github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/cmd/client/keys.go (about) 1 package client 2 3 import ( 4 "bufio" 5 "encoding/hex" 6 "fmt" 7 "github.com/fibonacci-chain/fbc/libs/tendermint/p2p" 8 "io" 9 10 "github.com/spf13/cobra" 11 "github.com/spf13/viper" 12 13 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/flags" 14 clientkeys "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/keys" 15 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/crypto/keys" 16 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 17 18 "github.com/fibonacci-chain/fbc/app/crypto/hd" 19 ) 20 21 const ( 22 flagDryRun = "dry-run" 23 ) 24 25 // KeyCommands registers a sub-tree of commands to interact with 26 // local private key storage. 27 func KeyCommands() *cobra.Command { 28 cmd := &cobra.Command{ 29 Use: "keys", 30 Short: "Add or view local private keys", 31 Long: `Keys allows you to manage your local keystore for tendermint. 32 33 These keys may be in any format supported by go-crypto and can be 34 used by light-clients, full nodes, or any other application that 35 needs to sign with a private key.`, 36 } 37 38 // support adding Ethereum supported keys 39 addCmd := clientkeys.AddKeyCommand() 40 41 // update the default signing algorithm value to "eth_secp256k1" 42 algoFlag := addCmd.Flag("algo") 43 algoFlag.DefValue = string(hd.EthSecp256k1) 44 err := algoFlag.Value.Set(string(hd.EthSecp256k1)) 45 if err != nil { 46 panic(err) 47 } 48 addCmd.RunE = runAddCmd 49 50 cmd.AddCommand( 51 clientkeys.MnemonicKeyCommand(), 52 addCmd, 53 clientkeys.ExportKeyCommand(), 54 clientkeys.ImportKeyCommand(), 55 clientkeys.ListKeysCmd(), 56 clientkeys.ShowKeysCmd(), 57 flags.LineBreak, 58 clientkeys.DeleteKeyCommand(), 59 clientkeys.ParseKeyStringCommand(), 60 clientkeys.MigrateCommand(), 61 flags.LineBreak, 62 UnsafeExportEthKeyCommand(), 63 ExportEthKeyStoreCommand(), 64 ImportEthKeyStoreCommand(), 65 ExportEthCompCommand(), 66 extractNodeKey(), 67 ) 68 return cmd 69 } 70 71 func runAddCmd(cmd *cobra.Command, args []string) error { 72 inBuf := bufio.NewReader(cmd.InOrStdin()) 73 kb, err := getKeybase(viper.GetBool(flagDryRun), inBuf) 74 if err != nil { 75 return err 76 } 77 78 return clientkeys.RunAddCmd(cmd, args, kb, inBuf) 79 } 80 81 func getKeybase(transient bool, buf io.Reader) (keys.Keybase, error) { 82 if transient { 83 return keys.NewInMemory( 84 hd.EthSecp256k1Options()..., 85 ), nil 86 } 87 88 return keys.NewKeyring( 89 sdk.KeyringServiceName(), 90 viper.GetString(flags.FlagKeyringBackend), 91 viper.GetString(flags.FlagHome), 92 buf, 93 hd.EthSecp256k1Options()..., 94 ) 95 } 96 97 func extractNodeKey() *cobra.Command { 98 cmd := &cobra.Command{ 99 Use: "extract-node-key [filename] ", 100 Short: "extract current node key or from specificed file", 101 RunE: func(cmd *cobra.Command, args []string) error { 102 var filename string 103 if len(args) >= 1 { 104 filename = args[0] 105 } 106 nodekey, err := p2p.LoadNodeKey(filename) 107 if err != nil { 108 return err 109 } 110 111 //fmt.Printf("base64: %s\n", base64.StdEncoding.EncodeToString(nodekey.PubKey().Bytes())) 112 fmt.Printf("hex: %s\n", hex.EncodeToString(nodekey.PubKey().Bytes())) 113 114 return nil 115 }, 116 } 117 return cmd 118 }