package receipts import ( "context" "encoding/json" "fmt" "time" "git.smesh.lol/orly/pkg/nostr/encoders/event" "git.smesh.lol/orly/pkg/nostr/encoders/hex" "git.smesh.lol/orly/pkg/nostr/interfaces/signer" "git.smesh.lol/orly/pkg/nostr/protocol/nip59" ) // Publisher publishes one event. A co-located host saves wraps to the relay's // database; a remote host sends them over NIP-01. type Publisher interface { Publish(ctx context.Context, ev *event.E) error } // Issuer produces, publishes, and archives delivery receipts. type Issuer struct { // Sign is the host's dedicated receipt key. It is never the relay identity. Sign signer.I // Archive is the durable receipt store. Archive *Archive // Publisher delivers the two wraps. Publisher Publisher // PublisherPub, when set, receives the second copy. The publisher is the // durable accounting authority, so this copy is not optional in production. PublisherPub []byte // Now overrides the clock. Now func() time.Time } // Serve is one granted download. type Serve struct { AssetID string Coordinate string Buyer []byte Blob string Host string Mode string Bytes int64 AmountSats int64 Paid []string } // Issue builds the receipt, wraps it to the buyer and the publisher, publishes // both wraps, and archives the receipt. // // Publication failures are returned, not swallowed: a serve without a receipt // breaks the accounting invariant, and the caller can decide to log or retry. // The archive write happens even when a wrap fails, so the publisher's copy is // never the only record. func (i *Issuer) Issue(ctx context.Context, s Serve) (*Issued, *Receipt, error) { if i == nil || i.Sign == nil || i.Archive == nil { return nil, nil, fmt.Errorf("receipts: issuer is not configured") } if len(s.Buyer) != 32 { return nil, nil, fmt.Errorf("receipts: serve has no buyer") } r := New(s.AssetID, s.Coordinate, hex.Enc(s.Buyer), s.Blob, s.Host, s.Mode, s.Bytes, s.AmountSats, s.Paid) now := time.Now() if i.Now != nil { now = i.Now() } r.Period = Period(now) issued, err := Issue(i.Sign, r, i.PublisherPub, nil) if err != nil { return nil, nil, err } if err = i.Archive.Add(r); err != nil { return issued, r, fmt.Errorf("receipts: archive: %w", err) } if i.Publisher != nil { if issued.ToBuyer != nil { if err = i.Publisher.Publish(ctx, issued.ToBuyer); err != nil { return issued, r, fmt.Errorf("receipts: publish buyer copy: %w", err) } } if issued.ToPublisher != nil { if err = i.Publisher.Publish(ctx, issued.ToPublisher); err != nil { return issued, r, fmt.Errorf("receipts: publish publisher copy: %w", err) } } } return issued, r, nil } // Unwrap is a helper for the buyer or publisher side: it verifies the seal and // returns the receipt. func Unwrap(sign signer.I, wrap *event.E, cfg *nip59.Config) (*Receipt, *event.E, error) { u, err := nip59.Unwrap(sign, wrap, cfg) if err != nil { return nil, nil, err } if u.Payload.Kind != 32218 { return nil, nil, fmt.Errorf("receipts: payload is not a delivery receipt") } var r Receipt if err = json.Unmarshal(u.Payload.Content, &r); err != nil { return nil, nil, fmt.Errorf("receipts: decode receipt: %w", err) } if err = r.Validate(); err != nil { return nil, nil, err } return &r, u.Seal, nil }