Skip to content

Commit

Permalink
fix(p2p): handle encode_and_sign errors (#2038)
Browse files Browse the repository at this point in the history
  • Loading branch information
shamardy authored Dec 15, 2023
1 parent f2efb10 commit 466d4f2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
24 changes: 21 additions & 3 deletions mm2src/mm2_main/src/lp_ordermatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}

Expand Down
16 changes: 14 additions & 2 deletions mm2src/mm2_main/src/lp_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,13 @@ pub fn broadcast_swap_msg_every_delayed<T: 'static + Serialize + Clone + Send>(
/// Broadcast the swap message once
pub fn broadcast_swap_message<T: Serialize>(ctx: &MmArc, topic: String, msg: T, p2p_privkey: &Option<KeyPair>) {
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);
}

Expand All @@ -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);
}

Expand Down
6 changes: 4 additions & 2 deletions mm2src/mm2_p2p/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,11 @@ struct SignedMessageSerdeHelper<'a> {
}

pub fn encode_and_sign<T: Serialize>(message: &T, secret: &[u8; 32]) -> Result<Vec<u8>, 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));
Expand Down

0 comments on commit 466d4f2

Please sign in to comment.