invoice: fail gracefully with large amount
This commit is contained in:
@@ -318,21 +318,24 @@ class SendScreen(CScreen, Logger):
|
|||||||
self.app.show_error(_('Invalid amount') + ':\n' + self.amount)
|
self.app.show_error(_('Invalid amount') + ':\n' + self.amount)
|
||||||
return
|
return
|
||||||
message = self.message
|
message = self.message
|
||||||
if self.is_lightning:
|
try:
|
||||||
return LNInvoice.from_bech32(address)
|
if self.is_lightning:
|
||||||
else: # on-chain
|
return LNInvoice.from_bech32(address)
|
||||||
if self.payment_request:
|
else: # on-chain
|
||||||
outputs = self.payment_request.get_outputs()
|
if self.payment_request:
|
||||||
else:
|
outputs = self.payment_request.get_outputs()
|
||||||
if not bitcoin.is_address(address):
|
else:
|
||||||
self.app.show_error(_('Invalid Bitcoin Address') + ':\n' + address)
|
if not bitcoin.is_address(address):
|
||||||
return
|
self.app.show_error(_('Invalid Bitcoin Address') + ':\n' + address)
|
||||||
outputs = [PartialTxOutput.from_address_and_value(address, amount)]
|
return
|
||||||
return self.app.wallet.create_invoice(
|
outputs = [PartialTxOutput.from_address_and_value(address, amount)]
|
||||||
outputs=outputs,
|
return self.app.wallet.create_invoice(
|
||||||
message=message,
|
outputs=outputs,
|
||||||
pr=self.payment_request,
|
message=message,
|
||||||
URI=self.parsed_URI)
|
pr=self.payment_request,
|
||||||
|
URI=self.parsed_URI)
|
||||||
|
except InvoiceError as e:
|
||||||
|
self.app.show_error(_('Error creating payment') + ':\n' + str(e))
|
||||||
|
|
||||||
def do_save(self):
|
def do_save(self):
|
||||||
invoice = self.read_invoice()
|
invoice = self.read_invoice()
|
||||||
@@ -447,20 +450,24 @@ class ReceiveScreen(CScreen):
|
|||||||
amount = self.amount
|
amount = self.amount
|
||||||
amount = self.app.get_amount(amount) if amount else 0
|
amount = self.app.get_amount(amount) if amount else 0
|
||||||
message = self.message
|
message = self.message
|
||||||
if lightning:
|
try:
|
||||||
key = self.app.wallet.lnworker.add_request(amount, message, self.expiry())
|
if lightning:
|
||||||
else:
|
key = self.app.wallet.lnworker.add_request(amount, message, self.expiry())
|
||||||
addr = self.address or self.app.wallet.get_unused_address()
|
else:
|
||||||
if not addr:
|
addr = self.address or self.app.wallet.get_unused_address()
|
||||||
if not self.app.wallet.is_deterministic():
|
if not addr:
|
||||||
addr = self.app.wallet.get_receiving_address()
|
if not self.app.wallet.is_deterministic():
|
||||||
else:
|
addr = self.app.wallet.get_receiving_address()
|
||||||
self.app.show_info(_('No address available. Please remove some of your pending requests.'))
|
else:
|
||||||
return
|
self.app.show_info(_('No address available. Please remove some of your pending requests.'))
|
||||||
self.address = addr
|
return
|
||||||
req = self.app.wallet.make_payment_request(addr, amount, message, self.expiry())
|
self.address = addr
|
||||||
self.app.wallet.add_payment_request(req)
|
req = self.app.wallet.make_payment_request(addr, amount, message, self.expiry())
|
||||||
key = addr
|
self.app.wallet.add_payment_request(req)
|
||||||
|
key = addr
|
||||||
|
except InvoiceError as e:
|
||||||
|
self.app.show_error(_('Error creating payment request') + ':\n' + str(e))
|
||||||
|
return
|
||||||
self.clear()
|
self.clear()
|
||||||
self.update()
|
self.update()
|
||||||
self.app.show_request(lightning, key)
|
self.app.show_request(lightning, key)
|
||||||
|
|||||||
@@ -63,7 +63,8 @@ from electrum.util import (format_time,
|
|||||||
get_new_wallet_name, send_exception_to_crash_reporter,
|
get_new_wallet_name, send_exception_to_crash_reporter,
|
||||||
InvalidBitcoinURI, maybe_extract_bolt11_invoice, NotEnoughFunds,
|
InvalidBitcoinURI, maybe_extract_bolt11_invoice, NotEnoughFunds,
|
||||||
NoDynamicFeeEstimates, MultipleSpendMaxTxOutputs,
|
NoDynamicFeeEstimates, MultipleSpendMaxTxOutputs,
|
||||||
AddTransactionException, BITCOIN_BIP21_URI_SCHEME)
|
AddTransactionException, BITCOIN_BIP21_URI_SCHEME,
|
||||||
|
InvoiceError)
|
||||||
from electrum.invoices import PR_TYPE_ONCHAIN, PR_TYPE_LN, PR_DEFAULT_EXPIRATION_WHEN_CREATING, Invoice
|
from electrum.invoices import PR_TYPE_ONCHAIN, PR_TYPE_LN, PR_DEFAULT_EXPIRATION_WHEN_CREATING, Invoice
|
||||||
from electrum.invoices import PR_PAID, PR_FAILED, pr_expiration_values, LNInvoice, OnchainInvoice
|
from electrum.invoices import PR_PAID, PR_FAILED, pr_expiration_values, LNInvoice, OnchainInvoice
|
||||||
from electrum.transaction import (Transaction, PartialTxInput,
|
from electrum.transaction import (Transaction, PartialTxInput,
|
||||||
@@ -78,7 +79,7 @@ from electrum.exchange_rate import FxThread
|
|||||||
from electrum.simple_config import SimpleConfig
|
from electrum.simple_config import SimpleConfig
|
||||||
from electrum.logging import Logger
|
from electrum.logging import Logger
|
||||||
from electrum.lnutil import ln_dummy_address, extract_nodeid, ConnStringFormatError
|
from electrum.lnutil import ln_dummy_address, extract_nodeid, ConnStringFormatError
|
||||||
from electrum.lnaddr import lndecode, LnDecodeException
|
from electrum.lnaddr import lndecode, LnDecodeException, LnAddressError
|
||||||
|
|
||||||
from .exception_window import Exception_Hook
|
from .exception_window import Exception_Hook
|
||||||
from .amountedit import AmountEdit, BTCAmountEdit, FreezableLineEdit, FeerateEdit
|
from .amountedit import AmountEdit, BTCAmountEdit, FreezableLineEdit, FeerateEdit
|
||||||
@@ -1223,21 +1224,26 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger):
|
|||||||
else:
|
else:
|
||||||
return
|
return
|
||||||
|
|
||||||
def create_invoice(self, is_lightning):
|
def create_invoice(self, is_lightning: bool):
|
||||||
amount = self.receive_amount_e.get_amount()
|
amount = self.receive_amount_e.get_amount()
|
||||||
message = self.receive_message_e.text()
|
message = self.receive_message_e.text()
|
||||||
expiry = self.config.get('request_expiry', PR_DEFAULT_EXPIRATION_WHEN_CREATING)
|
expiry = self.config.get('request_expiry', PR_DEFAULT_EXPIRATION_WHEN_CREATING)
|
||||||
if is_lightning:
|
try:
|
||||||
if not self.wallet.lnworker.channels:
|
if is_lightning:
|
||||||
self.show_error(_("You need to open a Lightning channel first."))
|
if not self.wallet.lnworker.channels:
|
||||||
return
|
self.show_error(_("You need to open a Lightning channel first."))
|
||||||
# TODO maybe show a warning if amount exceeds lnworker.num_sats_can_receive (as in kivy)
|
return
|
||||||
key = self.wallet.lnworker.add_request(amount, message, expiry)
|
# TODO maybe show a warning if amount exceeds lnworker.num_sats_can_receive (as in kivy)
|
||||||
else:
|
key = self.wallet.lnworker.add_request(amount, message, expiry)
|
||||||
key = self.create_bitcoin_request(amount, message, expiry)
|
else:
|
||||||
if not key:
|
key = self.create_bitcoin_request(amount, message, expiry)
|
||||||
return
|
if not key:
|
||||||
self.address_list.update()
|
return
|
||||||
|
self.address_list.update()
|
||||||
|
except (InvoiceError, LnAddressError) as e:
|
||||||
|
self.show_error(_('Error creating payment request') + ':\n' + str(e))
|
||||||
|
return
|
||||||
|
|
||||||
assert key is not None
|
assert key is not None
|
||||||
self.request_list.update()
|
self.request_list.update()
|
||||||
self.request_list.select_key(key)
|
self.request_list.select_key(key)
|
||||||
@@ -1250,7 +1256,7 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger):
|
|||||||
title = _('Invoice') if is_lightning else _('Address')
|
title = _('Invoice') if is_lightning else _('Address')
|
||||||
self.do_copy(content, title=title)
|
self.do_copy(content, title=title)
|
||||||
|
|
||||||
def create_bitcoin_request(self, amount, message, expiration) -> Optional[str]:
|
def create_bitcoin_request(self, amount: int, message: str, expiration: int) -> Optional[str]:
|
||||||
addr = self.wallet.get_unused_address()
|
addr = self.wallet.get_unused_address()
|
||||||
if addr is None:
|
if addr is None:
|
||||||
if not self.wallet.is_deterministic(): # imported wallet
|
if not self.wallet.is_deterministic(): # imported wallet
|
||||||
@@ -1595,32 +1601,35 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger):
|
|||||||
def read_invoice(self):
|
def read_invoice(self):
|
||||||
if self.check_send_tab_payto_line_and_show_errors():
|
if self.check_send_tab_payto_line_and_show_errors():
|
||||||
return
|
return
|
||||||
if not self._is_onchain:
|
try:
|
||||||
invoice_str = self.payto_e.lightning_invoice
|
if not self._is_onchain:
|
||||||
if not invoice_str:
|
invoice_str = self.payto_e.lightning_invoice
|
||||||
return
|
if not invoice_str:
|
||||||
if not self.wallet.has_lightning():
|
|
||||||
self.show_error(_('Lightning is disabled'))
|
|
||||||
return
|
|
||||||
invoice = LNInvoice.from_bech32(invoice_str)
|
|
||||||
if invoice.get_amount_msat() is None:
|
|
||||||
amount_sat = self.amount_e.get_amount()
|
|
||||||
if amount_sat:
|
|
||||||
invoice.amount_msat = int(amount_sat * 1000)
|
|
||||||
else:
|
|
||||||
self.show_error(_('No amount'))
|
|
||||||
return
|
return
|
||||||
return invoice
|
if not self.wallet.has_lightning():
|
||||||
else:
|
self.show_error(_('Lightning is disabled'))
|
||||||
outputs = self.read_outputs()
|
return
|
||||||
if self.check_send_tab_onchain_outputs_and_show_errors(outputs):
|
invoice = LNInvoice.from_bech32(invoice_str)
|
||||||
return
|
if invoice.get_amount_msat() is None:
|
||||||
message = self.message_e.text()
|
amount_sat = self.amount_e.get_amount()
|
||||||
return self.wallet.create_invoice(
|
if amount_sat:
|
||||||
outputs=outputs,
|
invoice.amount_msat = int(amount_sat * 1000)
|
||||||
message=message,
|
else:
|
||||||
pr=self.payment_request,
|
self.show_error(_('No amount'))
|
||||||
URI=self.payto_URI)
|
return
|
||||||
|
return invoice
|
||||||
|
else:
|
||||||
|
outputs = self.read_outputs()
|
||||||
|
if self.check_send_tab_onchain_outputs_and_show_errors(outputs):
|
||||||
|
return
|
||||||
|
message = self.message_e.text()
|
||||||
|
return self.wallet.create_invoice(
|
||||||
|
outputs=outputs,
|
||||||
|
message=message,
|
||||||
|
pr=self.payment_request,
|
||||||
|
URI=self.payto_URI)
|
||||||
|
except InvoiceError as e:
|
||||||
|
self.show_error(_('Error creating payment') + ':\n' + str(e))
|
||||||
|
|
||||||
def do_save_invoice(self):
|
def do_save_invoice(self):
|
||||||
self.pending_invoice = self.read_invoice()
|
self.pending_invoice = self.read_invoice()
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import attr
|
|||||||
|
|
||||||
from .json_db import StoredObject
|
from .json_db import StoredObject
|
||||||
from .i18n import _
|
from .i18n import _
|
||||||
from .util import age
|
from .util import age, InvoiceError
|
||||||
from .lnaddr import lndecode, LnAddr
|
from .lnaddr import lndecode, LnAddr
|
||||||
from . import constants
|
from . import constants
|
||||||
from .bitcoin import COIN, TOTAL_COIN_SUPPLY_LIMIT_IN_BTC
|
from .bitcoin import COIN, TOTAL_COIN_SUPPLY_LIMIT_IN_BTC
|
||||||
@@ -134,12 +134,12 @@ class OnchainInvoice(Invoice):
|
|||||||
def _validate_amount(self, attribute, value):
|
def _validate_amount(self, attribute, value):
|
||||||
if isinstance(value, int):
|
if isinstance(value, int):
|
||||||
if not (0 <= value <= TOTAL_COIN_SUPPLY_LIMIT_IN_BTC * COIN):
|
if not (0 <= value <= TOTAL_COIN_SUPPLY_LIMIT_IN_BTC * COIN):
|
||||||
raise ValueError(f"amount is out-of-bounds: {value!r} sat")
|
raise InvoiceError(f"amount is out-of-bounds: {value!r} sat")
|
||||||
elif isinstance(value, str):
|
elif isinstance(value, str):
|
||||||
if value != "!":
|
if value != "!":
|
||||||
raise ValueError(f"unexpected amount: {value!r}")
|
raise InvoiceError(f"unexpected amount: {value!r}")
|
||||||
else:
|
else:
|
||||||
raise ValueError(f"unexpected amount: {value!r}")
|
raise InvoiceError(f"unexpected amount: {value!r}")
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_bip70_payreq(cls, pr: 'PaymentRequest', height:int) -> 'OnchainInvoice':
|
def from_bip70_payreq(cls, pr: 'PaymentRequest', height:int) -> 'OnchainInvoice':
|
||||||
@@ -173,9 +173,9 @@ class LNInvoice(Invoice):
|
|||||||
return
|
return
|
||||||
if isinstance(value, int):
|
if isinstance(value, int):
|
||||||
if not (0 <= value <= TOTAL_COIN_SUPPLY_LIMIT_IN_BTC * COIN * 1000):
|
if not (0 <= value <= TOTAL_COIN_SUPPLY_LIMIT_IN_BTC * COIN * 1000):
|
||||||
raise ValueError(f"amount is out-of-bounds: {value!r} msat")
|
raise InvoiceError(f"amount is out-of-bounds: {value!r} msat")
|
||||||
else:
|
else:
|
||||||
raise ValueError(f"unexpected amount: {value!r}")
|
raise InvoiceError(f"unexpected amount: {value!r}")
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _lnaddr(self) -> LnAddr:
|
def _lnaddr(self) -> LnAddr:
|
||||||
@@ -231,4 +231,3 @@ class LNInvoice(Invoice):
|
|||||||
# 'tags': str(lnaddr.tags),
|
# 'tags': str(lnaddr.tags),
|
||||||
})
|
})
|
||||||
return d
|
return d
|
||||||
|
|
||||||
|
|||||||
@@ -22,6 +22,10 @@ if TYPE_CHECKING:
|
|||||||
from .lnutil import LnFeatures
|
from .lnutil import LnFeatures
|
||||||
|
|
||||||
|
|
||||||
|
class LnAddressError(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
# BOLT #11:
|
# BOLT #11:
|
||||||
#
|
#
|
||||||
# A writer MUST encode `amount` as a positive decimal integer with no
|
# A writer MUST encode `amount` as a positive decimal integer with no
|
||||||
@@ -265,6 +269,7 @@ def lnencode(addr: 'LnAddr', privkey) -> str:
|
|||||||
|
|
||||||
return bech32_encode(segwit_addr.Encoding.BECH32, hrp, bitarray_to_u5(data))
|
return bech32_encode(segwit_addr.Encoding.BECH32, hrp, bitarray_to_u5(data))
|
||||||
|
|
||||||
|
|
||||||
class LnAddr(object):
|
class LnAddr(object):
|
||||||
def __init__(self, *, paymenthash: bytes = None, amount=None, currency=None, tags=None, date=None,
|
def __init__(self, *, paymenthash: bytes = None, amount=None, currency=None, tags=None, date=None,
|
||||||
payment_secret: bytes = None):
|
payment_secret: bytes = None):
|
||||||
@@ -286,16 +291,16 @@ class LnAddr(object):
|
|||||||
@amount.setter
|
@amount.setter
|
||||||
def amount(self, value):
|
def amount(self, value):
|
||||||
if not (isinstance(value, Decimal) or value is None):
|
if not (isinstance(value, Decimal) or value is None):
|
||||||
raise ValueError(f"amount must be Decimal or None, not {value!r}")
|
raise LnAddressError(f"amount must be Decimal or None, not {value!r}")
|
||||||
if value is None:
|
if value is None:
|
||||||
self._amount = None
|
self._amount = None
|
||||||
return
|
return
|
||||||
assert isinstance(value, Decimal)
|
assert isinstance(value, Decimal)
|
||||||
if value.is_nan() or not (0 <= value <= TOTAL_COIN_SUPPLY_LIMIT_IN_BTC):
|
if value.is_nan() or not (0 <= value <= TOTAL_COIN_SUPPLY_LIMIT_IN_BTC):
|
||||||
raise ValueError(f"amount is out-of-bounds: {value!r} BTC")
|
raise LnAddressError(f"amount is out-of-bounds: {value!r} BTC")
|
||||||
if value * 10**12 % 10:
|
if value * 10**12 % 10:
|
||||||
# max resolution is millisatoshi
|
# max resolution is millisatoshi
|
||||||
raise ValueError(f"Cannot encode {value!r}: too many decimal places")
|
raise LnAddressError(f"Cannot encode {value!r}: too many decimal places")
|
||||||
self._amount = value
|
self._amount = value
|
||||||
|
|
||||||
def get_amount_sat(self) -> Optional[Decimal]:
|
def get_amount_sat(self) -> Optional[Decimal]:
|
||||||
|
|||||||
Reference in New Issue
Block a user