From 31baa614f818405b4cd97d7c2758ae49fb297e0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zoe=20Faltib=C3=A0?= Date: Tue, 22 Oct 2024 14:20:10 +0200 Subject: [PATCH] add /checkproxyendpoint API --- README.md | 1 + openapi.yaml | 24 ++++++++++++++++++++++++ src/ldk.rs | 12 +++--------- src/main.rs | 16 +++++++++------- src/rgb.rs | 21 ++++++++++++++++----- src/routes.rs | 19 +++++++++++++++++-- 6 files changed, 70 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 4c10fff..4446f79 100644 --- a/README.md +++ b/README.md @@ -160,6 +160,7 @@ The node currently exposes the following APIs: - `/btcbalance` (POST) - `/changepassword` (POST) - `/checkindexerurl` (POST) +- `/checkproxyendpoint` (POST) - `/closechannel` (POST) - `/connectpeer` (POST) - `/createutxos` (POST) diff --git a/openapi.yaml b/openapi.yaml index f96b0a3..974139e 100644 --- a/openapi.yaml +++ b/openapi.yaml @@ -151,6 +151,24 @@ paths: application/json: schema: $ref: '#/components/schemas/CheckIndexerUrlResponse' + /checkproxyendpoint: + post: + tags: + - Other + summary: Check a proxy endpoint + description: Check the given proxy endpoint is valid + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/CheckProxyEndpointRequest' + responses: + '200': + description: Successful operation + content: + application/json: + schema: + $ref: '#/components/schemas/EmptyResponse' /closechannel: post: tags: @@ -1198,6 +1216,12 @@ components: properties: indexer_protocol: $ref: '#/components/schemas/IndexerProtocol' + CheckProxyEndpointRequest: + type: object + properties: + proxy_url: + type: string + example: rpc://127.0.0.1:3000/json-rpc CloseChannelRequest: type: object properties: diff --git a/src/ldk.rs b/src/ldk.rs index aeb385f..15b2783 100644 --- a/src/ldk.rs +++ b/src/ldk.rs @@ -54,12 +54,11 @@ use rgb_lib::{ }, utils::{get_account_xpub, recipient_id_from_script_buf, script_buf_from_recipient_id}, wallet::{ - rust_only::{check_indexer_url, check_proxy_url, AssetColoringInfo, ColoringInfo}, + rust_only::{check_indexer_url, AssetColoringInfo, ColoringInfo}, AssetIface, DatabaseType, Outpoint, Recipient, TransportEndpoint, Wallet as RgbLibWallet, WalletData, WitnessData, }, AssetSchema, BitcoinNetwork, ConsignmentExt, ContractId, FileContent, RgbTransfer, - RgbTransport, }; use std::collections::hash_map::Entry; use std::collections::HashMap; @@ -84,7 +83,7 @@ use crate::disk::{ MAKER_SWAPS_FNAME, OUTBOUND_PAYMENTS_FNAME, OUTPUT_SPENDER_TXES, TAKER_SWAPS_FNAME, }; use crate::error::APIError; -use crate::rgb::{get_rgb_channel_info_optional, RgbLibWalletWrapper}; +use crate::rgb::{check_rgb_proxy_endpoint, get_rgb_channel_info_optional, RgbLibWalletWrapper}; use crate::routes::{HTLCStatus, SwapStatus, UnlockRequest, DUST_LIMIT_MSAT}; use crate::swap::SwapData; use crate::utils::{ @@ -1429,12 +1428,7 @@ pub(crate) async fn start_ldk( } }; let proxy_endpoint = if let Some(proxy_endpoint) = &unlock_request.proxy_endpoint { - let rgb_transport = - RgbTransport::from_str(proxy_endpoint).map_err(|_| APIError::InvalidProxyEndpoint)?; - let proxy_url = TransportEndpoint::try_from(rgb_transport)?.endpoint; - tokio::task::spawn_blocking(move || check_proxy_url(&proxy_url)) - .await - .unwrap()?; + check_rgb_proxy_endpoint(proxy_endpoint).await?; tracing::info!("Using a custom proxy"); proxy_endpoint } else { diff --git a/src/main.rs b/src/main.rs index 02255a6..c823139 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,13 +30,14 @@ use crate::error::AppError; use crate::ldk::stop_ldk; use crate::routes::{ address, asset_balance, asset_metadata, backup, btc_balance, change_password, - check_indexer_url, close_channel, connect_peer, create_utxos, decode_ln_invoice, - decode_rgb_invoice, disconnect_peer, estimate_fee, fail_transfers, get_asset_media, - get_channel_id, init, invoice_status, issue_asset_cfa, issue_asset_nia, issue_asset_uda, - keysend, list_assets, list_channels, list_payments, list_peers, list_swaps, list_transactions, - list_transfers, list_unspents, ln_invoice, lock, maker_execute, maker_init, network_info, - node_info, open_channel, post_asset_media, refresh_transfers, restore, rgb_invoice, send_asset, - send_btc, send_onion_message, send_payment, shutdown, sign_message, sync, taker, unlock, + check_indexer_url, check_proxy_endpoint, close_channel, connect_peer, create_utxos, + decode_ln_invoice, decode_rgb_invoice, disconnect_peer, estimate_fee, fail_transfers, + get_asset_media, get_channel_id, init, invoice_status, issue_asset_cfa, issue_asset_nia, + issue_asset_uda, keysend, list_assets, list_channels, list_payments, list_peers, list_swaps, + list_transactions, list_transfers, list_unspents, ln_invoice, lock, maker_execute, maker_init, + network_info, node_info, open_channel, post_asset_media, refresh_transfers, restore, + rgb_invoice, send_asset, send_btc, send_onion_message, send_payment, shutdown, sign_message, + sync, taker, unlock, }; use crate::utils::{start_daemon, AppState, LOGS_DIR}; @@ -97,6 +98,7 @@ pub(crate) async fn app(args: LdkUserInfo) -> Result<(Router, Arc), Ap .route("/btcbalance", post(btc_balance)) .route("/changepassword", post(change_password)) .route("/checkindexerurl", post(check_indexer_url)) + .route("/checkproxyendpoint", post(check_proxy_endpoint)) .route("/closechannel", post(close_channel)) .route("/connectpeer", post(connect_peer)) .route("/createutxos", post(create_utxos)) diff --git a/src/rgb.rs b/src/rgb.rs index e0c28e5..67f776e 100644 --- a/src/rgb.rs +++ b/src/rgb.rs @@ -16,19 +16,20 @@ use rgb_lib::{ bdk::SignOptions, bitcoin::psbt::PartiallySignedTransaction as BitcoinPsbt, wallet::{ - rust_only::ColoringInfo, AssetCFA, AssetNIA, AssetUDA, Assets, Balance, BtcBalance, - Metadata, Online, ReceiveData, Recipient, RefreshResult, SendResult, - Transaction as RgbLibTransaction, Transfer, Unspent, WalletData, + rust_only::{check_proxy_url, ColoringInfo}, + AssetCFA, AssetNIA, AssetUDA, Assets, Balance, BtcBalance, Metadata, Online, ReceiveData, + Recipient, RefreshResult, SendResult, Transaction as RgbLibTransaction, Transfer, + TransportEndpoint, Unspent, WalletData, }, AssetSchema, BitcoinNetwork, Contract, ContractId, Error as RgbLibError, RgbTransfer, - UpdateRes, Wallet as RgbLibWallet, + RgbTransport, UpdateRes, Wallet as RgbLibWallet, }; use std::collections::HashMap; use std::path::{Path, PathBuf}; use std::str::FromStr; use std::sync::{Arc, Mutex, MutexGuard}; -use crate::utils::UnlockedAppState; +use crate::{error::APIError, utils::UnlockedAppState}; impl UnlockedAppState { pub(crate) fn rgb_blind_receive( @@ -693,6 +694,16 @@ impl WalletSource for RgbLibWalletWrapper { } } +pub(crate) async fn check_rgb_proxy_endpoint(proxy_endpoint: &str) -> Result<(), APIError> { + let rgb_transport = + RgbTransport::from_str(proxy_endpoint).map_err(|_| APIError::InvalidProxyEndpoint)?; + let proxy_url = TransportEndpoint::try_from(rgb_transport)?.endpoint; + tokio::task::spawn_blocking(move || check_proxy_url(&proxy_url)) + .await + .unwrap()?; + Ok(()) +} + pub(crate) fn get_rgb_channel_info_optional( channel_id: &ChannelId, ldk_data_dir: &Path, diff --git a/src/routes.rs b/src/routes.rs index d5a99c8..ff84617 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -71,15 +71,17 @@ use tokio::{ sync::MutexGuard as TokioMutexGuard, }; -use crate::backup::{do_backup, restore_backup}; use crate::ldk::{start_ldk, stop_ldk, LdkBackgroundServices, MIN_CHANNEL_CONFIRMATIONS}; -use crate::rgb::get_rgb_channel_info_optional; use crate::swap::{SwapData, SwapInfo, SwapString}; use crate::utils::{ check_already_initialized, check_channel_id, check_password_strength, check_password_validity, encrypt_and_save_mnemonic, get_max_local_rgb_amount, get_mnemonic_path, get_route, hex_str, hex_str_to_compressed_pubkey, hex_str_to_vec, UnlockedAppState, UserOnionMessageContents, }; +use crate::{ + backup::{do_backup, restore_backup}, + rgb::{check_rgb_proxy_endpoint, get_rgb_channel_info_optional}, +}; use crate::{ disk::{self, CHANNEL_PEER_DATA}, error::APIError, @@ -391,6 +393,11 @@ pub(crate) struct CheckIndexerUrlResponse { pub(crate) indexer_protocol: IndexerProtocol, } +#[derive(Debug, Deserialize, Serialize)] +pub(crate) struct CheckProxyEndpointRequest { + pub(crate) proxy_endpoint: String, +} + #[derive(Deserialize, Serialize)] pub(crate) struct CloseChannelRequest { pub(crate) channel_id: String, @@ -1342,6 +1349,14 @@ pub(crate) async fn check_indexer_url( Ok(Json(CheckIndexerUrlResponse { indexer_protocol })) } +pub(crate) async fn check_proxy_endpoint( + WithRejection(Json(payload), _): WithRejection, APIError>, +) -> Result, APIError> { + check_rgb_proxy_endpoint(&payload.proxy_endpoint).await?; + + Ok(Json(EmptyResponse {})) +} + pub(crate) async fn close_channel( State(state): State>, WithRejection(Json(payload), _): WithRejection, APIError>,