Broke, started added private/public key

This commit is contained in:
ben 2022-09-21 15:16:38 +01:00
parent cf849b260c
commit a9b6dd3d30
4 changed files with 39 additions and 7 deletions

View file

@ -5,13 +5,20 @@ from lnbits.helpers import urlsafe_short_hash
from . import db
from .models import Cashu, Pegs
from embit import script
from embit import ec
from embit.networks import NETWORKS
from binascii import unhexlify, hexlify
async def create_cashu(wallet_id: str, data: Cashu) -> Cashu:
cashu_id = urlsafe_short_hash()
prv = ec.PrivateKey.from_wif(urlsafe_short_hash())
pub = prv.get_public_key()
await db.execute(
"""
INSERT INTO cashu.cashu (id, wallet, name, tickershort, fraction, maxsats, coins)
VALUES (?, ?, ?, ?, ?, ?, ?)
INSERT INTO cashu.cashu (id, wallet, name, tickershort, fraction, maxsats, coins, prvkey, pubkey)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
""",
(
cashu_id,
@ -20,7 +27,9 @@ async def create_cashu(wallet_id: str, data: Cashu) -> Cashu:
data.tickershort,
data.fraction,
data.maxsats,
data.coins
data.coins,
prv,
pub
),
)
@ -29,6 +38,17 @@ async def create_cashu(wallet_id: str, data: Cashu) -> Cashu:
return cashu
async def update_cashu_keys(cashu_id, wif: str = None) -> Optional[Cashu]:
if not wif:
prv = ec.PrivateKey.from_wif(urlsafe_short_hash())
else:
prv = ec.PrivateKey.from_wif(wif)
pub = prv.get_public_key()
await db.execute("UPDATE cashu.cashu SET prv = ?, pub = ? WHERE id = ?", (hexlify(prv.serialize()), hexlify(pub.serialize()), cashu_id))
row = await db.fetchone("SELECT * FROM cashu.cashu WHERE id = ?", (cashu_id,))
return Cashu(**row) if row else None
async def get_cashu(cashu_id: str) -> Optional[Cashu]:
row = await db.fetchone("SELECT * FROM cashu.cashu WHERE id = ?", (cashu_id,))
return Cashu(**row) if row else None

View file

@ -11,8 +11,9 @@ async def m001_initial(db):
tickershort TEXT NOT NULL,
fraction BOOL,
maxsats INT,
coins INT
coins INT,
prvkey TEXT NOT NULL,
pubkey TEXT NOT NULL
);
"""
)

View file

@ -13,7 +13,8 @@ class Cashu(BaseModel):
fraction: bool = Query(None)
maxsats: int = Query(0)
coins: int = Query(0)
prvkey: str = Query(None)
pubkey: str = Query(None)
@classmethod
def from_row(cls, row: Row) -> "TPoS":

View file

@ -13,7 +13,7 @@ from lnbits.core.views.api import api_payment
from lnbits.decorators import WalletTypeInfo, get_key_type, require_admin_key
from . import cashu_ext
from .crud import create_cashu, delete_cashu, get_cashu, get_cashus
from .crud import create_cashu, delete_cashu, get_cashu, get_cashus, update_cashu_keys
from .models import Cashu, Pegs, PayLnurlWData
@ -36,6 +36,16 @@ async def api_cashu_create(
logger.debug(cashu)
return cashu.dict()
@cashu_ext.post("/api/v1/cashus/upodatekeys", status_code=HTTPStatus.CREATED)
async def api_cashu_update_keys(
data: Cashu, wallet: WalletTypeInfo = Depends(get_key_type)
):
cashu = await get_cashu(data.id)
cashu = await create_cashu(wallet_id=wallet.wallet.id, data=data)
logger.debug(cashu)
return cashu.dict()
@cashu_ext.delete("/api/v1/cashus/{cashu_id}")
async def api_cashu_delete(