-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
account.go
312 lines (260 loc) · 8.17 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package types
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"strings"
gogoprotoany "github.com/cosmos/gogoproto/types/any"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
)
var (
_ sdk.AccountI = (*BaseAccount)(nil)
_ GenesisAccount = (*BaseAccount)(nil)
_ gogoprotoany.UnpackInterfacesMessage = (*BaseAccount)(nil)
_ GenesisAccount = (*ModuleAccount)(nil)
_ sdk.ModuleAccountI = (*ModuleAccount)(nil)
)
// NewBaseAccount creates a new BaseAccount object.
func NewBaseAccount(address sdk.AccAddress, pubKey cryptotypes.PubKey, accountNumber, sequence uint64) *BaseAccount {
acc := &BaseAccount{
Address: address.String(),
AccountNumber: accountNumber,
Sequence: sequence,
}
err := acc.SetPubKey(pubKey)
if err != nil {
panic(err)
}
return acc
}
// ProtoBaseAccount - a prototype function for BaseAccount
func ProtoBaseAccount() sdk.AccountI {
return &BaseAccount{}
}
// NewBaseAccountWithAddress - returns a new base account with a given address
// leaving AccountNumber and Sequence to zero.
func NewBaseAccountWithAddress(addr sdk.AccAddress) *BaseAccount {
return &BaseAccount{
Address: addr.String(),
}
}
// GetAddress - Implements sdk.AccountI.
func (acc BaseAccount) GetAddress() sdk.AccAddress {
addr, _ := sdk.AccAddressFromBech32(acc.Address)
return addr
}
// SetAddress - Implements sdk.AccountI.
func (acc *BaseAccount) SetAddress(addr sdk.AccAddress) error {
if len(acc.Address) != 0 {
return errors.New("cannot override BaseAccount address")
}
acc.Address = addr.String()
return nil
}
// GetPubKey - Implements sdk.AccountI.
func (acc BaseAccount) GetPubKey() (pk cryptotypes.PubKey) {
if acc.PubKey == nil {
return nil
}
content, ok := acc.PubKey.GetCachedValue().(cryptotypes.PubKey)
if !ok {
return nil
}
return content
}
// SetPubKey - Implements sdk.AccountI.
func (acc *BaseAccount) SetPubKey(pubKey cryptotypes.PubKey) error {
if pubKey == nil {
acc.PubKey = nil
return nil
}
any, err := codectypes.NewAnyWithValue(pubKey)
if err == nil {
acc.PubKey = any
}
return err
}
// GetAccountNumber - Implements AccountI
func (acc BaseAccount) GetAccountNumber() uint64 {
return acc.AccountNumber
}
// SetAccountNumber - Implements AccountI
func (acc *BaseAccount) SetAccountNumber(accNumber uint64) error {
acc.AccountNumber = accNumber
return nil
}
// GetSequence - Implements sdk.AccountI.
func (acc BaseAccount) GetSequence() uint64 {
return acc.Sequence
}
// SetSequence - Implements sdk.AccountI.
func (acc *BaseAccount) SetSequence(seq uint64) error {
acc.Sequence = seq
return nil
}
// Validate checks for errors on the account fields
func (acc BaseAccount) Validate() error {
if acc.Address == "" || acc.PubKey == nil {
return nil
}
accAddr, err := sdk.AccAddressFromBech32(acc.Address)
if err != nil {
return err
}
if !bytes.Equal(acc.GetPubKey().Address().Bytes(), accAddr.Bytes()) {
return errors.New("account address and pubkey address do not match")
}
return nil
}
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (acc BaseAccount) UnpackInterfaces(unpacker gogoprotoany.AnyUnpacker) error {
if acc.PubKey == nil {
return nil
}
var pubKey cryptotypes.PubKey
return unpacker.UnpackAny(acc.PubKey, &pubKey)
}
// NewModuleAddressOrBech32Address gets an input string and returns an AccAddress.
// If the input is a valid address, it returns the address.
// If the input is a module name, it returns the module address.
func NewModuleAddressOrBech32Address(input string) sdk.AccAddress {
if addr, err := sdk.AccAddressFromBech32(input); err == nil {
return addr
}
return NewModuleAddress(input)
}
// NewModuleAddress creates an AccAddress from the hash of the module's name
func NewModuleAddress(name string) sdk.AccAddress {
return address.Module(name)
}
// NewEmptyModuleAccount creates a empty ModuleAccount from a string
func NewEmptyModuleAccount(name string, permissions ...string) *ModuleAccount {
moduleAddress := NewModuleAddress(name)
baseAcc := NewBaseAccountWithAddress(moduleAddress)
if err := validatePermissions(permissions...); err != nil {
panic(err)
}
return &ModuleAccount{
BaseAccount: baseAcc,
Name: name,
Permissions: permissions,
}
}
// NewModuleAccount creates a new ModuleAccount instance
func NewModuleAccount(ba *BaseAccount, name string, permissions ...string) *ModuleAccount {
if err := validatePermissions(permissions...); err != nil {
panic(err)
}
return &ModuleAccount{
BaseAccount: ba,
Name: name,
Permissions: permissions,
}
}
// HasPermission returns whether or not the module account has permission.
func (ma ModuleAccount) HasPermission(permission string) bool {
for _, perm := range ma.Permissions {
if perm == permission {
return true
}
}
return false
}
// GetName returns the name of the holder's module
func (ma ModuleAccount) GetName() string {
return ma.Name
}
// GetPermissions returns permissions granted to the module account
func (ma ModuleAccount) GetPermissions() []string {
return ma.Permissions
}
// SetPubKey - Implements AccountI
func (ma ModuleAccount) SetPubKey(pubKey cryptotypes.PubKey) error {
return errors.New("not supported for module accounts")
}
// Validate checks for errors on the account fields
func (ma ModuleAccount) Validate() error {
if strings.TrimSpace(ma.Name) == "" {
return errors.New("module account name cannot be blank")
}
if ma.BaseAccount == nil {
return errors.New("uninitialized ModuleAccount: BaseAccount is nil")
}
if ma.Address != sdk.AccAddress(AddressHash([]byte(ma.Name))).String() {
return fmt.Errorf("address %s cannot be derived from the module name '%s'", ma.Address, ma.Name)
}
return ma.BaseAccount.Validate()
}
type moduleAccountPretty struct {
Address sdk.AccAddress `json:"address"`
PubKey string `json:"public_key"`
AccountNumber uint64 `json:"account_number"`
Sequence uint64 `json:"sequence"`
Name string `json:"name"`
Permissions []string `json:"permissions"`
}
// MarshalJSON returns the JSON representation of a ModuleAccount.
func (ma ModuleAccount) MarshalJSON() ([]byte, error) {
accAddr, err := sdk.AccAddressFromBech32(ma.Address)
if err != nil {
return nil, err
}
return json.Marshal(moduleAccountPretty{
Address: accAddr,
PubKey: "",
AccountNumber: ma.AccountNumber,
Sequence: ma.Sequence,
Name: ma.Name,
Permissions: ma.Permissions,
})
}
// UnmarshalJSON unmarshals raw JSON bytes into a ModuleAccount.
func (ma *ModuleAccount) UnmarshalJSON(bz []byte) error {
var alias moduleAccountPretty
if err := json.Unmarshal(bz, &alias); err != nil {
return err
}
ma.BaseAccount = NewBaseAccount(alias.Address, nil, alias.AccountNumber, alias.Sequence)
ma.Name = alias.Name
ma.Permissions = alias.Permissions
return nil
}
// AccountI is an interface used to store coins at a given address within state.
// It presumes a notion of sequence numbers for replay protection,
// a notion of account numbers for replay protection for previously pruned accounts,
// and a pubkey for authentication purposes.
//
// Many complex conditions can be used in the concrete struct which implements AccountI.
//
// Deprecated: Use `AccountI` from types package instead.
type AccountI interface {
sdk.AccountI
}
// ModuleAccountI defines an account interface for modules that hold tokens in
// an escrow.
//
// Deprecated: Use `ModuleAccountI` from types package instead.
type ModuleAccountI interface {
sdk.ModuleAccountI
}
// GenesisAccounts defines a slice of GenesisAccount objects
type GenesisAccounts []GenesisAccount
// Contains returns true if the given address exists in a slice of GenesisAccount
// objects.
func (ga GenesisAccounts) Contains(addr sdk.Address) bool {
for _, acc := range ga {
if acc.GetAddress().Equals(addr) {
return true
}
}
return false
}
// GenesisAccount defines a genesis account that embeds an AccountI with validation capabilities.
type GenesisAccount interface {
sdk.AccountI
Validate() error
}