-
Notifications
You must be signed in to change notification settings - Fork 342
/
bech32.go
62 lines (52 loc) · 1.33 KB
/
bech32.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package crypto
import (
"errors"
"fmt"
"github.com/gnolang/gno/tm2/pkg/amino"
"github.com/gnolang/gno/tm2/pkg/bech32"
)
func AddressToBech32(addr Address) string {
bech32Addr, err := bech32.Encode(Bech32AddrPrefix, addr[:])
if err != nil {
panic(err)
}
return bech32Addr
}
func AddressFromBech32(bech32str string) (Address, error) {
bz, err := GetFromBech32(bech32str, Bech32AddrPrefix)
if err != nil {
return Address{}, err
} else {
return AddressFromBytes(bz), nil
}
}
func PubKeyToBech32(pub PubKey) string {
bech32PubKey, err := bech32.Encode(Bech32PubKeyPrefix, pub.Bytes())
if err != nil {
panic(err)
}
return bech32PubKey
}
func PubKeyFromBech32(bech32str string) (pubKey PubKey, err error) {
bz, err := GetFromBech32(bech32str, Bech32PubKeyPrefix)
if err != nil {
return PubKey(nil), err
} else {
err = amino.Unmarshal(bz, &pubKey)
return
}
}
// GetFromBech32 decodes a bytestring from a Bech32 encoded string.
func GetFromBech32(bech32str, prefix string) ([]byte, error) {
if len(bech32str) == 0 {
return nil, errors.New("decoding Bech32 failed: must provide a valid bech32 string")
}
hrp, bz, err := bech32.DecodeAndConvert(bech32str)
if err != nil {
return nil, err
}
if hrp != prefix {
return nil, fmt.Errorf("invalid Bech32 prefix; expected %s, got %s", prefix, hrp)
}
return bz, nil
}