1 package domain
2 3 import (
4 "encoding/binary"
5 "errors"
6 "fmt"
7 "sort"
8 )
9 10 // ErrNoSentTx is a transaction this wallet has no record of sending.
11 var ErrNoSentTx = errors.New("domain: this wallet has no record of that transaction")
12 13 // Sent is a transaction this wallet built and broadcast.
14 //
15 // It is kept because the wallet is now the only place that knows the bytes: a node wallet's
16 // gettransaction is not available, and a recovery or a receipt that needs the raw hex of what
17 // was sent has nowhere else to look. The amount and the destination are what a recovery matches
18 // on, and they are stored rather than re-decoded so a listing does not decode every send.
19 type Sent struct {
20 // Chain is the wallet chain that sent it.
21 Chain string
22 // TxID is the transaction id as it is printed.
23 TxID string
24 // Hex is the raw transaction, witnesses included.
25 Hex string
26 // Address is where the payment went.
27 Address string
28 // Amount is what the payment carried, in satoshis.
29 Amount uint64
30 // Fee is what the transaction paid, in satoshis.
31 Fee uint64
32 // Vout is the payment's output index.
33 Vout uint32
34 // Height is the confirming block, or zero while unconfirmed.
35 Height uint32
36 }
37 38 // sentKey names one sent transaction.
39 func sentKey(name, txid string) string { return "sent/" + name + "/" + txid }
40 41 // PutSent encodes a sent-transaction record.
42 func PutSent(sent Sent) []byte {
43 out := putLength(nil, []byte(sent.Address))
44 out = putLength(out, []byte(sent.TxID))
45 out = putLength(out, []byte(sent.Hex))
46 out = binary.LittleEndian.AppendUint64(out, sent.Amount)
47 out = binary.LittleEndian.AppendUint64(out, sent.Fee)
48 49 return binary.LittleEndian.AppendUint32(binary.LittleEndian.AppendUint32(out, sent.Vout), sent.Height)
50 }
51 52 // ReadSent decodes one, refusing a record whose lengths do not add up.
53 func ReadSent(name string, raw []byte) (Sent, error) {
54 sent := Sent{Chain: name}
55 rest := raw
56 57 var err error
58 59 for _, field := range []*string{&sent.Address, &sent.TxID, &sent.Hex} {
60 var value []byte
61 62 value, rest, err = readLength(rest)
63 64 if err != nil {
65 return Sent{}, err
66 }
67 68 *field = string(value)
69 }
70 71 if len(rest) != 24 {
72 return Sent{}, fmt.Errorf("domain: a sent record ends with %d bytes", len(rest))
73 }
74 75 sent.Amount = binary.LittleEndian.Uint64(rest[0:8])
76 sent.Fee = binary.LittleEndian.Uint64(rest[8:16])
77 sent.Vout = binary.LittleEndian.Uint32(rest[16:20])
78 sent.Height = binary.LittleEndian.Uint32(rest[20:24])
79 80 return sent, nil
81 }
82 83 // RecordSent remembers a transaction the wallet broadcast.
84 func (d *Domain) RecordSent(sent Sent) error {
85 if sent.Chain == "" || sent.TxID == "" || sent.Hex == "" {
86 return fmt.Errorf("domain: a sent record needs a chain, a txid and its bytes")
87 }
88 89 return d.store.Put(sentKey(sent.Chain, sent.TxID), PutSent(sent))
90 }
91 92 // SentTx reads one sent transaction, if the wallet has it.
93 func (d *Domain) SentTx(name, txid string) (Sent, error) {
94 raw, ok := d.store.Get(sentKey(name, txid))
95 96 if !ok {
97 return Sent{}, fmt.Errorf("%w: %s", ErrNoSentTx, txid)
98 }
99 100 return ReadSent(name, raw)
101 }
102 103 // Sends lists a chain's sent transactions, newest txid last so two runs agree.
104 func (d *Domain) Sends(name string) ([]Sent, error) {
105 prefix := "sent/" + name + "/"
106 107 keys := d.store.Keys(prefix)
108 109 sent := make([]Sent, 0, len(keys))
110 111 for _, key := range keys {
112 raw, ok := d.store.Get(key)
113 114 if !ok {
115 continue
116 }
117 118 record, err := ReadSent(name, raw)
119 120 if err != nil {
121 return nil, err
122 }
123 124 sent = append(sent, record)
125 }
126 127 sort.Slice(sent, func(i, j int) bool { return sent[i].TxID < sent[j].TxID })
128 129 return sent, nil
130 }
131 132 // putLength appends a length-prefixed field.
133 func putLength(out, value []byte) []byte {
134 out = binary.LittleEndian.AppendUint16(out, uint16(len(value)))
135 136 return append(out, value...)
137 }
138 139 // readLength reads one, refusing a length that runs past the record.
140 func readLength(raw []byte) ([]byte, []byte, error) {
141 if len(raw) < 2 {
142 return nil, nil, fmt.Errorf("domain: a sent record ends inside a length")
143 }
144 145 length := int(binary.LittleEndian.Uint16(raw))
146 147 if len(raw) < 2+length {
148 return nil, nil, fmt.Errorf("domain: a %d-byte field runs past the record", length)
149 }
150 151 return raw[2 : 2+length], raw[2+length:], nil
152 }
153