Skip to content

Commit

Permalink
qml: Handle situation where no more addresses are available without
Browse files Browse the repository at this point in the history
creating addresses beyond the gap limit.

- if lightning is enabled, propose to create a lightning-only invoice
- otherwise, propose to reuse an address
- never generate addresses beyond the gap limit

Implementation:
 - createDefaultRequest is removed
 - create_bitcoin_address is called whether the wallet has lightning
   or not
  • Loading branch information
ecdsa committed Mar 18, 2023
1 parent cb4c99d commit 2836dcc
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 89 deletions.
7 changes: 1 addition & 6 deletions electrum/gui/qml/components/ReceiveDialog.qml
Original file line number Diff line number Diff line change
Expand Up @@ -356,12 +356,7 @@ ElDialog {
}

Component.onCompleted: {
if (dialog.key) {
request.key = dialog.key
} else {
// callLater to make sure any popups are on top of the dialog stacking order
Qt.callLater(createDefaultRequest)
}
request.key = dialog.key
}

// hack. delay qr rendering until dialog is shown
Expand Down
38 changes: 21 additions & 17 deletions electrum/gui/qml/components/WalletMainView.qml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ Item {
property var _sendDialog
property string _intentUri

property bool _ignore_gaplimit: false
property bool _reuse_address: false
property string _request_amount
property string _request_description
property string _request_expiry

function openInvoice(key) {
var dialog = invoiceDialog.createObject(app, { invoice: invoiceParser, invoice_key: key })
Expand Down Expand Up @@ -171,7 +172,7 @@ Item {
icon.source: '../../icons/tab_receive.png'
text: qsTr('Receive')
onClicked: {
var dialog = receiveDetails.createObject(mainView)
var dialog = receiveDetailsDialog.createObject(mainView)
dialog.open()
}
}
Expand Down Expand Up @@ -244,20 +245,18 @@ Item {
Connections {
target: Daemon.currentWallet
function onRequestCreateSuccess(key) {
openRequest(key)
openRequest(key)
}
function onRequestCreateError(code, error) {
if (code == 'gaplimit') {
if (code == 'ln') {
var dialog = app.messageDialog.createObject(app, {text: error, yesno: true})
dialog.yesClicked.connect(function() {
_ignore_gaplimit = true
createDefaultRequest()
createRequest(true, false)
})
} else if (code == 'non-deterministic') {
} else if (code == 'reuse_addr') {
var dialog = app.messageDialog.createObject(app, {text: error, yesno: true})
dialog.yesClicked.connect(function() {
_reuse_address = true
createDefaultRequest()
createRequest(false, true)
})
} else {
console.log(error)
Expand Down Expand Up @@ -354,25 +353,30 @@ Item {
}
}

function createRequest(receiveDetailsDialog) {
var qamt = Config.unitsToSats(receiveDetailsDialog.amount)
Daemon.currentWallet.createRequest(qamt, receiveDetailsDialog.description, receiveDetailsDialog.expiry, _ignore_gaplimit, _reuse_address)
function createRequest(lightning_only, reuse_address) {
var qamt = Config.unitsToSats(_request_amount)
Daemon.currentWallet.createRequest(qamt, _request_description, _request_expiry, lightning_only, reuse_address)
}

Component {
id: receiveDetails
id: receiveDetailsDialog

ReceiveDetailsDialog {
id: receiveDetailsDialog
id: _receiveDetailsDialog
width: parent.width * 0.9
anchors.centerIn: parent
onAccepted: {
console.log('accepted')
createRequest(receiveDetailsDialog)
_request_amount = _receiveDetailsDialog.amount
_request_description = _receiveDetailsDialog.description
_request_expiry = _receiveDetailsDialog.expiry
createRequest(false, false)
}
onRejected: {
console.log('rejected')
}
}
onClosed: destroy()
}
}

Component {
Expand Down
87 changes: 21 additions & 66 deletions electrum/gui/qml/qewallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,39 +577,26 @@ def pay_thread():

threading.Thread(target=pay_thread, daemon=True).start()

def create_bitcoin_request(self, amount: int, message: str, expiration: int, *, ignore_gap: bool = False, reuse_address: bool = False) -> Optional[Tuple]:
def create_bitcoin_request(self, amount: int, message: str, expiration: int, *, lightning_only: bool = False, reuse_address: bool = False) -> Optional[Tuple]:
addr = self.wallet.get_unused_address()
if addr is None:
if not self.wallet.is_deterministic(): # imported wallet
if not reuse_address:
msg = [
_('No more addresses in your wallet.'), ' ',
_('You are using a non-deterministic wallet, which cannot create new addresses.'), ' ',
_('If you want to create new addresses, use a deterministic wallet instead.'), '\n\n',
_('Creating a new payment request will reuse one of your addresses and overwrite an existing request. Continue anyway?'),
]
self.requestCreateError.emit('non-deterministic',''.join(msg))
return
if reuse_address:
addr = self.wallet.get_receiving_address()
else: # deterministic wallet
if not ignore_gap:
self.requestCreateError.emit('gaplimit',_("Warning: The next address will not be recovered automatically if you restore your wallet from seed; you may need to add it manually.\n\nThis occurs because you have too many unused addresses in your wallet. To avoid this situation, use the existing addresses first.\n\nCreate anyway?"))
return
addr = self.wallet.create_new_address(False)
elif lightning_only:
addr = None
else:
has_lightning = self.wallet.has_lightning()
msg = [
_('No more unused addresses in your wallet.'),
_('All your addresses are used by unpaid requests.'),
]
msg.append(_('Do you wish to create a lightning-only request?') if has_lightning else _('Do you want to reuse an address?'))
return

req_key = self.wallet.create_request(amount, message, expiration, addr)
self._logger.debug(f'created request with key {req_key}')
#try:
#self.wallet.add_payment_request(req)
#except Exception as e:
#self.logger.exception('Error adding payment request')
#self.requestCreateError.emit('fatal',_('Error adding payment request') + ':\n' + repr(e))
#else:
## TODO: check this flow. Only if alias is defined in config. OpenAlias?
#pass
##self.sign_payment_request(addr)

return req_key, addr
self._logger.debug(f'created request with key {req_key} addr {addr}')

return req_key

def _delete_expired_requests(self):
keys = self.wallet.delete_expired_requests()
Expand All @@ -620,46 +607,14 @@ def _delete_expired_requests(self):
@pyqtSlot(QEAmount, str, int, bool)
@pyqtSlot(QEAmount, str, int, bool, bool)
@pyqtSlot(QEAmount, str, int, bool, bool, bool)
def createRequest(self, amount: QEAmount, message: str, expiration: int, ignore_gap: bool = False, reuse_address: bool = False):
self._delete_expired_requests()
try:
if self.wallet.lnworker and self.wallet.lnworker.channels:
# TODO maybe show a warning if amount exceeds lnworker.num_sats_can_receive (as in kivy)
# TODO fallback address robustness
addr = self.wallet.get_unused_address()
key = self.wallet.create_request(amount.satsInt, message, expiration, addr)
else:
key, addr = self.create_bitcoin_request(amount.satsInt, message, expiration, ignore_gap=ignore_gap, reuse_address=reuse_address)
if not key:
return
self.addressModel.setDirty()
except InvoiceError as e:
self.requestCreateError.emit('fatal',_('Error creating payment request') + ':\n' + str(e))
return

assert key is not None
self.requestModel.add_invoice(self.wallet.get_request(key))
self.requestCreateSuccess.emit(key)

@pyqtSlot()
@pyqtSlot(bool)
@pyqtSlot(bool, bool)
def createDefaultRequest(self, ignore_gap: bool = False, reuse_address: bool = False):
def createRequest(self, amount: QEAmount, message: str, expiration: int, lightning_only: bool = False, reuse_address: bool = False):
self._delete_expired_requests()
try:
default_expiry = self.wallet.config.get('request_expiry', PR_DEFAULT_EXPIRATION_WHEN_CREATING)
if self.wallet.lnworker and self.wallet.lnworker.channels:
addr = self.wallet.get_unused_address()
# if addr is None, we ran out of addresses
if addr is None:
# TODO: remove oldest unpaid request having a fallback address and try again
pass
key = self.wallet.create_request(None, None, default_expiry, addr)
else:
req = self.create_bitcoin_request(None, None, default_expiry, ignore_gap=ignore_gap, reuse_address=reuse_address)
if not req:
return
key, addr = req
key = self.create_bitcoin_request(amount.satsInt, message, expiration, lightning_only=lightning_only, reuse_address=reuse_address)
if not key:
self.requestCreateError.emit('ln' if self.wallet.has_lightning() else 'reuse_addr', ' '.join(msg))
return
self.addressModel.setDirty()
except InvoiceError as e:
self.requestCreateError.emit('fatal',_('Error creating payment request') + ':\n' + str(e))
return
Expand Down

0 comments on commit 2836dcc

Please sign in to comment.