-
Notifications
You must be signed in to change notification settings - Fork 2
/
crud.py
208 lines (161 loc) · 5.66 KB
/
crud.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
from typing import Optional
from lnbits.db import Database
from lnbits.helpers import urlsafe_short_hash
from .helpers import derive_address
from .models import Address, Config, ConfigDb, WalletAccount
db = Database("ext_watchonly")
async def create_watch_wallet(wallet: WalletAccount) -> WalletAccount:
await db.insert("watchonly.wallets", wallet)
return wallet
async def get_watch_wallet(wallet_id: str) -> Optional[WalletAccount]:
return await db.fetchone(
"SELECT * FROM watchonly.wallets WHERE id = :id",
{"id": wallet_id},
WalletAccount,
)
async def get_watch_wallets(user: str, network: str) -> list[WalletAccount]:
return await db.fetchall(
"""
SELECT * FROM watchonly.wallets
WHERE "user" = :user AND network = :network
""",
{"user": user, "network": network},
WalletAccount,
)
async def update_watch_wallet(wallet: WalletAccount) -> WalletAccount:
await db.update("watchonly.wallets", wallet)
return wallet
async def delete_watch_wallet(wallet_id: str) -> None:
await db.execute(
"DELETE FROM watchonly.wallets WHERE id = :id",
{"id": wallet_id},
)
async def get_fresh_address(wallet_id: str) -> Optional[Address]:
# todo: move logic to views_api after satspay refactoring
wallet = await get_watch_wallet(wallet_id)
if not wallet:
return None
wallet_addresses = await get_addresses(wallet_id)
receive_addresses = list(
filter(
lambda addr: addr.branch_index == 0 and addr.has_activity, wallet_addresses
)
)
last_receive_index = (
receive_addresses.pop().address_index if receive_addresses else -1
)
address_index = (
last_receive_index
if last_receive_index > wallet.address_no
else wallet.address_no
)
address = await get_address_at_index(wallet_id, 0, address_index + 1)
if not address:
addresses = await create_fresh_addresses(
wallet_id, address_index + 1, address_index + 2
)
address = addresses.pop()
wallet.address_no = address_index + 1
await update_watch_wallet(wallet)
return address
async def create_fresh_addresses(
wallet_id: str,
start_address_index: int,
end_address_index: int,
change_address=False,
) -> list[Address]:
if start_address_index > end_address_index:
return []
wallet = await get_watch_wallet(wallet_id)
if not wallet:
return []
branch_index = 1 if change_address else 0
for address_index in range(start_address_index, end_address_index):
address = await derive_address(wallet.masterpub, address_index, branch_index)
assert address # TODO: why optional
addr = Address(
id=urlsafe_short_hash(),
address=address,
wallet=wallet_id,
branch_index=branch_index,
address_index=address_index,
)
await db.insert("watchonly.addresses", addr)
# return fresh addresses
return await db.fetchall(
"""
SELECT * FROM watchonly.addresses WHERE wallet = :wallet
AND branch_index = :branch_index
AND address_index >= :start_address_index
AND address_index < :end_address_index
ORDER BY branch_index, address_index
""",
{
"wallet": wallet_id,
"branch_index": branch_index,
"start_address_index": start_address_index,
"end_address_index": end_address_index,
},
Address,
)
async def get_address(address: str) -> Optional[Address]:
return await db.fetchone(
"SELECT * FROM watchonly.addresses WHERE address = :address",
{"address": address},
Address,
)
async def get_address_by_id(address_id: str) -> Optional[Address]:
return await db.fetchone(
"SELECT * FROM watchonly.addresses WHERE id = :id",
{"id": address_id},
Address,
)
async def get_address_at_index(
wallet_id: str, branch_index: int, address_index: int
) -> Optional[Address]:
return await db.fetchone(
"""
SELECT * FROM watchonly.addresses
WHERE wallet = :wallet AND branch_index = :branch_index
AND address_index = :address_index
""",
{
"wallet": wallet_id,
"branch_index": branch_index,
"address_index": address_index,
},
Address,
)
async def get_addresses(wallet_id: str) -> list[Address]:
return await db.fetchall(
"""
SELECT * FROM watchonly.addresses WHERE wallet = :wallet
ORDER BY branch_index, address_index
""",
{"wallet": wallet_id},
Address,
)
async def update_address(address: Address) -> Address:
await db.update("watchonly.addresses", address)
return address
async def delete_addresses_for_wallet(wallet_id: str) -> None:
await db.execute(
"DELETE FROM watchonly.addresses WHERE wallet = :wallet", {"wallet": wallet_id}
)
async def create_config(user: str) -> Config:
config = Config()
await db.insert("watchonly.config", ConfigDb(user=user, json_data=config))
return config
async def update_config(config: Config, user: str) -> Config:
_config = ConfigDb(user=user, json_data=config)
await db.update("watchonly.config", _config, """WHERE "user" = :user""")
return config
async def get_config(user: str) -> Config:
_config = await db.fetchone(
"""SELECT * FROM watchonly.config WHERE "user" = :user""",
{"user": user},
ConfigDb,
)
if not _config:
return await create_config(user)
return _config.json_data