Skip to content

Commit

Permalink
Merge branch 'main' into serialize-cbor
Browse files Browse the repository at this point in the history
  • Loading branch information
callebtc committed Apr 5, 2024
2 parents 7650a30 + 3b2f1aa commit 526ddcf
Show file tree
Hide file tree
Showing 30 changed files with 1,256 additions and 245 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ jobs:
poetry-version: ["1.7.1"]
backend-wallet-class:
["LndRestWallet", "CoreLightningRestWallet", "LNbitsWallet"]
# mint-database: ["./test_data/test_mint", "postgres://cashu:cashu@localhost:5432/cashu"]
mint-database: ["./test_data/test_mint"]
with:
python-version: ${{ matrix.python-version }}
backend-wallet-class: ${{ matrix.backend-wallet-class }}
mint-database: "./test_data/test_mint"
mint-database: ${{ matrix.mint-database }}
4 changes: 0 additions & 4 deletions .github/workflows/regtest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ jobs:
chmod -R 777 .
bash ./start.sh
- name: Create fake admin
if: ${{ inputs.backend-wallet-class == 'LNbitsWallet' }}
run: docker exec cashu-lnbits-1 poetry run python tools/create_fake_admin.py

- name: Run Tests
env:
WALLET_NAME: test_wallet
Expand Down
9 changes: 9 additions & 0 deletions cashu/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,15 @@ class BlindedSignature(BaseModel):
C_: str # Hex-encoded signature
dleq: Optional[DLEQ] = None # DLEQ proof

@classmethod
def from_row(cls, row: Row):
return cls(
id=row["id"],
amount=row["amount"],
C_=row["c_"],
dleq=DLEQ(e=row["dleq_e"], s=row["dleq_s"]),
)


class BlindedMessages(BaseModel):
# NOTE: not used in Pydantic validation
Expand Down
4 changes: 3 additions & 1 deletion cashu/core/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ class KeysetNotFoundError(KeysetError):
detail = "keyset not found"
code = 12001

def __init__(self):
def __init__(self, keyset_id: Optional[str] = None):
if keyset_id:
self.detail = f"{self.detail}: {keyset_id}"
super().__init__(self.detail, code=self.code)


Expand Down
1 change: 1 addition & 0 deletions cashu/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class FakeWalletSettings(MintSettings):
fakewallet_brr: bool = Field(default=True)
fakewallet_delay_payment: bool = Field(default=False)
fakewallet_stochastic_invoice: bool = Field(default=False)
fakewallet_payment_state: Optional[bool] = Field(default=None)
mint_cache_secrets: bool = Field(default=True)


Expand Down
14 changes: 9 additions & 5 deletions cashu/lightning/corelightningrest.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,17 +247,21 @@ async def get_payment_status(self, checking_id: str) -> PaymentStatus:
r.raise_for_status()
data = r.json()

if r.is_error or "error" in data or not data.get("pays"):
raise Exception("error in corelightning-rest response")
if not data.get("pays"):
# payment not found
logger.error(f"payment not found: {data.get('pays')}")
raise Exception("payment not found")

if r.is_error or "error" in data:
message = data.get("error") or data
raise Exception(f"error in corelightning-rest response: {message}")

pay = data["pays"][0]

fee_msat, preimage = None, None
if self.statuses[pay["status"]]:
# cut off "msat" and convert to int
fee_msat = -int(pay["amount_sent_msat"][:-4]) - int(
pay["amount_msat"][:-4]
)
fee_msat = -int(pay["amount_sent_msat"]) - int(pay["amount_msat"])
preimage = pay["preimage"]

return PaymentStatus(
Expand Down
2 changes: 1 addition & 1 deletion cashu/lightning/fake.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ async def get_invoice_status(self, checking_id: str) -> PaymentStatus:
return PaymentStatus(paid=paid or None)

async def get_payment_status(self, _: str) -> PaymentStatus:
return PaymentStatus(paid=None)
return PaymentStatus(paid=settings.fakewallet_payment_state)

async def paid_invoices_stream(self) -> AsyncGenerator[str, None]:
while True:
Expand Down
12 changes: 11 additions & 1 deletion cashu/lightning/lnbits.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,18 @@ async def get_payment_status(self, checking_id: str) -> PaymentStatus:
if "paid" not in data and "details" not in data:
return PaymentStatus(paid=None)

paid_value = None
if data["paid"]:
paid_value = True
elif not data["paid"] and data["details"]["pending"]:
paid_value = None
elif not data["paid"] and not data["details"]["pending"]:
paid_value = False
else:
raise ValueError(f"unexpected value for paid: {data['paid']}")

return PaymentStatus(
paid=data["paid"],
paid=paid_value,
fee=Amount(unit=Unit.msat, amount=abs(data["details"]["fee"])),
preimage=data["preimage"],
)
Expand Down
8 changes: 7 additions & 1 deletion cashu/lightning/lndrest.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,20 @@ async def get_payment_status(self, checking_id: str) -> PaymentStatus:
async for json_line in r.aiter_lines():
try:
line = json.loads(json_line)

# check for errors
if line.get("error"):
logger.error(
message = (
line["error"]["message"]
if "message" in line["error"]
else line["error"]
)
logger.error(f"LND get_payment_status error: {message}")
return PaymentStatus(paid=None)

payment = line.get("result")

# payment exists
if payment is not None and payment.get("status"):
return PaymentStatus(
paid=statuses[payment["status"]],
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
Loading

0 comments on commit 526ddcf

Please sign in to comment.