package domain import ( "encoding/binary" "errors" "fmt" "sort" ) // ErrNoSentTx is a transaction this wallet has no record of sending. var ErrNoSentTx = errors.New("domain: this wallet has no record of that transaction") // Sent is a transaction this wallet built and broadcast. // // It is kept because the wallet is now the only place that knows the bytes: a node wallet's // gettransaction is not available, and a recovery or a receipt that needs the raw hex of what // was sent has nowhere else to look. The amount and the destination are what a recovery matches // on, and they are stored rather than re-decoded so a listing does not decode every send. type Sent struct { // Chain is the wallet chain that sent it. Chain string // TxID is the transaction id as it is printed. TxID string // Hex is the raw transaction, witnesses included. Hex string // Address is where the payment went. Address string // Amount is what the payment carried, in satoshis. Amount uint64 // Fee is what the transaction paid, in satoshis. Fee uint64 // Vout is the payment's output index. Vout uint32 // Height is the confirming block, or zero while unconfirmed. Height uint32 } // sentKey names one sent transaction. func sentKey(name, txid string) string { return "sent/" + name + "/" + txid } // PutSent encodes a sent-transaction record. func PutSent(sent Sent) []byte { out := putLength(nil, []byte(sent.Address)) out = putLength(out, []byte(sent.TxID)) out = putLength(out, []byte(sent.Hex)) out = binary.LittleEndian.AppendUint64(out, sent.Amount) out = binary.LittleEndian.AppendUint64(out, sent.Fee) return binary.LittleEndian.AppendUint32(binary.LittleEndian.AppendUint32(out, sent.Vout), sent.Height) } // ReadSent decodes one, refusing a record whose lengths do not add up. func ReadSent(name string, raw []byte) (Sent, error) { sent := Sent{Chain: name} rest := raw var err error for _, field := range []*string{&sent.Address, &sent.TxID, &sent.Hex} { var value []byte value, rest, err = readLength(rest) if err != nil { return Sent{}, err } *field = string(value) } if len(rest) != 24 { return Sent{}, fmt.Errorf("domain: a sent record ends with %d bytes", len(rest)) } sent.Amount = binary.LittleEndian.Uint64(rest[0:8]) sent.Fee = binary.LittleEndian.Uint64(rest[8:16]) sent.Vout = binary.LittleEndian.Uint32(rest[16:20]) sent.Height = binary.LittleEndian.Uint32(rest[20:24]) return sent, nil } // RecordSent remembers a transaction the wallet broadcast. func (d *Domain) RecordSent(sent Sent) error { if sent.Chain == "" || sent.TxID == "" || sent.Hex == "" { return fmt.Errorf("domain: a sent record needs a chain, a txid and its bytes") } return d.store.Put(sentKey(sent.Chain, sent.TxID), PutSent(sent)) } // SentTx reads one sent transaction, if the wallet has it. func (d *Domain) SentTx(name, txid string) (Sent, error) { raw, ok := d.store.Get(sentKey(name, txid)) if !ok { return Sent{}, fmt.Errorf("%w: %s", ErrNoSentTx, txid) } return ReadSent(name, raw) } // Sends lists a chain's sent transactions, newest txid last so two runs agree. func (d *Domain) Sends(name string) ([]Sent, error) { prefix := "sent/" + name + "/" keys := d.store.Keys(prefix) sent := make([]Sent, 0, len(keys)) for _, key := range keys { raw, ok := d.store.Get(key) if !ok { continue } record, err := ReadSent(name, raw) if err != nil { return nil, err } sent = append(sent, record) } sort.Slice(sent, func(i, j int) bool { return sent[i].TxID < sent[j].TxID }) return sent, nil } // putLength appends a length-prefixed field. func putLength(out, value []byte) []byte { out = binary.LittleEndian.AppendUint16(out, uint16(len(value))) return append(out, value...) } // readLength reads one, refusing a length that runs past the record. func readLength(raw []byte) ([]byte, []byte, error) { if len(raw) < 2 { return nil, nil, fmt.Errorf("domain: a sent record ends inside a length") } length := int(binary.LittleEndian.Uint16(raw)) if len(raw) < 2+length { return nil, nil, fmt.Errorf("domain: a %d-byte field runs past the record", length) } return raw[2 : 2+length], raw[2+length:], nil }