From 9aea4d75c69f692db6449484bf19b41131f351cd Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Fri, 24 Nov 2023 14:12:13 -0300 Subject: [PATCH] make format --- cashu/core/base.py | 7 ++++++- cashu/nostr/client/client.py | 22 ++++++++++------------ cashu/nostr/event.py | 26 ++++++++++++-------------- tests/test_wallet.py | 11 +++++++++-- 4 files changed, 37 insertions(+), 29 deletions(-) diff --git a/cashu/core/base.py b/cashu/core/base.py index 7e0d4b21..b5e14f8d 100644 --- a/cashu/core/base.py +++ b/cashu/core/base.py @@ -348,6 +348,11 @@ def __init__( self.public_keys = public_keys # overwrite id by deriving it from the public keys self.id = derive_keyset_id(self.public_keys) + logger.trace(f"Derived keyset id {self.id} from public keys.") + if id and id != self.id: + logger.warning( + f"WARNING: Keyset id {self.id} does not match the given id {id}." + ) def serialize(self): return json.dumps( @@ -356,7 +361,7 @@ def serialize(self): @classmethod def from_row(cls, row: Row): - def deserialize(serialized: str): + def deserialize(serialized: str) -> Dict[int, PublicKey]: return { int(amount): PublicKey(bytes.fromhex(hex_key), raw=True) for amount, hex_key in dict(json.loads(serialized)).items() diff --git a/cashu/nostr/client/client.py b/cashu/nostr/client/client.py index 26d523fa..dc148c8f 100644 --- a/cashu/nostr/client/client.py +++ b/cashu/nostr/client/client.py @@ -45,9 +45,9 @@ def __init__(self, private_key: str = "", relays: List[str] = [], connect=True): def connect(self): for relay in self.relays: self.relay_manager.add_relay(relay) - self.relay_manager.open_connections( - {"cert_reqs": ssl.CERT_NONE} - ) # NOTE: This disables ssl certificate verification + self.relay_manager.open_connections({ + "cert_reqs": ssl.CERT_NONE + }) # NOTE: This disables ssl certificate verification def close(self): self.relay_manager.close_connections() @@ -105,15 +105,13 @@ def dm(self, message: str, to_pubkey: PublicKey): self.relay_manager.publish_event(dm) def get_dm(self, sender_publickey: PublicKey, callback_func=None, filter_kwargs={}): - filters = Filters( - [ - Filter( - kinds=[EventKind.ENCRYPTED_DIRECT_MESSAGE], - pubkey_refs=[sender_publickey.hex()], - **filter_kwargs, - ) - ] - ) + filters = Filters([ + Filter( + kinds=[EventKind.ENCRYPTED_DIRECT_MESSAGE], + pubkey_refs=[sender_publickey.hex()], + **filter_kwargs, + ) + ]) subscription_id = os.urandom(4).hex() self.relay_manager.add_subscription(subscription_id, filters) diff --git a/cashu/nostr/event.py b/cashu/nostr/event.py index 0ee7ad0b..6271e7ef 100644 --- a/cashu/nostr/event.py +++ b/cashu/nostr/event.py @@ -77,20 +77,18 @@ def verify(self) -> bool: ) def to_message(self) -> str: - return json.dumps( - [ - ClientMessageType.EVENT, - { - "id": self.id, - "pubkey": self.public_key, - "created_at": self.created_at, - "kind": self.kind, - "tags": self.tags, - "content": self.content, - "sig": self.signature, - }, - ] - ) + return json.dumps([ + ClientMessageType.EVENT, + { + "id": self.id, + "pubkey": self.public_key, + "created_at": self.created_at, + "kind": self.kind, + "tags": self.tags, + "content": self.content, + "sig": self.signature, + }, + ]) @dataclass diff --git a/tests/test_wallet.py b/tests/test_wallet.py index 3ddcb511..d719ce94 100644 --- a/tests/test_wallet.py +++ b/tests/test_wallet.py @@ -10,7 +10,7 @@ from cashu.core.errors import CashuError, KeysetNotFoundError from cashu.core.helpers import sum_proofs from cashu.core.settings import settings -from cashu.wallet.crud import get_lightning_invoice, get_proofs +from cashu.wallet.crud import get_keyset, get_lightning_invoice, get_proofs from cashu.wallet.wallet import Wallet from cashu.wallet.wallet import Wallet as Wallet1 from cashu.wallet.wallet import Wallet as Wallet2 @@ -120,14 +120,21 @@ async def test_get_keyset_from_db(wallet1: Wallet): # first load it from the mint # await wallet1._load_mint_keys() # NOTE: conftest already called wallet.load_mint() which got the keys from the mint + keyset1 = copy.copy(wallet1.keysets[wallet1.keyset_id]) # then load it from the db - keyset1 = copy.copy(wallet1.keysets[wallet1.keyset_id]) await wallet1._load_mint_keys() keyset2 = copy.copy(wallet1.keysets[wallet1.keyset_id]) + assert keyset1.public_keys == keyset2.public_keys assert keyset1.id == keyset2.id + # load it directly from the db + keyset3 = await get_keyset(db=wallet1.db, id=keyset1.id) + assert keyset3 + assert keyset1.public_keys == keyset3.public_keys + assert keyset1.id == keyset3.id + @pytest.mark.asyncio async def test_get_info(wallet1: Wallet):