Skip to content

Commit

Permalink
add /checkproxyendpoint API
Browse files Browse the repository at this point in the history
  • Loading branch information
zoedberg committed Oct 22, 2024
1 parent 86bbf6e commit 31baa61
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 23 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 24 additions & 0 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
12 changes: 3 additions & 9 deletions src/ldk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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::{
Expand Down Expand Up @@ -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 {
Expand Down
16 changes: 9 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -97,6 +98,7 @@ pub(crate) async fn app(args: LdkUserInfo) -> Result<(Router, Arc<AppState>), 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))
Expand Down
21 changes: 16 additions & 5 deletions src/rgb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down
19 changes: 17 additions & 2 deletions src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<Json<CheckProxyEndpointRequest>, APIError>,
) -> Result<Json<EmptyResponse>, APIError> {
check_rgb_proxy_endpoint(&payload.proxy_endpoint).await?;

Ok(Json(EmptyResponse {}))
}

pub(crate) async fn close_channel(
State(state): State<Arc<AppState>>,
WithRejection(Json(payload), _): WithRejection<Json<CloseChannelRequest>, APIError>,
Expand Down

0 comments on commit 31baa61

Please sign in to comment.