nwc: don't announce spending methods if limit is 0
don't return the spending methods pay_invoice and multi_pay_invoice in the get_info request and the info event so connections can be used for services that enforce receive only connections.
This commit is contained in:
@@ -96,7 +96,7 @@ class NWCServerPlugin(BasePlugin):
|
|||||||
"our_secret": our_connection_secret.hex(),
|
"our_secret": our_connection_secret.hex(),
|
||||||
"budget_spends": []
|
"budget_spends": []
|
||||||
}
|
}
|
||||||
if daily_limit_sat:
|
if daily_limit_sat is not None:
|
||||||
connection['daily_limit_sat'] = daily_limit_sat
|
connection['daily_limit_sat'] = daily_limit_sat
|
||||||
if valid_for_sec:
|
if valid_for_sec:
|
||||||
connection['valid_until'] = int(time.time()) + valid_for_sec
|
connection['valid_until'] = int(time.time()) + valid_for_sec
|
||||||
@@ -153,9 +153,9 @@ class NWCServer(Logger, EventListener):
|
|||||||
REQUEST_EVENT_KIND: int = 23194
|
REQUEST_EVENT_KIND: int = 23194
|
||||||
RESPONSE_EVENT_KIND: int = 23195
|
RESPONSE_EVENT_KIND: int = 23195
|
||||||
NOTIFICATION_EVENT_KIND: int = 23196
|
NOTIFICATION_EVENT_KIND: int = 23196
|
||||||
SUPPORTED_METHODS: list[str] = ['pay_invoice', 'multi_pay_invoice', 'make_invoice',
|
SUPPORTED_SPENDING_METHODS: set[str] = {'pay_invoice', 'multi_pay_invoice'}
|
||||||
'lookup_invoice', 'get_balance', 'get_info', 'list_transactions',
|
SUPPORTED_METHODS: set[str] = {'make_invoice', 'lookup_invoice', 'get_balance', 'get_info',
|
||||||
'notifications']
|
'list_transactions', 'notifications'}.union(SUPPORTED_SPENDING_METHODS)
|
||||||
SUPPORTED_NOTIFICATIONS: list[str] = ["payment_sent", "payment_received"]
|
SUPPORTED_NOTIFICATIONS: list[str] = ["payment_sent", "payment_received"]
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
@@ -310,9 +310,9 @@ class NWCServer(Logger, EventListener):
|
|||||||
method: str = content.get('method')
|
method: str = content.get('method')
|
||||||
self.logger.debug(f"got request: {method=}, {params=}")
|
self.logger.debug(f"got request: {method=}, {params=}")
|
||||||
task: Optional[Awaitable] = None
|
task: Optional[Awaitable] = None
|
||||||
if method == "pay_invoice":
|
if method == "pay_invoice" and not self.is_receive_only(event.pubkey):
|
||||||
task = self.handle_pay_invoice(event, params)
|
task = self.handle_pay_invoice(event, params)
|
||||||
elif method == "multi_pay_invoice":
|
elif method == "multi_pay_invoice" and not self.is_receive_only(event.pubkey):
|
||||||
task = self.handle_multi_pay_invoice(event, params)
|
task = self.handle_multi_pay_invoice(event, params)
|
||||||
elif method == "make_invoice":
|
elif method == "make_invoice":
|
||||||
task = self.handle_make_invoice(event, params)
|
task = self.handle_make_invoice(event, params)
|
||||||
@@ -559,6 +559,9 @@ class NWCServer(Logger, EventListener):
|
|||||||
"""
|
"""
|
||||||
height = self.wallet.lnworker.network.blockchain().height()
|
height = self.wallet.lnworker.network.blockchain().height()
|
||||||
blockhash = self.wallet.lnworker.network.blockchain().get_hash(height)
|
blockhash = self.wallet.lnworker.network.blockchain().get_hash(height)
|
||||||
|
supported_methods = self.SUPPORTED_METHODS.copy()
|
||||||
|
if self.is_receive_only(request_event.pubkey):
|
||||||
|
supported_methods -= self.SUPPORTED_SPENDING_METHODS
|
||||||
response = {
|
response = {
|
||||||
"result_type": "get_info",
|
"result_type": "get_info",
|
||||||
"result": {
|
"result": {
|
||||||
@@ -568,7 +571,7 @@ class NWCServer(Logger, EventListener):
|
|||||||
"network": net.NET_NAME,
|
"network": net.NET_NAME,
|
||||||
"block_height": height,
|
"block_height": height,
|
||||||
"block_hash": blockhash,
|
"block_hash": blockhash,
|
||||||
"methods": self.SUPPORTED_METHODS,
|
"methods": list(supported_methods),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if self.SUPPORTED_NOTIFICATIONS:
|
if self.SUPPORTED_NOTIFICATIONS:
|
||||||
@@ -861,12 +864,15 @@ class NWCServer(Logger, EventListener):
|
|||||||
We publish one info event for each client connection.
|
We publish one info event for each client connection.
|
||||||
https://github.com/nostr-protocol/nips/blob/75f246ed987c23c99d77bfa6aeeb1afb669e23f7/47.md#example-nip-47-info-event
|
https://github.com/nostr-protocol/nips/blob/75f246ed987c23c99d77bfa6aeeb1afb669e23f7/47.md#example-nip-47-info-event
|
||||||
"""
|
"""
|
||||||
content = ' '.join(self.SUPPORTED_METHODS)
|
|
||||||
if self.SUPPORTED_NOTIFICATIONS:
|
if self.SUPPORTED_NOTIFICATIONS:
|
||||||
tags = [['notifications', ' '.join(self.SUPPORTED_NOTIFICATIONS)]]
|
tags = [['notifications', ' '.join(self.SUPPORTED_NOTIFICATIONS)]]
|
||||||
else:
|
else:
|
||||||
tags = None
|
tags = None
|
||||||
for client_pubkey, connection in list(self.connections.items()):
|
for client_pubkey, connection in list(self.connections.items()):
|
||||||
|
supported_methods = self.SUPPORTED_METHODS.copy()
|
||||||
|
if self.is_receive_only(client_pubkey):
|
||||||
|
supported_methods -= self.SUPPORTED_SPENDING_METHODS
|
||||||
|
content = ' '.join(supported_methods)
|
||||||
event_id = await aionostr._add_event(
|
event_id = await aionostr._add_event(
|
||||||
self.manager,
|
self.manager,
|
||||||
kind=self.INFO_EVENT_KIND,
|
kind=self.INFO_EVENT_KIND,
|
||||||
@@ -913,3 +919,6 @@ class NWCServer(Logger, EventListener):
|
|||||||
fee = abs(fee) if fee else None
|
fee = abs(fee) if fee else None
|
||||||
return dir, abs(amount), fee, ts
|
return dir, abs(amount), fee, ts
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def is_receive_only(self, pubkey: str) -> bool:
|
||||||
|
return self.connections[pubkey].get('daily_limit_sat') == 0
|
||||||
|
|||||||
@@ -222,14 +222,13 @@ class Plugin(NWCServerPlugin):
|
|||||||
if not name or len(name) < 1:
|
if not name or len(name) < 1:
|
||||||
window.show_error(_("Connection name is required"))
|
window.show_error(_("Connection name is required"))
|
||||||
return None
|
return None
|
||||||
value_limit = limit_edit.value() if limit_edit.value() else None
|
|
||||||
duration_limit = validity_edit.value() if validity_edit.value() else None
|
duration_limit = validity_edit.value() if validity_edit.value() else None
|
||||||
|
|
||||||
# Call create_connection function with user-provided parameters
|
# Call create_connection function with user-provided parameters
|
||||||
try:
|
try:
|
||||||
connection_string = self.create_connection(
|
connection_string = self.create_connection(
|
||||||
name=name,
|
name=name,
|
||||||
daily_limit_sat=value_limit,
|
daily_limit_sat=limit_edit.value(),
|
||||||
valid_for_sec=duration_limit
|
valid_for_sec=duration_limit
|
||||||
)
|
)
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
|
|||||||
Reference in New Issue
Block a user