Skip to content

Commit

Permalink
Handling accept_only_from in progress.
Browse files Browse the repository at this point in the history
  • Loading branch information
artemii235 committed Dec 13, 2023
1 parent 30da7a8 commit 56a45b0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
3 changes: 3 additions & 0 deletions mm2src/mm2_main/src/lp_network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ pub enum P2PProcessError {
DecodeError(String),
/// Message signature is invalid.
InvalidSignature(String),
/// Unexpected message sender.
#[display(fmt = "Unexpected message sender {}", _0)]
UnexpectedSender(String),
}

impl From<rmp_serde::encode::Error> for P2PRequestError {
Expand Down
21 changes: 15 additions & 6 deletions mm2src/mm2_main/src/lp_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ impl SwapMsgStore {
}

/// Storage for P2P messages, which are exchanged during SwapV2 protocol execution.
#[derive(Debug, Default)]
#[derive(Debug)]
pub struct SwapV2MsgStore {
maker_negotiation: Option<MakerNegotiation>,
taker_negotiation: Option<TakerNegotiation>,
Expand All @@ -209,16 +209,21 @@ pub struct SwapV2MsgStore {
maker_payment: Option<MakerPaymentInfo>,
taker_payment: Option<TakerPaymentInfo>,
taker_payment_spend_preimage: Option<TakerPaymentSpendPreimage>,
#[allow(dead_code)]
accept_only_from: bits256,
accept_only_from: PublicKey,
}

impl SwapV2MsgStore {
/// Creates new SwapV2MsgStore
pub fn new(accept_only_from: bits256) -> Self {
pub fn new(accept_only_from: PublicKey) -> Self {
SwapV2MsgStore {
maker_negotiation: None,
taker_negotiation: None,
maker_negotiated: None,
taker_funding: None,
maker_payment: None,
taker_payment: None,
taker_payment_spend_preimage: None,
accept_only_from,
..Default::default()
}
}
}
Expand Down Expand Up @@ -536,7 +541,7 @@ impl SwapsContext {
}

/// Initializes storage for the swap with specific uuid.
pub fn init_msg_v2_store(&self, uuid: Uuid, accept_only_from: bits256) {
pub fn init_msg_v2_store(&self, uuid: Uuid, accept_only_from: PublicKey) {
let store = SwapV2MsgStore::new(accept_only_from);
self.swap_v2_msgs.lock().unwrap().insert(uuid, store);
}
Expand Down Expand Up @@ -1722,6 +1727,10 @@ pub fn process_swap_v2_msg(ctx: MmArc, topic: &str, msg: &[u8]) -> P2PProcessRes

let pubkey =
PublicKey::from_slice(&signed_message.from).map_to_mm(|e| P2PProcessError::DecodeError(e.to_string()))?;
if pubkey != msg_store.accept_only_from {
return MmError::err(P2PProcessError::UnexpectedSender(pubkey.to_string()));
}

let signature = Signature::from_compact(&signed_message.signature)
.map_to_mm(|e| P2PProcessError::DecodeError(e.to_string()))?;
let secp_message = secp256k1::Message::from_slice(sha256(&signed_message.payload).as_slice())
Expand Down
12 changes: 10 additions & 2 deletions mm2src/mm2_main/src/lp_swap/swap_v2_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ use crate::mm2::lp_swap::swap_lock::{SwapLock, SwapLockError, SwapLockOps};
use crate::mm2::lp_swap::{swap_v2_topic, SwapsContext};
use coins::utxo::utxo_standard::UtxoStandardCoin;
use coins::{lp_coinfind, MmCoinEnum};
use common::bits256;
use common::executor::abortable_queue::AbortableQueue;
use common::executor::{SpawnFuture, Timer};
use common::log::{error, info, warn};
use mm2_core::mm_ctx::MmArc;
use mm2_err_handle::prelude::*;
use mm2_state_machine::storable_state_machine::{StateMachineDbRepr, StateMachineStorage, StorableStateMachine};
use rpc::v1::types::Bytes as BytesJson;
use secp256k1::PublicKey;
use serde::de::DeserializeOwned;
use serde::Serialize;
use serde_json::Error;
Expand Down Expand Up @@ -231,7 +231,15 @@ pub(super) async fn mark_swap_as_finished(ctx: MmArc, id: Uuid) -> MmResult<(),
pub(super) fn init_additional_context_impl(ctx: &MmArc, swap_info: ActiveSwapV2Info) {
subscribe_to_topic(ctx, swap_v2_topic(&swap_info.uuid));
let swap_ctx = SwapsContext::from_ctx(ctx).expect("SwapsContext::from_ctx should not fail");
swap_ctx.init_msg_v2_store(swap_info.uuid, bits256::default());
swap_ctx.init_msg_v2_store(
swap_info.uuid,
// just a "random" pubkey for now
PublicKey::from_slice(&[
3, 23, 183, 225, 206, 31, 159, 148, 195, 42, 67, 115, 146, 41, 248, 140, 11, 3, 51, 41, 111, 180, 110, 143,
114, 134, 88, 73, 198, 174, 52, 184, 78,
])
.unwrap(),
);
swap_ctx
.active_swaps_v2_infos
.lock()
Expand Down

0 comments on commit 56a45b0

Please sign in to comment.