issuer.go raw
1 package receipts
2
3 import (
4 "context"
5 "encoding/json"
6 "fmt"
7 "time"
8
9 "git.smesh.lol/orly/pkg/nostr/encoders/event"
10 "git.smesh.lol/orly/pkg/nostr/encoders/hex"
11 "git.smesh.lol/orly/pkg/nostr/interfaces/signer"
12 "git.smesh.lol/orly/pkg/nostr/protocol/nip59"
13 )
14
15 // Publisher publishes one event. A co-located host saves wraps to the relay's
16 // database; a remote host sends them over NIP-01.
17 type Publisher interface {
18 Publish(ctx context.Context, ev *event.E) error
19 }
20
21 // Issuer produces, publishes, and archives delivery receipts.
22 type Issuer struct {
23 // Sign is the host's dedicated receipt key. It is never the relay identity.
24 Sign signer.I
25 // Archive is the durable receipt store.
26 Archive *Archive
27 // Publisher delivers the two wraps.
28 Publisher Publisher
29 // PublisherPub, when set, receives the second copy. The publisher is the
30 // durable accounting authority, so this copy is not optional in production.
31 PublisherPub []byte
32 // Now overrides the clock.
33 Now func() time.Time
34 }
35
36 // Serve is one granted download.
37 type Serve struct {
38 AssetID string
39 Coordinate string
40 Buyer []byte
41 Blob string
42 Host string
43 Mode string
44 Bytes int64
45 AmountSats int64
46 Paid []string
47 }
48
49 // Issue builds the receipt, wraps it to the buyer and the publisher, publishes
50 // both wraps, and archives the receipt.
51 //
52 // Publication failures are returned, not swallowed: a serve without a receipt
53 // breaks the accounting invariant, and the caller can decide to log or retry.
54 // The archive write happens even when a wrap fails, so the publisher's copy is
55 // never the only record.
56 func (i *Issuer) Issue(ctx context.Context, s Serve) (*Issued, *Receipt, error) {
57 if i == nil || i.Sign == nil || i.Archive == nil {
58 return nil, nil, fmt.Errorf("receipts: issuer is not configured")
59 }
60 if len(s.Buyer) != 32 {
61 return nil, nil, fmt.Errorf("receipts: serve has no buyer")
62 }
63 r := New(s.AssetID, s.Coordinate, hex.Enc(s.Buyer), s.Blob, s.Host, s.Mode,
64 s.Bytes, s.AmountSats, s.Paid)
65 now := time.Now()
66 if i.Now != nil {
67 now = i.Now()
68 }
69 r.Period = Period(now)
70
71 issued, err := Issue(i.Sign, r, i.PublisherPub, nil)
72 if err != nil {
73 return nil, nil, err
74 }
75 if err = i.Archive.Add(r); err != nil {
76 return issued, r, fmt.Errorf("receipts: archive: %w", err)
77 }
78 if i.Publisher != nil {
79 if issued.ToBuyer != nil {
80 if err = i.Publisher.Publish(ctx, issued.ToBuyer); err != nil {
81 return issued, r, fmt.Errorf("receipts: publish buyer copy: %w", err)
82 }
83 }
84 if issued.ToPublisher != nil {
85 if err = i.Publisher.Publish(ctx, issued.ToPublisher); err != nil {
86 return issued, r, fmt.Errorf("receipts: publish publisher copy: %w", err)
87 }
88 }
89 }
90 return issued, r, nil
91 }
92
93 // Unwrap is a helper for the buyer or publisher side: it verifies the seal and
94 // returns the receipt.
95 func Unwrap(sign signer.I, wrap *event.E, cfg *nip59.Config) (*Receipt, *event.E, error) {
96 u, err := nip59.Unwrap(sign, wrap, cfg)
97 if err != nil {
98 return nil, nil, err
99 }
100 if u.Payload.Kind != 32218 {
101 return nil, nil, fmt.Errorf("receipts: payload is not a delivery receipt")
102 }
103 var r Receipt
104 if err = json.Unmarshal(u.Payload.Content, &r); err != nil {
105 return nil, nil, fmt.Errorf("receipts: decode receipt: %w", err)
106 }
107 if err = r.Validate(); err != nil {
108 return nil, nil, err
109 }
110 return &r, u.Seal, nil
111 }
112