github.com/Finschia/finschia-sdk@v0.48.1/types/bech32/bech32.go (about) 1 package bech32 2 3 import ( 4 "fmt" 5 6 "github.com/cosmos/btcutil/bech32" 7 ) 8 9 // ConvertAndEncode converts from a base64 encoded byte string to base32 encoded byte string and then to bech32. 10 func ConvertAndEncode(hrp string, data []byte) (string, error) { 11 converted, err := bech32.ConvertBits(data, 8, 5, true) 12 if err != nil { 13 return "", fmt.Errorf("encoding bech32 failed: %w", err) 14 } 15 16 return bech32.Encode(hrp, converted) 17 } 18 19 // DecodeAndConvert decodes a bech32 encoded string and converts to base64 encoded bytes. 20 func DecodeAndConvert(bech string) (string, []byte, error) { 21 hrp, data, err := bech32.Decode(bech, 1023) 22 if err != nil { 23 return "", nil, fmt.Errorf("decoding bech32 failed: %w", err) 24 } 25 26 converted, err := bech32.ConvertBits(data, 5, 8, false) 27 if err != nil { 28 return "", nil, fmt.Errorf("decoding bech32 failed: %w", err) 29 } 30 31 return hrp, converted, nil 32 }