/
account.go
414 lines (351 loc) · 10.3 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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package types
import (
"encoding/json"
"fmt"
"strings"
"time"
"github.com/cosmos/cosmos-sdk/x/auth/vesting/exported"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/ghodss/yaml"
"github.com/gogo/protobuf/proto"
)
var (
_ authtypes.AccountI = (*Profile)(nil)
_ authtypes.GenesisAccount = (*Profile)(nil)
_ exported.VestingAccount = (*Profile)(nil)
)
// NewProfile builds a new profile having the given DTag, creator and creation date
func NewProfile(
dTag string, nickname, bio string, pictures Pictures, creationDate time.Time,
account authtypes.AccountI,
) (*Profile, error) {
// Make sure myAccount is a proto.Message, e.g. a BaseAccount etc.
protoAccount, ok := account.(proto.Message)
if !ok {
return nil, fmt.Errorf("the given account cannot be serialized using Protobuf")
}
myAccountAny, err := codectypes.NewAnyWithValue(protoAccount)
if err != nil {
return nil, err
}
return &Profile{
DTag: dTag,
Nickname: nickname,
Bio: bio,
Pictures: pictures,
CreationDate: creationDate,
Account: myAccountAny,
}, nil
}
// NewProfileFromAccount allows to build a new Profile instance from a provided DTag, and account and a creation time
func NewProfileFromAccount(dTag string, account authtypes.AccountI, creationTime time.Time) (*Profile, error) {
return NewProfile(
dTag,
"",
"",
NewPictures("", ""),
creationTime,
account,
)
}
// GetAccount returns the underlying account as an authtypes.AccountI instance
func (p *Profile) GetAccount() authtypes.AccountI {
return p.Account.GetCachedValue().(authtypes.AccountI)
}
// getVestingAccount returns the underlying account as an exported.VestingAccount instance
func (p *Profile) getVestingAccount() exported.VestingAccount {
acc, ok := p.Account.GetCachedValue().(exported.VestingAccount)
if !ok {
return nil
}
return acc
}
// setAccount sets the given account as the underlying account instance.
// This should be called after updating anything about the account (eg. after calling SetSequence).
func (p *Profile) setAccount(account authtypes.AccountI) error {
accAny, err := codectypes.NewAnyWithValue(account)
if err != nil {
return err
}
p.Account = accAny
return nil
}
// GetAddress implements authtypes.AccountI
func (p *Profile) GetAddress() sdk.AccAddress {
return p.GetAccount().GetAddress()
}
// SetAddress implements authtypes.AccountI
func (p *Profile) SetAddress(addr sdk.AccAddress) error {
acc := p.GetAccount()
err := acc.SetAddress(addr)
if err != nil {
return err
}
return p.setAccount(acc)
}
// GetPubKey implements authtypes.AccountI
func (p *Profile) GetPubKey() cryptotypes.PubKey {
return p.GetAccount().GetPubKey()
}
// SetPubKey implements authtypes.AccountI
func (p *Profile) SetPubKey(pubKey cryptotypes.PubKey) error {
acc := p.GetAccount()
err := acc.SetPubKey(pubKey)
if err != nil {
return err
}
return p.setAccount(acc)
}
// GetAccountNumber implements authtypes.AccountI
func (p *Profile) GetAccountNumber() uint64 {
return p.GetAccount().GetAccountNumber()
}
// SetAccountNumber implements authtypes.AccountI
func (p *Profile) SetAccountNumber(accountNumber uint64) error {
acc := p.GetAccount()
err := acc.SetAccountNumber(accountNumber)
if err != nil {
return err
}
return p.setAccount(acc)
}
// GetSequence implements authtypes.AccountI
func (p *Profile) GetSequence() uint64 {
return p.GetAccount().GetSequence()
}
// SetSequence implements authtypes.AccountI
func (p *Profile) SetSequence(sequence uint64) error {
acc := p.GetAccount()
err := acc.SetSequence(sequence)
if err != nil {
return err
}
return p.setAccount(acc)
}
// LockedCoins implements exported.VestingAccount
func (p *Profile) LockedCoins(blockTime time.Time) sdk.Coins {
acc := p.getVestingAccount()
if acc == nil {
return sdk.NewCoins()
}
return acc.LockedCoins(blockTime)
}
// TrackDelegation implements exported.VestingAccount
func (p *Profile) TrackDelegation(blockTime time.Time, balance, amount sdk.Coins) {
acc := p.getVestingAccount()
if acc == nil {
return
}
acc.TrackDelegation(blockTime, balance, amount)
err := p.setAccount(acc)
if err != nil {
panic(err)
}
}
// TrackUndelegation implements exported.VestingAccount
func (p *Profile) TrackUndelegation(amount sdk.Coins) {
acc := p.getVestingAccount()
if acc == nil {
return
}
acc.TrackUndelegation(amount)
err := p.setAccount(acc)
if err != nil {
panic(err)
}
}
// GetVestedCoins implements exported.VestingAccount
func (p *Profile) GetVestedCoins(blockTime time.Time) sdk.Coins {
acc := p.getVestingAccount()
if acc == nil {
return sdk.NewCoins()
}
return acc.GetVestedCoins(blockTime)
}
// GetVestingCoins implements exported.VestingAccount
func (p *Profile) GetVestingCoins(blockTime time.Time) sdk.Coins {
acc := p.getVestingAccount()
if acc == nil {
return sdk.NewCoins()
}
return acc.GetVestingCoins(blockTime)
}
// GetStartTime implements exported.VestingAccount
func (p *Profile) GetStartTime() int64 {
acc := p.getVestingAccount()
if acc == nil {
return -1
}
return acc.GetStartTime()
}
// GetEndTime implements exported.VestingAccount
func (p *Profile) GetEndTime() int64 {
acc := p.getVestingAccount()
if acc == nil {
return -1
}
return acc.GetEndTime()
}
// GetOriginalVesting implements exported.VestingAccount
func (p *Profile) GetOriginalVesting() sdk.Coins {
acc := p.getVestingAccount()
if acc == nil {
return sdk.NewCoins()
}
return acc.GetOriginalVesting()
}
// GetDelegatedFree implements exported.VestingAccount
func (p *Profile) GetDelegatedFree() sdk.Coins {
acc := p.getVestingAccount()
if acc == nil {
return sdk.NewCoins()
}
return acc.GetDelegatedFree()
}
// GetDelegatedVesting implements exported.VestingAccount
func (p *Profile) GetDelegatedVesting() sdk.Coins {
acc := p.getVestingAccount()
if acc == nil {
return sdk.NewCoins()
}
return acc.GetDelegatedVesting()
}
// UnpackInterfaces implements codectypes.UnpackInterfacesMessage
func (p *Profile) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
if p.Account != nil {
var account authtypes.AccountI
err := unpacker.UnpackAny(p.Account, &account)
if err != nil {
return err
}
}
return nil
}
// Validate check the validity of the Profile
func (p *Profile) Validate() error {
if strings.TrimSpace(p.DTag) == "" || p.DTag == DoNotModify {
return fmt.Errorf("invalid profile DTag: %s", p.DTag)
}
if p.Nickname == DoNotModify {
return fmt.Errorf("invalid profile nickname: %s", p.Nickname)
}
if p.Bio == DoNotModify {
return fmt.Errorf("invalid profile bio: %s", p.Bio)
}
if p.Pictures.Profile == DoNotModify {
return fmt.Errorf("invalid profile picture: %s", p.Pictures.Profile)
}
if p.Pictures.Cover == DoNotModify {
return fmt.Errorf("invalid profile cover: %s", p.Pictures.Cover)
}
if len(p.GetAddress()) == 0 {
return fmt.Errorf("invalid address: %s", p.GetAddress().String())
}
return p.Pictures.Validate()
}
// -------------------------------------------------------------------------------------------------------------------
type profilePretty 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"`
DTag string `json:"dtag" yaml:"dtag"`
Nickname string `json:"nickname" yaml:"nickname"`
Bio string `json:"bio" yaml:"bio"`
Pictures Pictures `json:"pictures" yaml:"pictures"`
CreationDate time.Time `json:"creation_date" yaml:"creation_date"`
}
// String implements authtypes.AccountI implements stringer
func (p *Profile) String() string {
out, _ := p.MarshalYAML()
return out.(string)
}
// MarshalYAML returns the YAML representation of a Profile.
func (p *Profile) MarshalYAML() (interface{}, error) {
bs, err := yaml.Marshal(profilePretty{
Address: p.GetAddress(),
PubKey: p.GetPubKey().String(),
AccountNumber: p.GetAccountNumber(),
Sequence: p.GetSequence(),
DTag: p.DTag,
Nickname: p.Nickname,
Bio: p.Bio,
Pictures: p.Pictures,
CreationDate: p.CreationDate,
})
if err != nil {
return nil, err
}
return string(bs), nil
}
// MarshalJSON returns the JSON representation of a Profile.
func (p *Profile) MarshalJSON() ([]byte, error) {
var pubKey = ""
if p.GetPubKey() != nil {
pubKey = p.GetPubKey().String()
}
return json.Marshal(profilePretty{
Address: p.GetAddress(),
PubKey: pubKey,
AccountNumber: p.GetAccountNumber(),
Sequence: p.GetSequence(),
DTag: p.DTag,
Nickname: p.Nickname,
Bio: p.Bio,
Pictures: p.Pictures,
CreationDate: p.CreationDate,
})
}
// -------------------------------------------------------------------------------------------------------------------
// ProfileUpdate contains all the data that can be updated about a profile.
// When performing an update, if a field should not be edited then it must be set to types.DoNotModify
type ProfileUpdate struct {
DTag string
Nickname string
Bio string
Pictures Pictures
}
// NewProfileUpdate builds a new ProfileUpdate instance containing the given data
func NewProfileUpdate(dTag, nickname, bio string, pictures Pictures) *ProfileUpdate {
return &ProfileUpdate{
DTag: dTag,
Nickname: nickname,
Bio: bio,
Pictures: pictures,
}
}
// Update updates the fields of a given profile without validating it.
// Before storing the updated profile, a validation with Validate() should
// be performed.
func (p *Profile) Update(update *ProfileUpdate) (*Profile, error) {
if update.DTag == DoNotModify {
update.DTag = p.DTag
}
if update.Nickname == DoNotModify {
update.Nickname = p.Nickname
}
if update.Bio == DoNotModify {
update.Bio = p.Bio
}
if update.Pictures.Profile == DoNotModify {
update.Pictures.Profile = p.Pictures.Profile
}
if update.Pictures.Cover == DoNotModify {
update.Pictures.Cover = p.Pictures.Cover
}
newProfile, err := NewProfile(
update.DTag,
update.Nickname,
update.Bio,
update.Pictures,
p.CreationDate,
p.GetAccount(),
)
if err != nil {
return nil, err
}
return newProfile, nil
}