Skip to content

Commit

Permalink
remove p2sh
Browse files Browse the repository at this point in the history
  • Loading branch information
callebtc committed Oct 13, 2023
1 parent c3b3a45 commit c288524
Show file tree
Hide file tree
Showing 17 changed files with 119 additions and 530 deletions.
2 changes: 0 additions & 2 deletions cashu/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from .crypto.keys import derive_keys, derive_keyset_id, derive_pubkeys
from .crypto.secp import PrivateKey, PublicKey
from .legacy import derive_keys_backwards_compatible_insecure_pre_0_12
from .p2pk import P2SHScript


class DLEQ(BaseModel):
Expand Down Expand Up @@ -47,7 +46,6 @@ class Proof(BaseModel):
dleq: Union[DLEQWallet, None] = None # DLEQ proof

p2pksigs: Union[List[str], None] = [] # P2PK signature
p2shscript: Union[P2SHScript, None] = None # P2SH spending condition
htlcpreimage: Union[str, None] = None # HTLC unlocking preimage
htlcsignature: Union[str, None] = None # HTLC unlocking signature
# whether this proof is reserved for sending, used for coin management in the wallet
Expand Down
11 changes: 0 additions & 11 deletions cashu/core/p2pk.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from typing import List, Union

from loguru import logger
from pydantic import BaseModel

from .crypto.secp import PrivateKey, PublicKey
from .secret import Secret, SecretKind
Expand Down Expand Up @@ -64,16 +63,6 @@ def n_sigs(self) -> Union[None, int]:
return int(n_sigs) if n_sigs else None


class P2SHScript(BaseModel):
"""
Unlocks P2SH spending condition of a Proof
"""

script: str
signature: str
address: Union[str, None] = None


def sign_p2pk_sign(message: bytes, private_key: PrivateKey):
# ecdsa version
# signature = private_key.ecdsa_serialize(private_key.ecdsa_sign(message))
Expand Down
168 changes: 0 additions & 168 deletions cashu/core/script.py

This file was deleted.

1 change: 0 additions & 1 deletion cashu/core/secret.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@


class SecretKind:
P2SH = "P2SH"
P2PK = "P2PK"
HTLC = "HTLC"

Expand Down
33 changes: 1 addition & 32 deletions cashu/mint/conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,55 +18,24 @@
SigFlags,
verify_p2pk_signature,
)
from ..core.script import verify_bitcoin_script
from ..core.secret import Secret, SecretKind


class LedgerSpendingConditions:
def _verify_input_spending_conditions(self, proof: Proof) -> bool:
"""
Verify spending conditions:
Condition: P2SH - Witnesses proof.p2shscript
Condition: P2PK - Witness: proof.p2pksigs
Condition: HTLC - Witness: proof.htlcpreimage, proof.htlcsignature
"""
# P2SH

try:
secret = Secret.deserialize(proof.secret)
logger.trace(f"proof.secret: {proof.secret}")
logger.trace(f"secret: {secret}")
except Exception:
# secret is not a spending condition so we treat is a normal secret
return True
if secret.kind == SecretKind.P2SH:
p2pk_secret = P2PKSecret.from_secret(secret)
# check if locktime is in the past
now = time.time()
if p2pk_secret.locktime and p2pk_secret.locktime < now:
logger.trace(f"p2sh locktime ran out ({p2pk_secret.locktime}<{now}).")
return True
logger.trace(f"p2sh locktime still active ({p2pk_secret.locktime}>{now}).")

if (
proof.p2shscript is None
or proof.p2shscript.script is None
or proof.p2shscript.signature is None
):
# no script present although secret indicates one
raise TransactionError("no script in proof.")

# execute and verify P2SH
txin_p2sh_address, valid = verify_bitcoin_script(
proof.p2shscript.script, proof.p2shscript.signature
)
if not valid:
raise TransactionError("script invalid.")
# check if secret commits to script address
assert secret.data == str(txin_p2sh_address), (
f"secret does not contain correct P2SH address: {secret.data} is not"
f" {txin_p2sh_address}."
)
return True

# P2PK
if secret.kind == SecretKind.P2PK:
Expand Down
6 changes: 3 additions & 3 deletions cashu/wallet/api/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from pydantic import BaseModel

from ...core.base import Invoice, P2SHScript
from ...core.base import Invoice


class PayResponse(BaseModel):
Expand Down Expand Up @@ -50,11 +50,11 @@ class PendingResponse(BaseModel):


class LockResponse(BaseModel):
P2SH: Optional[str]
P2PK: Optional[str]


class LocksResponse(BaseModel):
locks: List[P2SHScript]
locks: List[str]


class InvoicesResponse(BaseModel):
Expand Down
12 changes: 6 additions & 6 deletions cashu/wallet/api/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from ...core.settings import settings
from ...nostr.nostr.client.client import NostrClient
from ...tor.tor import TorProxy
from ...wallet.crud import get_lightning_invoices, get_reserved_proofs, get_unused_locks
from ...wallet.crud import get_lightning_invoices, get_reserved_proofs
from ...wallet.helpers import (
deserialize_token_from_string,
init_wallet,
Expand Down Expand Up @@ -213,7 +213,7 @@ async def balance():
async def send_command(
amount: int = Query(default=..., description="Amount to send"),
nostr: str = Query(default=None, description="Send to nostr pubkey"),
lock: str = Query(default=None, description="Lock tokens (P2SH)"),
lock: str = Query(default=None, description="Lock tokens (P2PK)"),
mint: str = Query(
default=None,
description="Mint URL to send from (None for default mint)",
Expand Down Expand Up @@ -354,14 +354,14 @@ async def pending(

@router.get("/lock", name="Generate receiving lock", response_model=LockResponse)
async def lock():
address = await wallet.create_p2sh_address_and_store()
return LockResponse(P2SH=address)
pubkey = await wallet.create_p2pk_pubkey()
return LockResponse(P2PK=pubkey)


@router.get("/locks", name="Show unused receiving locks", response_model=LocksResponse)
async def locks():
locks = await get_unused_locks(db=wallet.db)
return LocksResponse(locks=locks)
pubkey = await wallet.create_p2pk_pubkey()
return LocksResponse(locks=[pubkey])


@router.get(
Expand Down
Loading

0 comments on commit c288524

Please sign in to comment.