Skip to content

Commit

Permalink
fix kind enum deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
callebtc committed Oct 21, 2023
1 parent aa91964 commit 305598b
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 35 deletions.
11 changes: 3 additions & 8 deletions cashu/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,12 @@ def __setitem__(self, key, val):

@property
def p2pksigs(self) -> List[str]:
assert self.witness, "Witness is missing"
assert self.witness, "Witness is missing for p2pk signature"
return P2PKWitness.from_witness(self.witness).signatures

@property
def p2shscript(self) -> P2SHWitness:
assert self.witness, "Witness is missing"
return P2SHWitness.from_witness(self.witness)

@property
def htlcpreimage(self) -> Union[str, None]:
assert self.witness, "Witness is missing"
assert self.witness, "Witness is missing for htlc preimage"
return HTLCWitness.from_witness(self.witness).preimage


Expand All @@ -162,7 +157,7 @@ class BlindedMessage(BaseModel):

@property
def p2pksigs(self) -> List[str]:
assert self.witness, "Witness is missing"
assert self.witness, "Witness missing in output"
return P2PKWitness.from_witness(self.witness).signatures


Expand Down
2 changes: 1 addition & 1 deletion cashu/core/htlc.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class HTLCSecret(Secret):
@classmethod
def from_secret(cls, secret: Secret):
assert secret.kind == SecretKind.HTLC, "Secret is not a HTLC secret"
assert SecretKind(secret.kind) == SecretKind.HTLC, "Secret is not a HTLC secret"
# NOTE: exclude tags in .dict() because it doesn't deserialize it properly
# need to add it back in manually with tags=secret.tags
return cls(**secret.dict(exclude={"tags"}), tags=secret.tags)
Expand Down
18 changes: 10 additions & 8 deletions cashu/core/p2pk.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import hashlib
import time
from enum import Enum
from typing import List, Union

from loguru import logger
Expand All @@ -8,17 +9,17 @@
from .secret import Secret, SecretKind


class SigFlags:
SIG_INPUTS = ( # require signatures only on the inputs (default signature flag)
"SIG_INPUTS"
)
SIG_ALL = "SIG_ALL" # require signatures on inputs and outputs
class SigFlags(Enum):
# require signatures only on the inputs (default signature flag)
SIG_INPUTS = "SIG_INPUTS"
# require signatures on inputs and outputs
SIG_ALL = "SIG_ALL"


class P2PKSecret(Secret):
@classmethod
def from_secret(cls, secret: Secret):
assert secret.kind == SecretKind.P2PK, "Secret is not a P2PK secret"
assert SecretKind(secret.kind) == SecretKind.P2PK, "Secret is not a P2PK secret"
# NOTE: exclude tags in .dict() because it doesn't deserialize it properly
# need to add it back in manually with tags=secret.tags
return cls(**secret.dict(exclude={"tags"}), tags=secret.tags)
Expand Down Expand Up @@ -57,8 +58,9 @@ def locktime(self) -> Union[None, int]:
return int(locktime) if locktime else None

@property
def sigflag(self) -> Union[None, str]:
return self.tags.get_tag("sigflag")
def sigflag(self) -> Union[None, SigFlags]:
sigflag = self.tags.get_tag("sigflag")
return SigFlags(sigflag) if sigflag else None

@property
def n_sigs(self) -> Union[None, int]:
Expand Down
12 changes: 7 additions & 5 deletions cashu/mint/conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def _verify_p2pk_spending_conditions(self, proof: Proof, secret: Secret) -> bool
- if no valid signatures are present
- if the signature threshold is not met
"""
if secret.kind != SecretKind.P2PK:
if SecretKind(secret.kind) != SecretKind.P2PK:
# not a P2PK secret
return True

Expand Down Expand Up @@ -139,7 +139,7 @@ def _verify_htlc_spending_conditions(self, proof: Proof, secret: Secret) -> bool
- if 'pubkeys' are present but no valid signature is provided
"""

if secret.kind != SecretKind.HTLC:
if SecretKind(secret.kind) != SecretKind.HTLC:
# not a P2PK secret
return True
htlc_secret = HTLCSecret.from_secret(secret)
Expand Down Expand Up @@ -208,11 +208,11 @@ def _verify_input_spending_conditions(self, proof: Proof) -> bool:
return True

# P2PK
if secret.kind == SecretKind.P2PK:
if SecretKind(secret.kind) == SecretKind.P2PK:
return self._verify_p2pk_spending_conditions(proof, secret)

# HTLC
if secret.kind == SecretKind.HTLC:
if SecretKind(secret.kind) == SecretKind.HTLC:
return self._verify_htlc_spending_conditions(proof, secret)

# no spending condition present
Expand Down Expand Up @@ -255,7 +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([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/wallet/htlc.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async def create_htlc_lock(
tags["pubkeys"] = hashlock_pubkey

return HTLCSecret(
kind=SecretKind.HTLC,
kind=SecretKind.HTLC.value,
data=preimage_hash,
tags=tags,
)
Expand Down
10 changes: 7 additions & 3 deletions cashu/wallet/p2pk.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@ async def create_p2pk_lock(
tags["locktime"] = str(
int((datetime.now() + timedelta(seconds=locktime_seconds)).timestamp())
)
tags["sigflag"] = SigFlags.SIG_ALL if sig_all else SigFlags.SIG_INPUTS
tags["sigflag"] = (
SigFlags.SIG_ALL.value if sig_all else SigFlags.SIG_INPUTS.value
)
if n_sigs > 1:
tags["n_sigs"] = str(n_sigs)
logger.debug(f"After tags: {tags}")
return P2PKSecret(
kind=SecretKind.P2PK,
kind=SecretKind.P2PK.value,
data=pubkey,
tags=tags,
)
Expand Down Expand Up @@ -182,7 +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 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
9 changes: 0 additions & 9 deletions tests/test_wallet_htlc.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ async def test_htlc_split(wallet1: Wallet, wallet2: Wallet):
preimage = "00000000000000000000000000000000"
preimage_hash = hashlib.sha256(bytes.fromhex(preimage)).hexdigest()
secret = await wallet1.create_htlc_lock(preimage=preimage)
# p2pk test
_, send_proofs = await wallet1.split_to_send(wallet1.proofs, 8, secret_lock=secret)
for p in send_proofs:
assert HTLCSecret.deserialize(p.secret).data == preimage_hash
Expand All @@ -86,7 +85,6 @@ async def test_htlc_redeem_with_preimage(wallet1: Wallet, wallet2: Wallet):
preimage = "00000000000000000000000000000000"
# preimage_hash = hashlib.sha256(bytes.fromhex(preimage)).hexdigest()
secret = await wallet1.create_htlc_lock(preimage=preimage)
# p2pk test
_, send_proofs = await wallet1.split_to_send(wallet1.proofs, 8, secret_lock=secret)
for p in send_proofs:
p.witness = HTLCWitness(preimage=preimage).json()
Expand All @@ -102,7 +100,6 @@ async def test_htlc_redeem_with_wrong_preimage(wallet1: Wallet, wallet2: Wallet)
secret = await wallet1.create_htlc_lock(
preimage=preimage[:-5] + "11111"
) # wrong preimage
# p2pk test
_, send_proofs = await wallet1.split_to_send(wallet1.proofs, 8, secret_lock=secret)
for p in send_proofs:
p.witness = HTLCWitness(preimage=preimage).json()
Expand All @@ -121,7 +118,6 @@ async def test_htlc_redeem_with_no_signature(wallet1: Wallet, wallet2: Wallet):
secret = await wallet1.create_htlc_lock(
preimage=preimage, hashlock_pubkey=pubkey_wallet1
)
# p2pk test
_, send_proofs = await wallet1.split_to_send(wallet1.proofs, 8, secret_lock=secret)
for p in send_proofs:
p.witness = HTLCWitness(preimage=preimage).json()
Expand All @@ -141,8 +137,6 @@ async def test_htlc_redeem_with_wrong_signature(wallet1: Wallet, wallet2: Wallet
secret = await wallet1.create_htlc_lock(
preimage=preimage, hashlock_pubkey=pubkey_wallet1
)

# p2pk test
_, send_proofs = await wallet1.split_to_send(wallet1.proofs, 8, secret_lock=secret)
signatures = await wallet1.sign_p2pk_proofs(send_proofs)
for p, s in zip(send_proofs, signatures):
Expand All @@ -166,7 +160,6 @@ async def test_htlc_redeem_with_correct_signature(wallet1: Wallet, wallet2: Wall
secret = await wallet1.create_htlc_lock(
preimage=preimage, hashlock_pubkey=pubkey_wallet1
)
# p2pk test
_, send_proofs = await wallet1.split_to_send(wallet1.proofs, 8, secret_lock=secret)

signatures = await wallet1.sign_p2pk_proofs(send_proofs)
Expand All @@ -192,7 +185,6 @@ async def test_htlc_redeem_hashlock_wrong_signature_timelock_correct_signature(
locktime_seconds=5,
locktime_pubkey=pubkey_wallet1,
)
# p2pk test
_, send_proofs = await wallet1.split_to_send(wallet1.proofs, 8, secret_lock=secret)

signatures = await wallet1.sign_p2pk_proofs(send_proofs)
Expand Down Expand Up @@ -226,7 +218,6 @@ async def test_htlc_redeem_hashlock_wrong_signature_timelock_wrong_signature(
locktime_seconds=5,
locktime_pubkey=pubkey_wallet1,
)
# p2pk test
_, send_proofs = await wallet1.split_to_send(wallet1.proofs, 8, secret_lock=secret)

signatures = await wallet1.sign_p2pk_proofs(send_proofs)
Expand Down

0 comments on commit 305598b

Please sign in to comment.