lnbits-legend/lnbits/extensions/boltcards/models.py

79 lines
1.8 KiB
Python
Raw Normal View History

2022-06-13 21:08:06 +02:00
from fastapi.params import Query
2022-07-15 16:43:06 +02:00
from pydantic import BaseModel
2022-08-22 23:29:42 +01:00
from sqlite3 import Row
from typing import Optional
from fastapi import Request
from lnurl import Lnurl
from lnurl import encode as lnurl_encode # type: ignore
from lnurl.models import LnurlPaySuccessAction, UrlAction # type: ignore
from lnurl.types import LnurlPayMetadata # type: ignore
from pydantic import BaseModel
from pydantic.main import BaseModel
2022-07-15 16:43:06 +02:00
2022-08-14 23:52:55 +02:00
ZERO_KEY = "00000000000000000000000000000000"
2022-06-13 21:08:06 +02:00
class Card(BaseModel):
id: str
wallet: str
card_name: str
uid: str
counter: int
2022-08-22 22:33:20 +01:00
tx_limit: int
daily_limit: int
2022-08-14 23:52:55 +02:00
k0: str
k1: str
k2: str
prev_k0: str
prev_k1: str
prev_k2: str
otp: str
2022-06-13 21:08:06 +02:00
time: int
2022-08-22 22:33:20 +01:00
def from_row(cls, row: Row) -> "Card":
return cls(**dict(row))
def lnurl(self, req: Request) -> Lnurl:
url = req.url_for(
"boltcard.lnurl_response", device_id=self.id, _external=True
)
return lnurl_encode(url)
async def lnurlpay_metadata(self) -> LnurlPayMetadata:
return LnurlPayMetadata(json.dumps([["text/plain", self.title]]))
2022-07-15 16:43:06 +02:00
2022-06-13 21:08:06 +02:00
class CreateCardData(BaseModel):
card_name: str = Query(...)
uid: str = Query(...)
2022-08-14 23:52:55 +02:00
counter: int = Query(0)
2022-08-22 22:33:20 +01:00
tx_limit: int = Query(0)
daily_limit: int = Query(0)
2022-08-14 23:52:55 +02:00
k0: str = Query(ZERO_KEY)
k1: str = Query(ZERO_KEY)
k2: str = Query(ZERO_KEY)
prev_k0: str = Query(ZERO_KEY)
prev_k1: str = Query(ZERO_KEY)
prev_k2: str = Query(ZERO_KEY)
2022-06-21 18:03:20 +02:00
class Hit(BaseModel):
id: str
card_id: str
ip: str
2022-08-22 22:33:20 +01:00
spent: bool
2022-06-21 18:03:20 +02:00
useragent: str
old_ctr: int
new_ctr: int
time: int
2022-08-22 22:33:20 +01:00
2022-08-22 23:29:42 +01:00
def from_row(cls, row: Row) -> "Hit":
return cls(**dict(row))
2022-08-22 22:33:20 +01:00
class Refund(BaseModel):
id: str
hit_id: str
refund_amount: int
2022-08-22 23:29:42 +01:00
time: int
def from_row(cls, row: Row) -> "Refund":
return cls(**dict(row))