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.

29 lines
878 B

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