Skip to content

Commit

Permalink
added authorise hook to take some action before a signing happens
Browse files Browse the repository at this point in the history
  • Loading branch information
monty committed Jun 1, 2024
1 parent cb68125 commit 66ee763
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/monstr/__about__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__='0.1.6'
__version__='0.1.7'
24 changes: 21 additions & 3 deletions src/monstr/signing/nip46.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from abc import ABC, abstractmethod
import logging
import asyncio
import json
Expand All @@ -14,6 +15,12 @@ class SignerException(Exception):
pass


class NIP46AuthoriseInterface(ABC):
@abstractmethod
async def authorise(self, method: str, id: str, params: [str]) -> bool:
pass


class NIP46ServerConnection(EventHandler):
"""
this is the server side of a NIP46 signing process.
Expand All @@ -28,11 +35,14 @@ class NIP46ServerConnection(EventHandler):
def __init__(self,
signer: SignerInterface,
relay: [str],
comm_k: str = None):
comm_k: str = None,
authoriser: NIP46AuthoriseInterface = None):

self._signer = signer
self._comm_k = comm_k
self._relay = relay
self._authoriser = authoriser

self._run = True

# TODO - make client so that if it gets async for _on_connect, _do_event
Expand Down Expand Up @@ -271,8 +281,16 @@ async def ado_event(self, the_client: Client, sub_id, evt: Event):
'nip04_decrypt',
'nip04_encrypt',
'sign_event'}:

await getattr(self, method)(id, params)
logging.info(f'{method} - {params}')

if self._authoriser:
if await self._authoriser.authorise(method, id, params):
await getattr(self, method)(id, params)
else:
await self._do_response(error='request not authorised!',
id=id)
else:
await getattr(self, method)(id, params)

except Exception as e:
logging.debug(f'SignerConnection::ado_event {e}')
Expand Down

0 comments on commit 66ee763

Please sign in to comment.