github.com/Finschia/finschia-sdk@v0.48.1/types/bech32/legacybech32/pk.go (about) 1 // Deprecated: The module provides legacy bech32 functions which will be removed in a future 2 // release. 3 package legacybech32 4 5 import ( 6 "github.com/Finschia/finschia-sdk/codec/legacy" 7 cryptotypes "github.com/Finschia/finschia-sdk/crypto/types" 8 sdk "github.com/Finschia/finschia-sdk/types" 9 "github.com/Finschia/finschia-sdk/types/bech32" 10 ) 11 12 // TODO: when removing this package remove: 13 // + sdk:config.GetBech32AccountPubPrefix (and other related functions) 14 // + Bech32PrefixAccAddr and other related constants 15 16 // Deprecated: Bech32PubKeyType defines a string type alias for a Bech32 public key type. 17 type Bech32PubKeyType string 18 19 // Bech32 conversion constants 20 const ( 21 AccPK Bech32PubKeyType = "accpub" 22 ValPK Bech32PubKeyType = "valpub" 23 ConsPK Bech32PubKeyType = "conspub" 24 ) 25 26 // Deprecated: MarshalPubKey returns a Bech32 encoded string containing the appropriate 27 // prefix based on the key type provided for a given PublicKey. 28 func MarshalPubKey(pkt Bech32PubKeyType, pubkey cryptotypes.PubKey) (string, error) { 29 bech32Prefix := getPrefix(pkt) 30 return bech32.ConvertAndEncode(bech32Prefix, legacy.Cdc.MustMarshal(pubkey)) 31 } 32 33 // Deprecated: MustMarshalPubKey calls MarshalPubKey and panics on error. 34 func MustMarshalPubKey(pkt Bech32PubKeyType, pubkey cryptotypes.PubKey) string { 35 res, err := MarshalPubKey(pkt, pubkey) 36 if err != nil { 37 panic(err) 38 } 39 40 return res 41 } 42 43 func getPrefix(pkt Bech32PubKeyType) string { 44 cfg := sdk.GetConfig() 45 switch pkt { 46 case AccPK: 47 return cfg.GetBech32AccountPubPrefix() 48 49 case ValPK: 50 return cfg.GetBech32ValidatorPubPrefix() 51 case ConsPK: 52 return cfg.GetBech32ConsensusPubPrefix() 53 } 54 55 return "" 56 } 57 58 // Deprecated: UnmarshalPubKey returns a PublicKey from a bech32-encoded PublicKey with 59 // a given key type. 60 func UnmarshalPubKey(pkt Bech32PubKeyType, pubkeyStr string) (cryptotypes.PubKey, error) { 61 bech32Prefix := getPrefix(pkt) 62 63 bz, err := sdk.GetFromBech32(pubkeyStr, bech32Prefix) 64 if err != nil { 65 return nil, err 66 } 67 return legacy.PubKeyFromBytes(bz) 68 }