Skip to content

Commit

Permalink
make format
Browse files Browse the repository at this point in the history
  • Loading branch information
callebtc committed Nov 26, 2023
1 parent df843bb commit ed8c0e1
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 63 deletions.
6 changes: 3 additions & 3 deletions cashu/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 3 additions & 1 deletion cashu/core/crypto/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]):
Expand Down
6 changes: 3 additions & 3 deletions cashu/mint/conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion cashu/mint/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,7 @@ async def store_keyset(
keyset.unit.name,
),
)

async def get_balance(
self,
db: Database,
Expand All @@ -629,7 +630,6 @@ async def get_balance(
assert row, "Balance not found"
return int(row[0])


async def get_keyset(
self,
*,
Expand Down
2 changes: 1 addition & 1 deletion cashu/mint/ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions cashu/mint/migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
12 changes: 7 additions & 5 deletions cashu/mint/verification.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 12 additions & 10 deletions cashu/nostr/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)

Expand Down
26 changes: 14 additions & 12 deletions cashu/nostr/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 19 additions & 15 deletions cashu/wallet/api/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down Expand Up @@ -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)
Expand Down
15 changes: 9 additions & 6 deletions cashu/wallet/p2pk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)

Expand Down
12 changes: 6 additions & 6 deletions tests/test_wallet_p2pk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit ed8c0e1

Please sign in to comment.