plugins: structure plugin storage in wallet
store all plugin data by plugin name in a root dictionary `plugin_data` inside the wallet db so that plugin data can get deleted again. Prunes the data of plugins from the wallet db on wallet stop if the plugin is not installed anymore.
This commit is contained in:
@@ -25,8 +25,6 @@ if TYPE_CHECKING:
|
||||
from aiohttp_socks import ProxyConnector
|
||||
|
||||
|
||||
STORAGE_NAME = 'nwc_plugin'
|
||||
|
||||
class NWCServerPlugin(BasePlugin):
|
||||
URI_SCHEME = 'nostr+walletconnect://'
|
||||
|
||||
@@ -47,10 +45,10 @@ class NWCServerPlugin(BasePlugin):
|
||||
if self.initialized:
|
||||
# this might be called for several wallets. only use one.
|
||||
return
|
||||
storage = self.get_plugin_storage(wallet)
|
||||
self.connections = storage['connections']
|
||||
storage = self.get_storage(wallet)
|
||||
self.connections = storage.setdefault('connections', {})
|
||||
self.delete_expired_connections()
|
||||
self.nwc_server = NWCServer(self.config, wallet, self.taskgroup)
|
||||
self.nwc_server = NWCServer(self.config, wallet, self.taskgroup, self.connections)
|
||||
asyncio.run_coroutine_threadsafe(self.taskgroup.spawn(self.nwc_server.run()), get_asyncio_loop())
|
||||
self.initialized = True
|
||||
|
||||
@@ -67,13 +65,6 @@ class NWCServerPlugin(BasePlugin):
|
||||
)
|
||||
self.logger.debug(f"NWCServerPlugin closed, stopping taskgroup")
|
||||
|
||||
@staticmethod
|
||||
def get_plugin_storage(wallet: 'Abstract_Wallet') -> dict:
|
||||
storage = wallet.db.get_dict(STORAGE_NAME)
|
||||
if 'connections' not in storage:
|
||||
storage['connections'] = {}
|
||||
return storage
|
||||
|
||||
def delete_expired_connections(self):
|
||||
if self.connections is None:
|
||||
return
|
||||
@@ -167,12 +158,17 @@ class NWCServer(Logger, EventListener):
|
||||
'notifications']
|
||||
SUPPORTED_NOTIFICATIONS: list[str] = ["payment_sent", "payment_received"]
|
||||
|
||||
def __init__(self, config: 'SimpleConfig', wallet: 'Abstract_Wallet', taskgroup: 'OldTaskGroup'):
|
||||
def __init__(
|
||||
self,
|
||||
config: 'SimpleConfig',
|
||||
wallet: 'Abstract_Wallet',
|
||||
taskgroup: 'OldTaskGroup',
|
||||
connection_storage: dict,
|
||||
):
|
||||
Logger.__init__(self)
|
||||
self.config = config # type: 'SimpleConfig'
|
||||
self.wallet = wallet # type: 'Abstract_Wallet'
|
||||
storage = wallet.db.get_dict(STORAGE_NAME) # type: dict
|
||||
self.connections = storage['connections'] # type: dict[str, dict] # client hex pubkey -> connection data
|
||||
self.connections = connection_storage # type: dict[str, dict] # client hex pubkey -> connection data
|
||||
self.relays = config.NOSTR_RELAYS.split(",") or [] # type: List[str]
|
||||
self.do_stop = False
|
||||
self.taskgroup = taskgroup # type: 'OldTaskGroup'
|
||||
|
||||
@@ -78,7 +78,7 @@ class CosignerWallet(Logger):
|
||||
|
||||
KEEP_DELAY = 24*60*60
|
||||
|
||||
def __init__(self, wallet: 'Multisig_Wallet'):
|
||||
def __init__(self, wallet: 'Multisig_Wallet', db_storage: dict):
|
||||
assert isinstance(wallet, Multisig_Wallet)
|
||||
self.wallet = wallet
|
||||
|
||||
@@ -90,7 +90,7 @@ class CosignerWallet(Logger):
|
||||
self.pending = asyncio.Event()
|
||||
self.wallet_uptodate = asyncio.Event()
|
||||
|
||||
self.known_events = wallet.db.get_dict('cosigner_events')
|
||||
self.known_events = db_storage.setdefault('cosigner_events', {})
|
||||
|
||||
for k, v in list(self.known_events.items()):
|
||||
if v < now() - self.KEEP_DELAY:
|
||||
|
||||
@@ -117,7 +117,8 @@ class Plugin(PsbtNostrPlugin):
|
||||
class QmlCosignerWallet(EventListener, CosignerWallet):
|
||||
|
||||
def __init__(self, wallet: 'Multisig_Wallet', plugin: 'Plugin'):
|
||||
CosignerWallet.__init__(self, wallet)
|
||||
db_storage = plugin.get_storage(wallet)
|
||||
CosignerWallet.__init__(self, wallet, db_storage)
|
||||
self.plugin = plugin
|
||||
self.register_callbacks()
|
||||
|
||||
|
||||
@@ -57,7 +57,7 @@ class Plugin(PsbtNostrPlugin):
|
||||
return
|
||||
if wallet.wallet_type == '2fa':
|
||||
return
|
||||
self.add_cosigner_wallet(wallet, QtCosignerWallet(wallet, window))
|
||||
self.add_cosigner_wallet(wallet, QtCosignerWallet(wallet, window, self))
|
||||
|
||||
@hook
|
||||
def on_close_window(self, window):
|
||||
@@ -83,8 +83,9 @@ class Plugin(PsbtNostrPlugin):
|
||||
|
||||
|
||||
class QtCosignerWallet(EventListener, CosignerWallet):
|
||||
def __init__(self, wallet: 'Multisig_Wallet', window: 'ElectrumWindow'):
|
||||
CosignerWallet.__init__(self, wallet)
|
||||
def __init__(self, wallet: 'Multisig_Wallet', window: 'ElectrumWindow', plugin: 'Plugin'):
|
||||
db_storage = plugin.get_storage(wallet)
|
||||
CosignerWallet.__init__(self, wallet, db_storage)
|
||||
self.window = window
|
||||
self.obj = QReceiveSignalObject()
|
||||
self.obj.cosignerReceivedPsbt.connect(self.on_receive)
|
||||
|
||||
Reference in New Issue
Block a user