Skip to content

Commit

Permalink
Store proofs used for paying a lightning invoice
Browse files Browse the repository at this point in the history
  • Loading branch information
sihamon committed Sep 7, 2023
1 parent 87c0adc commit c198abe
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
27 changes: 27 additions & 0 deletions cashu/mint/crud.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Any, List, Optional

from ..core.base import BlindedSignature, Invoice, MintKeyset, Proof
from ..core.bolt11 import Invoice as bolt11_invoice
from ..core.db import Connection, Database, table_with_schema


Expand Down Expand Up @@ -47,6 +48,9 @@ async def get_promise(*args, **kwags):
async def update_lightning_invoice(*args, **kwags):
return await update_lightning_invoice(*args, **kwags) # type: ignore

async def store_invoice_and_proofs(*args, **kwargs):
return await store_invoice_and_proofs(*args, **kwargs)


async def store_promise(
db: Database,
Expand Down Expand Up @@ -264,3 +268,26 @@ async def get_keyset(
tuple(values),
)
return [MintKeyset(**row) for row in rows]


async def store_invoice_and_proofs(
db: Database,
invoice: bolt11_invoice,
proof: Proof,
conn: Optional[Connection] = None,
):
await (conn or db).execute(
f"""
INSERT INTO {table_with_schema(db, 'payments')}
(invoice_amount, date, payment_hash, proof_amount, proof_C, proof_secret)
VALUES (?, ?, ?, ?, ?, ?)
""",
(
invoice.amount_msat / 1000,
invoice.date,
invoice.payment_hash,
proof.amount,
proof.C,
proof.secret,
),
)
2 changes: 2 additions & 0 deletions cashu/mint/ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,8 @@ async def melt(
)

if status:
for p in proofs:
await self.crud.store_invoice_and_proofs(self.db, invoice_obj, p)
logger.trace("invalidating proofs")
await self._invalidate_proofs(proofs)
logger.trace("invalidated proofs")
Expand Down
19 changes: 19 additions & 0 deletions cashu/mint/migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,22 @@ async def m007_proofs_and_promises_store_id(db: Database):
await db.execute(
f"ALTER TABLE {table_with_schema(db, 'promises')} ADD COLUMN id TEXT"
)


async def m008_paid_invoices_and_proofs(db: Database):
"""
Table to store invoices paid by the mint and corresponding proofs.
"""
await db.execute(f"""
CREATE TABLE IF NOT EXISTS {table_with_schema(db, 'payments')} (
invoice_amount {db.big_int} NOT NULL,
date TEXT NOT NULL,
payment_hash TEXT NOT NULL,
proof_amount TEXT NOT NULL,
proof_C TEXT NOT NULL,
proof_secret TEXT NOT NULL,
UNIQUE (proof_secret)
);
""")

0 comments on commit c198abe

Please sign in to comment.