From 466d4f25debbcd0c4a05a27bb0ec9a56ca38c71e Mon Sep 17 00:00:00 2001 From: shamardy <39480341+shamardy@users.noreply.github.com> Date: Fri, 15 Dec 2023 17:44:00 +0200 Subject: [PATCH] fix(p2p): handle encode_and_sign errors (#2038) --- mm2src/mm2_main/src/lp_ordermatch.rs | 24 +++++++++++++++++++++--- mm2src/mm2_main/src/lp_swap.rs | 16 ++++++++++++++-- mm2src/mm2_p2p/src/lib.rs | 6 ++++-- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/mm2src/mm2_main/src/lp_ordermatch.rs b/mm2src/mm2_main/src/lp_ordermatch.rs index 92055cf43d..688f9ffbb2 100644 --- a/mm2src/mm2_main/src/lp_ordermatch.rs +++ b/mm2src/mm2_main/src/lp_ordermatch.rs @@ -1049,7 +1049,13 @@ fn maker_order_created_p2p_notify( let to_broadcast = new_protocol::OrdermatchMessage::MakerOrderCreated(message.clone()); let (key_pair, peer_id) = p2p_keypair_and_peer_id_to_broadcast(&ctx, order.p2p_keypair()); - let encoded_msg = encode_and_sign(&to_broadcast, key_pair.private_ref()).unwrap(); + let encoded_msg = match encode_and_sign(&to_broadcast, key_pair.private_ref()) { + Ok(msg) => msg, + Err(e) => { + error!("Couldn't encode and sign the 'maker_order_created' message: {}", e); + return; + }, + }; let item: OrderbookItem = (message, hex::encode(key_pair.public_slice())).into(); insert_or_update_my_order(&ctx, item, order); broadcast_p2p_msg(&ctx, topic, encoded_msg, peer_id); @@ -1074,7 +1080,13 @@ fn maker_order_updated_p2p_notify( ) { let msg: new_protocol::OrdermatchMessage = message.clone().into(); let (secret, peer_id) = p2p_private_and_peer_id_to_broadcast(&ctx, p2p_privkey); - let encoded_msg = encode_and_sign(&msg, &secret).unwrap(); + let encoded_msg = match encode_and_sign(&msg, &secret) { + Ok(msg) => msg, + Err(e) => { + error!("Couldn't encode and sign the 'maker_order_updated' message: {}", e); + return; + }, + }; process_my_maker_order_updated(&ctx, &message); broadcast_p2p_msg(&ctx, topic, encoded_msg, peer_id); } @@ -2325,7 +2337,13 @@ fn broadcast_ordermatch_message( p2p_privkey: Option<&KeyPair>, ) { let (secret, peer_id) = p2p_private_and_peer_id_to_broadcast(ctx, p2p_privkey); - let encoded_msg = encode_and_sign(&msg, &secret).unwrap(); + let encoded_msg = match encode_and_sign(&msg, &secret) { + Ok(encoded_msg) => encoded_msg, + Err(e) => { + error!("Failed to encode and sign ordermatch message: {}", e); + return; + }, + }; broadcast_p2p_msg(ctx, topic, encoded_msg, peer_id); } diff --git a/mm2src/mm2_main/src/lp_swap.rs b/mm2src/mm2_main/src/lp_swap.rs index 8d207542fc..96fd72805f 100644 --- a/mm2src/mm2_main/src/lp_swap.rs +++ b/mm2src/mm2_main/src/lp_swap.rs @@ -290,7 +290,13 @@ pub fn broadcast_swap_msg_every_delayed( /// Broadcast the swap message once pub fn broadcast_swap_message(ctx: &MmArc, topic: String, msg: T, p2p_privkey: &Option) { let (p2p_private, from) = p2p_private_and_peer_id_to_broadcast(ctx, p2p_privkey.as_ref()); - let encoded_msg = encode_and_sign(&msg, &p2p_private).unwrap(); + let encoded_msg = match encode_and_sign(&msg, &p2p_private) { + Ok(m) => m, + Err(e) => { + error!("Error encoding and signing swap message: {}", e); + return; + }, + }; broadcast_p2p_msg(ctx, topic, encoded_msg, from); } @@ -301,7 +307,13 @@ pub fn broadcast_p2p_tx_msg(ctx: &MmArc, topic: String, msg: &TransactionEnum, p } let (p2p_private, from) = p2p_private_and_peer_id_to_broadcast(ctx, p2p_privkey.as_ref()); - let encoded_msg = encode_and_sign(&msg.tx_hex(), &p2p_private).unwrap(); + let encoded_msg = match encode_and_sign(&msg.tx_hex(), &p2p_private) { + Ok(m) => m, + Err(e) => { + error!("Error encoding and signing tx message: {}", e); + return; + }, + }; broadcast_p2p_msg(ctx, topic, encoded_msg, from); } diff --git a/mm2src/mm2_p2p/src/lib.rs b/mm2src/mm2_p2p/src/lib.rs index 953e711423..6163e757a1 100644 --- a/mm2src/mm2_p2p/src/lib.rs +++ b/mm2src/mm2_p2p/src/lib.rs @@ -78,9 +78,11 @@ struct SignedMessageSerdeHelper<'a> { } pub fn encode_and_sign(message: &T, secret: &[u8; 32]) -> Result, rmp_serde::encode::Error> { - let secret = SecretKey::from_slice(secret).unwrap(); + let secret = SecretKey::from_slice(secret) + .map_err(|e| rmp_serde::encode::Error::Syntax(format!("Error {} parsing secret", e)))?; let encoded = encode_message(message)?; - let sig_hash = SecpMessage::from_slice(&sha256(&encoded)).expect("Message::from_slice should never fail"); + let sig_hash = SecpMessage::from_slice(&sha256(&encoded)) + .map_err(|e| rmp_serde::encode::Error::Syntax(format!("Error {} parsing message", e)))?; let sig = SECP_SIGN.sign(&sig_hash, &secret); let serialized_sig = sig.serialize_compact(); let pubkey = PublicKey::from(Secp256k1Pubkey::from_secret_key(&*SECP_SIGN, &secret));