invoices: don't modify .amount_msat directly
This commit is contained in:
@@ -351,7 +351,7 @@ class SendScreen(CScreen, Logger):
|
|||||||
assert type(amount_sat) is int
|
assert type(amount_sat) is int
|
||||||
invoice = Invoice.from_bech32(address)
|
invoice = Invoice.from_bech32(address)
|
||||||
if invoice.amount_msat is None:
|
if invoice.amount_msat is None:
|
||||||
invoice.amount_msat = int(amount_sat * 1000)
|
invoice.set_amount_msat(int(amount_sat * 1000))
|
||||||
return invoice
|
return invoice
|
||||||
else:
|
else:
|
||||||
# on-chain
|
# on-chain
|
||||||
|
|||||||
@@ -379,8 +379,7 @@ class QEInvoice(QObject, QtEventListener):
|
|||||||
if self.amount.isEmpty:
|
if self.amount.isEmpty:
|
||||||
if self.amountOverride.isEmpty:
|
if self.amountOverride.isEmpty:
|
||||||
raise Exception('can not pay 0 amount')
|
raise Exception('can not pay 0 amount')
|
||||||
# TODO: is update amount_msat for overrideAmount sufficient?
|
self._effectiveInvoice.set_amount_msat(self.amountOverride.satsInt * 1000)
|
||||||
self._effectiveInvoice.amount_msat = self.amountOverride.satsInt * 1000
|
|
||||||
|
|
||||||
self._wallet.pay_lightning_invoice(self._effectiveInvoice)
|
self._wallet.pay_lightning_invoice(self._effectiveInvoice)
|
||||||
|
|
||||||
@@ -627,9 +626,9 @@ class QEInvoiceParser(QEInvoice):
|
|||||||
|
|
||||||
if not self._effectiveInvoice.amount_msat and not self.amountOverride.isEmpty:
|
if not self._effectiveInvoice.amount_msat and not self.amountOverride.isEmpty:
|
||||||
if self.invoiceType == QEInvoice.Type.OnchainInvoice and self.amountOverride.isMax:
|
if self.invoiceType == QEInvoice.Type.OnchainInvoice and self.amountOverride.isMax:
|
||||||
self._effectiveInvoice.amount_msat = '!'
|
self._effectiveInvoice.set_amount_msat('!')
|
||||||
else:
|
else:
|
||||||
self._effectiveInvoice.amount_msat = self.amountOverride.satsInt * 1000
|
self._effectiveInvoice.set_amount_msat(self.amountOverride.satsInt * 1000)
|
||||||
|
|
||||||
self.canSave = False
|
self.canSave = False
|
||||||
|
|
||||||
|
|||||||
@@ -606,7 +606,7 @@ class ElectrumGui(BaseElectrumGui, EventListener):
|
|||||||
if invoice.amount_msat is None:
|
if invoice.amount_msat is None:
|
||||||
amount_sat = self.parse_amount(self.str_amount)
|
amount_sat = self.parse_amount(self.str_amount)
|
||||||
if amount_sat:
|
if amount_sat:
|
||||||
invoice.amount_msat = int(amount_sat * 1000)
|
invoice.set_amount_msat(int(amount_sat * 1000))
|
||||||
else:
|
else:
|
||||||
self.show_error(_('No amount'))
|
self.show_error(_('No amount'))
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -178,6 +178,19 @@ class BaseInvoice(StoredObject):
|
|||||||
return amount_msat
|
return amount_msat
|
||||||
return int(amount_msat // 1000)
|
return int(amount_msat // 1000)
|
||||||
|
|
||||||
|
def set_amount_msat(self, amount_msat: Union[int, str]) -> None:
|
||||||
|
"""The GUI uses this to fill the amount for a zero-amount invoice."""
|
||||||
|
if amount_msat == "!":
|
||||||
|
amount_sat = amount_msat
|
||||||
|
else:
|
||||||
|
assert isinstance(amount_msat, int), f"{amount_msat=!r}"
|
||||||
|
assert amount_msat >= 0, amount_msat
|
||||||
|
amount_sat = (amount_msat // 1000) + int(amount_msat % 1000 > 0) # round up
|
||||||
|
if outputs := self.outputs:
|
||||||
|
assert len(self.outputs) == 1, len(self.outputs)
|
||||||
|
self.outputs = [PartialTxOutput(scriptpubkey=outputs[0].scriptpubkey, value=amount_sat)]
|
||||||
|
self.amount_msat = amount_msat
|
||||||
|
|
||||||
@amount_msat.validator
|
@amount_msat.validator
|
||||||
def _validate_amount(self, attribute, value):
|
def _validate_amount(self, attribute, value):
|
||||||
if value is None:
|
if value is None:
|
||||||
|
|||||||
@@ -674,7 +674,7 @@ def invoice_from_payment_identifier(
|
|||||||
if not invoice:
|
if not invoice:
|
||||||
return
|
return
|
||||||
if invoice.amount_msat is None:
|
if invoice.amount_msat is None:
|
||||||
invoice.amount_msat = int(amount_sat * 1000)
|
invoice.set_amount_msat(int(amount_sat * 1000))
|
||||||
return invoice
|
return invoice
|
||||||
else:
|
else:
|
||||||
outputs = pi.get_onchain_outputs(amount_sat)
|
outputs = pi.get_onchain_outputs(amount_sat)
|
||||||
|
|||||||
@@ -286,6 +286,8 @@ class TestBaseInvoice(ElectrumTestCase):
|
|||||||
)
|
)
|
||||||
with self.assertRaises(InvoiceError):
|
with self.assertRaises(InvoiceError):
|
||||||
invoice.amount_msat = 10**20
|
invoice.amount_msat = 10**20
|
||||||
|
with self.assertRaises(InvoiceError):
|
||||||
|
invoice.set_amount_msat(10**20)
|
||||||
with self.assertRaises(InvoiceError):
|
with self.assertRaises(InvoiceError):
|
||||||
invoice2 = Invoice(
|
invoice2 = Invoice(
|
||||||
amount_msat=10**20,
|
amount_msat=10**20,
|
||||||
|
|||||||
Reference in New Issue
Block a user