2020-05-12 09:53:30 -07:00
|
|
|
package bech32
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2020-07-23 09:53:47 -07:00
|
|
|
"github.com/enigmampc/btcutil/bech32"
|
2020-05-12 09:53:30 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// ConvertAndEncode converts from a base64 encoded byte string to base32 encoded byte string and then to bech32.
|
|
|
|
func ConvertAndEncode(hrp string, data []byte) (string, error) {
|
|
|
|
converted, err := bech32.ConvertBits(data, 8, 5, true)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("encoding bech32 failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return bech32.Encode(hrp, converted)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeAndConvert decodes a bech32 encoded string and converts to base64 encoded bytes.
|
|
|
|
func DecodeAndConvert(bech string) (string, []byte, error) {
|
2020-07-23 09:53:47 -07:00
|
|
|
hrp, data, err := bech32.Decode(bech, 1023)
|
2020-05-12 09:53:30 -07:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, fmt.Errorf("decoding bech32 failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
converted, err := bech32.ConvertBits(data, 5, 8, false)
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, fmt.Errorf("decoding bech32 failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return hrp, converted, nil
|
|
|
|
}
|