From ed8c0e1ff42b197d6c5e74fcf3a75762c0a771e4 Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Sun, 26 Nov 2023 07:34:21 -0300 Subject: [PATCH] make format --- cashu/core/base.py | 6 +++--- cashu/core/crypto/keys.py | 4 +++- cashu/mint/conditions.py | 6 +++--- cashu/mint/crud.py | 2 +- cashu/mint/ledger.py | 2 +- cashu/mint/migrations.py | 2 ++ cashu/mint/verification.py | 12 +++++++----- cashu/nostr/client/client.py | 22 ++++++++++++---------- cashu/nostr/event.py | 26 ++++++++++++++------------ cashu/wallet/api/router.py | 34 +++++++++++++++++++--------------- cashu/wallet/p2pk.py | 15 +++++++++------ tests/test_wallet_p2pk.py | 12 ++++++------ 12 files changed, 80 insertions(+), 63 deletions(-) diff --git a/cashu/core/base.py b/cashu/core/base.py index cc3b3425..a10f5fcc 100644 --- a/cashu/core/base.py +++ b/cashu/core/base.py @@ -587,9 +587,9 @@ def __init__( ) def serialize(self): - return json.dumps({ - amount: key.serialize().hex() for amount, key in self.public_keys.items() - }) + return json.dumps( + {amount: key.serialize().hex() for amount, key in self.public_keys.items()} + ) @classmethod def from_row(cls, row: Row): diff --git a/cashu/core/crypto/keys.py b/cashu/core/crypto/keys.py index ec029b06..87259837 100644 --- a/cashu/core/crypto/keys.py +++ b/cashu/core/crypto/keys.py @@ -50,7 +50,9 @@ def derive_pubkey(master_key: str): def derive_pubkeys(keys: Dict[int, PrivateKey]): - return {amt: keys[amt].pubkey for amt in [2**i for i in range(settings.max_order)]} + return { + amt: keys[amt].pubkey for amt in [2**i for i in range(settings.max_order)] + } def derive_keyset_id(keys: Dict[int, PublicKey]): diff --git a/cashu/mint/conditions.py b/cashu/mint/conditions.py index d48c06ef..4967a07f 100644 --- a/cashu/mint/conditions.py +++ b/cashu/mint/conditions.py @@ -255,9 +255,9 @@ def _verify_output_p2pk_spending_conditions( # check if all secrets are P2PK # NOTE: This is redundant, because P2PKSecret.from_secret() already checks for the kind # Leaving it in for explicitness - if not all([ - SecretKind(secret.kind) == SecretKind.P2PK for secret in p2pk_secrets - ]): + if not all( + [SecretKind(secret.kind) == SecretKind.P2PK for secret in p2pk_secrets] + ): # not all secrets are P2PK return True diff --git a/cashu/mint/crud.py b/cashu/mint/crud.py index 4e306d7c..ab841b1e 100644 --- a/cashu/mint/crud.py +++ b/cashu/mint/crud.py @@ -618,6 +618,7 @@ async def store_keyset( keyset.unit.name, ), ) + async def get_balance( self, db: Database, @@ -629,7 +630,6 @@ async def get_balance( assert row, "Balance not found" return int(row[0]) - async def get_keyset( self, *, diff --git a/cashu/mint/ledger.py b/cashu/mint/ledger.py index dd691046..873c3ecb 100644 --- a/cashu/mint/ledger.py +++ b/cashu/mint/ledger.py @@ -634,7 +634,7 @@ async def _generate_promises( list[BlindedSignature]: Generated BlindedSignatures. """ keyset = keyset if keyset else self.keyset - promises : List[Tuple[PublicKey, int, PublicKey, PrivateKey, PrivateKey]]= [] + promises: List[Tuple[PublicKey, int, PublicKey, PrivateKey, PrivateKey]] = [] for output in outputs: B_ = PublicKey(bytes.fromhex(output.B_), raw=True) assert output.id, "output id not set" diff --git a/cashu/mint/migrations.py b/cashu/mint/migrations.py index 92edc735..bb7a0ce4 100644 --- a/cashu/mint/migrations.py +++ b/cashu/mint/migrations.py @@ -208,6 +208,7 @@ async def m009_add_out_to_invoices(db: Database): f"ALTER TABLE {table_with_schema(db, 'invoices')} ADD COLUMN out BOOL" ) + async def m010_add_index_to_proofs_used(db: Database): # create index on proofs_used table for secret async with db.connect() as conn: @@ -217,6 +218,7 @@ async def m010_add_index_to_proofs_used(db: Database): f" {table_with_schema(db, 'proofs_used')} (secret)" ) + async def m011_add_quote_tables(db: Database): async with db.connect() as conn: # add column "created" to tables invoices, promises, proofs_used, proofs_pending diff --git a/cashu/mint/verification.py b/cashu/mint/verification.py index cd3b2d32..ce73ed79 100644 --- a/cashu/mint/verification.py +++ b/cashu/mint/verification.py @@ -82,11 +82,13 @@ async def verify_inputs_and_outputs( # Verify that input keyset units are the same as output keyset unit # We have previously verified that all outputs have the same keyset id in `_verify_outputs` assert outputs[0].id, "output id not set" - if not all([ - self.keysets[p.id].unit == self.keysets[outputs[0].id].unit - for p in proofs - if p.id - ]): + if not all( + [ + self.keysets[p.id].unit == self.keysets[outputs[0].id].unit + for p in proofs + if p.id + ] + ): raise TransactionError("input and output keysets have different units.") # Verify output spending conditions diff --git a/cashu/nostr/client/client.py b/cashu/nostr/client/client.py index dc148c8f..26d523fa 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,13 +105,15 @@ 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 6271e7ef..0ee7ad0b 100644 --- a/cashu/nostr/event.py +++ b/cashu/nostr/event.py @@ -77,18 +77,20 @@ 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/cashu/wallet/api/router.py b/cashu/wallet/api/router.py index b6501277..5a29ff32 100644 --- a/cashu/wallet/api/router.py +++ b/cashu/wallet/api/router.py @@ -359,15 +359,17 @@ async def pending( reserved_date = datetime.utcfromtimestamp( int(grouped_proofs[0].time_reserved) # type: ignore ).strftime("%Y-%m-%d %H:%M:%S") - result.update({ - f"{i}": { - "amount": sum_proofs(grouped_proofs), - "time": reserved_date, - "ID": key, - "token": token, - "mint": mint, + result.update( + { + f"{i}": { + "amount": sum_proofs(grouped_proofs), + "time": reserved_date, + "ID": key, + "token": token, + "mint": mint, + } } - }) + ) return PendingResponse(pending_token=result) @@ -412,14 +414,16 @@ async def wallets(): if w == wallet.name: active_wallet = True if active_wallet: - result.update({ - f"{w}": { - "balance": sum_proofs(wallet.proofs), - "available": sum_proofs([ - p for p in wallet.proofs if not p.reserved - ]), + result.update( + { + f"{w}": { + "balance": sum_proofs(wallet.proofs), + "available": sum_proofs( + [p for p in wallet.proofs if not p.reserved] + ), + } } - }) + ) except Exception: pass return WalletsResponse(wallets=result) diff --git a/cashu/wallet/p2pk.py b/cashu/wallet/p2pk.py index a2d824c7..315d9dbd 100644 --- a/cashu/wallet/p2pk.py +++ b/cashu/wallet/p2pk.py @@ -133,9 +133,12 @@ async def add_witnesses_to_outputs( return outputs # if any of the proofs provided require SIG_ALL, we must provide it - if any([ - P2PKSecret.deserialize(p.secret).sigflag == SigFlags.SIG_ALL for p in proofs - ]): + if any( + [ + P2PKSecret.deserialize(p.secret).sigflag == SigFlags.SIG_ALL + for p in proofs + ] + ): outputs = await self.add_p2pk_witnesses_to_outputs(outputs) return outputs @@ -181,9 +184,9 @@ async def add_witnesses_to_proofs(self, proofs: List[Proof]) -> List[Proof]: return proofs logger.debug("Spending conditions detected.") # P2PK signatures - if all([ - Secret.deserialize(p.secret).kind == SecretKind.P2PK.value for p in proofs - ]): + if all( + [Secret.deserialize(p.secret).kind == SecretKind.P2PK.value for p in proofs] + ): logger.debug("P2PK redemption detected.") proofs = await self.add_p2pk_witnesses_to_proofs(proofs) diff --git a/tests/test_wallet_p2pk.py b/tests/test_wallet_p2pk.py index da6fe14d..860c58ec 100644 --- a/tests/test_wallet_p2pk.py +++ b/tests/test_wallet_p2pk.py @@ -220,9 +220,9 @@ async def test_p2pk_locktime_with_second_refund_pubkey( secret_lock = await wallet1.create_p2pk_lock( garbage_pubkey.serialize().hex(), # create lock to unspendable pubkey locktime_seconds=2, # locktime - tags=Tags([ - ["refund", pubkey_wallet2, pubkey_wallet1] - ]), # multiple refund pubkeys + tags=Tags( + [["refund", pubkey_wallet2, pubkey_wallet1]] + ), # multiple refund pubkeys ) # sender side _, send_proofs = await wallet1.split_to_send( wallet1.proofs, 8, secret_lock=secret_lock @@ -377,9 +377,9 @@ async def test_p2pk_multisig_with_wrong_first_private_key( def test_tags(): - tags = Tags([ - ["key1", "value1"], ["key2", "value2", "value2_1"], ["key2", "value3"] - ]) + tags = Tags( + [["key1", "value1"], ["key2", "value2", "value2_1"], ["key2", "value3"]] + ) assert tags.get_tag("key1") == "value1" assert tags["key1"] == "value1" assert tags.get_tag("key2") == "value2"