sending kinda working
This commit is contained in:
@@ -4,6 +4,7 @@ from typing import Any
|
|||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
|
from bolt11 import decode as bolt11_decode
|
||||||
|
|
||||||
from lnbits.helpers import normalize_endpoint
|
from lnbits.helpers import normalize_endpoint
|
||||||
from lnbits.settings import settings
|
from lnbits.settings import settings
|
||||||
@@ -131,9 +132,18 @@ class LightsparkSparkWallet(Wallet):
|
|||||||
async def pay_invoice(self, bolt11: str, fee_limit_msat: int) -> PaymentResponse:
|
async def pay_invoice(self, bolt11: str, fee_limit_msat: int) -> PaymentResponse:
|
||||||
try:
|
try:
|
||||||
max_fee_sats = (int(fee_limit_msat) + 999) // 1000
|
max_fee_sats = (int(fee_limit_msat) + 999) // 1000
|
||||||
payload = {"bolt11": bolt11, "max_fee_sats": max_fee_sats}
|
payment_hash = None
|
||||||
|
try:
|
||||||
|
payment_hash = bolt11_decode(bolt11).payment_hash
|
||||||
|
except Exception:
|
||||||
|
payment_hash = None
|
||||||
|
payload = {
|
||||||
|
"bolt11": bolt11,
|
||||||
|
"max_fee_sats": max_fee_sats,
|
||||||
|
"payment_hash": payment_hash,
|
||||||
|
}
|
||||||
res = await self._request("POST", "/v1/payments", payload)
|
res = await self._request("POST", "/v1/payments", payload)
|
||||||
checking_id = res.get("checking_id")
|
checking_id = payment_hash or res.get("checking_id")
|
||||||
if not checking_id:
|
if not checking_id:
|
||||||
raise SparkSidecarError(
|
raise SparkSidecarError(
|
||||||
"Spark sidecar payment response missing checking_id."
|
"Spark sidecar payment response missing checking_id."
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ if (!MNEMONIC) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let walletPromise
|
let walletPromise
|
||||||
|
const paymentHashToRequestId = new Map()
|
||||||
async function getWallet() {
|
async function getWallet() {
|
||||||
if (!walletPromise) {
|
if (!walletPromise) {
|
||||||
walletPromise = SparkWallet.initialize({
|
walletPromise = SparkWallet.initialize({
|
||||||
@@ -142,13 +143,17 @@ const server = http.createServer(async (req, res) => {
|
|||||||
const amountSatsToSend = body.amount_sats
|
const amountSatsToSend = body.amount_sats
|
||||||
? Number(body.amount_sats)
|
? Number(body.amount_sats)
|
||||||
: undefined
|
: undefined
|
||||||
|
const paymentHash = body.payment_hash || null
|
||||||
const payment = await wallet.payLightningInvoice({
|
const payment = await wallet.payLightningInvoice({
|
||||||
invoice: bolt11,
|
invoice: bolt11,
|
||||||
maxFeeSats,
|
maxFeeSats,
|
||||||
amountSatsToSend
|
amountSatsToSend
|
||||||
})
|
})
|
||||||
|
if (paymentHash && payment?.id) {
|
||||||
|
paymentHashToRequestId.set(paymentHash, payment.id)
|
||||||
|
}
|
||||||
return sendJson(res, 200, {
|
return sendJson(res, 200, {
|
||||||
checking_id: payment.id,
|
checking_id: paymentHash || payment.id,
|
||||||
status: payment.status,
|
status: payment.status,
|
||||||
fee_msat: feeToMsat(payment.fee),
|
fee_msat: feeToMsat(payment.fee),
|
||||||
preimage: payment.paymentPreimage || null
|
preimage: payment.paymentPreimage || null
|
||||||
@@ -172,12 +177,14 @@ const server = http.createServer(async (req, res) => {
|
|||||||
|
|
||||||
if (parts.length === 3 && parts[0] === 'v1' && parts[1] === 'payments') {
|
if (parts.length === 3 && parts[0] === 'v1' && parts[1] === 'payments') {
|
||||||
const wallet = await getWallet()
|
const wallet = await getWallet()
|
||||||
const payment = await wallet.getLightningSendRequest(parts[2])
|
const requestedId = parts[2]
|
||||||
|
const lookupId = paymentHashToRequestId.get(requestedId) || requestedId
|
||||||
|
const payment = await wallet.getLightningSendRequest(lookupId)
|
||||||
if (!payment) {
|
if (!payment) {
|
||||||
return sendJson(res, 404, {error: 'Not found'})
|
return sendJson(res, 404, {error: 'Not found'})
|
||||||
}
|
}
|
||||||
return sendJson(res, 200, {
|
return sendJson(res, 200, {
|
||||||
checking_id: payment.id,
|
checking_id: requestedId,
|
||||||
status: payment.status,
|
status: payment.status,
|
||||||
fee_msat: feeToMsat(payment.fee),
|
fee_msat: feeToMsat(payment.fee),
|
||||||
preimage: payment.paymentPreimage || null
|
preimage: payment.paymentPreimage || null
|
||||||
|
|||||||
Reference in New Issue
Block a user