-
Notifications
You must be signed in to change notification settings - Fork 44
/
models_profile.go
313 lines (267 loc) · 7.77 KB
/
models_profile.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
package types
// DONTCOVER
import (
"encoding/json"
"fmt"
"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)
_ 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
}
// 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
}
// -------------------------------------------------------------------------------------------------------------------
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,
})
}