-
Notifications
You must be signed in to change notification settings - Fork 35
/
address_util.go
75 lines (59 loc) · 1.96 KB
/
address_util.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
63
64
65
66
67
68
69
70
71
72
73
74
75
package cosmos
import (
"errors"
"strings"
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/bech32"
)
type Util struct {
AddressPrefix string
}
func NewUtilWithPrefix(addressPrefix string) *Util {
return &Util{addressPrefix}
}
// MARK - Implement the protocol wallet.Util
// @param publicKey can start with 0x or not.
func (u *Util) EncodePublicKeyToAddress(publicKey string) (string, error) {
return EncodePublicKeyToAddress(publicKey, u.AddressPrefix)
}
// @throw decode address to public key is not supported
func (u *Util) DecodeAddressToPublicKey(address string) (string, error) {
return "", errors.New("decode address to public key is not supported")
}
func (u *Util) IsValidAddress(address string) bool {
return IsValidAddress(address, u.AddressPrefix)
}
// MARK - like wallet.Util
// @param publicKey can start with 0x or not.
func EncodePublicKeyToAddress(publicKey string, addressPrefix string) (string, error) {
pubBytes, err := types.HexDecodeString(publicKey)
if err != nil {
return "", err
}
pubKey := secp256k1.PubKey{Key: pubBytes}
return Bech32FromAccAddress(pubKey.Address().Bytes(), addressPrefix)
}
// @param chainnet chain name
func IsValidAddress(address string, addressPrefix string) bool {
_, err := AccAddressFromBech32(address, addressPrefix)
return err == nil
}
func AccAddressFromBech32(address string, addressPrefix string) (sdk.AccAddress, error) {
if len(strings.TrimSpace(address)) == 0 {
return nil, errors.New("empty address string is not allowed")
}
bz, err := sdk.GetFromBech32(address, addressPrefix)
if err != nil {
return nil, err
}
err = sdk.VerifyAddressFormat(bz)
if err != nil {
return nil, err
}
return sdk.AccAddress(bz), nil
}
func Bech32FromAccAddress(address []byte, addressPrefix string) (string, error) {
return bech32.ConvertAndEncode(addressPrefix, address)
}