Skip to content

Commit

Permalink
check proxy URL during /unlock
Browse files Browse the repository at this point in the history
  • Loading branch information
zoedberg committed Oct 22, 2024
1 parent 41018a0 commit 86bbf6e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 14 deletions.
15 changes: 15 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ pub enum APIError {
#[error("Invalid precision: {0}")]
InvalidPrecision(String),

/// The provided proxy endpoint is invalid
#[error("Invalid proxy endpoint")]
InvalidProxyEndpoint,

/// The provided proxy is running an unsupported protocol version
#[error("Invalid proxy protocol version: {0}")]
InvalidProxyProtocol(String),

#[error("Invalid pubkey")]
InvalidPubkey,

Expand Down Expand Up @@ -202,6 +210,10 @@ pub enum APIError {
#[error("Output below the dust limit")]
OutputBelowDustLimit,

/// Error contacting the RGB proxy
#[error("Proxy error: {0}")]
Proxy(String),

#[error("Recipient ID already used")]
RecipientIDAlreadyUsed,

Expand Down Expand Up @@ -295,13 +307,16 @@ impl IntoResponse for APIError {
| APIError::InsufficientAssets
| APIError::InsufficientFunds(_)
| APIError::InvalidIndexer(_)
| APIError::InvalidProxyEndpoint
| APIError::InvalidProxyProtocol(_)
| APIError::LockedNode
| APIError::MinFeeNotMet(_)
| APIError::NetworkMismatch(_, _)
| APIError::NoAvailableUtxos
| APIError::NoRoute
| APIError::NotInitialized
| APIError::OpenChannelInProgress
| APIError::Proxy(_)
| APIError::RecipientIDAlreadyUsed
| APIError::TemporaryChannelIdAlreadyUsed
| APIError::UnknownContractId
Expand Down
36 changes: 25 additions & 11 deletions src/ldk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@ use rgb_lib::{
},
utils::{get_account_xpub, recipient_id_from_script_buf, script_buf_from_recipient_id},
wallet::{
rust_only::{check_indexer_url, AssetColoringInfo, ColoringInfo},
rust_only::{check_indexer_url, check_proxy_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 Down Expand Up @@ -1413,24 +1414,37 @@ pub(crate) async fn start_ldk(

// RGB setup
let indexer_url = if let Some(indexer_url) = &unlock_request.indexer_url {
check_indexer_url(indexer_url, bitcoin_network)?;
let indexer_protocol = check_indexer_url(indexer_url, bitcoin_network)?;
tracing::info!(
"Connected to an indexer with the {} protocol",
indexer_protocol
);
indexer_url
} else {
tracing::info!("Using the default indexer");
match bitcoin_network {
BitcoinNetwork::Testnet => ELECTRUM_URL_TESTNET,
BitcoinNetwork::Regtest => ELECTRUM_URL_REGTEST,
_ => unimplemented!("unsupported network"),
}
};
let proxy_endpoint =
unlock_request
.proxy_endpoint
.as_deref()
.unwrap_or_else(|| match bitcoin_network {
BitcoinNetwork::Testnet => PROXY_ENDPOINT_TESTNET,
BitcoinNetwork::Regtest => PROXY_ENDPOINT_REGTEST,
_ => unimplemented!("unsupported network"),
});
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()?;
tracing::info!("Using a custom proxy");
proxy_endpoint
} else {
tracing::info!("Using the default proxy");
match bitcoin_network {
BitcoinNetwork::Testnet => PROXY_ENDPOINT_TESTNET,
BitcoinNetwork::Regtest => PROXY_ENDPOINT_REGTEST,
_ => unimplemented!("unsupported network"),
}
};
let storage_dir_path = app_state.static_state.storage_dir_path.clone();
fs::write(storage_dir_path.join(INDEXER_URL_FNAME), indexer_url).expect("able to write");
fs::write(
Expand Down
4 changes: 4 additions & 0 deletions src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1169,13 +1169,17 @@ impl From<RgbLibError> for APIError {
RgbLibError::InvalidIndexer { details } => APIError::InvalidIndexer(details),
RgbLibError::InvalidName { details } => APIError::InvalidName(details),
RgbLibError::InvalidPrecision { details } => APIError::InvalidPrecision(details),
RgbLibError::InvalidProxyProtocol { version } => {
APIError::InvalidProxyProtocol(version)
}
RgbLibError::InvalidRecipientID => APIError::InvalidRecipientID,
RgbLibError::InvalidRecipientNetwork => APIError::InvalidRecipientNetwork,
RgbLibError::InvalidTicker { details } => APIError::InvalidTicker(details),
RgbLibError::InvalidTransportEndpoints { details } => {
APIError::InvalidTransportEndpoints(details)
}
RgbLibError::MinFeeNotMet { txid } => APIError::MinFeeNotMet(txid),
RgbLibError::Proxy { details } => APIError::Proxy(details),
RgbLibError::RecipientIDAlreadyUsed => APIError::RecipientIDAlreadyUsed,
RgbLibError::OutputBelowDustLimit => APIError::OutputBelowDustLimit,
_ => {
Expand Down
6 changes: 3 additions & 3 deletions src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use crate::routes::{
SendBtcResponse, SendPaymentRequest, SendPaymentResponse, SwapStatus, TakerRequest,
Transaction, Transfer, UnlockRequest, Unspent,
};
use crate::utils::{hex_str_to_vec, PROXY_ENDPOINT_REGTEST};
use crate::utils::{hex_str_to_vec, ELECTRUM_URL_REGTEST, PROXY_ENDPOINT_REGTEST};

use super::*;

Expand Down Expand Up @@ -1341,8 +1341,8 @@ async fn unlock(node_address: SocketAddr, password: &str) {
bitcoind_rpc_password: s!("password"),
bitcoind_rpc_host: s!("localhost"),
bitcoind_rpc_port: 18443,
indexer_url: None,
proxy_endpoint: None,
indexer_url: Some(ELECTRUM_URL_REGTEST.to_string()),
proxy_endpoint: Some(PROXY_ENDPOINT_REGTEST.to_string()),
};
let res = reqwest::Client::new()
.post(format!("http://{}/unlock", node_address))
Expand Down

0 comments on commit 86bbf6e

Please sign in to comment.