-
Notifications
You must be signed in to change notification settings - Fork 568
/
account.go
155 lines (128 loc) · 4.87 KB
/
account.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package types
import (
"encoding/json"
"regexp"
"strings"
crypto "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkaddress "github.com/cosmos/cosmos-sdk/types/address"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
yaml "gopkg.in/yaml.v2"
)
var (
_ authtypes.GenesisAccount = (*InterchainAccount)(nil)
_ InterchainAccountI = (*InterchainAccount)(nil)
)
// DefaultMaxAddrLength defines the default maximum character length used in validation of addresses
var DefaultMaxAddrLength = 128
// isValidAddr defines a regular expression to check if the provided string consists of
// strictly alphanumeric characters and is non empty.
var isValidAddr = regexp.MustCompile("^[a-zA-Z0-9]+$").MatchString
// InterchainAccountI wraps the authtypes.AccountI interface
type InterchainAccountI interface {
authtypes.AccountI
}
// interchainAccountPretty defines an unexported struct used for encoding the InterchainAccount details
type interchainAccountPretty struct {
Address sdk.AccAddress `json:"address" yaml:"address"`
PubKey string `json:"public_key" yaml:"public_key"`
AccountNumber uint64 `json:"account_number" yaml:"account_number"`
Sequence uint64 `json:"sequence" yaml:"sequence"`
AccountOwner string `json:"account_owner" yaml:"account_owner"`
}
// GenerateAddress returns an sdk.AccAddress derived using a host module account address, host connection ID, the controller portID,
// the current block app hash, and the current block data hash. The sdk.AccAddress returned is a sub-address of the host module account.
func GenerateAddress(ctx sdk.Context, connectionID, portID string) sdk.AccAddress {
hostModuleAcc := sdkaddress.Module(ModuleName, []byte(hostAccountsKey))
header := ctx.BlockHeader()
buf := []byte(connectionID + portID)
buf = append(buf, header.AppHash...)
buf = append(buf, header.DataHash...)
return sdkaddress.Derive(hostModuleAcc, buf)
}
// ValidateAccountAddress performs basic validation of interchain account addresses, enforcing constraints
// on address length and character set
func ValidateAccountAddress(addr string) error {
if !isValidAddr(addr) || len(addr) > DefaultMaxAddrLength {
return sdkerrors.Wrapf(
ErrInvalidAccountAddress,
"address must contain strictly alphanumeric characters, not exceeding %d characters in length",
DefaultMaxAddrLength,
)
}
return nil
}
// NewInterchainAccount creates and returns a new InterchainAccount type
func NewInterchainAccount(ba *authtypes.BaseAccount, accountOwner string) *InterchainAccount {
return &InterchainAccount{
BaseAccount: ba,
AccountOwner: accountOwner,
}
}
// SetPubKey implements the authtypes.AccountI interface
func (ia InterchainAccount) SetPubKey(pubKey crypto.PubKey) error {
return sdkerrors.Wrap(ErrUnsupported, "cannot set public key for interchain account")
}
// SetSequence implements the authtypes.AccountI interface
func (ia InterchainAccount) SetSequence(seq uint64) error {
return sdkerrors.Wrap(ErrUnsupported, "cannot set sequence number for interchain account")
}
// Validate implements basic validation of the InterchainAccount
func (ia InterchainAccount) Validate() error {
if strings.TrimSpace(ia.AccountOwner) == "" {
return sdkerrors.Wrap(ErrInvalidAccountAddress, "AccountOwner cannot be empty")
}
return ia.BaseAccount.Validate()
}
// String returns a string representation of the InterchainAccount
func (ia InterchainAccount) String() string {
out, _ := ia.MarshalYAML()
return string(out)
}
// MarshalYAML returns the YAML representation of the InterchainAccount
func (ia InterchainAccount) MarshalYAML() ([]byte, error) {
accAddr, err := sdk.AccAddressFromBech32(ia.Address)
if err != nil {
return nil, err
}
bz, err := yaml.Marshal(interchainAccountPretty{
Address: accAddr,
PubKey: "",
AccountNumber: ia.AccountNumber,
Sequence: ia.Sequence,
AccountOwner: ia.AccountOwner,
})
if err != nil {
return nil, err
}
return bz, nil
}
// MarshalJSON returns the JSON representation of the InterchainAccount
func (ia InterchainAccount) MarshalJSON() ([]byte, error) {
accAddr, err := sdk.AccAddressFromBech32(ia.Address)
if err != nil {
return nil, err
}
bz, err := json.Marshal(interchainAccountPretty{
Address: accAddr,
PubKey: "",
AccountNumber: ia.AccountNumber,
Sequence: ia.Sequence,
AccountOwner: ia.AccountOwner,
})
if err != nil {
return nil, err
}
return bz, nil
}
// UnmarshalJSON unmarshals raw JSON bytes into the InterchainAccount
func (ia *InterchainAccount) UnmarshalJSON(bz []byte) error {
var alias interchainAccountPretty
if err := json.Unmarshal(bz, &alias); err != nil {
return err
}
ia.BaseAccount = authtypes.NewBaseAccount(alias.Address, nil, alias.AccountNumber, alias.Sequence)
ia.AccountOwner = alias.AccountOwner
return nil
}