The webhook listener accepted any POST as proof of payment and fired the invoice callbacks (inline receive, group tickets, satdress payout, DALL-E refund) for whatever payment hash the body named. It now: - limits the body to 1 MB and only accepts a 64-hex payment hash, - asks LNbits whether the named wallet really received a settled incoming payment for that hash, and uses the amount LNbits reports, - handles every payment hash once (bunt key "webhook:<hash>"), - refuses to dispatch an invoice event registered for another wallet, - runs the callback with a recover, so a panic can't take the bot down. The pure checks live in internal/lnbits/webhook/verify, which does not depend on the bot configuration and is unit tested. Port of kravens/BitTipBot b7e56da and 27eb079 onto our simpler handler. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
50 lines
1.7 KiB
Go
50 lines
1.7 KiB
Go
// Package verify holds the checks the LNbits webhook listener applies before
|
|
// it treats a request as proof of payment. It has no dependency on the bot
|
|
// configuration, so it can be unit tested.
|
|
package verify
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
)
|
|
|
|
// paymentHashFormat is the only form LNbits uses for a payment hash. The hash
|
|
// becomes part of an LNbits URL and of the replay key, so nothing else is
|
|
// accepted (a suffix such as "?x=1" would reach the same payment but be a new
|
|
// replay key).
|
|
var paymentHashFormat = regexp.MustCompile("^[0-9a-f]{64}$")
|
|
|
|
// ValidPaymentHash reports whether h is 64 lowercase hex characters.
|
|
func ValidPaymentHash(h string) bool {
|
|
return paymentHashFormat.MatchString(h)
|
|
}
|
|
|
|
// Payment is what LNbits reports for a payment hash, reduced to the fields
|
|
// the webhook needs.
|
|
type Payment struct {
|
|
Paid bool
|
|
PaymentHash string
|
|
WalletID string
|
|
AmountMsat int64
|
|
}
|
|
|
|
// SettledIncoming returns the amount in msat of a settled incoming payment of
|
|
// walletID with paymentHash, or an error when p is not that. LNbits only
|
|
// returns the details to the wallet that owns the payment; an outgoing payment
|
|
// of the same wallet has a negative amount.
|
|
func SettledIncoming(p Payment, paymentHash, walletID string) (int64, error) {
|
|
if !p.Paid {
|
|
return 0, fmt.Errorf("payment %s is not settled", paymentHash)
|
|
}
|
|
if p.PaymentHash != paymentHash {
|
|
return 0, fmt.Errorf("payment hash mismatch: got %s, want %s", p.PaymentHash, paymentHash)
|
|
}
|
|
if p.WalletID != walletID {
|
|
return 0, fmt.Errorf("payment %s does not belong to wallet %s", paymentHash, walletID)
|
|
}
|
|
if p.AmountMsat <= 0 {
|
|
return 0, fmt.Errorf("payment %s is not an incoming payment", paymentHash)
|
|
}
|
|
return p.AmountMsat, nil
|
|
}
|