mod msg_authority;
pub(super) use self::msg_authority::NodeMsgAuthorityUtils;
use crate::node::{Error, Result};
use sn_interface::messaging::{
system::{SigShare, SystemMsg},
AuthorityProof, BlsShareAuth, DstLocation, MsgId, MsgKind, NodeAuth, WireMsg,
};
use bls::PublicKey as BlsPublicKey;
use sn_interface::network_knowledge::{NodeInfo, SectionKeyShare};
use xor_name::XorName;
pub(crate) trait WireMsgUtils {
fn for_dst_accumulation(
key_share: &SectionKeyShare,
src_name: XorName,
dst: DstLocation,
node_msg: SystemMsg,
src_section_pk: BlsPublicKey,
) -> Result<WireMsg, Error>;
fn single_src(
node: &NodeInfo,
dst: DstLocation,
node_msg: SystemMsg,
src_section_pk: BlsPublicKey,
) -> Result<WireMsg>;
}
impl WireMsgUtils for WireMsg {
fn for_dst_accumulation(
key_share: &SectionKeyShare,
src_name: XorName,
dst: DstLocation,
node_msg: SystemMsg,
src_section_pk: BlsPublicKey,
) -> Result<WireMsg, Error> {
let msg_payload =
WireMsg::serialize_msg_payload(&node_msg).map_err(|_| Error::InvalidMessage)?;
let msg_kind = MsgKind::NodeBlsShareAuthMsg(
bls_share_authorize(src_section_pk, src_name, key_share, &msg_payload).into_inner(),
);
let wire_msg = WireMsg::new_msg(MsgId::new(), msg_payload, msg_kind, dst)?;
#[cfg(feature = "test-utils")]
let wire_msg = wire_msg.set_payload_debug(node_msg);
Ok(wire_msg)
}
fn single_src(
node: &NodeInfo,
dst: DstLocation,
node_msg: SystemMsg,
src_section_pk: BlsPublicKey,
) -> Result<WireMsg> {
let msg_payload =
WireMsg::serialize_msg_payload(&node_msg).map_err(|_| Error::InvalidMessage)?;
let msg_kind = MsgKind::NodeAuthMsg(
NodeAuth::authorize(src_section_pk, &node.keypair, &msg_payload).into_inner(),
);
let wire_msg = WireMsg::new_msg(MsgId::new(), msg_payload, msg_kind, dst)?;
#[cfg(feature = "test-utils")]
let wire_msg = wire_msg.set_payload_debug(node_msg);
Ok(wire_msg)
}
}
fn bls_share_authorize(
section_pk: BlsPublicKey,
src_name: XorName,
key_share: &SectionKeyShare,
payload: impl AsRef<[u8]>,
) -> AuthorityProof<BlsShareAuth> {
AuthorityProof(BlsShareAuth {
section_pk,
src_name,
sig_share: SigShare {
public_key_set: key_share.public_key_set.clone(),
index: key_share.index,
signature_share: key_share.secret_key_share.sign(payload),
},
})
}