initial
This commit is contained in:
parent
f4de36d1b7
commit
0da40bd5b7
2 changed files with 29 additions and 62 deletions
81
crud.py
81
crud.py
|
|
@ -4,72 +4,31 @@ from typing import List, Optional
|
||||||
from lnbits.db import Database
|
from lnbits.db import Database
|
||||||
|
|
||||||
from .execution_queue import enqueue
|
from .execution_queue import enqueue
|
||||||
from .models import NWCBudget, NWCKey, NWCNewBudget
|
from .models import NWCBudget, NWCKey, CreateNWCKey
|
||||||
|
|
||||||
db = Database("ext_nwcprovider")
|
db = Database("ext_nwcprovider")
|
||||||
|
|
||||||
|
async def create_nwc(data: CreateNWCKey) -> NWCKey:
|
||||||
async def create_nwc(
|
nwckey_entry = NWCKey(
|
||||||
pubkey: str,
|
pubkey=data.pubkey,
|
||||||
wallet_id: str,
|
wallet=data.wallet_id,
|
||||||
description: str,
|
description=data.description,
|
||||||
expires_at: int,
|
expires_at=int(data.expires_at) if data.expires_at else 0,
|
||||||
permissions: List[str],
|
permissions=" ".join(data.permissions),
|
||||||
budgets: Optional[List[NWCNewBudget]] = None,
|
created_at=int(time.time()),
|
||||||
) -> NWCKey:
|
last_used=int(time.time()),
|
||||||
# Check if the key already exists
|
|
||||||
if await get_nwc(pubkey, None, True):
|
|
||||||
raise Exception("Public key already used")
|
|
||||||
# If not, create it
|
|
||||||
now = int(time.time())
|
|
||||||
await db.execute(
|
|
||||||
"""
|
|
||||||
INSERT INTO nwcprovider.keys (
|
|
||||||
pubkey,
|
|
||||||
wallet,
|
|
||||||
description,
|
|
||||||
permissions,
|
|
||||||
created_at,
|
|
||||||
expires_at,
|
|
||||||
last_used
|
|
||||||
)
|
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
||||||
""",
|
|
||||||
(
|
|
||||||
pubkey,
|
|
||||||
wallet_id,
|
|
||||||
description,
|
|
||||||
" ".join(permissions),
|
|
||||||
now,
|
|
||||||
int(expires_at) if expires_at else 0,
|
|
||||||
now,
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
# Add budgets
|
await db.insert("nwcprovider.keys", nwckey_entry)
|
||||||
if budgets:
|
if data.budgets:
|
||||||
for budget in budgets:
|
for budget in data.budgets:
|
||||||
await db.execute(
|
budget_entry = NWCKey(
|
||||||
"""
|
pubkey=data.pubkey,
|
||||||
INSERT INTO nwcprovider.budgets (
|
budget_msats=budget.budget_msats,
|
||||||
pubkey,
|
refresh_window=budget.refresh_window,
|
||||||
budget_msats,
|
created_at=budget.created_at
|
||||||
refresh_window,
|
|
||||||
created_at
|
|
||||||
)
|
|
||||||
VALUES (?, ?, ?, ?)
|
|
||||||
""",
|
|
||||||
(pubkey, budget.budget_msats, budget.refresh_window, budget.created_at),
|
|
||||||
)
|
)
|
||||||
# Return the created key
|
await db.insert("nwcprovider.budgets", budget_entry)
|
||||||
return NWCKey(
|
return NWCKey(**data.dict())
|
||||||
pubkey=pubkey,
|
|
||||||
wallet=wallet_id,
|
|
||||||
description=description,
|
|
||||||
expires_at=expires_at,
|
|
||||||
permissions=" ".join(permissions),
|
|
||||||
created_at=now,
|
|
||||||
last_used=now,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
async def delete_nwc(pubkey: str, wallet_id: str):
|
async def delete_nwc(pubkey: str, wallet_id: str):
|
||||||
|
|
|
||||||
10
models.py
10
models.py
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
import time
|
import time
|
||||||
from sqlite3 import Row
|
from sqlite3 import Row
|
||||||
from typing import Any, Dict, List
|
from typing import Any, Dict, List, Optional
|
||||||
|
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
@ -79,3 +79,11 @@ class NWCRegistrationRequest(BaseModel):
|
||||||
class NWCGetResponse(BaseModel):
|
class NWCGetResponse(BaseModel):
|
||||||
data: NWCKey
|
data: NWCKey
|
||||||
budgets: List[NWCBudget]
|
budgets: List[NWCBudget]
|
||||||
|
|
||||||
|
class CreateNWCKey(BaseModel):
|
||||||
|
pubkey: str
|
||||||
|
wallet_id: str
|
||||||
|
description: str
|
||||||
|
expires_at: int
|
||||||
|
permissions: List[str]
|
||||||
|
budgets: Optional[List[NWCNewBudget]] = None
|
||||||
Loading…
Add table
Add a link
Reference in a new issue