mirror of
https://github.com/lnbits/lnbits.git
synced 2025-10-09 20:12:34 +02:00
retrieve preimage from wallet backends, save it and show on UI dialog.
closes https://github.com/lnbits/lnbits/issues/77
This commit is contained in:
@@ -11,7 +11,7 @@ except ImportError: # pragma: nocover
|
|||||||
from lnbits import bolt11
|
from lnbits import bolt11
|
||||||
from lnbits.helpers import urlsafe_short_hash
|
from lnbits.helpers import urlsafe_short_hash
|
||||||
from lnbits.settings import WALLET
|
from lnbits.settings import WALLET
|
||||||
from lnbits.wallets.base import PaymentStatus
|
from lnbits.wallets.base import PaymentStatus, PaymentResponse
|
||||||
|
|
||||||
from .crud import get_wallet, create_payment, delete_payment, check_internal, update_payment_status, get_wallet_payment
|
from .crud import get_wallet, create_payment, delete_payment, check_internal, update_payment_status, get_wallet_payment
|
||||||
|
|
||||||
@@ -110,12 +110,17 @@ def pay_invoice(
|
|||||||
update_payment_status(checking_id=internal, pending=False)
|
update_payment_status(checking_id=internal, pending=False)
|
||||||
else:
|
else:
|
||||||
# actually pay the external invoice
|
# actually pay the external invoice
|
||||||
ok, checking_id, fee_msat, error_message = WALLET.pay_invoice(payment_request)
|
payment: PaymentResponse = WALLET.pay_invoice(payment_request)
|
||||||
if ok:
|
if payment.ok:
|
||||||
create_payment(checking_id=checking_id, fee=fee_msat, **payment_kwargs)
|
create_payment(
|
||||||
|
checking_id=payment.checking_id,
|
||||||
|
fee=payment.fee_msat,
|
||||||
|
preimage=payment.preimage,
|
||||||
|
**payment_kwargs,
|
||||||
|
)
|
||||||
delete_payment(temp_id)
|
delete_payment(temp_id)
|
||||||
else:
|
else:
|
||||||
raise Exception(error_message or "Failed to pay_invoice on backend.")
|
raise Exception(payment.error_message or "Failed to pay_invoice on backend.")
|
||||||
|
|
||||||
g.db.commit()
|
g.db.commit()
|
||||||
return invoice.payment_hash
|
return invoice.payment_hash
|
||||||
|
@@ -199,6 +199,10 @@ Vue.component('lnbits-payment-details', {
|
|||||||
<div class="col-3"><b>Payment hash</b>:</div>
|
<div class="col-3"><b>Payment hash</b>:</div>
|
||||||
<div class="col-9 text-wrap mono">{{ payment.payment_hash }}</div>
|
<div class="col-9 text-wrap mono">{{ payment.payment_hash }}</div>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="row" v-if="payment.preimage">
|
||||||
|
<div class="col-3"><b>Payment proof</b>:</div>
|
||||||
|
<div class="col-9 text-wrap mono">{{ payment.preimage }}</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
`
|
`
|
||||||
})
|
})
|
||||||
|
@@ -18,6 +18,7 @@ class PaymentResponse(NamedTuple):
|
|||||||
ok: bool
|
ok: bool
|
||||||
checking_id: Optional[str] = None # payment_hash, rcp_id
|
checking_id: Optional[str] = None # payment_hash, rcp_id
|
||||||
fee_msat: int = 0
|
fee_msat: int = 0
|
||||||
|
preimage: Optional[str] = None
|
||||||
error_message: Optional[str] = None
|
error_message: Optional[str] = None
|
||||||
|
|
||||||
|
|
||||||
|
@@ -73,8 +73,14 @@ class CLightningWallet(Wallet):
|
|||||||
return InvoiceResponse(False, label, None, error_message)
|
return InvoiceResponse(False, label, None, error_message)
|
||||||
|
|
||||||
def pay_invoice(self, bolt11: str) -> PaymentResponse:
|
def pay_invoice(self, bolt11: str) -> PaymentResponse:
|
||||||
r = self.ln.pay(bolt11)
|
try:
|
||||||
return PaymentResponse(True, r["payment_hash"], r["msatoshi_sent"] - r["msatoshi"], None)
|
r = self.ln.pay(bolt11)
|
||||||
|
except RpcError as exc:
|
||||||
|
return PaymentResponse(False, None, 0, None, str(exc))
|
||||||
|
|
||||||
|
fee_msat = r["msatoshi_sent"] - r["msatoshi"]
|
||||||
|
preimage = r["payment_preimage"]
|
||||||
|
return PaymentResponse(True, r["payment_hash"], fee_msat, preimage, None)
|
||||||
|
|
||||||
def get_invoice_status(self, checking_id: str) -> PaymentStatus:
|
def get_invoice_status(self, checking_id: str) -> PaymentStatus:
|
||||||
r = self.ln.listinvoices(checking_id)
|
r = self.ln.listinvoices(checking_id)
|
||||||
|
@@ -135,11 +135,13 @@ class LndWallet(Wallet):
|
|||||||
resp = self.rpc.send_payment(payment_request=bolt11)
|
resp = self.rpc.send_payment(payment_request=bolt11)
|
||||||
|
|
||||||
if resp.payment_error:
|
if resp.payment_error:
|
||||||
return PaymentResponse(False, "", 0, resp.payment_error)
|
return PaymentResponse(False, "", 0, None, resp.payment_error)
|
||||||
|
|
||||||
r_hash = hashlib.sha256(resp.payment_preimage).digest()
|
r_hash = hashlib.sha256(resp.payment_preimage).digest()
|
||||||
checking_id = stringify_checking_id(r_hash)
|
checking_id = stringify_checking_id(r_hash)
|
||||||
return PaymentResponse(True, checking_id, 0, None)
|
fee_msat = resp.payment_route.total_fees_msat
|
||||||
|
preimage = resp.payment_preimage.hex()
|
||||||
|
return PaymentResponse(True, checking_id, fee_msat, preimage, None)
|
||||||
|
|
||||||
def get_invoice_status(self, checking_id: str) -> PaymentStatus:
|
def get_invoice_status(self, checking_id: str) -> PaymentStatus:
|
||||||
try:
|
try:
|
||||||
|
@@ -94,12 +94,13 @@ class LndRestWallet(Wallet):
|
|||||||
error_message = r.json()["error"]
|
error_message = r.json()["error"]
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
return PaymentResponse(False, None, 0, error_message)
|
return PaymentResponse(False, None, 0, None, error_message)
|
||||||
|
|
||||||
payment_hash = r.json()["payment_hash"]
|
data = r.json()
|
||||||
|
payment_hash = data["payment_hash"]
|
||||||
checking_id = payment_hash
|
checking_id = payment_hash
|
||||||
|
preimage = base64.b64decode(data["payment_preimage"]).hex()
|
||||||
return PaymentResponse(True, checking_id, 0, None)
|
return PaymentResponse(True, checking_id, 0, preimage, None)
|
||||||
|
|
||||||
def get_invoice_status(self, checking_id: str) -> PaymentStatus:
|
def get_invoice_status(self, checking_id: str) -> PaymentStatus:
|
||||||
checking_id = checking_id.replace("_", "/")
|
checking_id = checking_id.replace("_", "/")
|
||||||
|
@@ -72,12 +72,19 @@ class LNPayWallet(Wallet):
|
|||||||
headers=self.auth,
|
headers=self.auth,
|
||||||
json={"payment_request": bolt11},
|
json={"payment_request": bolt11},
|
||||||
)
|
)
|
||||||
ok, checking_id, fee_msat, error_message = r.status_code == 201, None, 0, None
|
|
||||||
|
|
||||||
if ok:
|
try:
|
||||||
checking_id = r.json()["lnTx"]["id"]
|
data = r.json()
|
||||||
|
except:
|
||||||
|
return PaymentResponse(False, None, 0, None, f"Got invalid JSON: {r.text[:200]}")
|
||||||
|
|
||||||
return PaymentResponse(ok, checking_id, fee_msat, error_message)
|
if r.is_error:
|
||||||
|
return PaymentResponse(False, None, 0, None, data["message"])
|
||||||
|
|
||||||
|
checking_id = data["lnTx"]["id"]
|
||||||
|
fee_msat = 0
|
||||||
|
preimage = data["lnTx"]["payment_preimage"]
|
||||||
|
return PaymentResponse(True, checking_id, fee_msat, preimage, None)
|
||||||
|
|
||||||
def get_invoice_status(self, checking_id: str) -> PaymentStatus:
|
def get_invoice_status(self, checking_id: str) -> PaymentStatus:
|
||||||
return self.get_payment_status(checking_id)
|
return self.get_payment_status(checking_id)
|
||||||
|
@@ -67,10 +67,13 @@ class LntxbotWallet(Wallet):
|
|||||||
error_message = r.text
|
error_message = r.text
|
||||||
pass
|
pass
|
||||||
|
|
||||||
return PaymentResponse(False, None, 0, error_message)
|
return PaymentResponse(False, None, 0, None, error_message)
|
||||||
|
|
||||||
data = r.json()
|
data = r.json()
|
||||||
return PaymentResponse(True, data["decoded"]["payment_hash"], data["fee_msat"], None)
|
checking_id = data["payment_hash"]
|
||||||
|
fee_msat = data["fee_msat"]
|
||||||
|
preimage = data["payment_preimage"]
|
||||||
|
return PaymentResponse(True, checking_id, fee_msat, preimage, None)
|
||||||
|
|
||||||
def get_invoice_status(self, checking_id: str) -> PaymentStatus:
|
def get_invoice_status(self, checking_id: str) -> PaymentStatus:
|
||||||
r = httpx.post(url=f"{self.endpoint}/invoicestatus/{checking_id}?wait=false", headers=self.auth)
|
r = httpx.post(url=f"{self.endpoint}/invoicestatus/{checking_id}?wait=false", headers=self.auth)
|
||||||
|
@@ -62,12 +62,12 @@ class OpenNodeWallet(Wallet):
|
|||||||
|
|
||||||
if r.is_error:
|
if r.is_error:
|
||||||
error_message = r.json()["message"]
|
error_message = r.json()["message"]
|
||||||
return PaymentResponse(False, None, 0, error_message)
|
return PaymentResponse(False, None, 0, None, error_message)
|
||||||
|
|
||||||
data = r.json()["data"]
|
data = r.json()["data"]
|
||||||
checking_id = data["id"]
|
checking_id = data["id"]
|
||||||
fee_msat = data["fee"] * 1000
|
fee_msat = data["fee"] * 1000
|
||||||
return PaymentResponse(True, checking_id, fee_msat, None)
|
return PaymentResponse(True, checking_id, fee_msat, None, None)
|
||||||
|
|
||||||
def get_invoice_status(self, checking_id: str) -> PaymentStatus:
|
def get_invoice_status(self, checking_id: str) -> PaymentStatus:
|
||||||
r = httpx.get(f"{self.endpoint}/v1/charge/{checking_id}", headers=self.auth)
|
r = httpx.get(f"{self.endpoint}/v1/charge/{checking_id}", headers=self.auth)
|
||||||
|
@@ -85,11 +85,12 @@ class SparkWallet(Wallet):
|
|||||||
def pay_invoice(self, bolt11: str) -> PaymentResponse:
|
def pay_invoice(self, bolt11: str) -> PaymentResponse:
|
||||||
try:
|
try:
|
||||||
r = self.pay(bolt11)
|
r = self.pay(bolt11)
|
||||||
ok, checking_id, fee_msat, error_message = True, r["payment_hash"], r["msatoshi_sent"] - r["msatoshi"], None
|
except (SparkError, UnknownError) as exc:
|
||||||
except (SparkError, UnknownError) as e:
|
return PaymentResponse(False, None, 0, None, str(exc))
|
||||||
ok, checking_id, fee_msat, error_message = False, None, None, str(e)
|
|
||||||
|
|
||||||
return PaymentResponse(ok, checking_id, fee_msat, error_message)
|
fee_msat = r["msatoshi_sent"] - r["msatoshi"]
|
||||||
|
preimage = r["payment_preimage"]
|
||||||
|
return PaymentResponse(True, r["payment_hash"], fee_msat, preimage, None)
|
||||||
|
|
||||||
def get_invoice_status(self, checking_id: str) -> PaymentStatus:
|
def get_invoice_status(self, checking_id: str) -> PaymentStatus:
|
||||||
r = self.listinvoices(label=checking_id)
|
r = self.listinvoices(label=checking_id)
|
||||||
|
Reference in New Issue
Block a user