// Package rental implements time-boxed streaming access. // // A rental is a payment for a window, not an entitlement. The host opens a // window keyed by (buyer, asset) when a NUT-24 payment settles, serves inside // it, and issues a receipt with mode = stream. When the window expires the next // request is challenged again. Ownership is never modelled here: a fan set or a // deed is the only durable entitlement. package rental import ( "context" "encoding/json" "fmt" "strconv" "sync" "time" "git.smesh.lol/orly/pkg/musiquay/deed" "git.smesh.lol/orly/pkg/musiquay/nutreq" "git.smesh.lol/orly/pkg/nostr/encoders/hex" ) // Store persists rental windows. database.Database satisfies it. type Store interface { GetMarker(key string) ([]byte, error) SetMarker(key string, value []byte) error } // Window is one buyer's access to one asset until Expires. type Window struct { Buyer string `json:"buyer"` Asset string `json:"asset"` Expires int64 `json:"expires"` Amount int64 `json:"amount_sats"` Token string `json:"token_hash,omitempty"` } func windowKey(buyerHex, assetID string) string { return "musiquay:rental:" + buyerHex + ":" + assetID } // Manager opens and checks rental windows. type Manager struct { store Store bucket time.Duration mu sync.Mutex now func() time.Time } // New creates a manager. bucket is the maximum window length; 600 seconds is // the suggested host policy. func New(store Store, bucket time.Duration) *Manager { if bucket <= 0 { bucket = 10 * time.Minute } return &Manager{store: store, bucket: bucket, now: time.Now} } // Bucket is the host's window length. func (m *Manager) Bucket() time.Duration { return m.bucket } // Challenge builds the NUT-24 request for one bucket. func (m *Manager) Challenge(assetID string, priceSats int64, mintURL string) (string, error) { if !deed.IsAssetID(assetID) { return "", fmt.Errorf("rental: invalid asset id") } if priceSats <= 0 { return "", fmt.Errorf("rental: price must be positive") } return nutreq.New(uint64(priceSats), mintURL, "musiquay rental").EncodeA() } // Open records a paid window. The expiry is capped at now+bucket so a client // cannot buy an arbitrarily long rental at one bucket's price. func (m *Manager) Open(ctx context.Context, buyer []byte, assetID string, requested time.Duration, amountSats int64) (*Window, error) { if len(buyer) != 32 { return nil, fmt.Errorf("rental: invalid buyer") } if !deed.IsAssetID(assetID) { return nil, fmt.Errorf("rental: invalid asset id") } if requested <= 0 || requested > m.bucket { requested = m.bucket } w := &Window{ Buyer: hex.Enc(buyer), Asset: assetID, Expires: m.now().Add(requested).Unix(), Amount: amountSats, } b, err := json.Marshal(w) if err != nil { return nil, err } m.mu.Lock() err = m.store.SetMarker(windowKey(w.Buyer, assetID), b) m.mu.Unlock() if err != nil { return nil, err } return w, nil } // Valid reports whether unexpired access exists. func (m *Manager) Valid(ctx context.Context, buyer []byte, assetID string) bool { w := m.window(buyer, assetID) if w == nil { return false } return w.Expires > m.now().Unix() } // Remaining returns the seconds left, or zero. func (m *Manager) Remaining(ctx context.Context, buyer []byte, assetID string) int64 { w := m.window(buyer, assetID) if w == nil { return 0 } left := w.Expires - m.now().Unix() if left < 0 { return 0 } return left } func (m *Manager) window(buyer []byte, assetID string) *Window { if len(buyer) != 32 { return nil } b, err := m.store.GetMarker(windowKey(hex.Enc(buyer), assetID)) if err != nil || len(b) == 0 { return nil } var w Window if err = json.Unmarshal(b, &w); err != nil { return nil } return &w } // ReceiptAmount returns the settled amount and the window for a stream receipt. func (m *Manager) ReceiptAmount(buyer []byte, assetID string) (int64, int64) { w := m.window(buyer, assetID) if w == nil { return 0, 0 } return w.Amount, w.Expires } // FormatDuration is a small helper for the challenge description. func FormatDuration(d time.Duration) string { return strconv.FormatInt(int64(d/time.Second), 10) + "s" }