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
+23 -3
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