Add get_monero_recovery_info RPC endpoint

- Add `get_monero_recovery_info` RPC endpoint
- format PrivateViewKey using Display
This commit is contained in:
binarybaron 2023-08-28 00:47:32 +02:00 committed by binarybaron
parent d76f24c37f
commit 6f66e9cba5
3 changed files with 54 additions and 41 deletions

View file

@ -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 async fn handle_cmd(self, context: Arc<Context>) -> Result<serde_json::Value> {
// This ensures that tasks like the event_loop are all part of the same tracing span
async fn handle_cmd(
self,
context: Arc<Context>,
) -> Result<serde_json::Value> {
match self.cmd { match self.cmd {
Method::SuspendCurrentSwap => { Method::SuspendCurrentSwap => {
let swap_id = context.swap_lock.get_current_swap_id().await; let swap_id = context.swap_lock.get_current_swap_id().await;
@ -679,40 +674,28 @@ impl Request {
Method::MoneroRecovery { swap_id } => { Method::MoneroRecovery { swap_id } => {
let swap_state: BobState = context.db.get_state(swap_id).await?.try_into()?; let swap_state: BobState = context.db.get_state(swap_id).await?.try_into()?;
match swap_state { if let BobState::BtcRedeemed(state5) = swap_state {
BobState::Started { .. } let (spend_key, view_key) = state5.xmr_keys();
| 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();
let address = monero::Address::standard( let address = monero::Address::standard(
context.config.env_config.monero_network, context.config.env_config.monero_network,
monero::PublicKey::from_private_key(&spend_key), monero::PublicKey::from_private_key(&spend_key),
monero::PublicKey::from(view_key.public()), monero::PublicKey::from(view_key.public()),
); );
tracing::info!("Wallet address: {}", address.to_string());
let view_key = serde_json::to_string(&view_key)?; tracing::info!(address=%address, spend_key=%spend_key, view_key=%view_key, "Monero recovery information");
println!("View key: {}", view_key);
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!({ Method::GetCurrentSwap => Ok(json!({
"swap_id": context.swap_lock.get_current_swap_id().await "swap_id": context.swap_lock.get_current_swap_id().await
@ -721,11 +704,12 @@ impl Request {
} }
pub async fn call(self, context: Arc<Context>) -> Result<serde_json::Value> { pub async fn call(self, context: Arc<Context>) -> Result<serde_json::Value> {
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) self.handle_cmd(context).instrument(method_span).await
.instrument(method_span)
.await
} }
} }

View file

@ -42,6 +42,13 @@ pub fn private_key_from_secp256k1_scalar(scalar: bitcoin::Scalar) -> PrivateKey
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)] #[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct PrivateViewKey(#[serde(with = "monero_private_key")] PrivateKey); 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 { impl PrivateViewKey {
pub fn new_random<R: RngCore + CryptoRng>(rng: &mut R) -> Self { pub fn new_random<R: RngCore + CryptoRng>(rng: &mut R) -> Self {
let scalar = Scalar::random(rng); let scalar = Scalar::random(rng);

View file

@ -85,6 +85,26 @@ pub fn register_modules(context: Arc<Context>) -> RpcModule<Arc<Context>> {
}) })
.unwrap(); .unwrap();
module
.register_async_method(
"get_monero_recovery_info",
|params_raw, context| async move {
let params: HashMap<String, Uuid> = 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 module
.register_async_method("withdraw_btc", |params_raw, context| async move { .register_async_method("withdraw_btc", |params_raw, context| async move {
let params: HashMap<String, String> = params_raw.parse()?; let params: HashMap<String, String> = params_raw.parse()?;
@ -118,7 +138,8 @@ pub fn register_modules(context: Arc<Context>) -> RpcModule<Arc<Context>> {
) )
.await .await
}) })
.expect("Could not register RPC method withdraw_btc"); .unwrap();
module module
.register_async_method("buy_xmr", |params_raw, context| async move { .register_async_method("buy_xmr", |params_raw, context| async move {
let params: HashMap<String, String> = params_raw.parse()?; let params: HashMap<String, String> = params_raw.parse()?;
@ -165,6 +186,7 @@ pub fn register_modules(context: Arc<Context>) -> RpcModule<Arc<Context>> {
.await .await
}) })
.unwrap(); .unwrap();
module module
.register_async_method("list_sellers", |params_raw, context| async move { .register_async_method("list_sellers", |params_raw, context| async move {
let params: HashMap<String, Multiaddr> = params_raw.parse()?; let params: HashMap<String, Multiaddr> = params_raw.parse()?;