Skip to content

Commit 0f7224e

Browse files
committed
bugfix: make sure refresh data key matches bond info
1 parent a94c035 commit 0f7224e

File tree

2 files changed

+53
-18
lines changed
  • nym-api/src

2 files changed

+53
-18
lines changed

nym-api/src/node_describe_cache/mod.rs

+50-15
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use futures::{stream, StreamExt};
1212
use nym_api_requests::legacy::{LegacyGatewayBondWithId, LegacyMixNodeDetailsWithLayer};
1313
use nym_api_requests::models::{DescribedNodeType, NymNodeData, NymNodeDescription};
1414
use nym_config::defaults::DEFAULT_NYM_NODE_HTTP_PORT;
15+
use nym_crypto::asymmetric::ed25519;
1516
use nym_mixnet_contract_common::{LegacyMixLayer, NodeId, NymNodeDetails};
1617
use nym_node_requests::api::client::{NymNodeApiClientError, NymNodeApiClientExt};
1718
use nym_topology::gateway::GatewayConversionError;
@@ -58,6 +59,13 @@ pub enum NodeDescribeCacheError {
5859
#[error("could not verify signed host information for node {node_id}")]
5960
MissignedHostInformation { node_id: NodeId },
6061

62+
#[error("identity of node {node_id} does not match. expected {expected} but got {got}")]
63+
MismatchedIdentity {
64+
node_id: NodeId,
65+
expected: String,
66+
got: String,
67+
},
68+
6169
#[error("node {node_id} is announcing an illegal ip address")]
6270
IllegalIpAddress { node_id: NodeId },
6371
}
@@ -289,6 +297,15 @@ async fn try_get_description(
289297

290298
let host_info = client.get_host_information().await.map_err(map_query_err)?;
291299

300+
// check if the identity key matches the information provided during bonding
301+
if data.expected_identity != host_info.keys.ed25519_identity {
302+
return Err(NodeDescribeCacheError::MismatchedIdentity {
303+
node_id: data.node_id,
304+
expected: data.expected_identity.to_base58_string(),
305+
got: host_info.keys.ed25519_identity.to_base58_string(),
306+
});
307+
}
308+
292309
if !host_info.verify_host_information() {
293310
return Err(NodeDescribeCacheError::MissignedHostInformation {
294311
node_id: data.node_id,
@@ -315,54 +332,66 @@ async fn try_get_description(
315332
pub(crate) struct RefreshData {
316333
host: String,
317334
node_id: NodeId,
335+
expected_identity: ed25519::PublicKey,
318336
node_type: DescribedNodeType,
319337

320338
port: Option<u16>,
321339
}
322340

323-
impl<'a> From<&'a LegacyMixNodeDetailsWithLayer> for RefreshData {
324-
fn from(node: &'a LegacyMixNodeDetailsWithLayer) -> Self {
325-
RefreshData::new(
341+
impl<'a> TryFrom<&'a LegacyMixNodeDetailsWithLayer> for RefreshData {
342+
type Error = ed25519::Ed25519RecoveryError;
343+
344+
fn try_from(node: &'a LegacyMixNodeDetailsWithLayer) -> Result<Self, Self::Error> {
345+
Ok(RefreshData::new(
326346
&node.bond_information.mix_node.host,
347+
node.bond_information.identity().parse()?,
327348
DescribedNodeType::LegacyMixnode,
328349
node.mix_id(),
329350
Some(node.bond_information.mix_node.http_api_port),
330-
)
351+
))
331352
}
332353
}
333354

334-
impl<'a> From<&'a LegacyGatewayBondWithId> for RefreshData {
335-
fn from(node: &'a LegacyGatewayBondWithId) -> Self {
336-
RefreshData::new(
355+
impl<'a> TryFrom<&'a LegacyGatewayBondWithId> for RefreshData {
356+
type Error = ed25519::Ed25519RecoveryError;
357+
358+
fn try_from(node: &'a LegacyGatewayBondWithId) -> Result<Self, Self::Error> {
359+
Ok(RefreshData::new(
337360
&node.bond.gateway.host,
361+
node.bond.identity().parse()?,
338362
DescribedNodeType::LegacyGateway,
339363
node.node_id,
340364
None,
341-
)
365+
))
342366
}
343367
}
344368

345-
impl<'a> From<&'a NymNodeDetails> for RefreshData {
346-
fn from(node: &'a NymNodeDetails) -> Self {
347-
RefreshData::new(
369+
impl<'a> TryFrom<&'a NymNodeDetails> for RefreshData {
370+
type Error = ed25519::Ed25519RecoveryError;
371+
372+
fn try_from(node: &'a NymNodeDetails) -> Result<Self, Self::Error> {
373+
Ok(RefreshData::new(
348374
&node.bond_information.node.host,
375+
node.bond_information.identity().parse()?,
349376
DescribedNodeType::NymNode,
350377
node.node_id(),
351378
node.bond_information.node.custom_http_port,
352-
)
379+
))
353380
}
354381
}
355382

356383
impl RefreshData {
357384
pub fn new(
358385
host: impl Into<String>,
386+
expected_identity: ed25519::PublicKey,
359387
node_type: DescribedNodeType,
360388
node_id: NodeId,
361389
port: Option<u16>,
362390
) -> Self {
363391
RefreshData {
364392
host: host.into(),
365393
node_id,
394+
expected_identity,
366395
node_type,
367396
port,
368397
}
@@ -404,7 +433,9 @@ impl CacheItemProvider for NodeDescriptionProvider {
404433
None => error!("failed to obtain mixnodes information from the cache"),
405434
Some(legacy_mixnodes) => {
406435
for node in &**legacy_mixnodes {
407-
nodes_to_query.push(node.into())
436+
if let Ok(data) = node.try_into() {
437+
nodes_to_query.push(data);
438+
}
408439
}
409440
}
410441
}
@@ -413,7 +444,9 @@ impl CacheItemProvider for NodeDescriptionProvider {
413444
None => error!("failed to obtain gateways information from the cache"),
414445
Some(legacy_gateways) => {
415446
for node in &**legacy_gateways {
416-
nodes_to_query.push(node.into())
447+
if let Ok(data) = node.try_into() {
448+
nodes_to_query.push(data);
449+
}
417450
}
418451
}
419452
}
@@ -422,7 +455,9 @@ impl CacheItemProvider for NodeDescriptionProvider {
422455
None => error!("failed to obtain nym-nodes information from the cache"),
423456
Some(nym_nodes) => {
424457
for node in &**nym_nodes {
425-
nodes_to_query.push(node.into())
458+
if let Ok(data) = node.try_into() {
459+
nodes_to_query.push(data);
460+
}
426461
}
427462
}
428463
}

nym-api/src/nym_contract_cache/cache/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ impl NymContractCache {
385385
.iter()
386386
.find(|n| n.bond_information.identity() == encoded_identity)
387387
{
388-
return Some(nym_node.into());
388+
return nym_node.try_into().ok();
389389
}
390390

391391
// 2. check legacy mixnodes
@@ -394,7 +394,7 @@ impl NymContractCache {
394394
.iter()
395395
.find(|n| n.bond_information.identity() == encoded_identity)
396396
{
397-
return Some(mixnode.into());
397+
return mixnode.try_into().ok();
398398
}
399399

400400
// 3. check legacy gateways
@@ -403,7 +403,7 @@ impl NymContractCache {
403403
.iter()
404404
.find(|n| n.identity() == &encoded_identity)
405405
{
406-
return Some(gateway.into());
406+
return gateway.try_into().ok();
407407
}
408408

409409
None

0 commit comments

Comments
 (0)