// Command nostrino-relay-billing turns incoming wallet payments into paid gateway time. // // It is the piece that closes the loop between a relay's wallet and the ledger the relay reads: it // asks the wallet for recent incoming payments, credits whichever gateway paid (the keysend // preimage is that gateway's public key), and writes the ledger file. The relay and this process // agree by reading one document, so neither has to talk to the other while serving clients. package main import ( "context" "flag" "fmt" "os" "time" "git.smesh.lol/nostrino/pkg/billing" "git.smesh.lol/nostrino/pkg/nwc" "git.smesh.lol/nostrino/pkg/relay" ) func main() { var ( wallet = flag.String("wallet", "", "NIP-47 connection string for the relay's wallet") wallet2 = flag.String("wallet-file", "", "file holding the NIP-47 connection string (preferred: keeps it out of ps)") ledger = flag.String("ledger", "", "ledger file the relay reads (default: /subscriptions.json)") rate = flag.Int64("rate", 0, "sats per day of access; zero keeps the ledger's own rate") limit = flag.Int("limit", 50, "how many recent payments to scan") once = flag.Bool("once", false, "scan once and exit") every = flag.Duration("every", 5*time.Minute, "how often to scan when not -once") ) flag.Parse() if *ledger == "" { fmt.Fprintln(os.Stderr, "nostrino-relay-billing: -ledger is required (the relay reads it)") os.Exit(2) } connection, err := walletString(*wallet, *wallet2) if err != nil { fail(err) } conn, err := nwc.ParseURI(connection) if err != nil { fail(err) } for { paid, err := scan(conn, *ledger, *rate, *limit) if err != nil { fmt.Fprintln(os.Stderr, "nostrino-relay-billing:", err) } else { fmt.Printf("relay-billing: %d gateway(s) paid up, ledger %s\n", paid, *ledger) } if *once { if err != nil { os.Exit(1) } return } time.Sleep(*every) } } // scan credits one pass over the wallet's incoming payments. func scan(conn nwc.Connection, ledgerPath string, rate int64, limit int) (int, error) { ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second) defer cancel() transport, err := relay.DialTransport(conn.RelayURL) if err != nil { return 0, err } client, err := nwc.Dial(ctx, conn, transport) if err != nil { return 0, err } defer client.Close() if _, err := client.GetInfo(ctx); err != nil { return 0, fmt.Errorf("wallet is not answering: %w", err) } payments, err := client.ListTransactions(ctx, limit) if err != nil { return 0, err } ledger, err := billing.OpenLedger(ledgerPath, rate) if err != nil { return 0, err } if err := credit(ledger, payments, time.Now()); err != nil { return 0, err } if err := ledger.Save(); err != nil { return 0, err } paid := 0 for _, subscription := range ledger.Subscriptions { if ledger.Paid(subscription.Gateway, time.Now()) { paid++ } } return paid, nil } // credit applies one pass of payments to the ledger. A payment is matched to a gateway by its // preimage, which the gateway chose to be its own public key. func credit(ledger *billing.Ledger, payments []nwc.Transaction, now time.Time) error { for _, payment := range payments { if payment.Type != "" && payment.Type != "incoming" { continue } if len(payment.Preimage) != 64 { // An ordinary invoice payment from a human: not a gateway subscription. continue } if _, err := ledger.Credit(payment.Preimage, payment.AmountSats, payment.SettledAt, now); err != nil { // A payment too small to buy a second, or a rate that is not set: report and keep // going, because one odd payment must not stop the rest of the pass. fmt.Fprintf(os.Stderr, "nostrino-relay-billing: %v\n", err) } } return nil } func walletString(inline, path string) (string, error) { if path != "" { body, err := os.ReadFile(path) if err != nil { return "", fmt.Errorf("wallet file: %w", err) } return string(body), nil } if inline != "" { return inline, nil } return "", fmt.Errorf("need -wallet or -wallet-file") } func fail(err error) { fmt.Fprintln(os.Stderr, "nostrino-relay-billing:", err) os.Exit(1) }