restart imports

This commit is contained in:
callebtc
2022-10-11 10:08:05 +02:00
parent 12a433780b
commit fe9de4cbca
5 changed files with 28 additions and 24 deletions

View File

@@ -1,4 +0,0 @@
from cashu.core.settings import MINT_PRIVATE_KEY
from cashu.mint.ledger import Ledger
ledger = Ledger(MINT_PRIVATE_KEY, "data/mint", derivation_path="0/0/0/0")

View File

@@ -11,7 +11,7 @@ from starlette_context.middleware import RawContextMiddleware
from cashu.core.settings import DEBUG, VERSION from cashu.core.settings import DEBUG, VERSION
from .router import router from .router import router
from .startup import load_ledger from .startup import start_mint_init
class CustomHeaderMiddleware(BaseHTTPMiddleware): class CustomHeaderMiddleware(BaseHTTPMiddleware):
@@ -86,5 +86,5 @@ app.include_router(router=router)
@app.on_event("startup") @app.on_event("startup")
async def startup_load_ledger(): async def startup_mint():
await load_ledger() await start_mint_init()

View File

@@ -40,11 +40,11 @@ from cashu.mint.crud import (
class Ledger: class Ledger:
def __init__(self, secret_key: str, db: str, derivation_path=""): def __init__(self, db: Database, seed: str, derivation_path=""):
self.proofs_used: Set[str] = set() self.proofs_used: Set[str] = set()
self.master_key = secret_key self.master_key = seed
self.derivation_path = derivation_path self.derivation_path = derivation_path
self.db: Database = Database("mint", db) self.db = db
async def load_used_proofs(self): async def load_used_proofs(self):
"""Load all used proofs from database.""" """Load all used proofs from database."""
@@ -86,8 +86,8 @@ class Ledger:
async def _generate_promise(self, amount: int, B_: PublicKey): async def _generate_promise(self, amount: int, B_: PublicKey):
"""Generates a promise for given amount and returns a pair (amount, C').""" """Generates a promise for given amount and returns a pair (amount, C')."""
secret_key = self.keyset.private_keys[amount] # Get the correct key private_key_amount = self.keyset.private_keys[amount] # Get the correct key
C_ = b_dhke.step2_bob(B_, secret_key) C_ = b_dhke.step2_bob(B_, private_key_amount)
await store_promise( await store_promise(
amount, B_=B_.serialize().hex(), C_=C_.serialize().hex(), db=self.db amount, B_=B_.serialize().hex(), C_=C_.serialize().hex(), db=self.db
) )
@@ -109,22 +109,24 @@ class Ledger:
raise Exception(f"tokens already spent. Secret: {proof.secret}") raise Exception(f"tokens already spent. Secret: {proof.secret}")
# if no keyset id is given in proof, assume the current one # if no keyset id is given in proof, assume the current one
if not proof.id: if not proof.id:
secret_key = self.keyset.private_keys[proof.amount] private_key_amount = self.keyset.private_keys[proof.amount]
else: else:
# use the appropriate active keyset for this proof.id # use the appropriate active keyset for this proof.id
secret_key = self.keysets.keysets[proof.id].private_keys[proof.amount] private_key_amount = self.keysets.keysets[proof.id].private_keys[
proof.amount
]
C = PublicKey(bytes.fromhex(proof.C), raw=True) C = PublicKey(bytes.fromhex(proof.C), raw=True)
# backwards compatibility with old hash_to_curve < 0.3.3 # backwards compatibility with old hash_to_curve < 0.3.3
try: try:
ret = legacy.verify_pre_0_3_3(secret_key, C, proof.secret) ret = legacy.verify_pre_0_3_3(private_key_amount, C, proof.secret)
if ret: if ret:
return ret return ret
except: except:
pass pass
return b_dhke.verify(secret_key, C, proof.secret) return b_dhke.verify(private_key_amount, C, proof.secret)
def _verify_script(self, idx: int, proof: Proof): def _verify_script(self, idx: int, proof: Proof):
""" """

View File

@@ -16,21 +16,21 @@ from cashu.core.base import (
SplitRequest, SplitRequest,
) )
from cashu.core.errors import CashuError from cashu.core.errors import CashuError
from cashu.mint import ledger from cashu.mint.__main__ import ledger
router: APIRouter = APIRouter() router: APIRouter = APIRouter()
@router.get("/keys") @router.get("/keys")
def keys() -> dict[int, str]: async def keys() -> dict[int, str]:
"""Get the public keys of the mint""" """Get the public keys of the mint"""
return ledger.get_keyset() return await ledger.get_keyset()
@router.get("/keysets") @router.get("/keysets")
def keysets() -> dict[str, list[str]]: async def keysets() -> dict[str, list[str]]:
"""Get all active keysets of the mint""" """Get all active keysets of the mint"""
return {"keysets": ledger.keysets.get_ids()} return {"keysets": await ledger.keysets.get_ids()}
@router.get("/mint") @router.get("/mint")

View File

@@ -7,12 +7,18 @@ from cashu.core.settings import CASHU_DIR, LIGHTNING
from cashu.lightning import WALLET from cashu.lightning import WALLET
from cashu.mint import migrations from cashu.mint import migrations
from . import ledger from cashu.core.settings import MINT_PRIVATE_KEY
from cashu.mint.ledger import Ledger
from cashu.core.db import Database
async def load_ledger(): async def start_mint_init():
ledger = Ledger(
db=Database("mint", "data/mint"),
seed=MINT_PRIVATE_KEY,
derivation_path="0/0/0/0",
)
await migrate_databases(ledger.db, migrations) await migrate_databases(ledger.db, migrations)
# await asyncio.wait([m001_initial(ledger.db)])
await ledger.load_used_proofs() await ledger.load_used_proofs()
await ledger.init_keysets() await ledger.init_keysets()