-
Notifications
You must be signed in to change notification settings - Fork 9
/
wallet.go
439 lines (357 loc) · 10.1 KB
/
wallet.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package persist
import (
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"io"
"strings"
"time"
"blockwatch.cc/tzgo/tezos"
"github.com/lib/pq"
)
// Wallet represents an address on any chain
type Wallet struct {
ID DBID `json:"id"`
Version NullInt64 `json:"version"`
CreationTime time.Time `json:"created_at"`
Deleted NullBool `json:"-"`
LastUpdated time.Time `json:"last_updated"`
Address Address `json:"address"`
Chain Chain `json:"chain"`
L1Chain L1Chain `json:"l1_chain"`
WalletType WalletType `json:"wallet_type"`
}
// WalletType is the type of wallet used to sign a message
type WalletType int
type WalletList []Wallet
// Address represents the value of an address
type Address string
// PubKey represents the public key of a wallet
type PubKey string
type ChainAddress struct {
addressSet bool
chainSet bool
address Address
chain Chain
}
type L1ChainAddress struct {
addressSet bool
chainSet bool
address Address
l1Chain L1Chain
}
type ChainPubKey struct {
pubKeySet bool
chainSet bool
pubKey PubKey
chain Chain
}
// IsGalleryUserOrAddress is an empty function that satisfies the gqlgen IsGalleryUserOrAddress interface,
// allowing ChainAddress to be used in GraphQL resolvers that return the GalleryUserOrAddress type.
func (c *ChainAddress) IsGalleryUserOrAddress() {}
func NewChainAddress(address Address, chain Chain) ChainAddress {
ca := ChainAddress{
addressSet: true,
chainSet: true,
address: address,
chain: chain,
}
ca.updateCasing()
return ca
}
func (c *ChainAddress) Address() Address {
return c.address
}
func (c *ChainAddress) Chain() Chain {
return c.chain
}
func (c *ChainAddress) updateCasing() {
switch c.chain.L1Chain() {
// TODO: Add an IsCaseSensitive to the Chain type?
case L1Chain(ChainETH):
c.address = Address(strings.ToLower(c.address.String()))
}
}
// GQLSetAddressFromResolver will be called automatically from the required gqlgen resolver and should
// never be called manually. To set a ChainAddress's fields, use NewChainAddress.
func (c *ChainAddress) GQLSetAddressFromResolver(address Address) error {
if c.addressSet {
return errors.New("ChainAddress.address may only be set once")
}
c.address = address
c.addressSet = true
if c.chainSet {
c.updateCasing()
}
return nil
}
// GQLSetChainFromResolver will be called automatically from the required gqlgen resolver and should
// never be called manually. To set a ChainAddress's fields, use NewChainAddress.
func (c *ChainAddress) GQLSetChainFromResolver(chain Chain) error {
if c.chainSet {
return errors.New("ChainAddress.chain may only be set once")
}
c.chain = chain
c.chainSet = true
if c.addressSet {
c.updateCasing()
}
return nil
}
func (c ChainAddress) String() string {
return fmt.Sprintf("%d:%s", c.chain, c.address)
}
func (c ChainAddress) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]any{"address": c.address.String(), "chain": int(c.chain)})
}
func (c *ChainAddress) UnmarshalJSON(data []byte) error {
var v map[string]any
err := json.Unmarshal(data, &v)
if err != nil {
return err
}
if v["address"] != nil {
c.address = Address(v["address"].(string))
c.addressSet = true
}
if v["chain"] != nil {
if chain, ok := v["chain"].(int); ok {
c.chain = Chain(chain)
c.chainSet = true
} else if chain, ok := v["chain"].(int32); ok {
c.chain = Chain(chain)
c.chainSet = true
}
}
return nil
}
func (c ChainAddress) ToL1ChainAddress() L1ChainAddress {
return NewL1ChainAddress(c.Address(), c.Chain())
}
func (c *L1ChainAddress) IsGalleryUserOrAddress() {}
func NewL1ChainAddress(address Address, chain Chain) L1ChainAddress {
ca := L1ChainAddress{
addressSet: true,
chainSet: true,
address: address,
l1Chain: chain.L1Chain(),
}
ca.updateCasing()
return ca
}
func (c *L1ChainAddress) Address() Address {
return c.address
}
func (c *L1ChainAddress) L1Chain() L1Chain {
return c.l1Chain
}
func (c *L1ChainAddress) updateCasing() {
switch c.l1Chain {
// TODO: Add an IsCaseSensitive to the Chain type?
case L1Chain(ChainETH):
c.address = Address(strings.ToLower(c.address.String()))
}
}
func (c L1ChainAddress) String() string {
return fmt.Sprintf("%d:%s", c.l1Chain, c.address)
}
func (c L1ChainAddress) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]any{"address": c.address.String(), "chain": int(c.l1Chain)})
}
func (c *L1ChainAddress) UnmarshalJSON(data []byte) error {
var v map[string]any
err := json.Unmarshal(data, &v)
if err != nil {
return err
}
if v["address"] != nil {
c.address = Address(v["address"].(string))
c.addressSet = true
}
if v["chain"] != nil {
if chain, ok := v["chain"].(int); ok {
c.l1Chain = L1Chain(chain)
c.chainSet = true
} else if chain, ok := v["chain"].(int32); ok {
c.l1Chain = L1Chain(chain)
c.chainSet = true
} else if chain, ok := v["chain"].(Chain); ok {
c.l1Chain = L1Chain(chain)
c.chainSet = true
}
}
return nil
}
func NewChainPubKey(pubKey PubKey, chain Chain) ChainPubKey {
ca := ChainPubKey{
pubKeySet: true,
chainSet: true,
pubKey: pubKey,
chain: chain,
}
ca.updateCasing()
return ca
}
func (c *ChainPubKey) PubKey() PubKey {
return c.pubKey
}
func (c *ChainPubKey) Chain() Chain {
return c.chain
}
func (c *ChainPubKey) updateCasing() {
switch c.chain {
// TODO: Add an IsCaseSensitive to the Chain type?
case ChainETH:
c.pubKey = PubKey(strings.ToLower(c.pubKey.String()))
}
}
// GQLSetPubKeyFromResolver will be called automatically from the required gqlgen resolver and should
// never be called manually. To set a ChainPubKey's fields, use NewChainPubKey.
func (c *ChainPubKey) GQLSetPubKeyFromResolver(pubKey PubKey) error {
if c.pubKeySet {
return errors.New("ChainAddress.address may only be set once")
}
c.pubKey = pubKey
c.pubKeySet = true
if c.chainSet {
c.updateCasing()
}
return nil
}
// GQLSetChainFromResolver will be called automatically from the required gqlgen resolver and should
// never be called manually. To set a ChainPubKey's fields, use NewChainPubKey.
func (c *ChainPubKey) GQLSetChainFromResolver(chain Chain) error {
if c.chainSet {
return errors.New("ChainAddress.chain may only be set once")
}
c.chain = chain
c.chainSet = true
if c.pubKeySet {
c.updateCasing()
}
return nil
}
func (c ChainPubKey) String() string {
return fmt.Sprintf("%d:%s", c.chain, c.pubKey)
}
// ToChainAddress converts a chain pub key to a chain address
func (c ChainPubKey) ToChainAddress() ChainAddress {
switch c.chain {
case ChainTezos:
key, err := tezos.ParseKey(c.pubKey.String())
if err != nil {
panic(err)
}
return NewChainAddress(Address(key.Address().String()), c.chain)
default:
return NewChainAddress(Address(c.pubKey), c.chain)
}
}
func (c ChainPubKey) ToL1ChainAddress() L1ChainAddress {
return c.ToChainAddress().ToL1ChainAddress()
}
const (
// WalletTypeEOA represents an externally owned account (regular wallet address)
WalletTypeEOA WalletType = iota
// WalletTypeGnosis represents a smart contract gnosis safe
WalletTypeGnosis
)
func (l WalletList) Value() (driver.Value, error) {
return pq.Array(l).Value()
}
// Scan implements the Scanner interface for the AddressList type
func (l *WalletList) Scan(value interface{}) error {
return pq.Array(l).Scan(value)
}
// Scan implements the Scanner interface for the Wallet type
func (w *Wallet) Scan(value interface{}) error {
if value == nil {
*w = Wallet{}
return nil
}
*w = Wallet{ID: DBID(string(value.([]uint8)))}
return nil
}
// Value implements the database/sql driver Valuer interface for the Wallet type
func (w Wallet) Value() (driver.Value, error) {
if w.ID == "" {
return "", nil
}
return w.ID.String(), nil
}
// UnmarshalGQL implements the graphql.Unmarshaler interface
func (wa *WalletType) UnmarshalGQL(v interface{}) error {
n, ok := v.(string)
if !ok {
return fmt.Errorf("wrong type for WalletType: %T", v)
}
switch n {
case "EOA":
*wa = WalletTypeEOA
case "GnosisSafe":
*wa = WalletTypeGnosis
default:
return fmt.Errorf("unknown WalletType: %s", n)
}
return nil
}
// MarshalGQL implements the graphql.Marshaler interface
func (wa WalletType) MarshalGQL(w io.Writer) {
switch wa {
case WalletTypeEOA:
w.Write([]byte(`"EOA"`))
case WalletTypeGnosis:
w.Write([]byte(`"GnosisSafe"`))
}
}
func (n Address) String() string {
return string(n)
}
// Value implements the database/sql driver Valuer interface for the NullString type
func (n Address) Value() (driver.Value, error) {
if n.String() == "" {
return "", nil
}
return strings.ToValidUTF8(strings.ReplaceAll(n.String(), "\\u0000", ""), ""), nil
}
// Scan implements the database/sql Scanner interface for the NullString type
func (n *Address) Scan(value interface{}) error {
if value == nil {
*n = Address("")
return nil
}
asString, ok := value.(string)
if !ok {
asUint8Array, ok := value.([]uint8)
if !ok {
return fmt.Errorf("Address must be a string or []uint8")
}
asString = string(asUint8Array)
}
*n = Address(asString)
return nil
}
func (p PubKey) String() string {
return string(p)
}
type ErrWalletAlreadyExists struct {
WalletID DBID
L1ChainAddress L1ChainAddress
OwnerID DBID
}
func (e ErrWalletAlreadyExists) Error() string {
return fmt.Sprintf("wallet already exists: wallet ID: %s | chain address: %s | chain: %s | owner ID: %s", e.WalletID, e.L1ChainAddress.Address(), e.L1ChainAddress.L1Chain(), e.OwnerID)
}
var errWalletNotFound ErrWalletNotFound
type ErrWalletNotFound struct{}
func (e ErrWalletNotFound) Unwrap() error { return notFoundError }
func (e ErrWalletNotFound) Error() string { return "wallet not found" }
type ErrWalletNotFoundByID struct{ ID DBID }
func (e ErrWalletNotFoundByID) Unwrap() error { return errWalletNotFound }
func (e ErrWalletNotFoundByID) Error() string { return "wallet not found by id: " + e.ID.String() }
type ErrWalletNotFoundByAddress struct{ Address L1ChainAddress }
func (e ErrWalletNotFoundByAddress) Unwrap() error { return errWalletNotFound }
func (e ErrWalletNotFoundByAddress) Error() string {
return fmt.Sprintf("wallet not found by chain=%s; address = %s", e.Address.L1Chain(), e.Address.Address())
}