-
Notifications
You must be signed in to change notification settings - Fork 86
/
types.go
804 lines (724 loc) · 22.3 KB
/
types.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
// This code is available on the terms of the project LICENSE.md file,
// also available online at https://blueoakcouncil.org/license/1.0.0.
package db
import (
"bytes"
"fmt"
"io/ioutil"
"strconv"
"strings"
"time"
"decred.org/dcrdex/client/asset"
"decred.org/dcrdex/dex"
"decred.org/dcrdex/dex/config"
"decred.org/dcrdex/dex/encode"
"decred.org/dcrdex/dex/order"
"github.com/decred/dcrd/dcrec/secp256k1/v3"
"golang.org/x/crypto/blake2s"
)
// Severity indicates the level of required action for a notification. The DEX
// db only stores notifications with Severity >= Success.
type Severity uint8
const (
Ignorable Severity = iota
// Data notifications are not meant for display to the user. These
// notifications are used only for communication of information necessary for
// UI updates or other high-level state changes.
Data
// Poke notifications are not persistent across sessions. These should be
// displayed if the user has a live notification feed. They are not stored in
// the database.
Poke
// Success and higher are stored and can be recalled using DB.NotificationsN.
Success
WarningLevel
ErrorLevel
)
// String satisfies fmt.Stringer for Severity.
func (s Severity) String() string {
switch s {
case Ignorable:
return "ignore"
case Data:
return "data"
case Poke:
return "poke"
case WarningLevel:
return "warning"
case ErrorLevel:
return "error"
case Success:
return "success"
}
return "unknown severity"
}
// AccountInfo is information about an account on a Decred DEX. The database
// is designed for one account per server.
type AccountInfo struct {
Host string
Cert []byte
// EncKey should be an encrypted private key. The database itself does not
// handle encryption (yet?).
EncKey []byte
DEXPubKey *secp256k1.PublicKey
FeeCoin []byte
// Paid will be set on retrieval based on whether there is an AccountProof
// set.
Paid bool
}
// Encode the AccountInfo as bytes.
func (ai *AccountInfo) Encode() []byte {
return dbBytes{0}.
AddData([]byte(ai.Host)).
AddData(ai.EncKey).
AddData(ai.DEXPubKey.SerializeCompressed()).
AddData(ai.FeeCoin).
AddData(ai.Cert)
}
// DecodeAccountInfo decodes the versioned blob into an *AccountInfo. The byte
// slice fields of AccountInfo reference the underlying buffer of the the input.
func DecodeAccountInfo(b []byte) (*AccountInfo, error) {
ver, pushes, err := encode.DecodeBlob(b)
if err != nil {
return nil, err
}
switch ver {
case 0:
return decodeAccountInfo_v0(pushes)
}
return nil, fmt.Errorf("unknown AccountInfo version %d", ver)
}
func decodeAccountInfo_v0(pushes [][]byte) (*AccountInfo, error) {
if len(pushes) != 5 {
return nil, fmt.Errorf("decodeAccountInfo: expected 5 data pushes, got %d", len(pushes))
}
hostB, keyB, dexB := pushes[0], pushes[1], pushes[2]
coinB, certB := pushes[3], pushes[4]
pk, err := secp256k1.ParsePubKey(dexB)
if err != nil {
return nil, err
}
return &AccountInfo{
Host: string(hostB),
EncKey: keyB,
DEXPubKey: pk,
FeeCoin: coinB,
Cert: certB,
}, nil
}
// Account proof is information necessary to prove that the DEX server accepted
// the account's fee payment. The fee coin is not part of the proof, since it
// is already stored as part of the AccountInfo blob.
type AccountProof struct {
Host string
Stamp uint64
Sig []byte
}
// Encode encodes the AccountProof to a versioned blob.
func (p *AccountProof) Encode() []byte {
return dbBytes{0}.
AddData([]byte(p.Host)).
AddData(uint64Bytes(p.Stamp)).
AddData(p.Sig)
}
// DecodeAccountProof decodes the versioned blob to a *MatchProof.
func DecodeAccountProof(b []byte) (*AccountProof, error) {
ver, pushes, err := encode.DecodeBlob(b)
if err != nil {
return nil, err
}
switch ver {
case 0:
return decodeAccountProof_v0(pushes)
}
return nil, fmt.Errorf("unknown AccountProof version %d", ver)
}
func decodeAccountProof_v0(pushes [][]byte) (*AccountProof, error) {
if len(pushes) != 3 {
return nil, fmt.Errorf("decodeAccountProof_v0: expected 3 pushes, got %d", len(pushes))
}
hostB, stampB := pushes[0], pushes[1]
return &AccountProof{
Host: string(hostB),
Stamp: intCoder.Uint64(stampB),
Sig: pushes[2],
}, nil
}
// MetaOrder is an order and its metadata.
type MetaOrder struct {
// MetaData is important auxiliary information about the order.
MetaData *OrderMetaData
// Order is the order.
Order order.Order
}
// OrderMetaData is important auxiliary information about an order.
type OrderMetaData struct {
// Status is the last known order status.
Status order.OrderStatus
// Host is the hostname of the server that this order is associated with.
Host string
// Proof is the signatures and other verification-related data for the order.
Proof OrderProof
// ChangeCoin is a change coin from a match. Change coins are "daisy-chained"
// for matches. All funding coins go into the first match, and the change coin
// from the initiation transaction is used to fund the next match. The
// change from that matches ini tx funds the next match, etc.
ChangeCoin order.CoinID
// LinkedOrder is used to specify the cancellation order for a trade, or
// vice-versa.
LinkedOrder order.OrderID
// SwapFeesPaid is the sum of the actual fees paid for all swaps.
SwapFeesPaid uint64
// RedemptionFeesPaid is the sum of the actual fees paid for all
// redemptions.
RedemptionFeesPaid uint64
// MaxFeeRate is the dex.Asset.MaxFeeRate at the time of ordering. The rates
// assigned to matches will be validated against this value.
MaxFeeRate uint64
}
// MetaMatch is a match and its metadata.
type MetaMatch struct {
// UserMatch is the match info.
*order.UserMatch
// MetaData is important auxiliary information about the match.
MetaData *MatchMetaData
}
// MatchOrderUniqueID is a unique ID for the match-order pair.
func (m *MetaMatch) MatchOrderUniqueID() []byte {
return hashKey(append(m.MatchID[:], m.OrderID[:]...))
}
// MatchMetaData is important auxiliary information about the match.
type MatchMetaData struct {
// Proof is the signatures and other verification-related data for the match.
Proof MatchProof
// DEX is the URL of the server that this match is associated with.
DEX string
// Base is the base asset of the exchange market.
Base uint32
// Quote is the quote asset of the exchange market.
Quote uint32
// Stamp is the match time (ms UNIX), according to the server's 'match'
// request timestamp.
Stamp uint64
}
// MatchAuth holds the DEX signatures and timestamps associated with the
// messages in the negotiation process.
type MatchAuth struct {
MatchSig []byte
MatchStamp uint64
InitSig []byte
InitStamp uint64
AuditSig []byte
AuditStamp uint64
RedeemSig []byte
RedeemStamp uint64
RedemptionSig []byte
RedemptionStamp uint64
}
// MatchProof is information related to the progression of the swap negotiation
// process.
type MatchProof struct {
Script []byte
CounterContract []byte
CounterTxData []byte
SecretHash []byte
Secret []byte
MakerSwap order.CoinID
MakerRedeem order.CoinID
TakerSwap order.CoinID
TakerRedeem order.CoinID
RefundCoin order.CoinID
Auth MatchAuth
ServerRevoked bool
SelfRevoked bool
}
// MatchProofVer is the current serialization version of a MatchProof.
const (
MatchProofVer = 2
matchProofPushes = 22
)
// Encode encodes the MatchProof to a versioned blob.
func (p *MatchProof) Encode() []byte {
auth := p.Auth
srvRevoked := encode.ByteFalse
if p.ServerRevoked {
srvRevoked = encode.ByteTrue
}
selfRevoked := encode.ByteFalse
if p.SelfRevoked {
selfRevoked = encode.ByteTrue
}
return dbBytes{MatchProofVer}.
AddData(p.Script).
AddData(p.CounterContract).
AddData(p.SecretHash).
AddData(p.Secret).
AddData(p.MakerSwap).
AddData(p.MakerRedeem).
AddData(p.TakerSwap).
AddData(p.TakerRedeem).
AddData(p.RefundCoin).
AddData(auth.MatchSig).
AddData(uint64Bytes(auth.MatchStamp)).
AddData(auth.InitSig).
AddData(uint64Bytes(auth.InitStamp)).
AddData(auth.AuditSig).
AddData(uint64Bytes(auth.AuditStamp)).
AddData(auth.RedeemSig).
AddData(uint64Bytes(auth.RedeemStamp)).
AddData(auth.RedemptionSig).
AddData(uint64Bytes(auth.RedemptionStamp)).
AddData(srvRevoked).
AddData(selfRevoked).
AddData(p.CounterTxData)
}
// DecodeMatchProof decodes the versioned blob to a *MatchProof.
func DecodeMatchProof(b []byte) (*MatchProof, uint8, error) {
ver, pushes, err := encode.DecodeBlob(b, matchProofPushes)
if err != nil {
return nil, 0, err
}
switch ver {
case 2: // MatchProofVer
proof, err := decodeMatchProof_v2(pushes)
return proof, ver, err
case 1:
proof, err := decodeMatchProof_v1(pushes)
return proof, ver, err
case 0:
proof, err := decodeMatchProof_v0(pushes)
return proof, ver, err
}
return nil, ver, fmt.Errorf("unknown MatchProof version %d", ver)
}
func decodeMatchProof_v0(pushes [][]byte) (*MatchProof, error) {
pushes = append(pushes, encode.ByteFalse)
return decodeMatchProof_v1(pushes)
}
func decodeMatchProof_v1(pushes [][]byte) (*MatchProof, error) {
pushes = append(pushes, nil)
return decodeMatchProof_v2(pushes)
}
func decodeMatchProof_v2(pushes [][]byte) (*MatchProof, error) {
if len(pushes) != matchProofPushes {
return nil, fmt.Errorf("DecodeMatchProof: expected %d pushes, got %d",
matchProofPushes, len(pushes))
}
return &MatchProof{
Script: pushes[0],
CounterContract: pushes[1],
CounterTxData: pushes[21],
SecretHash: pushes[2],
Secret: pushes[3],
MakerSwap: pushes[4],
MakerRedeem: pushes[5],
TakerSwap: pushes[6],
TakerRedeem: pushes[7],
RefundCoin: pushes[8],
Auth: MatchAuth{
MatchSig: pushes[9],
MatchStamp: intCoder.Uint64(pushes[10]),
InitSig: pushes[11],
InitStamp: intCoder.Uint64(pushes[12]),
AuditSig: pushes[13],
AuditStamp: intCoder.Uint64(pushes[14]),
RedeemSig: pushes[15],
RedeemStamp: intCoder.Uint64(pushes[16]),
RedemptionSig: pushes[17],
RedemptionStamp: intCoder.Uint64(pushes[18]),
},
ServerRevoked: bytes.Equal(pushes[19], encode.ByteTrue),
SelfRevoked: bytes.Equal(pushes[20], encode.ByteTrue),
}, nil
}
// IsRevoked is true if either ServerRevoked or SelfRevoked is true.
func (p *MatchProof) IsRevoked() bool {
return p.ServerRevoked || p.SelfRevoked
}
// OrderProof is information related to order authentication and matching.
type OrderProof struct {
DEXSig []byte
Preimage []byte
}
// Encode encodes the OrderProof to a versioned blob.
func (p *OrderProof) Encode() []byte {
return dbBytes{0}.AddData(p.DEXSig).AddData(p.Preimage)
}
// DecodeOrderProof decodes the versioned blob to an *OrderProof.
func DecodeOrderProof(b []byte) (*OrderProof, error) {
ver, pushes, err := encode.DecodeBlob(b)
if err != nil {
return nil, err
}
switch ver {
case 0:
return decodeOrderProof_v0(pushes)
}
return nil, fmt.Errorf("unknown OrderProof version %d", ver)
}
func decodeOrderProof_v0(pushes [][]byte) (*OrderProof, error) {
if len(pushes) != 2 {
return nil, fmt.Errorf("decodeOrderProof: expected 2 push, got %d", len(pushes))
}
return &OrderProof{
DEXSig: pushes[0],
Preimage: pushes[1],
}, nil
}
// encodeAssetBalance serializes an asset.Balance.
func encodeAssetBalance(bal *asset.Balance) []byte {
return dbBytes{0}.
AddData(uint64Bytes(bal.Available)).
AddData(uint64Bytes(bal.Immature)).
AddData(uint64Bytes(bal.Locked))
}
// decodeAssetBalance deserializes an asset.Balance.
func decodeAssetBalance(b []byte) (*asset.Balance, error) {
ver, pushes, err := encode.DecodeBlob(b)
if err != nil {
return nil, err
}
switch ver {
case 0:
return decodeAssetBalance_v0(pushes)
}
return nil, fmt.Errorf("unknown Balance version %d", ver)
}
func decodeAssetBalance_v0(pushes [][]byte) (*asset.Balance, error) {
if len(pushes) != 3 {
return nil, fmt.Errorf("decodeBalance_v0: expected 3 push, got %d", len(pushes))
}
return &asset.Balance{
Available: intCoder.Uint64(pushes[0]),
Immature: intCoder.Uint64(pushes[1]),
Locked: intCoder.Uint64(pushes[2]),
}, nil
}
// Balance represents a wallet's balance in various contexts.
type Balance struct {
asset.Balance
Stamp time.Time `json:"stamp"`
}
// Encode encodes the Balance to a versioned blob.
func (b *Balance) Encode() []byte {
return dbBytes{0}.
AddData(encodeAssetBalance(&b.Balance)).
AddData(uint64Bytes(encode.UnixMilliU(b.Stamp)))
}
// DecodeBalance decodes the versioned blob to a *Balance.
func DecodeBalance(b []byte) (*Balance, error) {
ver, pushes, err := encode.DecodeBlob(b)
if err != nil {
return nil, err
}
switch ver {
case 0:
return decodeBalance_v0(pushes)
}
return nil, fmt.Errorf("unknown Balance version %d", ver)
}
func decodeBalance_v0(pushes [][]byte) (*Balance, error) {
if len(pushes) < 2 {
return nil, fmt.Errorf("decodeBalances_v0: expected >= 2 pushes. got %d", len(pushes))
}
if len(pushes)%2 != 0 {
return nil, fmt.Errorf("decodeBalances_v0: expected an even number of pushes, got %d", len(pushes))
}
bal, err := decodeAssetBalance(pushes[0])
if err != nil {
return nil, fmt.Errorf("decodeBalances_v0: error decoding zero conf balance: %w", err)
}
return &Balance{
Balance: *bal,
Stamp: encode.UnixTimeMilli(int64(intCoder.Uint64(pushes[1]))),
}, nil
}
// Wallet is information necessary to create an asset.Wallet.
type Wallet struct {
AssetID uint32
Settings map[string]string
Balance *Balance
EncryptedPW []byte
Address string
}
// Encode encodes the Wallet to a versioned blob.
func (w *Wallet) Encode() []byte {
return dbBytes{0}.
AddData(uint32Bytes(w.AssetID)).
AddData(config.Data(w.Settings)).
AddData(w.EncryptedPW).
AddData([]byte(w.Address))
}
// DecodeWallet decodes the versioned blob to a *Wallet. The Balance is NOT set;
// the caller must retrieve it. See for example makeWallet and DecodeBalance.
func DecodeWallet(b []byte) (*Wallet, error) {
ver, pushes, err := encode.DecodeBlob(b)
if err != nil {
return nil, err
}
switch ver {
case 0:
return decodeWallet_v0(pushes)
}
return nil, fmt.Errorf("unknown DecodeWallet version %d", ver)
}
func decodeWallet_v0(pushes [][]byte) (*Wallet, error) {
if len(pushes) != 4 {
return nil, fmt.Errorf("decodeWallet_v0: expected 4 pushes, got %d", len(pushes))
}
idB, settingsB, keyB, addressB := pushes[0], pushes[1], pushes[2], pushes[3]
settings, err := config.Parse(settingsB)
if err != nil {
return nil, fmt.Errorf("unable to decode wallet settings")
}
return &Wallet{
AssetID: intCoder.Uint32(idB),
Settings: settings,
EncryptedPW: keyB,
Address: string(addressB),
}, nil
}
// ID is the byte-encoded asset ID for this wallet.
func (w *Wallet) ID() []byte {
return uint32Bytes(w.AssetID)
}
// SID is a string respresentation of the wallet's asset ID.
func (w *Wallet) SID() string {
return strconv.Itoa(int(w.AssetID))
}
type dbBytes = encode.BuildyBytes
var uint64Bytes = encode.Uint64Bytes
var uint32Bytes = encode.Uint32Bytes
var intCoder = encode.IntCoder
// AccountBackup represents a user account backup.
type AccountBackup struct {
KeyParams []byte
Accounts []*AccountInfo
}
// encodeDEXAccount serializes the details needed to backup a dex account.
func encodeDEXAccount(acct *AccountInfo) []byte {
return dbBytes{0}.
AddData([]byte(acct.Host)).
AddData(acct.EncKey).
AddData(acct.DEXPubKey.SerializeCompressed())
}
// decodeDEXAccount decodes the versioned blob into an AccountInfo.
func decodeDEXAccount(acctB []byte) (*AccountInfo, error) {
ver, pushes, err := encode.DecodeBlob(acctB)
if err != nil {
return nil, err
}
switch ver {
case 0:
if len(pushes) != 3 {
return nil, fmt.Errorf("expected 3 pushes, got %d", len(pushes))
}
var ai AccountInfo
ai.Host = string(pushes[0])
ai.EncKey = pushes[1]
ai.DEXPubKey, err = secp256k1.ParsePubKey(pushes[2])
if err != nil {
return nil, err
}
return &ai, nil
}
return nil, fmt.Errorf("unknown DEX account backup version %d", ver)
}
// Serialize encodes an account backup as bytes.
func (ab *AccountBackup) Serialize() []byte {
backup := dbBytes{0}.AddData(ab.KeyParams)
for _, acct := range ab.Accounts {
backup = backup.AddData(encodeDEXAccount(acct))
}
return backup
}
// decodeAccountBackup decodes the versioned blob into an *AccountBackup.
func decodeAccountBackup(b []byte) (*AccountBackup, error) {
ver, pushes, err := encode.DecodeBlob(b)
if err != nil {
return nil, err
}
switch ver {
case 0:
keyParams := pushes[0]
accts := make([]*AccountInfo, 0, len(pushes[1:]))
for _, push := range pushes[1:] {
ai, err := decodeDEXAccount(push)
if err != nil {
return nil, err
}
accts = append(accts, ai)
}
return &AccountBackup{
KeyParams: keyParams,
Accounts: accts,
}, nil
}
return nil, fmt.Errorf("unknown AccountBackup version %d", ver)
}
// Save persists an account backup to file.
func (ab *AccountBackup) Save(path string) error {
backup := ab.Serialize()
return ioutil.WriteFile(path, backup, 0o600)
}
// RestoreAccountBackup generates a user account from a backup file.
func RestoreAccountBackup(path string) (*AccountBackup, error) {
backup, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
ab, err := decodeAccountBackup(backup)
if err != nil {
return nil, err
}
return ab, nil
}
// Notification is information for the user that is typically meant for display,
// and is persisted for recall across sessions.
type Notification struct {
NoteType string `json:"type"`
SubjectText string `json:"subject"`
DetailText string `json:"details"`
Severeness Severity `json:"severity"`
TimeStamp uint64 `json:"stamp"`
Ack bool `json:"acked"`
Id dex.Bytes `json:"id"`
}
// NewNotification is a constructor for a Notification.
func NewNotification(noteType, subject, details string, severity Severity) Notification {
note := Notification{
NoteType: noteType,
SubjectText: subject,
DetailText: details,
Severeness: severity,
}
note.Stamp()
return note
}
// ID is a unique ID based on a hash of the notification data.
func (n *Notification) ID() dex.Bytes {
return noteKey(n.Encode())
}
// Type is the notification type.
func (n *Notification) Type() string {
return n.NoteType
}
// Subject is a short description of the notification contents.
func (n *Notification) Subject() string {
return n.SubjectText
}
// Details should contain more detailed information.
func (n *Notification) Details() string {
return n.DetailText
}
// Severity is the notification severity.
func (n *Notification) Severity() Severity {
return n.Severeness
}
// Time is the notification timestamp. The timestamp is set in NewNotification.
func (n *Notification) Time() uint64 {
return n.TimeStamp
}
// Acked is true if the user has seen the notification. Acknowledgement is
// recorded with DB.AckNotification.
func (n *Notification) Acked() bool {
return n.Ack
}
// Stamp sets the notification timestamp. If NewNotification is used to
// construct the Notification, the timestamp will already be set.
func (n *Notification) Stamp() {
n.TimeStamp = encode.UnixMilliU(time.Now())
n.Id = n.ID()
}
// DBNote is a function to return the *Notification itself. It should really be
// defined on the concrete types in core, but is ubiquitous so defined here for
// convenience.
func (n *Notification) DBNote() *Notification {
return n
}
// String generates a compact human-readable representation of the Notification
// that is suitable for logging. For example:
// |SUCCESS| (fee payment) Fee paid - Waiting for 2 confirmations before trading at https://superdex.tld:7232
// |DATA| (boring event) Subject without details
func (n *Notification) String() string {
// In case type and/or detail or empty strings, adjust the formatting to
// avoid extra whitespace.
var format strings.Builder
format.WriteString("|%s| (%s)") // always nil error
if len(n.DetailText) > 0 || len(n.SubjectText) > 0 {
format.WriteString(" ")
}
format.WriteString("%s")
if len(n.DetailText) > 0 && len(n.SubjectText) > 0 {
format.WriteString(" - ")
}
format.WriteString("%s")
severity := strings.ToUpper(n.Severity().String())
return fmt.Sprintf(format.String(), severity, n.NoteType, n.SubjectText, n.DetailText)
}
// DecodeNotification decodes the versioned blob to a *Notification.
func DecodeNotification(b []byte) (*Notification, error) {
ver, pushes, err := encode.DecodeBlob(b)
if err != nil {
return nil, err
}
switch ver {
case 0:
return decodeNotification_v0(pushes)
}
return nil, fmt.Errorf("unknown DecodeNotification version %d", ver)
}
func decodeNotification_v0(pushes [][]byte) (*Notification, error) {
if len(pushes) != 5 {
return nil, fmt.Errorf("decodeNotification_v0: expected 5 pushes, got %d", len(pushes))
}
if len(pushes[3]) != 1 {
return nil, fmt.Errorf("decodeNotification_v0: severity push is supposed to be length 1. got %d", len(pushes[2]))
}
return &Notification{
NoteType: string(pushes[0]),
SubjectText: string(pushes[1]),
DetailText: string(pushes[2]),
Severeness: Severity(pushes[3][0]),
TimeStamp: intCoder.Uint64(pushes[4]),
}, nil
}
// Encode encodes the Notification to a versioned blob.
func (n *Notification) Encode() []byte {
return dbBytes{0}.
AddData([]byte(n.NoteType)).
AddData([]byte(n.SubjectText)).
AddData([]byte(n.DetailText)).
AddData([]byte{byte(n.Severeness)}).
AddData(uint64Bytes(n.TimeStamp))
}
// OrderFilter is used to limit the results returned by a query to (DB).Orders.
type OrderFilter struct {
// N is the number of orders to return in the set.
N int
// Offset can be used to shift the window of the time-sorted orders such
// that any orders that would sort to index <= the order specified by Offset
// will be rejected.
Offset order.OrderID
// Hosts is a list of acceptable hosts. A zero-length Hosts means all
// hosts are accepted.
Hosts []string
// Assets is a list of BIP IDs for acceptable assets. A zero-length Assets
// means all assets are accepted.
Assets []uint32
// Statuses is a list of acceptable statuses. A zero-length Statuses means
// all statuses are accepted.
Statuses []order.OrderStatus
}
// noteKeySize must be <= 32.
const noteKeySize = 8
// noteKey creates a unique key from the hash of the supplied bytes.
func noteKey(b []byte) []byte {
h := blake2s.Sum256(b)
return h[:noteKeySize]
}
// hashKey creates a unique key from the hash of the supplied bytes.
func hashKey(b []byte) []byte {
h := blake2s.Sum256(b)
return h[:]
}