|
@ -2,13 +2,14 @@ package bech32 |
|
|
|
|
|
|
|
|
import ( |
|
|
import ( |
|
|
"github.com/btcsuite/btcutil/bech32" |
|
|
"github.com/btcsuite/btcutil/bech32" |
|
|
|
|
|
"github.com/pkg/errors" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
//ConvertAndEncode converts from a base64 encoded byte string to base32 encoded byte string and then to bech32
|
|
|
//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) { |
|
|
func ConvertAndEncode(hrp string, data []byte) (string, error) { |
|
|
converted, err := bech32.ConvertBits(data, 8, 5, true) |
|
|
converted, err := bech32.ConvertBits(data, 8, 5, true) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return "", err |
|
|
|
|
|
|
|
|
return "", errors.Wrap(err, "encoding bech32 failed") |
|
|
} |
|
|
} |
|
|
return bech32.Encode(hrp, converted) |
|
|
return bech32.Encode(hrp, converted) |
|
|
|
|
|
|
|
@ -18,11 +19,11 @@ func ConvertAndEncode(hrp string, data []byte) (string, error) { |
|
|
func DecodeAndConvert(bech string) (string, []byte, error) { |
|
|
func DecodeAndConvert(bech string) (string, []byte, error) { |
|
|
hrp, data, err := bech32.Decode(bech) |
|
|
hrp, data, err := bech32.Decode(bech) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return "", nil, err |
|
|
|
|
|
|
|
|
return "", nil, errors.Wrap(err, "decoding bech32 failed") |
|
|
} |
|
|
} |
|
|
converted, err := bech32.ConvertBits(data, 5, 8, false) |
|
|
converted, err := bech32.ConvertBits(data, 5, 8, false) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return "", nil, err |
|
|
|
|
|
|
|
|
return "", nil, errors.Wrap(err, "decoding bech32 failed") |
|
|
} |
|
|
} |
|
|
return hrp, converted, nil |
|
|
return hrp, converted, nil |
|
|
} |
|
|
} |