-
Notifications
You must be signed in to change notification settings - Fork 3
/
views_api.py
179 lines (157 loc) · 5.16 KB
/
views_api.py
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
from http import HTTPStatus
from typing import Dict, List, Optional
import secp256k1
from fastapi import APIRouter, Depends, Request
from fastapi.responses import JSONResponse
from lnbits.core.models import WalletTypeInfo
from lnbits.decorators import check_admin, require_admin_key
from .crud import (
create_nwc,
delete_nwc,
get_all_config_nwc,
get_budgets_nwc,
get_config_nwc,
get_nwc,
get_wallet_nwcs,
set_config_nwc,
)
from .models import NWCGetResponse, NWCRegistrationRequest
from .permission import nwc_permissions
nwcprovider_api_router = APIRouter()
# Get supported permissions
@nwcprovider_api_router.get("/api/v1/permissions", status_code=HTTPStatus.OK)
async def api_get_permissions(
req: Request,
wallet: WalletTypeInfo = Depends(require_admin_key),
) -> Dict:
return nwc_permissions
## Get nwc keys associated with the wallet
@nwcprovider_api_router.get(
"/api/v1/nwc", status_code=HTTPStatus.OK, response_model=List[NWCGetResponse]
)
async def api_get_nwcs(
req: Request,
include_expired: bool = False,
calculate_spent_budget: bool = False,
wallet: WalletTypeInfo = Depends(require_admin_key),
):
wallet_id = wallet.wallet.id
nwcs = await get_wallet_nwcs(wallet_id, include_expired)
out = []
for nwc in nwcs:
budgets = await get_budgets_nwc(nwc.pubkey, calculate_spent_budget)
res = NWCGetResponse(data=nwc, budgets=budgets)
out.append(res)
return out
# Get a nwc key
@nwcprovider_api_router.get(
"/api/v1/nwc/{pubkey}", status_code=HTTPStatus.OK, response_model=NWCGetResponse
)
async def api_get_nwc(
req: Request,
pubkey: str,
include_expired: Optional[bool] = False,
wallet: WalletTypeInfo = Depends(require_admin_key),
) -> NWCGetResponse:
wallet_id = wallet.wallet.id
nwc = await get_nwc(pubkey, wallet_id, include_expired)
if not nwc:
raise Exception("Pubkey has no associated wallet")
res = NWCGetResponse(data=nwc, budgets=await get_budgets_nwc(pubkey))
return res
# Get pairing url for given secret
@nwcprovider_api_router.get(
"/api/v1/pairing/{secret}", status_code=HTTPStatus.OK, response_model=str
)
async def api_get_pairing_url(req: Request, secret: str) -> str:
pprivkey: Optional[str] = await get_config_nwc("provider_key")
if not pprivkey:
raise Exception("Extension is not configured")
relay = await get_config_nwc("relay")
if not relay:
raise Exception("Extension is not configured")
relay_alias: Optional[str] = await get_config_nwc("relay_alias")
if relay_alias:
relay = relay_alias
else:
if relay == "nostrclient":
scheme = req.url.scheme # http or https
netloc = req.url.netloc # hostname and port
if scheme == "http":
scheme = "ws"
else:
scheme = "wss"
netloc += "/nostrclient/api/v1/relay"
relay = f"{scheme}://{netloc}"
psk = secp256k1.PrivateKey(bytes.fromhex(pprivkey))
ppk = psk.pubkey
if not ppk:
raise Exception("Error generating pubkey")
ppubkey = ppk.serialize().hex()[2:]
url = "nostr+walletconnect://"
url += ppubkey
url += "?relay=" + relay
url += "&secret=" + secret
# lud16=?
return url
## Register a new nwc key
@nwcprovider_api_router.put(
"/api/v1/nwc/{pubkey}",
status_code=HTTPStatus.CREATED,
response_model=NWCGetResponse,
)
async def api_register_nwc(
req: Request,
pubkey: str,
registration_data: NWCRegistrationRequest, # Use the Pydantic model here
wallet: WalletTypeInfo = Depends(require_admin_key),
):
wallet_id = wallet.wallet.id
nwc = await create_nwc(
pubkey,
wallet_id,
registration_data.description,
registration_data.expires_at,
registration_data.permissions,
registration_data.budgets,
)
budgets = await get_budgets_nwc(pubkey)
res = NWCGetResponse(data=nwc, budgets=budgets)
return res
# Delete a nwc key
@nwcprovider_api_router.delete("/api/v1/nwc/{pubkey}", status_code=HTTPStatus.OK)
async def api_delete_nwc(
req: Request, pubkey: str, wallet: WalletTypeInfo = Depends(require_admin_key)
):
wallet_id = wallet.wallet.id
await delete_nwc(pubkey, wallet_id)
return JSONResponse(content={"message": f"NWC key {pubkey} deleted successfully."})
# Get config
@nwcprovider_api_router.get(
"/api/v1/config", status_code=HTTPStatus.OK, dependencies=[Depends(check_admin)]
)
async def api_get_all_config_nwc(
req: Request,
):
config = await get_all_config_nwc()
return config
# Get config
@nwcprovider_api_router.get(
"/api/v1/config/{key}",
status_code=HTTPStatus.OK,
dependencies=[Depends(check_admin)],
)
async def api_get_config_nwc(req: Request, key: str):
config = await get_config_nwc(key)
out = {}
out[key] = config
return out
# Set config
@nwcprovider_api_router.post(
"/api/v1/config", status_code=HTTPStatus.OK, dependencies=[Depends(check_admin)]
)
async def api_set_config_nwc(req: Request):
data = await req.json()
for key, value in data.items():
await set_config_nwc(key, value)
return await api_get_all_config_nwc(req)