Phase 2: money

Lightning payments through LNbits, wallets, passes, credits, the
entitlement resolver and priority lanes.

- internal/entitle: pure resolver (one-off, own pass, group pass, group
  credits, own credits, free) with a table-driven suite and an invariant
  check over every input combination
- internal/billing: wallets, append-only ledger with dedupe keys, stacking
  passes, purchases, gift codes, referral reward
- LNbits client verified against v0.12.12 and v1.7.1 source
- Settlement by one conditional UPDATE: idempotent under any mix of
  webhooks, polls and the check button. Webhooks are unsigned, so every
  payment is confirmed with LNbits before it is credited
- Polling fallback, reconciliation of all unsettled invoices at startup,
  late payments still honored, no expiry while LNbits is unreachable
- Charge, quota and job are written in one transaction; failed, cancelled
  and undeliverable downloads are refunded exactly once
- Paid lane first with one slot of the fleet reserved for free users
- Daily download quota and daily volume cap, per-user and per-group rate
  limits
- Bot: /buy /account /limits /credits /redeem /referral /donate, QR invoice
  message that flips to paid, over-limit offer (passes or this file only),
  deep links from groups, skip the queue
- Fake LNbits server for tests; end-to-end money flows including a payment
  made while the core was down

Cashu redemption is behind an interface and not implemented.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Michilis
2026-11-18 08:09:35 +00:00
co-authored by Claude Fable 5.2
parent 3b361c17a1
commit 0d50f453a0
42 changed files with 7040 additions and 140 deletions
+60
View File
@@ -0,0 +1,60 @@
package bot
import (
"crypto/sha256"
"encoding/hex"
"strings"
"sync"
"time"
)
// rateLimiter is a sliding window counter per key. It lives in memory: a
// restart forgets it, which is fine for a limit measured in a minute.
type rateLimiter struct {
mu sync.Mutex
hits map[string][]time.Time
now func() time.Time
}
func newRateLimiter() *rateLimiter {
return &rateLimiter{hits: map[string][]time.Time{}, now: time.Now}
}
// allow records a hit for key and reports whether it stays within limit
// hits per window. A limit of 0 or less allows everything.
func (r *rateLimiter) allow(key string, limit int, window time.Duration) bool {
if limit <= 0 {
return true
}
now := r.now()
cut := now.Add(-window)
r.mu.Lock()
defer r.mu.Unlock()
kept := r.hits[key][:0]
for _, t := range r.hits[key] {
if t.After(cut) {
kept = append(kept, t)
}
}
if len(kept) >= limit {
r.hits[key] = kept
return false
}
r.hits[key] = append(kept, now)
// Keep the map from growing without bound.
if len(r.hits) > 50_000 {
for k, v := range r.hits {
if len(v) == 0 || !v[len(v)-1].After(cut) {
delete(r.hits, k)
}
}
}
return true
}
// tokenDigest returns a stable fingerprint of a bearer token, so the token
// itself is never stored or logged.
func tokenDigest(token string) string {
sum := sha256.Sum256([]byte(strings.TrimSpace(token)))
return hex.EncodeToString(sum[:16])
}