Skip to content

chore: merge main into dev (payment_hash hotfix #126) - #127

Open
cmnisal wants to merge 23 commits into
devfrom
claude/bitcoin-deepa-frontend-test-sync-neexk3
Open

cmnisal wants to merge 23 commits into
devfrom
claude/bitcoin-deepa-frontend-test-sync-neexk3

Conversation

@cmnisal

@cmnisal cmnisal commented Nov 21, 2026

Copy link
Copy Markdown
Member

Summary

Brings the launch-day hotfix from main into dev, which didn't have it:

The DCA backend's sell flow uses this hash to match a sale to its collect transfer in /api/v1/usertransactions.

Conflicts

main received these features through separate -main branches, so the merge had add/add conflicts in 4 files: internal/api/p2p.go, internal/api/p2p_test.go, docs/p2p-api.md, docs/dca-backend-integration.md. In every hunk main's side was dev's text plus the payment_hash lines, so main's side was taken. The hotfix files now match main exactly; dev-only work (standing orders, Lightning usertransactions, etc.) is untouched.

Testing

  • go build ./... and go vet ./internal/api/... pass.
  • go test ./internal/... passes, including the new TestTransferResponse check for internal payment_hash.

Generated by Claude Code

xbuddhi and others added 23 commits November 20, 2026 00:49
* feat: send sats and pay invoices from the Telegram Mini App

New /api/v1/p2p/* API for the Mini App, off by default (api.p2p.enabled).

The sender is always the Telegram user in the Mini App's signed initData,
verified with the bot token (internal/tma). Nothing else in a request can
name the sender, so a relaying backend cannot send on anyone's behalf.

Endpoints: me, resolve, send, invoice/decode, invoice/pay, transfers/{id}.

- Recipients are resolved to a numeric Telegram ID and paid by ID, never by
  username. Only users who started the bot can receive; no self-sends;
  banned users can neither send nor receive.
- Limits: 100k sats per transfer, 500k per sender per UTC day (atomic
  counter, refunded when a transfer fails), 10 payments per minute.
- Above 50k sats the sender confirms in the bot chat (Confirm/Cancel,
  10 minutes). Confirm, cancel and expiry share a one-time claim, so exactly
  one of them happens.
- idempotency_key: a retry returns the same transfer and can never pay twice.
- Invoices: the same checks as the send API (not already paid, fee reserve,
  settlement before reporting success). Each invoice can be paid once.
  Payment runs in the background; the request waits up to 15 s, then
  reports in_flight for the client to poll.

docs/p2p-api.md is the contract for the Mini App.

* feat: require a service signature and fresh init data for Mini App spends

- The Mini App server signs every /api/v1/p2p call with Ed25519. The
  signature covers method, path, query, timestamp, nonce, init data and body,
  and each nonce works once. P2P stays off until service_public_key is set.
- /send and /invoice/pay accept init data up to spend_init_data_max_age
  (default 10 minutes); read endpoints keep init_data_max_age.

* fix: address P2P review findings

- Release the invoice lock on every failure path: the lock holds the
  transfer ID, only its owner releases it, and stale holders are cleared.
- After an internal send errors, ask lnbits whether it paid before calling
  it failed; unknown outcomes become in_flight without a refund.
- Apply each final outcome once (one claim per transfer), so overlapping
  polls can't refund or notify twice; a late save can't undo a final status.
- Add api.p2p.allowed_networks and check it on every P2P route.
- Count small sends against a daily no-confirmation allowance, so splitting
  a payment doesn't skip confirmation.
- Resolve transfers stuck in processing after a restart; save the internal
  invoice hash before paying so they can be checked.
- Rate-limit transfer polls and check lnbits at most every 20s per transfer.
- Run bot-chat confirmations in the background with panic recovery.
- Answer recipient_not_found for users who can't receive.
- Give transfer records a 30-day TTL; show expiry as minutes left.
- Fix the idempotency and polling docs; add tests, including the review's
  repro tests.

* feat: let the web app send P2P with Log In with Telegram

The web app logs users in with Telegram OIDC. P2P calls can now carry the
ID token (X-Telegram-ID-Token) instead of Mini App init data.

- Verify ID tokens against Telegram's JWKS (RS256, ES256, EdDSA) with the
  standard library: issuer, audience (this bot), expiry, and login time.
- The user is read from the id claim (needs the profile scope).
- The same age limits apply: 1 hour for reads, 10 minutes for spends.
- The service signature now covers whichever credential is sent.
- Off by default (api.p2p.oidc_enabled).

* fix: start the API server after the P2P routes

After rebasing onto the IP allowlist PR, the P2P routes were added after
s.ListenAndServe(), which brings back the router race that PR fixed. Start
serving after them. Note that api.allowed_networks also covers
/api/v1/p2p/*.

* docs: DCA backend integration guide; DCA backend serves the Mini App and web app

- Add docs/dca-backend-integration.md: payouts (send key), P2P relay (P2P
  key), the reset-retry-counts callback, errors, payout flow, and the code
  changes and migration checklist for the DCA backend.
- The DCA backend, not a separate TMA server, relays Mini App and web app
  P2P calls. Its P2P key is separate from its send wallet key.
#86)

* feat: replace send API HMAC with Ed25519 request signing

Wallet API callers now sign requests with an Ed25519 private key. The bot
stores only the public key, so a config leak cannot be used to forge sends.
HMAC is no longer accepted on the wallet routes.

- Sign method, path, query, timestamp, nonce and body hash.
- Each nonce works once; timestamps must be within the tolerance.
- Cap signed bodies at 1 MiB.
- Require ed25519_public_key per wallet; a leftover hmac_secret disables
  the send API with an error.
- Add docs/send-api-ed25519.md with bash, Node.js and Python signers.

* feat: move the analytics API from HMAC to Ed25519 signing

- Analytics keys now use ed25519_public_key; a leftover hmac_secret
  disables the analytics API with an error.
- Requests carry X-Key-ID, X-Timestamp, X-Nonce and X-Signature. The
  signing string starts with BDB-ANALYTICS-ED25519-V1, so a send API
  signature is not valid here.
- Share one verifier between the send and analytics APIs. Analytics now
  also rejects future timestamps and replayed nonces.
- Remove the last HMAC request-signing code.

* docs: remove leftover HMAC signing text from the invoice payments guide

* fix: address send API signing review

- Refuse weak (small-order) Ed25519 public keys, which verify forged
  signatures.
- Keep each nonce until its signed timestamp expires.
- Check timestamp, nonce and signature format before the key lookup, so
  wallet IDs can't be probed.
- Sign the path exactly as sent (percent-encoded), like the query.
- 413 only for bodies over 1 MiB; other read errors are 400.
- Port send_api_test.sh and analytics_api_test.sh to Ed25519.
- Docs: one key pair per environment.

* refactor: make the weak Ed25519 key check self-contained

So the P2P PR can share the same file without a merge conflict.

* style: sort imports in config.go
* feat: replace send API HMAC with Ed25519 request signing

Wallet API callers now sign requests with an Ed25519 private key. The bot
stores only the public key, so a config leak cannot be used to forge sends.
HMAC is no longer accepted on the wallet routes.

- Sign method, path, query, timestamp, nonce and body hash.
- Each nonce works once; timestamps must be within the tolerance.
- Cap signed bodies at 1 MiB.
- Require ed25519_public_key per wallet; a leftover hmac_secret disables
  the send API with an error.
- Add docs/send-api-ed25519.md with bash, Node.js and Python signers.

* feat: move the analytics API from HMAC to Ed25519 signing

- Analytics keys now use ed25519_public_key; a leftover hmac_secret
  disables the analytics API with an error.
- Requests carry X-Key-ID, X-Timestamp, X-Nonce and X-Signature. The
  signing string starts with BDB-ANALYTICS-ED25519-V1, so a send API
  signature is not valid here.
- Share one verifier between the send and analytics APIs. Analytics now
  also rejects future timestamps and replayed nonces.
- Remove the last HMAC request-signing code.

* docs: remove leftover HMAC signing text from the invoice payments guide

* fix: address send API signing review

- Refuse weak (small-order) Ed25519 public keys, which verify forged
  signatures.
- Keep each nonce until its signed timestamp expires.
- Check timestamp, nonce and signature format before the key lookup, so
  wallet IDs can't be probed.
- Sign the path exactly as sent (percent-encoded), like the query.
- 413 only for bodies over 1 MiB; other read errors are 400.
- Port send_api_test.sh and analytics_api_test.sh to Ed25519.
- Docs: one key pair per environment.

* refactor: make the weak Ed25519 key check self-contained

So the P2P PR can share the same file without a merge conflict.

* style: sort imports in config.go

* fix: apply the signing review fixes to the P2P service signature

- Refuse a weak (small-order) service public key.
- Keep each nonce until its signed timestamp expires.
- Sign the path exactly as sent (percent-encoded).
- 413 for bodies over the limit and 400 for unreadable bodies, not 401.
* fix: apply the signing review fixes to the P2P service signature

- Refuse a weak (small-order) service public key.
- Keep each nonce until its signed timestamp expires.
- Sign the path exactly as sent (percent-encoded).
- 413 for bodies over the limit and 400 for unreadable bodies, not 401.

* feat: collect API so wallets can ask users to pay them

A wallet with can_collect creates a request; the user approves it with
Pay in the bot chat or from the Mini App / web app. Nothing moves
without that approval. Approving creates a normal P2P transfer.

(cherry picked from commit 01615c9)

* docs: collect flow in the DCA backend guide

(cherry picked from commit 2072275)

* feat: collect requests can skip the bot chat prompt

POST /api/v1/collect takes "chat_prompt" (default true). With false,
the user gets no Pay / Decline message and approves in the Mini App or
web app only.

(cherry picked from commit a72a873)
* feat: create wallets for Mini App and web app users who never pressed Start

POST /api/v1/init creates the wallet from the verified Telegram
credential, like /start, with the same P2P service signature.

(cherry picked from commit 4e60b07)

* docs: init API guide

(cherry picked from commit 6e0a3e3)
* fix: use the full name when a Telegram login has no given name

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016uf9cmd4w65NVRsvrctidb
* feat: remove bot-chat confirmations from P2P and collect

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016uf9cmd4w65NVRsvrctidb
* feat: let the DCA backend name the user (P2P signature V2)
* docs: spec for DCA-issued sessions and bot-verified passkeys

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016uf9cmd4w65NVRsvrctidb
* docs: spec status - login-only Telegram check is built, passkeys planned
* feat: check the Telegram login only at login (/api/v1/init)
* docs: one complete DCA backend guide

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016uf9cmd4w65NVRsvrctidb
#114)

* feat: collect names the payer with signed X-BDB-User
* feat: collect charges the user at once, no approval

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016uf9cmd4w65NVRsvrctidb
* feat: X-BDB-User works for any bot user, no prior /api/v1/init

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016uf9cmd4w65NVRsvrctidb
…ns-lightning-main

feat: usertransactions includes Lightning payments
…ne-message-main

fix: one message for Lightning-address receipts
Removes omitempty, fills status, fee and payment_hash for internal
transfers, and uses the stored Lightning-address comment as the memo when
LNbits has none.
…ns-uniform-main

feat: every usertransactions entry has the same fields
hotfix(p2p): return payment_hash for internal transfers
Brings the launch-day hotfix from main: /send and executed approvals
return payment_hash for internal sends, and P2P transfers return it too.
Conflicts in p2p.go, p2p_test.go and the two docs took main's side
(dev's text plus the payment_hash additions).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016uf9cmdAPYqVDD75dT1NK4

@helloscoopa helloscoopa left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review

LGTM: my verdict is approve. I'm posting this as a comment because this session couldn't submit an approval.

What I checked

  • The 9-file diff against dev is just the #125/#126 payment_hash work, and dev-only work isn't touched.
  • t.Invoice is a value (lnbits.Invoice), not a pointer, so t.Invoice.PaymentHash can't nil-panic in Send, executePendingTransaction or approveAPITransactionHandler. All three read it only after t.Send() succeeded, and by then SendTransaction has set the invoice.
  • InternalPaymentHash is already on dev's P2PTransfer (set in telegram/p2p.go), so toTransferResponse compiles and gets real data.
  • Nothing in internal/api treats PaymentHash != "" as meaning "this is an invoice tx", so filling it in for internal sends doesn't send anything down the invoice path.
  • I merged the PR head into current dev (which has moved since the branch was cut; see the note below). It merges cleanly, and go build ./..., go vet on the touched packages and TestTransferResponse all pass.

Note: the description is out of date
The description says the hotfix files "match main exactly". That's no longer true: main has since received #224 (escape usernames in API approval messages), so internal/telegram/api_approval.go on this branch is behind main. Nothing breaks, because dev already has the same fix via #223 and GitHub's 3-way merge keeps it (I checked: the merged api_approval.go still has apiRecipientMention). Please update the description, or merge main again, so the history doesn't look like the escape fix was dropped.

The inline comments are non-blocking nits.


🤖 Generated with Claude Code


Generated by Claude Code

// Record the transfer's hash first (the status update below reloads and
// saves the same record), so /api/v1/send/status returns it with executed.
if storage.SetPendingTxPaymentHashFn != nil {
storage.SetPendingTxPaymentHashFn(approvalData.TransactionID, t.Invoice.PaymentHash)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit (non-blocking, same on main): This adds a second load→save of the same BuntDB record straight before UpdatePendingTxStatusFn does its own load→save. It works because the two calls run one after the other. But it costs two round trips, the record could be written by someone else between the saves, and if the process dies between them we end up with a hash but no executed status. A single hook such as MarkPendingTxExecuted(id, actor, paymentHash) would make it one write. Fine to leave for a follow-up, since this PR is a sync of main.


Generated by Claude Code

Comment thread internal/api/send.go
w.WriteHeader(http.StatusOK)
// PaymentHash is set for invoice transactions, so a caller that sees
// in_flight can resolve the outcome via /api/v1/send/payment/{payment_hash}.
// For an executed internal send it is the hash of the transfer's invoice,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: payment_hash now shows up in /send/status for both in_flight invoice payments and executed internal sends. A client that decides by whether payment_hash is present (rather than by status) would now poll /send/payment/{hash} with an internal hash. The DCA doc says to branch on status, so this is fine as long as the DCA BE does that. Worth confirming on their side.


Generated by Claude Code

// RegisterUpdatePendingTxHook sets the storage-level hook so the telegram
// package can update PendingTransaction status without an import cycle.
func RegisterUpdatePendingTxHook(bot *telegram.TipBot) {
storage.SetPendingTxPaymentHashFn = func(id, paymentHash string) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: This hook has no test, and neither does the executed path of /send/status returning the hash. A small test that registers the hooks against a temp Bunt, calls SetPendingTxPaymentHashFn then UpdatePendingTxStatusFn, and checks that both PaymentHash and Status survive would lock in the ordering that the comment in api_approval.go relies on. Not needed for this merge PR.


Generated by Claude Code

@helloscoopa helloscoopa added the bug Something isn't working label Dec 2, 2026 — with Claude
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants