-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtimelock.go
265 lines (196 loc) · 6.4 KB
/
timelock.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
package timelock
import (
"time"
"github.com/mr-tron/base58"
"github.com/pkg/errors"
timelock_token_legacy "github.com/code-payments/code-server/pkg/solana/timelock/legacy_2022"
timelock_token_v1 "github.com/code-payments/code-server/pkg/solana/timelock/v1"
)
var (
ErrTimelockNotFound = errors.New("no records could be found")
ErrInvalidTimelock = errors.New("invalid timelock")
ErrStaleTimelockState = errors.New("timelock state is stale")
)
// Based off of https://github.com/code-payments/code-program-library/blob/main/timelock-token/programs/timelock-token/src/state.rs
//
// This record supports both the legacy 2022 (pre-privacy) and v1 versions of
// the timelock program, since they're easily interchangeable. All legacy fields
// will be converted accordingly, or dropped if never used.
type Record struct {
Id uint64
DataVersion timelock_token_v1.TimelockDataVersion
Address string
Bump uint8
VaultAddress string
VaultBump uint8
VaultOwner string
VaultState timelock_token_v1.TimelockState
TimeAuthority string
CloseAuthority string
Mint string
NumDaysLocked uint8
UnlockAt *uint64
Block uint64
LastUpdatedAt time.Time
}
func (r *Record) IsLocked() bool {
// Special case where the account is being actively created and we have no
// known state from the blockchain. Newly initialized accounts are guaranteed
// to be in the locked state by the timelock program.
if r.VaultState == timelock_token_v1.StateUnknown && r.Block == 0 {
return true
}
return r.VaultState == timelock_token_v1.StateLocked
}
func (r *Record) IsClosed() bool {
return r.VaultState == timelock_token_v1.StateClosed
}
func (r *Record) ExistsOnBlockchain() bool {
return r.VaultState != timelock_token_v1.StateUnknown && r.VaultState != timelock_token_v1.StateClosed
}
func (r *Record) UpdateFromV1ProgramAccount(data *timelock_token_v1.TimelockAccount, block uint64) error {
// Avoid updates looking backwards in blockchain history
if block <= r.Block {
return ErrStaleTimelockState
}
// Check the expected data version. If we encounter a closed version, simply
// update the record's data version. Expect all other data to be garbage, so
// don't include it. Ideally this should never happen, but we need to be
// extra safe in the event of an unknown attack vector. This can directly
// affect our ability to manage an account's funds.
if data.DataVersion == timelock_token_v1.DataVersionClosed {
r.DataVersion = timelock_token_v1.DataVersionClosed
r.Block = block
return nil
}
if data.DataVersion != timelock_token_v1.DataVersion1 {
return errors.New("timelock data version must be 1")
}
// It's now safe to update the record
var unlockAt *uint64
if data.UnlockAt != nil {
value := *data.UnlockAt
unlockAt = &value
}
// These 2 fields should never change under ideal circumstances, but we need
// to be extra safe in the event of an unknown attack vector. These directly
// affect our ability to manage an account's funds.
r.TimeAuthority = base58.Encode(data.TimeAuthority)
r.CloseAuthority = base58.Encode(data.CloseAuthority)
r.VaultState = data.VaultState
r.UnlockAt = unlockAt
r.Block = block
return nil
}
func (r *Record) UpdateFromLegacy2022ProgramAccount(data *timelock_token_legacy.TimelockAccount, block uint64) error {
// Avod updates looking backwards in blockchain history
if block <= r.Block {
return ErrStaleTimelockState
}
// Check the expected data version
// Handle init_offset similarly to how we handle DataVersionClosed in the v1
// update method.
if data.InitOffset != 0 {
r.DataVersion = timelock_token_v1.DataVersionClosed
r.Block = block
return nil
}
if data.DataVersion != timelock_token_legacy.TimelockDataVersion(timelock_token_v1.DataVersionLegacy) {
return errors.New("timelock data version must be legacy")
}
// It's now safe to update the record
var unlockAt *uint64
if data.UnlockAt != nil {
value := *data.UnlockAt
unlockAt = &value
}
// These 2 fields should never change under ideal circumstances, but we need
// to be extra safe in the event of an unknown attack vector. These directly
// affect our ability to manage an account's funds.
r.TimeAuthority = base58.Encode(data.TimeAuthority)
r.CloseAuthority = base58.Encode(data.CloseAuthority)
r.VaultState = timelock_token_v1.TimelockState(data.VaultState)
r.UnlockAt = unlockAt
r.Block = block
return nil
}
func (r *Record) Clone() *Record {
var unlockAt *uint64
if r.UnlockAt != nil {
value := *r.UnlockAt
unlockAt = &value
}
return &Record{
Id: r.Id,
DataVersion: r.DataVersion,
Address: r.Address,
Bump: r.Bump,
VaultAddress: r.VaultAddress,
VaultBump: r.VaultBump,
VaultOwner: r.VaultOwner,
VaultState: r.VaultState,
TimeAuthority: r.TimeAuthority,
CloseAuthority: r.CloseAuthority,
Mint: r.Mint,
NumDaysLocked: r.NumDaysLocked,
UnlockAt: unlockAt,
Block: r.Block,
LastUpdatedAt: r.LastUpdatedAt,
}
}
func (r *Record) CopyTo(dst *Record) {
var unlockAt *uint64
if r.UnlockAt != nil {
value := *r.UnlockAt
unlockAt = &value
}
dst.Id = r.Id
dst.DataVersion = r.DataVersion
dst.Address = r.Address
dst.Bump = r.Bump
dst.VaultAddress = r.VaultAddress
dst.VaultBump = r.VaultBump
dst.VaultOwner = r.VaultOwner
dst.VaultState = r.VaultState
dst.TimeAuthority = r.TimeAuthority
dst.CloseAuthority = r.CloseAuthority
dst.Mint = r.Mint
dst.NumDaysLocked = r.NumDaysLocked
dst.UnlockAt = unlockAt
dst.Block = r.Block
dst.LastUpdatedAt = r.LastUpdatedAt
}
func (r *Record) Validate() error {
if r == nil {
return errors.New("record is nil")
}
switch r.DataVersion {
case timelock_token_v1.DataVersionLegacy,
timelock_token_v1.DataVersion1,
timelock_token_v1.DataVersionClosed:
default:
return errors.New("invalid timelock data version")
}
if len(r.Address) == 0 {
return errors.New("state address is required")
}
if len(r.VaultAddress) == 0 {
return errors.New("vault address is required")
}
if len(r.VaultOwner) == 0 {
return errors.New("vault owner is required")
}
if len(r.TimeAuthority) == 0 {
return errors.New("time authority is required")
}
if len(r.CloseAuthority) == 0 {
return errors.New("close authority is required")
}
if len(r.Mint) == 0 {
return errors.New("mint is required")
}
if r.NumDaysLocked != timelock_token_v1.DefaultNumDaysLocked {
return errors.Errorf("num days locked must be %d days", timelock_token_v1.DefaultNumDaysLocked)
}
return nil
}