-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathowner.go
275 lines (233 loc) · 8.6 KB
/
owner.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
package common
import (
"context"
"github.com/pkg/errors"
commonpb "github.com/code-payments/code-protobuf-api/generated/go/common/v1"
code_data "github.com/code-payments/code-server/pkg/code/data"
"github.com/code-payments/code-server/pkg/code/data/account"
"github.com/code-payments/code-server/pkg/code/data/intent"
"github.com/code-payments/code-server/pkg/code/data/phone"
"github.com/code-payments/code-server/pkg/code/data/timelock"
timelock_token_v1 "github.com/code-payments/code-server/pkg/solana/timelock/v1"
)
var (
ErrOwnerNotFound = errors.New("owner account not found")
)
type OwnerType uint8
const (
OwnerTypeUnknown OwnerType = iota
OwnerTypeUser12Words
OwnerTypeRemoteSendGiftCard
)
type OwnerManagementState uint8
const (
OwnerManagementStateUnknown OwnerManagementState = iota
OwnerManagementStateCodeAccount
OwnerManagementStateNotFound
OwnerManagementStateUnlocked
)
type OwnerMetadata struct {
Type OwnerType
Account *Account
VerificationRecord *phone.Verification
State OwnerManagementState
}
// GetOwnerMetadata gets metadata about an owner account
func GetOwnerMetadata(ctx context.Context, data code_data.Provider, owner *Account) (*OwnerMetadata, error) {
mtdt := &OwnerMetadata{
Account: owner,
}
// Is the owner account a remote send gift card?
_, err := data.GetLatestAccountInfoByOwnerAddressAndType(ctx, owner.publicKey.ToBase58(), commonpb.AccountType_REMOTE_SEND_GIFT_CARD)
if err == nil {
mtdt.Type = OwnerTypeRemoteSendGiftCard
} else if err != account.ErrAccountInfoNotFound {
return nil, err
}
if mtdt.Type == OwnerTypeUnknown {
// Is the owner account a user's 12 words that's phone verified?
//
// This should be the last thing checked, since it's technically possible
// today for a malicious user to phone very any owner account type.
verificationRecord, err := data.GetLatestPhoneVerificationForAccount(ctx, owner.publicKey.ToBase58())
if err == nil {
mtdt.Type = OwnerTypeUser12Words
mtdt.VerificationRecord = verificationRecord
} else if err != phone.ErrVerificationNotFound {
return nil, err
}
}
// No other cases for an owner account, so error out
if mtdt.Type == OwnerTypeUnknown {
return nil, ErrOwnerNotFound
}
state, err := GetOwnerManagementState(ctx, data, owner)
if err != nil {
return nil, err
}
mtdt.State = state
return mtdt, nil
}
// GetOwnerManagementState returns an aggregate management state for an owner
// account based on the set of sub accounts it owns.
//
// todo: Needs tests here, but most already exist in account service
func GetOwnerManagementState(ctx context.Context, data code_data.Provider, owner *Account) (OwnerManagementState, error) {
legacyPrimary2022Records, err := GetLegacyPrimary2022AccountRecordsIfNotMigrated(ctx, data, owner)
if err != ErrNoPrivacyMigration2022 && err != nil {
return OwnerManagementStateUnknown, err
}
hasLegacyPrimary2022Account := (err == nil)
recordsByType, err := GetLatestTokenAccountRecordsForOwner(ctx, data, owner)
if err != nil {
return OwnerManagementStateUnknown, err
}
// Has an account ever been opened with the owner? If not, the owner is not a Code account.
// SubmitIntent guarantees all accounts are opened, so there's no need to do anything more
// than an empty check.
if len(recordsByType) == 0 && !hasLegacyPrimary2022Account {
return OwnerManagementStateNotFound, nil
}
// Are all opened accounts managed by Code? If not, the owner is not a Code account.
if hasLegacyPrimary2022Account && !legacyPrimary2022Records.IsManagedByCode(ctx) {
return OwnerManagementStateUnlocked, nil
}
for _, batchAccountRecords := range recordsByType {
for _, accountRecords := range batchAccountRecords {
if accountRecords.IsTimelock() && !accountRecords.IsManagedByCode(ctx) {
return OwnerManagementStateUnlocked, nil
}
}
}
return OwnerManagementStateCodeAccount, nil
}
// GetLatestTokenAccountRecordsForOwner gets DB records for the latest set of
// token accounts for an owner account.
func GetLatestTokenAccountRecordsForOwner(ctx context.Context, data code_data.Provider, owner *Account) (map[commonpb.AccountType][]*AccountRecords, error) {
res := make(map[commonpb.AccountType][]*AccountRecords)
infoRecordsByType, err := data.GetLatestAccountInfosByOwnerAddress(ctx, owner.publicKey.ToBase58())
if err != account.ErrAccountInfoNotFound && err != nil {
return nil, err
}
if len(infoRecordsByType) == 0 {
return res, nil
}
var timelockAccounts []string
for _, infoRecords := range infoRecordsByType {
for _, infoRecord := range infoRecords {
if infoRecord.IsTimelock() {
timelockAccounts = append(timelockAccounts, infoRecord.TokenAccount)
}
}
}
timelockRecordsByVault, err := data.GetTimelockByVaultBatch(ctx, timelockAccounts...)
if err != nil {
return nil, err
}
for _, generalRecords := range infoRecordsByType {
for _, generalRecord := range generalRecords {
var timelockRecord *timelock.Record
var ok bool
if generalRecord.IsTimelock() {
timelockRecord, ok = timelockRecordsByVault[generalRecord.TokenAccount]
if !ok {
return nil, errors.New("timelock record unexpectedly doesn't exist")
}
}
res[generalRecord.AccountType] = append(res[generalRecord.AccountType], &AccountRecords{
General: generalRecord,
Timelock: timelockRecord,
})
}
}
// The record should never exist, but this is precautionary. Pre-privacy timelock
// accounts should only be used in a migration.
delete(res, commonpb.AccountType_LEGACY_PRIMARY_2022)
return res, nil
}
// GetLatestCodeTimelockAccountRecordsForOwner is a utility wrapper over GetLatestTokenAccountRecordsForOwner
// that filters for Code Timelock accounts.
func GetLatestCodeTimelockAccountRecordsForOwner(ctx context.Context, data code_data.Provider, owner *Account) (map[commonpb.AccountType][]*AccountRecords, error) {
res := make(map[commonpb.AccountType][]*AccountRecords)
recordsByType, err := GetLatestTokenAccountRecordsForOwner(ctx, data, owner)
if err != nil {
return nil, err
}
for _, recordsList := range recordsByType {
for _, records := range recordsList {
if records.IsTimelock() {
res[records.General.AccountType] = append(res[records.General.AccountType], records)
}
}
}
return res, nil
}
// GetLegacyPrimary2022AccountRecordsIfNotMigrated gets a faked AccountRecords
// for the LEGACY_PRIMARY_2022 account associated with the provided owner. If
// the account doesn't exist, or was migrated, then ErrNoPrivacyMigration2022
// is returned.
//
// Note: Legacy Timelock accounts were always Kin accounts
//
// todo: Needs tests here, but most already exist in account service
func GetLegacyPrimary2022AccountRecordsIfNotMigrated(ctx context.Context, data code_data.Provider, owner *Account) (*AccountRecords, error) {
tokenAccount, err := owner.ToTimelockVault(timelock_token_v1.DataVersionLegacy, KinMintAccount)
if err != nil {
return nil, err
}
timelockRecord, err := data.GetTimelockByVault(ctx, tokenAccount.PublicKey().ToBase58())
if err == timelock.ErrTimelockNotFound {
return nil, ErrNoPrivacyMigration2022
} else if err != nil {
return nil, err
}
// Timelock account is closed, so it doesn't need migrating
if timelockRecord.IsClosed() {
return nil, ErrNoPrivacyMigration2022
}
// Client has already submitted an intent to migrate to privacy
_, err = data.GetLatestIntentByInitiatorAndType(ctx, intent.MigrateToPrivacy2022, owner.PublicKey().ToBase58())
if err == nil {
return nil, ErrNoPrivacyMigration2022
} else if err != intent.ErrIntentNotFound && err != nil {
return nil, err
}
// Fake an account info record, since we don't save it for legacy primary 2022
// accounts, but require it for downstream functions.
accountInfoRecord := &account.Record{
OwnerAccount: owner.PublicKey().ToBase58(),
AuthorityAccount: owner.PublicKey().ToBase58(),
TokenAccount: timelockRecord.VaultAddress,
MintAccount: KinMintAccount.PublicKey().ToBase58(),
AccountType: commonpb.AccountType_LEGACY_PRIMARY_2022,
Index: 0,
}
return &AccountRecords{
General: accountInfoRecord,
Timelock: timelockRecord,
}, nil
}
func (t OwnerType) String() string {
switch t {
case OwnerTypeUnknown:
return "unknown"
case OwnerTypeUser12Words:
return "user_12_words"
case OwnerTypeRemoteSendGiftCard:
return "remote_send_gift_card"
}
return "unknown"
}
func (t OwnerManagementState) String() string {
switch t {
case OwnerManagementStateUnknown:
return "unknown"
case OwnerManagementStateNotFound:
return "not_found"
case OwnerManagementStateUnlocked:
return "unlocked"
case OwnerManagementStateCodeAccount:
return "code_account"
}
return "unknown"
}