Skip to content

Commit

Permalink
Fix failing DID IOTA tests and add NetworkName alias for well known n…
Browse files Browse the repository at this point in the history
…etworks (#1482)

* fix did test, add optional network alias

* update tests for client connecting to well known networks
  • Loading branch information
UMR1352 authored Dec 5, 2024
1 parent 19a0946 commit 3d75eea
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
17 changes: 14 additions & 3 deletions identity_iota_core/src/did/iota_did.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ impl IotaDID {
pub const METHOD: &'static str = "iota";

/// The default network name (`"iota"`).
// TODO: replace this with main net chain ID
// as soon as IOTA rebased lands on mainnet.
pub const DEFAULT_NETWORK: &'static str = "iota";

/// The tag of the placeholder DID.
Expand Down Expand Up @@ -431,7 +433,16 @@ mod tests {
let mut check_network_executed: bool = false;

const INVALID_NETWORK_NAMES: [&str; 10] = [
"Main", "fOo", "deV", "féta", "", " ", "foo ", " foo", "1234567", "foobar0",
"Main",
"fOo",
"deV",
"féta",
"",
" ",
"foo ",
" foo",
"123456789",
"foobar123",
];
for network_name in INVALID_NETWORK_NAMES {
let did_string: String = format!("did:method:{network_name}:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK");
Expand Down Expand Up @@ -559,9 +570,9 @@ mod tests {
Err(DIDError::InvalidMethodName)
));

// invalid network name (exceeded six characters)
// invalid network name (exceeded eight characters)
assert!(matches!(
IotaDID::parse(format!("did:{}:1234567:{}", IotaDID::METHOD, valid_object_id)),
IotaDID::parse(format!("did:{}:123456789:{}", IotaDID::METHOD, valid_object_id)),
Err(DIDError::Other(_))
));

Expand Down
2 changes: 2 additions & 0 deletions identity_iota_core/src/rebased/client/read_only.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ impl IdentityClientReadOnly {
"unrecognized network \"{network}\". Use `new_with_pkg_id` instead."
))
})?;
// If the network has a well known alias use it otherwise default to the network's chain ID.
let network = metadata.network_alias().unwrap_or(network);

let pkg_id = metadata.latest_pkg_id();

Expand Down
23 changes: 18 additions & 5 deletions identity_iota_core/src/rebased/iota/well_known_networks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ use iota_sdk::types::base_types::ObjectID;
use phf::phf_map;
use phf::Map;

use crate::NetworkName;

/// A Mapping `network_id` -> metadata needed by the library.
pub(crate) static IOTA_NETWORKS: Map<&str, IdentityNetworkMetadata> = phf_map! {
// devnet
"e678123a" => IdentityNetworkMetadata::new(
Some("devnet"),
&["0x156dfa0c4d4e576f5675de7d4bbe161c767947ffceefd7498cb39c406bc1cb67"],
"0x0247da7f3b8708fc1d326f70153c01b7caf52a19a6f42dd3b868ac8777486b11",
),
// testnet
"2304aa97" => IdentityNetworkMetadata::new(
Some("testnet"),
&["0x7a67dd504eb1291958495c71a07d20985951648dd5ebf01ac921a50257346818"],
"0xf1e20e6e3fa4de99ca269a0168f431dc459bc3a1ee5b76b426d5cf3094680483",
),
Expand All @@ -22,6 +24,7 @@ pub(crate) static IOTA_NETWORKS: Map<&str, IdentityNetworkMetadata> = phf_map! {
/// `iota_identity` package information for a given network.
#[derive(Debug)]
pub(crate) struct IdentityNetworkMetadata {
pub alias: Option<&'static str>,
/// `package[0]` is the current version, `package[1]`
/// is the version before, and so forth.
pub package: &'static [&'static str],
Expand All @@ -34,9 +37,10 @@ pub(crate) fn network_metadata(network_id: &str) -> Option<&'static IdentityNetw
}

impl IdentityNetworkMetadata {
const fn new(pkgs: &'static [&'static str], migration_registry: &'static str) -> Self {
const fn new(alias: Option<&'static str>, pkgs: &'static [&'static str], migration_registry: &'static str) -> Self {
assert!(!pkgs.is_empty());
Self {
alias,
package: pkgs,
migration_registry,
}
Expand All @@ -56,6 +60,13 @@ impl IdentityNetworkMetadata {
pub(crate) fn migration_registry(&self) -> ObjectID {
self.migration_registry.parse().expect("valid ObjectID")
}

/// Returns a [`NetworkName`] if `alias` is set.
pub(crate) fn network_alias(&self) -> Option<NetworkName> {
self.alias.map(|alias| {
NetworkName::try_from(alias).expect("an hardcoded network alias is valid (unless a dev messed it up)")
})
}
}

#[cfg(test)]
Expand All @@ -66,13 +77,15 @@ mod test {

#[tokio::test]
async fn identity_client_connection_to_devnet_works() -> anyhow::Result<()> {
IdentityClientReadOnly::new(IotaClientBuilder::default().build_devnet().await?).await?;
let client = IdentityClientReadOnly::new(IotaClientBuilder::default().build_devnet().await?).await?;
assert_eq!(client.network().as_ref(), "devnet");
Ok(())
}

#[tokio::test]
async fn identity_client_connection_to_testnet_works() -> anyhow::Result<()> {
IdentityClientReadOnly::new(IotaClientBuilder::default().build_testnet().await?).await?;
let client = IdentityClientReadOnly::new(IotaClientBuilder::default().build_testnet().await?).await?;
assert_eq!(client.network().as_ref(), "testnet");
Ok(())
}
}

0 comments on commit 3d75eea

Please sign in to comment.