mirror of
https://github.com/0ceanSlim/grain.git
synced 2024-11-22 16:47:13 +00:00
28 lines
522 B
Go
28 lines
522 B
Go
|
package utils
|
||
|
|
||
|
import (
|
||
|
"encoding/hex"
|
||
|
"errors"
|
||
|
"strings"
|
||
|
|
||
|
"github.com/btcsuite/btcutil/bech32"
|
||
|
)
|
||
|
|
||
|
// DecodeNpub decodes a Bech32 encoded npub to its corresponding pubkey
|
||
|
func DecodeNpub(npub string) (string, error) {
|
||
|
hrp, data, err := bech32.Decode(npub)
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
if hrp != "npub" {
|
||
|
return "", errors.New("invalid hrp")
|
||
|
}
|
||
|
|
||
|
decodedData, err := bech32.ConvertBits(data, 5, 8, false)
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
|
||
|
return strings.ToLower(hex.EncodeToString(decodedData)), nil
|
||
|
}
|