You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

28 lines
736 B

  1. package bech32
  2. import (
  3. "github.com/btcsuite/btcutil/bech32"
  4. )
  5. //ConvertAndEncode converts from a base64 encoded byte string to base32 encoded byte string and then to bech32
  6. func ConvertAndEncode(hrp string, data []byte) (string, error) {
  7. converted, err := bech32.ConvertBits(data, 8, 5, true)
  8. if err != nil {
  9. return "", err
  10. }
  11. return bech32.Encode(hrp, converted)
  12. }
  13. //DecodeAndConvert decodes a bech32 encoded string and converts to base64 encoded bytes
  14. func DecodeAndConvert(bech string) (string, []byte, error) {
  15. hrp, data, err := bech32.Decode(bech)
  16. if err != nil {
  17. return "", nil, err
  18. }
  19. converted, err := bech32.ConvertBits(data, 5, 8, false)
  20. if err != nil {
  21. return "", nil, err
  22. }
  23. return hrp, converted, nil
  24. }