From 6f66e9cba5cc34cfddba6e91eb9494270c0dc297 Mon Sep 17 00:00:00 2001 From: binarybaron <86064887+binarybaron@users.noreply.github.com> Date: Mon, 28 Aug 2023 00:47:32 +0200 Subject: [PATCH] Add `get_monero_recovery_info` RPC endpoint - Add `get_monero_recovery_info` RPC endpoint - format PrivateViewKey using Display --- swap/src/api/request.rs | 64 ++++++++++++++++------------------------- swap/src/monero.rs | 7 +++++ swap/src/rpc/methods.rs | 24 +++++++++++++++- 3 files changed, 54 insertions(+), 41 deletions(-) diff --git a/swap/src/api/request.rs b/swap/src/api/request.rs index 110e7b2a..c0e87474 100644 --- a/swap/src/api/request.rs +++ b/swap/src/api/request.rs @@ -173,12 +173,7 @@ impl Request { } } - // We pass the outer tracing span down to this function such that it can be passed down to other spawned tokio tasks - // This ensures that tasks like the event_loop are all part of the same tracing span - async fn handle_cmd( - self, - context: Arc, - ) -> Result { + async fn handle_cmd(self, context: Arc) -> Result { match self.cmd { Method::SuspendCurrentSwap => { let swap_id = context.swap_lock.get_current_swap_id().await; @@ -679,40 +674,28 @@ impl Request { Method::MoneroRecovery { swap_id } => { let swap_state: BobState = context.db.get_state(swap_id).await?.try_into()?; - match swap_state { - BobState::Started { .. } - | BobState::SwapSetupCompleted(_) - | BobState::BtcLocked { .. } - | BobState::XmrLockProofReceived { .. } - | BobState::XmrLocked(_) - | BobState::EncSigSent(_) - | BobState::CancelTimelockExpired(_) - | BobState::BtcCancelled(_) - | BobState::BtcRefunded(_) - | BobState::BtcPunished { .. } - | BobState::SafelyAborted - | BobState::XmrRedeemed { .. } => { - bail!("Cannot print monero recovery information in state {}, only possible for BtcRedeemed", swap_state) - } - BobState::BtcRedeemed(state5) => { - let (spend_key, view_key) = state5.xmr_keys(); + if let BobState::BtcRedeemed(state5) = swap_state { + let (spend_key, view_key) = state5.xmr_keys(); - let address = monero::Address::standard( - context.config.env_config.monero_network, - monero::PublicKey::from_private_key(&spend_key), - monero::PublicKey::from(view_key.public()), - ); - tracing::info!("Wallet address: {}", address.to_string()); + let address = monero::Address::standard( + context.config.env_config.monero_network, + monero::PublicKey::from_private_key(&spend_key), + monero::PublicKey::from(view_key.public()), + ); - let view_key = serde_json::to_string(&view_key)?; - println!("View key: {}", view_key); + tracing::info!(address=%address, spend_key=%spend_key, view_key=%view_key, "Monero recovery information"); - println!("Spend key: {}", spend_key); - } + return Ok(json!({ + "address": address, + "spend_key": spend_key.to_string(), + "view_key": view_key.to_string(), + })); + } else { + bail!( + "Cannot print monero recovery information in state {}, only possible for BtcRedeemed", + swap_state + ) } - Ok(json!({ - "result": [] - })) } Method::GetCurrentSwap => Ok(json!({ "swap_id": context.swap_lock.get_current_swap_id().await @@ -721,11 +704,12 @@ impl Request { } pub async fn call(self, context: Arc) -> Result { - let method_span = self.cmd.get_tracing_span(self.log_reference.clone()).clone(); + let method_span = self + .cmd + .get_tracing_span(self.log_reference.clone()) + .clone(); - self.handle_cmd(context) - .instrument(method_span) - .await + self.handle_cmd(context).instrument(method_span).await } } diff --git a/swap/src/monero.rs b/swap/src/monero.rs index f13b7ee0..1e1e5583 100644 --- a/swap/src/monero.rs +++ b/swap/src/monero.rs @@ -42,6 +42,13 @@ pub fn private_key_from_secp256k1_scalar(scalar: bitcoin::Scalar) -> PrivateKey #[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)] pub struct PrivateViewKey(#[serde(with = "monero_private_key")] PrivateKey); +impl fmt::Display for PrivateViewKey { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + // Delegate to the Display implementation of PrivateKey + write!(f, "{}", self.0) + } +} + impl PrivateViewKey { pub fn new_random(rng: &mut R) -> Self { let scalar = Scalar::random(rng); diff --git a/swap/src/rpc/methods.rs b/swap/src/rpc/methods.rs index 9144cfbb..7f51ac6e 100644 --- a/swap/src/rpc/methods.rs +++ b/swap/src/rpc/methods.rs @@ -85,6 +85,26 @@ pub fn register_modules(context: Arc) -> RpcModule> { }) .unwrap(); + module + .register_async_method( + "get_monero_recovery_info", + |params_raw, context| async move { + let params: HashMap = params_raw.parse()?; + + let swap_id = params.get("swap_id").ok_or_else(|| { + jsonrpsee_core::Error::Custom("Does not contain swap_id".to_string()) + })?; + + execute_request( + params_raw, + Method::MoneroRecovery { swap_id: *swap_id }, + &context, + ) + .await + }, + ) + .unwrap(); + module .register_async_method("withdraw_btc", |params_raw, context| async move { let params: HashMap = params_raw.parse()?; @@ -118,7 +138,8 @@ pub fn register_modules(context: Arc) -> RpcModule> { ) .await }) - .expect("Could not register RPC method withdraw_btc"); + .unwrap(); + module .register_async_method("buy_xmr", |params_raw, context| async move { let params: HashMap = params_raw.parse()?; @@ -165,6 +186,7 @@ pub fn register_modules(context: Arc) -> RpcModule> { .await }) .unwrap(); + module .register_async_method("list_sellers", |params_raw, context| async move { let params: HashMap = params_raw.parse()?;