Files
lspd/db.go
Yaacov Akiba Slama f407ec9e9c Handle interception probing payments
probing payments uses a probing payment hash which is:
sha256("probing-01:" || payment_hash).

When the interceptor detects such a hash for a payment which is supposed
to trigger a channel creation , it checks if the destination is online,
and if online, fails with INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS error in
order to let the payer knows that the payment would be successful.
2020-11-08 17:46:26 +02:00

72 lines
2.5 KiB
Go

package main
import (
"context"
"fmt"
"log"
"os"
"github.com/jackc/pgtype"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
)
var (
pgxPool *pgxpool.Pool
)
func pgConnect() error {
var err error
pgxPool, err = pgxpool.Connect(context.Background(), os.Getenv("DATABASE_URL"))
if err != nil {
return fmt.Errorf("pgxpool.Connect(%v): %w", os.Getenv("DATABASE_URL"), err)
}
return nil
}
func paymentInfo(htlcPaymentHash []byte) ([]byte, []byte, []byte, int64, int64, []byte, uint32, error) {
var (
paymentHash, paymentSecret, destination []byte
incomingAmountMsat, outgoingAmountMsat int64
fundingTxID []byte
fundingTxOutnum pgtype.Int4
)
err := pgxPool.QueryRow(context.Background(),
`SELECT payment_hash, payment_secret, destination, incoming_amount_msat, outgoing_amount_msat, funding_tx_id, funding_tx_outnum
FROM payments
WHERE payment_hash=$1 OR sha256('probing-01:' || payment_hash)=$1`,
htlcPaymentHash).Scan(&paymentHash, &paymentSecret, &destination, &incomingAmountMsat, &outgoingAmountMsat, &fundingTxID, &fundingTxOutnum)
if err != nil {
if err == pgx.ErrNoRows {
err = nil
}
return nil, nil, nil, 0, 0, nil, 0, err
}
return paymentHash, paymentSecret, destination, incomingAmountMsat, outgoingAmountMsat, fundingTxID, uint32(fundingTxOutnum.Int), nil
}
func setFundingTx(paymentHash, fundingTxID []byte, fundingTxOutnum int) error {
commandTag, err := pgxPool.Exec(context.Background(),
`UPDATE payments
SET funding_tx_id = $2, funding_tx_outnum = $3
WHERE payment_hash=$1`,
paymentHash, fundingTxID, fundingTxOutnum)
log.Printf("setFundingTx(%x, %x, %v): %s err: %v", paymentHash, fundingTxID, fundingTxOutnum, commandTag, err)
return err
}
func registerPayment(destination, paymentHash, paymentSecret []byte, incomingAmountMsat, outgoingAmountMsat int64) error {
commandTag, err := pgxPool.Exec(context.Background(),
`INSERT INTO
payments (destination, payment_hash, payment_secret, incoming_amount_msat, outgoing_amount_msat)
VALUES ($1, $2, $3, $4, $5)`,
destination, paymentHash, paymentSecret, incomingAmountMsat, outgoingAmountMsat)
log.Printf("registerPayment(%x, %x, %x, %v, %v) rows: %v err: %v",
destination, paymentHash, paymentSecret, incomingAmountMsat, outgoingAmountMsat, commandTag.RowsAffected(), err)
if err != nil {
return fmt.Errorf("registerPayment(%x, %x, %x, %v, %v) error: %w",
destination, paymentHash, paymentSecret, incomingAmountMsat, outgoingAmountMsat, err)
}
return nil
}