-
Notifications
You must be signed in to change notification settings - Fork 45
/
account.go
282 lines (234 loc) · 7.36 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
package types
import (
"encoding/json"
"fmt"
"strings"
"time"
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)
)
// 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)
}
// 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)
}
// UnpackInterfaces implements codectypes.UnpackInterfacesMessage
func (p *Profile) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
if p.Account != nil {
var account authtypes.AccountI
return unpacker.UnpackAny(p.Account, &account)
}
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"`
}
// Ensure that acc
//// String implements authtypes.AccountIount 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. An error is
// returned if the resulting profile contains invalid values.
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
}
err = newProfile.Validate()
if err != nil {
return nil, err
}
return newProfile, nil
}