/
utils.go
81 lines (68 loc) · 2.36 KB
/
utils.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
76
77
78
79
80
81
package types
import (
"strings"
"github.com/evmos/ethermint/crypto/ethsecp256k1"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/crypto/types/multisig"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
const (
// MainnetChainID defines the canto EIP155 chain ID for mainnet
MainnetChainID = "canto_9001"
// TestnetChainID defines the canto EIP155 chain ID for testnet
TestnetChainID = "canto_9000"
)
// IsMainnet returns true if the chain-id has the canto mainnet EIP155 chain prefix.
func IsMainnet(chainID string) bool {
return strings.HasPrefix(chainID, MainnetChainID)
}
// IsTestnet returns true if the chain-id has the canto testnet EIP155 chain prefix.
func IsTestnet(chainID string) bool {
return strings.HasPrefix(chainID, TestnetChainID)
}
// IsSupportedKey returns true if the pubkey type is supported by the chain
// (i.e eth_secp256k1, amino multisig, ed25519).
// NOTE: Nested multisigs are not supported.
func IsSupportedKey(pubkey cryptotypes.PubKey) bool {
switch pubkey := pubkey.(type) {
case *ethsecp256k1.PubKey, *ed25519.PubKey:
return true
case multisig.PubKey:
if len(pubkey.GetPubKeys()) == 0 {
return false
}
for _, pk := range pubkey.GetPubKeys() {
switch pk.(type) {
case *ethsecp256k1.PubKey, *ed25519.PubKey:
continue
default:
// Nested multisigs are unsupported
return false
}
}
return true
default:
return false
}
}
// GetcantoAddressFromBech32 returns the sdk.Account address of given address,
// while also changing bech32 human readable prefix (HRP) to the value set on
// the global sdk.Config (eg: `canto`).
// The function fails if the provided bech32 address is invalid.
func GetcantoAddressFromBech32(address string) (sdk.AccAddress, error) {
bech32Prefix := strings.SplitN(address, "1", 2)[0]
if bech32Prefix == address {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid bech32 address: %s", address)
}
addressBz, err := sdk.GetFromBech32(address, bech32Prefix)
if err != nil {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid address %s, %s", address, err.Error())
}
// safety check: shouldn't happen
if err := sdk.VerifyAddressFormat(addressBz); err != nil {
return nil, err
}
return sdk.AccAddress(addressBz), nil
}