-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
account.go
361 lines (298 loc) · 9.2 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package types
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"strings"
"github.com/gogo/protobuf/proto"
"github.com/tendermint/tendermint/crypto"
"gopkg.in/yaml.v2"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
var (
_ AccountI = (*BaseAccount)(nil)
_ GenesisAccount = (*BaseAccount)(nil)
_ codectypes.UnpackInterfacesMessage = (*BaseAccount)(nil)
_ GenesisAccount = (*ModuleAccount)(nil)
_ ModuleAccountI = (*ModuleAccount)(nil)
)
// NewBaseAccount creates a new BaseAccount object
//nolint:interfacer
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() AccountI {
return &BaseAccount{}
}
// NewBaseAccountWithAddress - returns a new base account with a given address
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
}
func (acc BaseAccount) String() string {
out, _ := acc.MarshalYAML()
return out.(string)
}
// MarshalYAML returns the YAML representation of an account.
func (acc BaseAccount) MarshalYAML() (interface{}, error) {
bz, err := codec.MarshalYAML(codec.NewProtoCodec(codectypes.NewInterfaceRegistry()), &acc)
if err != nil {
return nil, err
}
return string(bz), err
}
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (acc BaseAccount) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
if acc.PubKey == nil {
return nil
}
var pubKey cryptotypes.PubKey
return unpacker.UnpackAny(acc.PubKey, &pubKey)
}
// NewModuleAddress creates an AccAddress from the hash of the module's name
func NewModuleAddress(name string) sdk.AccAddress {
return sdk.AccAddress(crypto.AddressHash([]byte(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 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 fmt.Errorf("not supported for module accounts")
}
// SetSequence - Implements AccountI
func (ma ModuleAccount) SetSequence(seq uint64) error {
return fmt.Errorf("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.Address != sdk.AccAddress(crypto.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" yaml:"address"`
PubKey string `json:"public_key" yaml:"public_key"`
AccountNumber uint64 `json:"account_number" yaml:"account_number"`
Sequence uint64 `json:"sequence" yaml:"sequence"`
Name string `json:"name" yaml:"name"`
Permissions []string `json:"permissions" yaml:"permissions"`
}
func (ma ModuleAccount) String() string {
out, _ := ma.MarshalYAML()
return out.(string)
}
// MarshalYAML returns the YAML representation of a ModuleAccount.
func (ma ModuleAccount) MarshalYAML() (interface{}, error) {
accAddr, err := sdk.AccAddressFromBech32(ma.Address)
if err != nil {
return nil, err
}
bs, err := yaml.Marshal(moduleAccountPretty{
Address: accAddr,
PubKey: "",
AccountNumber: ma.AccountNumber,
Sequence: ma.Sequence,
Name: ma.Name,
Permissions: ma.Permissions,
})
if err != nil {
return nil, err
}
return string(bs), nil
}
// 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.
type AccountI interface {
proto.Message
GetAddress() sdk.AccAddress
SetAddress(sdk.AccAddress) error // errors if already set.
GetPubKey() cryptotypes.PubKey // can return nil.
SetPubKey(cryptotypes.PubKey) error
GetAccountNumber() uint64
SetAccountNumber(uint64) error
GetSequence() uint64
SetSequence(uint64) error
// Ensure that account implements stringer
String() string
}
// ModuleAccountI defines an account interface for modules that hold tokens in
// an escrow.
type ModuleAccountI interface {
AccountI
GetName() string
GetPermissions() []string
HasPermission(string) bool
}
// 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 {
AccountI
Validate() error
}