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.
34 lines
1.0 KiB
34 lines
1.0 KiB
package bech32
|
|
|
|
import (
|
|
"github.com/btcsuite/btcutil/bech32"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
// Charset Copied from https://github.com/btcsuite/btcutil/blob/9e5f4b9a998d263e3ce9c56664a7816001ac8000/bech32/bech32.go#L12
|
|
const Charset = "qpzry9x8gf2tvdw0s3jn54khce6mua7l"
|
|
|
|
// 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 "", errors.Wrap(err, "encoding bech32 failed")
|
|
}
|
|
return bech32.Encode(hrp, converted)
|
|
|
|
}
|
|
|
|
// DecodeAndConvert decodes a bech32 encoded string and converts to base64
|
|
// encoded bytes.
|
|
func DecodeAndConvert(bech string) (string, []byte, error) {
|
|
hrp, data, err := bech32.Decode(bech)
|
|
if err != nil {
|
|
return "", nil, errors.Wrap(err, "decoding bech32 failed")
|
|
}
|
|
converted, err := bech32.ConvertBits(data, 5, 8, false)
|
|
if err != nil {
|
|
return "", nil, errors.Wrap(err, "decoding bech32 failed")
|
|
}
|
|
return hrp, converted, nil
|
|
}
|
|
|