github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/client/keys/utils.go (about) 1 package keys 2 3 import ( 4 "fmt" 5 "path/filepath" 6 7 "github.com/99designs/keyring" 8 "github.com/fibonacci-chain/fbc/libs/tendermint/libs/cli" 9 "github.com/spf13/viper" 10 "gopkg.in/yaml.v2" 11 12 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/flags" 13 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/crypto/keys" 14 ) 15 16 // available output formats. 17 const ( 18 OutputFormatText = "text" 19 OutputFormatJSON = "json" 20 21 // defaultKeyDBName is the client's subdirectory where keys are stored. 22 defaultKeyDBName = "keys" 23 ) 24 25 type bechKeyOutFn func(keyInfo keys.Info) (keys.KeyOutput, error) 26 27 // NewKeyBaseFromDir initializes a keybase at the rootDir directory. Keybase 28 // options can be applied when generating this new Keybase. 29 func NewKeyBaseFromDir(rootDir string, opts ...keys.KeybaseOption) (keys.Keybase, error) { 30 return getLazyKeyBaseFromDir(rootDir, opts...) 31 } 32 33 // NewInMemoryKeyBase returns a storage-less keybase. 34 func NewInMemoryKeyBase() keys.Keybase { return keys.NewInMemory() } 35 36 func getLazyKeyBaseFromDir(rootDir string, opts ...keys.KeybaseOption) (keys.Keybase, error) { 37 return keys.New(defaultKeyDBName, filepath.Join(rootDir, "keys"), opts...), nil 38 } 39 40 func printKeyInfo(keyInfo keys.Info, bechKeyOut bechKeyOutFn) { 41 ko, err := bechKeyOut(keyInfo) 42 if err != nil { 43 panic(err) 44 } 45 46 switch viper.Get(cli.OutputFlag) { 47 case OutputFormatText: 48 printTextInfos([]keys.KeyOutput{ko}) 49 50 case OutputFormatJSON: 51 var out []byte 52 var err error 53 if viper.GetBool(flags.FlagIndentResponse) { 54 out, err = KeysCdc.MarshalJSONIndent(ko, "", " ") 55 } else { 56 out, err = KeysCdc.MarshalJSON(ko) 57 } 58 if err != nil { 59 panic(err) 60 } 61 62 fmt.Println(string(out)) 63 } 64 } 65 66 func printInfos(infos []keys.Info) { 67 kos, err := keys.Bech32KeysOutput(infos) 68 if err != nil { 69 panic(err) 70 } 71 72 switch viper.Get(cli.OutputFlag) { 73 case OutputFormatText: 74 printTextInfos(kos) 75 76 case OutputFormatJSON: 77 var out []byte 78 var err error 79 80 if viper.GetBool(flags.FlagIndentResponse) { 81 out, err = KeysCdc.MarshalJSONIndent(kos, "", " ") 82 } else { 83 out, err = KeysCdc.MarshalJSON(kos) 84 } 85 86 if err != nil { 87 panic(err) 88 } 89 fmt.Printf("%s", out) 90 } 91 } 92 93 func printTextInfos(kos []keys.KeyOutput) { 94 out, err := yaml.Marshal(&kos) 95 if err != nil { 96 panic(err) 97 } 98 fmt.Println(string(out)) 99 } 100 101 func printKeyAddress(info keys.Info, bechKeyOut bechKeyOutFn) { 102 ko, err := bechKeyOut(info) 103 if err != nil { 104 panic(err) 105 } 106 107 fmt.Println(ko.Address) 108 } 109 110 func printPubKey(info keys.Info, bechKeyOut bechKeyOutFn) { 111 ko, err := bechKeyOut(info) 112 if err != nil { 113 panic(err) 114 } 115 116 fmt.Println(ko.PubKey) 117 } 118 119 func isRunningUnattended() bool { 120 backends := keyring.AvailableBackends() 121 return len(backends) == 2 && backends[1] == keyring.BackendType("file") 122 }