feat: extract utxos. from PSBT

This commit is contained in:
Vlad Stan 2022-12-02 13:40:59 +02:00
parent 223c980710
commit 3ad0463dfe
2 changed files with 25 additions and 7 deletions

View file

@ -76,9 +76,13 @@ class CreatePsbt(BaseModel):
tx_size: int tx_size: int
class SerializedTransaction(BaseModel):
tx_hex: str
class ExtractPsbt(BaseModel): class ExtractPsbt(BaseModel):
psbtBase64 = "" # // todo snake case psbtBase64 = "" # // todo snake case
inputs: List[TransactionInput] inputs: List[SerializedTransaction]
network = "Mainnet" network = "Mainnet"
@ -87,10 +91,6 @@ class SignedTransaction(BaseModel):
tx_json: Optional[str] tx_json: Optional[str]
class BroadcastTransaction(BaseModel):
tx_hex: str
class Config(BaseModel): class Config(BaseModel):
mempool_endpoint = "https://mempool.space" mempool_endpoint = "https://mempool.space"
receive_gap_limit = 20 receive_gap_limit = 20

View file

@ -31,11 +31,11 @@ from .crud import (
) )
from .helpers import parse_key from .helpers import parse_key
from .models import ( from .models import (
BroadcastTransaction,
Config, Config,
CreatePsbt, CreatePsbt,
CreateWallet, CreateWallet,
ExtractPsbt, ExtractPsbt,
SerializedTransaction,
SignedTransaction, SignedTransaction,
WalletAccount, WalletAccount,
) )
@ -291,6 +291,24 @@ async def api_psbt_create(
raise HTTPException(status_code=HTTPStatus.BAD_REQUEST, detail=str(e)) raise HTTPException(status_code=HTTPStatus.BAD_REQUEST, detail=str(e))
@watchonly_ext.put("/api/v1/psbt/utxos")
async def api_psbt_extract_tx(
req: Request, w: WalletTypeInfo = Depends(require_admin_key)
):
"""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 e:
raise HTTPException(status_code=HTTPStatus.BAD_REQUEST, detail=str(e))
@watchonly_ext.put("/api/v1/psbt/extract") @watchonly_ext.put("/api/v1/psbt/extract")
async def api_psbt_extract_tx( async def api_psbt_extract_tx(
data: ExtractPsbt, w: WalletTypeInfo = Depends(require_admin_key) data: ExtractPsbt, w: WalletTypeInfo = Depends(require_admin_key)
@ -327,7 +345,7 @@ async def api_psbt_extract_tx(
@watchonly_ext.post("/api/v1/tx") @watchonly_ext.post("/api/v1/tx")
async def api_tx_broadcast( async def api_tx_broadcast(
data: BroadcastTransaction, w: WalletTypeInfo = Depends(require_admin_key) data: SerializedTransaction, w: WalletTypeInfo = Depends(require_admin_key)
): ):
try: try:
config = await get_config(w.wallet.user) config = await get_config(w.wallet.user)