-
Notifications
You must be signed in to change notification settings - Fork 2
/
views_api.py
415 lines (344 loc) · 13.1 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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
import json
from http import HTTPStatus
import httpx
from embit import finalizer, script
from embit.ec import PublicKey
from embit.networks import NETWORKS
from embit.psbt import PSBT, DerivationPath
from embit.transaction import Transaction, TransactionInput, TransactionOutput
from fastapi import APIRouter, Depends, HTTPException, Query, Request
from lnbits.core.models import WalletTypeInfo
from lnbits.decorators import require_admin_key, require_invoice_key
from lnbits.helpers import urlsafe_short_hash
from .crud import (
create_fresh_addresses,
create_watch_wallet,
delete_addresses_for_wallet,
delete_watch_wallet,
get_address_by_id,
get_addresses,
get_config,
get_fresh_address,
get_watch_wallet,
get_watch_wallets,
update_address,
update_config,
update_watch_wallet,
)
from .helpers import parse_key
from .models import (
Address,
Config,
CreatePsbt,
CreateWallet,
ExtractPsbt,
ExtractTx,
SerializedTransaction,
SignedTransaction,
WalletAccount,
)
watchonly_api_router = APIRouter()
@watchonly_api_router.get("/api/v1/wallet")
async def api_wallets_retrieve(
network: str = Query("Mainnet"),
key_info: WalletTypeInfo = Depends(require_invoice_key),
) -> list[WalletAccount]:
return await get_watch_wallets(key_info.wallet.user, network)
@watchonly_api_router.get(
"/api/v1/wallet/{wallet_id}", dependencies=[Depends(require_invoice_key)]
)
async def api_wallet_retrieve(wallet_id: str) -> WalletAccount:
watch_wallet = await get_watch_wallet(wallet_id)
if not watch_wallet:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Wallet does not exist."
)
return watch_wallet
@watchonly_api_router.post("/api/v1/wallet")
async def api_wallet_create_or_update(
data: CreateWallet, key_info: WalletTypeInfo = Depends(require_admin_key)
) -> WalletAccount:
try:
descriptor, network = parse_key(data.masterpub)
assert network
if data.network != network["name"]:
raise ValueError(
"Account network error. This account is for '{}'".format(
network["name"]
)
)
new_wallet = WalletAccount(
id=urlsafe_short_hash(),
user=key_info.wallet.user,
masterpub=data.masterpub,
fingerprint=descriptor.keys[0].fingerprint.hex(),
type=descriptor.scriptpubkey_type(),
title=data.title,
address_no=-1, # fresh address on empty wallet can get address with index 0
balance=0,
network=network["name"],
meta=data.meta,
)
wallets = await get_watch_wallets(key_info.wallet.user, network["name"])
existing_wallet = next(
(
ew
for ew in wallets
if ew.fingerprint == new_wallet.fingerprint
and ew.network == new_wallet.network
and ew.masterpub == new_wallet.masterpub
),
None,
)
if existing_wallet:
raise ValueError(
f"Account '{existing_wallet.title}' has the same master pulic key"
)
wallet = await create_watch_wallet(new_wallet)
await api_get_addresses(wallet.id, key_info)
except Exception as exc:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST, detail=str(exc)
) from exc
return wallet
@watchonly_api_router.delete(
"/api/v1/wallet/{wallet_id}", dependencies=[Depends(require_admin_key)]
)
async def api_wallet_delete(wallet_id: str):
wallet = await get_watch_wallet(wallet_id)
if not wallet:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Wallet does not exist."
)
await delete_watch_wallet(wallet_id)
await delete_addresses_for_wallet(wallet_id)
return "", HTTPStatus.NO_CONTENT
#############################ADDRESSES##########################
@watchonly_api_router.get(
"/api/v1/address/{wallet_id}", dependencies=[Depends(require_invoice_key)]
)
async def api_fresh_address(wallet_id: str) -> Address:
address = await get_fresh_address(wallet_id)
assert address
return address
@watchonly_api_router.put(
"/api/v1/address/{address_id}", dependencies=[Depends(require_admin_key)]
)
async def api_update_address(address_id: str, req: Request):
address = await get_address_by_id(address_id)
if not address:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Address does not exist."
)
body = await req.json()
# amount is only updated if the address has history
if "amount" in body:
address.amount = int(body["amount"])
address.has_activity = True
if "note" in body:
address.note = body["note"]
address = await update_address(address)
wallet = (
await get_watch_wallet(address.wallet)
if address.branch_index == 0 and address.amount != 0
else None
)
if wallet and wallet.address_no < address.address_index:
wallet.address_no = address.address_index
await update_watch_wallet(wallet)
return address
@watchonly_api_router.get("/api/v1/addresses/{wallet_id}")
async def api_get_addresses(
wallet_id, key_info: WalletTypeInfo = Depends(require_invoice_key)
) -> list[Address]:
wallet = await get_watch_wallet(wallet_id)
if not wallet:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Wallet does not exist."
)
addresses = await get_addresses(wallet_id)
config = await get_config(key_info.wallet.user)
assert config, "Config not found"
if not addresses:
await create_fresh_addresses(wallet_id, 0, config.receive_gap_limit)
await create_fresh_addresses(wallet_id, 0, config.change_gap_limit, True)
addresses = await get_addresses(wallet_id)
receive_addresses = list(filter(lambda addr: addr.branch_index == 0, addresses))
change_addresses = list(filter(lambda addr: addr.branch_index == 1, addresses))
last_receive_address = list(
filter(lambda addr: addr.has_activity, receive_addresses)
)[-1:]
last_change_address = list(
filter(lambda addr: addr.has_activity, change_addresses)
)[-1:]
if last_receive_address:
current_index = receive_addresses[-1].address_index
address_index = last_receive_address[0].address_index
await create_fresh_addresses(
wallet_id, current_index + 1, address_index + config.receive_gap_limit + 1
)
if last_change_address:
current_index = change_addresses[-1].address_index
address_index = last_change_address[0].address_index
await create_fresh_addresses(
wallet_id,
current_index + 1,
address_index + config.change_gap_limit + 1,
True,
)
return await get_addresses(wallet_id)
@watchonly_api_router.post("/api/v1/psbt", dependencies=[Depends(require_admin_key)])
async def api_psbt_create(data: CreatePsbt):
try:
vin = [
TransactionInput(bytes.fromhex(inp.tx_id), inp.vout) for inp in data.inputs
]
vout = [
TransactionOutput(out.amount, script.address_to_scriptpubkey(out.address))
for out in data.outputs
]
descriptors = {}
for _, masterpub in enumerate(data.masterpubs):
descriptors[masterpub.id] = parse_key(masterpub.public_key)
inputs_extra: list[dict] = []
for inp in data.inputs:
bip32_derivations = {}
descriptor = descriptors[inp.wallet][0]
d = descriptor.derive(inp.address_index, inp.branch_index)
for k in d.keys:
bip32_derivations[PublicKey.parse(k.sec())] = DerivationPath(
k.origin.fingerprint, k.origin.derivation
)
inputs_extra.append(
{
"bip32_derivations": bip32_derivations,
"non_witness_utxo": Transaction.from_string(inp.tx_hex),
}
)
tx = Transaction(vin=vin, vout=vout)
psbt = PSBT(tx)
for i, inp_extra in enumerate(inputs_extra):
psbt.inputs[i].bip32_derivations = inp_extra["bip32_derivations"]
psbt.inputs[i].non_witness_utxo = inp_extra.get("non_witness_utxo", None)
outputs_extra = []
bip32_derivations = {}
for out in data.outputs:
if out.branch_index == 1:
assert out.wallet
descriptor = descriptors[out.wallet][0]
d = descriptor.derive(out.address_index, out.branch_index)
for k in d.keys:
bip32_derivations[PublicKey.parse(k.sec())] = DerivationPath(
k.origin.fingerprint, k.origin.derivation
)
outputs_extra.append({"bip32_derivations": bip32_derivations})
for i, out_extra in enumerate(outputs_extra):
psbt.outputs[i].bip32_derivations = out_extra["bip32_derivations"]
return psbt.to_string()
except Exception as exc:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST, detail=str(exc)
) from exc
@watchonly_api_router.put(
"/api/v1/psbt/utxos", dependencies=[Depends(require_admin_key)]
)
async def api_psbt_utxos_tx(req: Request):
"""Extract previous unspent transaction outputs (tx_id, vout) from PSBT"""
body = await req.json()
try:
psbt = PSBT.from_base64(body["psbtBase64"])
res = []
for _, inp in enumerate(psbt.inputs):
res.append({"tx_id": inp.txid.hex(), "vout": inp.vout})
return res
except Exception as exc:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST, detail=str(exc)
) from exc
@watchonly_api_router.put(
"/api/v1/psbt/extract", dependencies=[Depends(require_admin_key)]
)
async def api_psbt_extract_tx(data: ExtractPsbt) -> SignedTransaction:
network = NETWORKS["main"] if data.network == "Mainnet" else NETWORKS["test"]
try:
psbt = PSBT.from_base64(data.psbt_base64)
for i, inp in enumerate(data.inputs):
psbt.inputs[i].non_witness_utxo = Transaction.from_string(inp.tx_hex)
final_psbt = finalizer.finalize_psbt(psbt)
if not final_psbt:
raise ValueError("PSBT cannot be finalized!")
tx_hex = final_psbt.to_string()
transaction = Transaction.from_string(tx_hex)
tx = {
"locktime": transaction.locktime,
"version": transaction.version,
"outputs": [],
"fee": psbt.fee(),
}
for out in transaction.vout:
tx["outputs"].append(
{"amount": out.value, "address": out.script_pubkey.address(network)}
)
signed_tx = SignedTransaction(tx_hex=tx_hex, tx_json=json.dumps(tx))
return signed_tx
except Exception as exc:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST, detail=str(exc)
) from exc
@watchonly_api_router.put(
"/api/v1/tx/extract", dependencies=[Depends(require_admin_key)]
)
async def api_extract_tx(data: ExtractTx):
network = NETWORKS["main"] if data.network == "Mainnet" else NETWORKS["test"]
try:
transaction = Transaction.from_string(data.tx_hex)
tx = {
"locktime": transaction.locktime,
"version": transaction.version,
"outputs": [],
}
for out in transaction.vout:
tx["outputs"].append(
{"amount": out.value, "address": out.script_pubkey.address(network)}
)
return {"tx_json": tx}
except Exception as exc:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST, detail=str(exc)
) from exc
@watchonly_api_router.post("/api/v1/tx")
async def api_tx_broadcast(
data: SerializedTransaction, key_info: WalletTypeInfo = Depends(require_admin_key)
):
try:
config = await get_config(key_info.wallet.user)
if not config:
raise ValueError(
"Cannot broadcast transaction. Mempool endpoint not defined!"
)
endpoint = (
config.mempool_endpoint
if config.network == "Mainnet"
else config.mempool_endpoint + "/testnet"
)
async with httpx.AsyncClient() as client:
r = await client.post(endpoint + "/api/tx", content=data.tx_hex)
r.raise_for_status()
tx_id = r.text
return tx_id
except Exception as exc:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST, detail=str(exc)
) from exc
@watchonly_api_router.put("/api/v1/config")
async def api_update_config(
data: Config, key_info: WalletTypeInfo = Depends(require_admin_key)
) -> Config:
config = await update_config(data, user=key_info.wallet.user)
return config
@watchonly_api_router.get("/api/v1/config")
async def api_get_config(
key_info: WalletTypeInfo = Depends(require_invoice_key),
) -> Config:
config = await get_config(key_info.wallet.user)
return config