lnbits migrations work

This commit is contained in:
callebtc
2022-10-12 23:16:39 +02:00
parent 7111639446
commit c36f7a48aa
8 changed files with 358 additions and 158 deletions
+113 -4
View File
@@ -1,7 +1,8 @@
import os
import random
import time
from binascii import hexlify, unhexlify
from typing import List, Optional, Union
from typing import List, Optional, Union, Any
from embit import bip32, bip39, ec, script
from embit.networks import NETWORKS
@@ -13,6 +14,49 @@ from . import db
from .core.base import Invoice
from .models import Cashu, Pegs, Promises, Proof
from cashu.core.base import MintKeyset
from lnbits.db import Database, Connection
class LedgerCrud:
"""
Database interface for Cashu mint.
This class needs to be overloaded by any app that imports the Cashu mint.
"""
async def get_keyset(*args, **kwags):
return await get_keyset(*args, **kwags)
async def get_lightning_invoice(*args, **kwags):
return await get_lightning_invoice(*args, **kwags)
async def get_proofs_used(*args, **kwags):
return await get_proofs_used(*args, **kwags)
async def invalidate_proof(*args, **kwags):
return await invalidate_proof(*args, **kwags)
async def store_keyset(*args, **kwags):
return await store_keyset(*args, **kwags)
async def store_lightning_invoice(*args, **kwags):
return await store_lightning_invoice(*args, **kwags)
async def store_promise(*args, **kwags):
return await store_promise(*args, **kwags)
async def update_lightning_invoice(*args, **kwags):
return await update_lightning_invoice(*args, **kwags)
async def create_cashu(wallet_id: str, data: Cashu) -> Cashu:
cashu_id = urlsafe_short_hash()
@@ -120,9 +164,15 @@ async def get_promises(cashu_id) -> Optional[Cashu]:
return Promises(**row) if row else None
async def get_proofs_used(cashu_id):
rows = await db.fetchall(
"SELECT secret from cashu.proofs_used WHERE cashu_id = ?", (cashu_id,)
async def get_proofs_used(
db: Database,
conn: Optional[Connection] = None,
):
rows = await (conn or db).fetchall(
"""
SELECT secret from cashu.proofs_used
"""
)
return [row[0] for row in rows]
@@ -184,3 +234,62 @@ async def update_lightning_invoice(cashu_id: str, hash: str, issued: bool):
hash,
),
)
##############################
######### KEYSETS ############
##############################
async def store_keyset(
keyset: MintKeyset,
db: Database = None,
conn: Optional[Connection] = None,
):
await (conn or db).execute( # type: ignore
"""
INSERT INTO cashu.keysets
(id, derivation_path, valid_from, valid_to, first_seen, active, version)
VALUES (?, ?, ?, ?, ?, ?, ?)
""",
(
keyset.id,
keyset.derivation_path,
keyset.valid_from or db.timestamp_now,
keyset.valid_to or db.timestamp_now,
keyset.first_seen or db.timestamp_now,
True,
keyset.version,
),
)
async def get_keyset(
id: str = None,
derivation_path: str = "",
db: Database = None,
conn: Optional[Connection] = None,
):
clauses = []
values: List[Any] = []
clauses.append("active = ?")
values.append(True)
if id:
clauses.append("id = ?")
values.append(id)
if derivation_path:
clauses.append("derivation_path = ?")
values.append(derivation_path)
where = ""
if clauses:
where = f"WHERE {' AND '.join(clauses)}"
rows = await (conn or db).fetchall( # type: ignore
f"""
SELECT * from cashu.keysets
{where}
""",
tuple(values),
)
return [MintKeyset.from_row(row) for row in rows]