package bridge import ( "context" "fmt" "strconv" "time" "git.smesh.lol/nostrino/pkg/listing" "git.smesh.lol/nostrino/pkg/nwc" "git.smesh.lol/nostrino/pkg/relay" ) // wallet opens the operator's NWC connection. Every money path goes through here, so there is one // place that knows how a wallet is reached and what it means to have none configured. func (s *Server) wallet(ctx context.Context) (*nwc.Client, error) { s.billingMu.Lock() connection := s.billing.Wallet s.billingMu.Unlock() if connection == "" { return nil, fmt.Errorf("no wallet is configured: send billing-set -arg wallet=nostr+walletconnect://…") } conn, err := nwc.ParseURI(connection) if err != nil { return nil, err } transport, err := relay.DialTransport(conn.RelayURL) if err != nil { return nil, err } return nwc.Dial(ctx, conn, transport) } // quoteListing prices a swap and asks the wallet for an invoice for it. The listing is not lodged // until that invoice settles, which is what quoteListingIDLodged reports. func (s *Server) quoteListing(ctx context.Context, id string, btcSats int64) (map[string]any, error) { if id == "" { return nil, fmt.Errorf("listing-quote needs -arg id=") } if btcSats <= 0 { return nil, fmt.Errorf("listing-quote needs -arg btc_sats=") } s.billingMu.Lock() fee := s.billing.ListingFee(btcSats) margin := s.billing.SwapFeeBPS s.billingMu.Unlock() reply := map[string]any{"id": id, "btc_sats": btcSats, "fee_sats": fee, "swap_fee_bps": margin} // A gateway that charges nothing has nothing to gate. if fee == 0 { reply["lodged"] = true reply["free"] = true return reply, nil } if s.listings != nil && s.listings.Lodged(id) { reply["lodged"] = true reply["already_paid"] = true return reply, nil } client, err := s.wallet(ctx) if err != nil { return nil, err } defer client.Close() made, err := client.MakeInvoice(ctx, fee, "listing "+id) if err != nil { return nil, fmt.Errorf("the wallet did not make an invoice: %w", err) } // The wallet's payment hash is the reference the listing is bound to. hash := made.PaymentHash invoice := made.Invoice if s.listings != nil { if err := s.listings.Record(listing.Quote{ ID: id, PaymentHash: hash, Invoice: invoice, AmountSats: fee, FeeBPS: margin, CreatedAt: time.Now().UTC().Format(time.RFC3339), }); err != nil { return nil, err } } reply["invoice"] = invoice reply["payment_hash"] = hash reply["lodged"] = false return reply, nil } // settleListing asks the wallet whether the invoice was paid, and records the answer. func (s *Server) settleListing(ctx context.Context, id string) (map[string]any, error) { if s.listings == nil { return nil, fmt.Errorf("this gateway keeps no listing quotes") } quote, ok := s.listings.Quotes[id] if !ok { return nil, fmt.Errorf("no quote for %s", id) } if s.listings.Lodged(id) { return map[string]any{"id": id, "lodged": true, "paid_at": quote.PaidAt}, nil } client, err := s.wallet(ctx) if err != nil { return nil, err } defer client.Close() invoice, err := client.LookupInvoice(ctx, quote.PaymentHash) if err != nil { return nil, err } settled := invoice.SettledAt > 0 || invoice.Preimage != "" if !settled { return map[string]any{"id": id, "lodged": false, "fee_sats": quote.AmountSats}, nil } paidAt := time.Now() if invoice.SettledAt > 0 { paidAt = time.Unix(invoice.SettledAt, 0) } if err := s.listings.Settle(id, paidAt); err != nil { return nil, err } return map[string]any{"id": id, "lodged": true, "fee_sats": quote.AmountSats, "paid_at": paidAt.UTC().Format(time.RFC3339)}, nil } // listingStatus reports what is waiting for payment. func (s *Server) listingStatus() map[string]any { if s.listings == nil { return map[string]any{"pending": []any{}} } pending := s.listings.Pending() out := make([]map[string]any, 0, len(pending)) for _, quote := range pending { out = append(out, map[string]any{ "id": quote.ID, "fee_sats": quote.AmountSats, "payment_hash": quote.PaymentHash, "created_at": quote.CreatedAt, }) } return map[string]any{"pending": out} } func parseSats(value string) (int64, error) { n, err := strconv.ParseInt(value, 10, 64) if err != nil { return 0, fmt.Errorf("%q is not a whole number of sats", value) } if n < 0 { return 0, fmt.Errorf("a negative amount is not a price") } return n, nil }