-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathacccount_info.go
197 lines (164 loc) · 5.61 KB
/
acccount_info.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
package account
import (
"time"
"github.com/pkg/errors"
commonpb "github.com/code-payments/code-protobuf-api/generated/go/common/v1"
"github.com/code-payments/code-server/pkg/pointer"
)
var AllAccountTypes = []commonpb.AccountType{
commonpb.AccountType_PRIMARY,
commonpb.AccountType_TEMPORARY_INCOMING,
commonpb.AccountType_TEMPORARY_OUTGOING,
commonpb.AccountType_BUCKET_1_KIN,
commonpb.AccountType_BUCKET_10_KIN,
commonpb.AccountType_BUCKET_100_KIN,
commonpb.AccountType_BUCKET_1_000_KIN,
commonpb.AccountType_BUCKET_10_000_KIN,
commonpb.AccountType_BUCKET_100_000_KIN,
commonpb.AccountType_BUCKET_1_000_000_KIN,
commonpb.AccountType_REMOTE_SEND_GIFT_CARD,
commonpb.AccountType_RELATIONSHIP,
commonpb.AccountType_SWAP,
}
type Record struct {
Id uint64
OwnerAccount string
AuthorityAccount string
TokenAccount string
MintAccount string
AccountType commonpb.AccountType
Index uint64
RelationshipTo *string
RequiresDepositSync bool
DepositsLastSyncedAt time.Time
RequiresAutoReturnCheck bool
CreatedAt time.Time
}
func (r *Record) IsBucket() bool {
switch r.AccountType {
case commonpb.AccountType_BUCKET_1_KIN,
commonpb.AccountType_BUCKET_10_KIN,
commonpb.AccountType_BUCKET_100_KIN,
commonpb.AccountType_BUCKET_1_000_KIN,
commonpb.AccountType_BUCKET_10_000_KIN,
commonpb.AccountType_BUCKET_100_000_KIN,
commonpb.AccountType_BUCKET_1_000_000_KIN:
return true
}
return false
}
func (r *Record) Clone() Record {
return Record{
Id: r.Id,
OwnerAccount: r.OwnerAccount,
AuthorityAccount: r.AuthorityAccount,
TokenAccount: r.TokenAccount,
MintAccount: r.MintAccount,
AccountType: r.AccountType,
Index: r.Index,
RelationshipTo: pointer.StringCopy(r.RelationshipTo),
RequiresDepositSync: r.RequiresDepositSync,
DepositsLastSyncedAt: r.DepositsLastSyncedAt,
RequiresAutoReturnCheck: r.RequiresAutoReturnCheck,
CreatedAt: r.CreatedAt,
}
}
func (r *Record) CopyTo(dst *Record) {
dst.Id = r.Id
dst.OwnerAccount = r.OwnerAccount
dst.AuthorityAccount = r.AuthorityAccount
dst.TokenAccount = r.TokenAccount
dst.MintAccount = r.MintAccount
dst.AccountType = r.AccountType
dst.Index = r.Index
dst.RelationshipTo = pointer.StringCopy(dst.RelationshipTo)
dst.RequiresDepositSync = r.RequiresDepositSync
dst.DepositsLastSyncedAt = r.DepositsLastSyncedAt
dst.RequiresAutoReturnCheck = r.RequiresAutoReturnCheck
dst.CreatedAt = r.CreatedAt
}
func (r *Record) Validate() error {
if len(r.OwnerAccount) == 0 {
return errors.New("owner address is required")
}
if len(r.AuthorityAccount) == 0 {
return errors.New("authority address is required")
}
if len(r.TokenAccount) == 0 {
return errors.New("token address is required")
}
if len(r.MintAccount) == 0 {
return errors.New("mint address is required")
}
if r.AccountType == commonpb.AccountType_UNKNOWN {
return errors.New("account type is required")
}
switch r.AccountType {
case commonpb.AccountType_LEGACY_PRIMARY_2022:
return errors.New("cannot store legacy primary 2022 account")
case commonpb.AccountType_PRIMARY:
if r.Index != 0 {
return errors.New("index must be 0 for primary account")
}
if r.OwnerAccount != r.AuthorityAccount {
return errors.New("owner must be authority of primary account")
}
case commonpb.AccountType_REMOTE_SEND_GIFT_CARD:
if r.Index != 0 {
return errors.New("index must be 0 for remote send gift card account")
}
if r.OwnerAccount != r.AuthorityAccount {
return errors.New("owner must be authority of remote send gift card account")
}
case commonpb.AccountType_RELATIONSHIP:
if r.Index != 0 {
return errors.New("index must be 0 for relationship account")
}
if r.OwnerAccount == r.AuthorityAccount {
return errors.New("owner cannot be authority for relationship account")
}
if r.RelationshipTo == nil || len(*r.RelationshipTo) == 0 {
return errors.New("relationship metadata required for relationship account")
}
case commonpb.AccountType_BUCKET_1_KIN,
commonpb.AccountType_BUCKET_10_KIN,
commonpb.AccountType_BUCKET_100_KIN,
commonpb.AccountType_BUCKET_1_000_KIN,
commonpb.AccountType_BUCKET_10_000_KIN,
commonpb.AccountType_BUCKET_100_000_KIN,
commonpb.AccountType_BUCKET_1_000_000_KIN:
if r.Index != 0 {
return errors.New("index must be 0 for bucket account")
}
if r.OwnerAccount == r.AuthorityAccount {
return errors.New("owner cannot be authority for bucket account")
}
case commonpb.AccountType_TEMPORARY_INCOMING,
commonpb.AccountType_TEMPORARY_OUTGOING:
if r.OwnerAccount == r.AuthorityAccount {
return errors.New("owner cannot be authority for temporary rotating account")
}
case commonpb.AccountType_SWAP:
if r.Index != 0 {
return errors.New("index must be 0 for swap account")
}
if r.OwnerAccount == r.AuthorityAccount {
return errors.New("owner cannot be authority for swap account")
}
default:
return errors.Errorf("unhandled account type: %s", r.AccountType.String())
}
if r.TokenAccount == r.OwnerAccount || r.TokenAccount == r.AuthorityAccount {
return errors.New("token account cannot be owner or authority of account")
}
if r.RequiresAutoReturnCheck && r.AccountType != commonpb.AccountType_REMOTE_SEND_GIFT_CARD {
return errors.New("only remote send gift cards can have auto-return checks")
}
if r.RelationshipTo != nil && r.AccountType != commonpb.AccountType_RELATIONSHIP {
return errors.New("only relationship accounts can have a relationship metadata")
}
return nil
}
func (r *Record) IsTimelock() bool {
return r.AccountType != commonpb.AccountType_SWAP
}