-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmodels.py
More file actions
114 lines (84 loc) · 2.39 KB
/
Copy pathmodels.py
File metadata and controls
114 lines (84 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# Data models for your extension
import time
from sqlite3 import Row
from typing import Any
from pydantic import BaseModel
class NWCKey(BaseModel):
pubkey: str
wallet: str
description: str
expires_at: int
permissions: str
created_at: int
last_used: int
def get_permissions(self) -> list[str]:
try:
return self.permissions.split(" ")
except Exception:
return []
@classmethod
def from_row(cls, row: dict[str, Any]) -> "NWCKey":
return cls(**row)
class NWCBudget(BaseModel):
id: int
pubkey: str
budget_msats: int
refresh_window: int
created_at: int
used_budget_msats: int = 0
def get_timestamp_range(self) -> tuple[int, int]:
c = int(time.time())
if self.refresh_window <= 0: # never refresh
# return a timestamp in the future
return self.created_at, c + 21000000
# calculate the next refresh timestamp
elapsed = c - self.created_at
passed_cycles = elapsed // self.refresh_window
last_cycle = self.created_at + (passed_cycles * self.refresh_window)
next_cycle = last_cycle + self.refresh_window
return last_cycle, next_cycle
@classmethod
def from_row(cls, row: Row) -> "NWCBudget":
return cls(**dict(row))
class NWCNewBudget(BaseModel):
pubkey: str | None
budget_msats: int
refresh_window: int
created_at: int
# CRUD models
class CreateNWCKey(BaseModel):
pubkey: str
wallet: str
description: str
expires_at: int
permissions: list[str]
budgets: list[NWCNewBudget] | None = None
class DeleteNWC(BaseModel):
pubkey: str
wallet: str | None = None
class GetWalletNWC(BaseModel):
wallet: str | None = None
include_expired: bool | None = False
class GetNWC(BaseModel):
pubkey: str
wallet: str | None = None
include_expired: bool | None = False
refresh_last_used: bool | None = False
class GetBudgetsNWC(BaseModel):
pubkey: str
calculate_spent: bool | None = False
class TrackedSpendNWC(BaseModel):
pubkey: str
amount_msats: int
# API models
class NWCRegistrationRequest(BaseModel):
permissions: list[str]
description: str
expires_at: int
budgets: list[NWCNewBudget]
class NWCGetResponse(BaseModel):
data: NWCKey
budgets: list[NWCBudget]
class NWCConfig(BaseModel):
key: str
value: str