From 1c0af2ef694aae8bd889225a0a37a51cbbcd3c2c Mon Sep 17 00:00:00 2001 From: Andrew Plaza Date: Wed, 14 Feb 2024 18:12:52 -0500 Subject: [PATCH] fix bugs with hex/byte conversion & timestamp --- Cargo.lock | 1 + lib-xps/Cargo.toml | 4 + lib-xps/tests/integration_test.rs | 107 ++++++++++++-------------- lib-xps/tests/integration_util/mod.rs | 15 +++- registry/src/lib.rs | 2 +- 5 files changed, 69 insertions(+), 60 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d486387..b76782e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2179,6 +2179,7 @@ dependencies = [ "log", "messaging", "rand", + "regex", "registry", "serde", "serde_json", diff --git a/lib-xps/Cargo.toml b/lib-xps/Cargo.toml index 667d3b7..0541fdb 100644 --- a/lib-xps/Cargo.toml +++ b/lib-xps/Cargo.toml @@ -26,8 +26,12 @@ tokio-stream = { version = "0.1", features = ["net"] } registry = { path = "../registry" } messaging = { path = "../messaging" } +# Integration Tests + [dev-dependencies] jsonrpsee = { workspace = true, features = ["macros", "server", "client"] } tokio = { workspace = true, features = ["macros", "rt", "time"] } futures = "0.3" serde_json.workspace = true +regex = { version = "1.10" } + diff --git a/lib-xps/tests/integration_test.rs b/lib-xps/tests/integration_test.rs index 3ab3dd0..d936956 100644 --- a/lib-xps/tests/integration_test.rs +++ b/lib-xps/tests/integration_test.rs @@ -8,6 +8,7 @@ use ethers::providers::Middleware; use ethers::types::{Address, Bytes, TransactionRequest, U256}; use ethers::utils::keccak256; use ethers::{signers::LocalWallet, signers::Signer}; +use hex::FromHex; use integration_util::*; use jsonrpsee::core::ClientError; use lib_didethresolver::{ @@ -121,8 +122,7 @@ async fn test_wallet_address() -> Result<(), Error> { #[tokio::test] async fn test_grant_revoke() -> Result<(), Error> { with_xps_client(None, None, |client, context, resolver, anvil| async move { - for (key_index, key) in anvil.keys().iter().enumerate() { - let wallet: LocalWallet = key.clone().into(); + for (key_index, _) in anvil.keys().iter().enumerate() { let me = get_user(&anvil, key_index).await; let name = *b"xmtp/installation/hex "; let value = b"02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71"; @@ -131,7 +131,8 @@ async fn test_grant_revoke() -> Result<(), Error> { purpose: XmtpKeyPurpose::Installation, encoding: KeyEncoding::Hex, }; - let signature = wallet + let signature = me + .signer() .sign_attribute( &context.registry, name, @@ -164,16 +165,12 @@ async fn test_grant_revoke() -> Result<(), Error> { )) .unwrap() ); - assert_eq!( - doc.verification_method[1].id, - DidUrl::parse(format!( - "did:ethr:0x{}?meta=installation#xmtp-0", - hex::encode(me.address()) - )) - .unwrap() - ); - let signature = wallet + let test = did_ethr_xmtp_regex(&me.signer(), 0); + assert!(test.is_match(&doc.verification_method[1].id.to_string())); + + let signature = me + .signer() .sign_revoke_attribute(&context.registry, name, value.to_vec()) .await?; @@ -334,26 +331,21 @@ async fn test_revoke_installation() -> Result<(), Error> { with_xps_client(None, None, |client, context, resolver, anvil| async move { let me: LocalWallet = anvil.keys()[3].clone().into(); let name = *b"xmtp/installation/hex "; - let value = b"02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71"; + let value = "02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71"; - set_attribute(name, value.to_vec(), &me, &context.registry).await?; + set_attribute(name, value, &me, &context.registry).await?; let doc = resolver .resolve_did(me.address(), None) .await .unwrap() .document; - assert_eq!( - doc.verification_method[1].id, - DidUrl::parse(format!( - "did:ethr:0x{}?meta=installation#xmtp-0", - hex::encode(me.address()) - )) - .unwrap() - ); + + let test = did_ethr_xmtp_regex(&me, 0); + assert!(test.is_match(&doc.verification_method[1].id.to_string())); let signature = me - .sign_revoke_attribute(&context.registry, name, value.to_vec()) + .sign_revoke_attribute(&context.registry, name, Bytes::from_hex(value)?.to_vec()) .await?; let attribute = XmtpAttribute { @@ -365,7 +357,7 @@ async fn test_revoke_installation() -> Result<(), Error> { .revoke_installation( format!("0x{}", hex::encode(me.address())), attribute, - value.to_vec(), + Bytes::from_hex(value)?.to_vec(), signature, ) .await?; @@ -430,27 +422,28 @@ async fn test_fetch_key_packages() -> Result<(), Error> { with_xps_client(None, None, |client, context, _, anvil| async move { let me: LocalWallet = anvil.keys()[3].clone().into(); let name = *b"xmtp/installation/hex "; - let value = b"000000000000000000000000000000000000000000000000000000000000000000"; - set_attribute(name, value.to_vec(), &me, &context.registry).await?; + let value = "000000000000000000000000000000000000000000000000000000000000000000"; + set_attribute(name, value, &me, &context.registry).await?; - let value = b"111111111111111111111111111111111111111111111111111111111111111111"; - set_attribute(name, value.to_vec(), &me, &context.registry).await?; + let value = "111111111111111111111111111111111111111111111111111111111111111111"; + set_attribute(name, value, &me, &context.registry).await?; let res = client - .fetch_key_packages(format!("0x{}", hex::encode(me.address()))) + .fetch_key_packages(format!("0x{}", hex::encode(me.address())), 0) .await?; assert_eq!(res.status, Status::Success); assert_eq!(&res.message, "Key packages retrieved"); + assert_eq!( - res.installation, - vec![ - hex::decode(b"000000000000000000000000000000000000000000000000000000000000000000") - .unwrap(), - hex::decode(b"111111111111111111111111111111111111111111111111111111111111111111") - .unwrap() - ] + res.installations[0].id, + Bytes::from_hex("000000000000000000000000000000000000000000000000000000000000000000")? ); + assert_eq!( + res.installations[1].id, + Bytes::from_hex("111111111111111111111111111111111111111111111111111111111111111111")? + ); + Ok(()) }) .await @@ -461,11 +454,11 @@ async fn test_fetch_key_packages_revoke() -> Result<(), Error> { with_xps_client(None, None, |client, context, _, anvil| async move { let me: LocalWallet = anvil.keys()[3].clone().into(); let name = *b"xmtp/installation/hex "; - let value = b"000000000000000000000000000000000000000000000000000000000000000000"; - set_attribute(name, value.to_vec(), &me, &context.registry).await?; + let value = "000000000000000000000000000000000000000000000000000000000000000000"; + set_attribute(name, value, &me, &context.registry).await?; - let value = b"111111111111111111111111111111111111111111111111111111111111111111"; - set_attribute(name, value.to_vec(), &me, &context.registry).await?; + let value = "111111111111111111111111111111111111111111111111111111111111111111"; + set_attribute(name, value, &me, &context.registry).await?; client .revoke_installation( @@ -474,24 +467,23 @@ async fn test_fetch_key_packages_revoke() -> Result<(), Error> { purpose: XmtpKeyPurpose::Installation, encoding: KeyEncoding::Hex, }, - value.to_vec(), - me.sign_revoke_attribute(&context.registry, name, value.to_vec()) + Bytes::from_hex(value)?.to_vec(), + me.sign_revoke_attribute(&context.registry, name, Bytes::from_hex(value)?.to_vec()) .await?, ) .await?; let res = client - .fetch_key_packages(format!("0x{}", hex::encode(me.address()))) + .fetch_key_packages(format!("0x{}", hex::encode(me.address())), 0) .await?; assert_eq!(res.status, Status::Success); assert_eq!(&res.message, "Key packages retrieved"); + + assert_eq!(res.installations.len(), 1); assert_eq!( - res.installation, - vec![hex::decode( - b"000000000000000000000000000000000000000000000000000000000000000000" - ) - .unwrap()] + res.installations[0].id, + Bytes::from_hex("000000000000000000000000000000000000000000000000000000000000000000")? ); Ok(()) @@ -513,28 +505,27 @@ async fn test_fetch_key_packages_client() -> Result<(), Error> { .grant_installation( format!("0x{}", hex::encode(me.address())), attribute.clone(), - value.to_vec(), + Bytes::from_hex(value)?.to_vec(), me.sign_attribute( &context.registry, attribute.into(), - value.to_vec(), + Bytes::from_hex(value)?.to_vec(), U256::from(DEFAULT_ATTRIBUTE_VALIDITY), ) .await?, ) .await?; let res = client - .fetch_key_packages(format!("0x{}", hex::encode(me.address()))) + .fetch_key_packages(format!("0x{}", hex::encode(me.address())), 0) .await?; + log::debug!("{:?}", res); assert_eq!(res.status, Status::Success); - assert_eq!(&res.message, "Key packages retrieved"); + assert_eq!(&res.message, "Identities retrieved"); + assert_eq!(res.installations.len(), 1); assert_eq!( - res.installation, - vec![hex::decode( - b"000000000000000000000000000000000000000000000000000000000000000000" - ) - .unwrap()] + Bytes::from(res.installations[0].id.clone()), + Bytes::from_hex(value)? ); Ok(()) @@ -563,7 +554,7 @@ async fn test_did_deactivation() -> Result<(), Error> { .await?; let res = client - .fetch_key_packages(format!("0x{}", hex::encode(me.address()))) + .fetch_key_packages(format!("0x{}", hex::encode(me.address())), 0) .await .unwrap_err(); diff --git a/lib-xps/tests/integration_util/mod.rs b/lib-xps/tests/integration_util/mod.rs index a374c82..0e55ea1 100644 --- a/lib-xps/tests/integration_util/mod.rs +++ b/lib-xps/tests/integration_util/mod.rs @@ -1,4 +1,5 @@ use anyhow::Error; +use hex::FromHex; use jsonrpsee::{ server::Server, ws_client::{WsClient, WsClientBuilder}, @@ -20,6 +21,7 @@ use lib_didethresolver::{ Resolver, }; use messaging::Conversation; +use regex::Regex; use std::{ future::Future, sync::{Arc, Once}, @@ -27,6 +29,7 @@ use std::{ }; use tokio::time::timeout as timeout_tokio; use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Registry}; +use xps_types::Bytes; use lib_xps::{ types::{GatewayContext, GatewaySigner}, @@ -170,11 +173,12 @@ fn init_test_logging() { pub async fn set_attribute( name: [u8; 32], - value: Vec, + value: &str, wallet: &LocalWallet, registry: &DIDRegistry>>, ) -> Result<(), Error> { let validity = U256::from(604_800); + let value = Bytes::from_hex(value)?; let signature = wallet .sign_attribute(registry, name, value.to_vec(), validity) .await?; @@ -191,3 +195,12 @@ pub async fn set_attribute( attr.send().await?.await?; Ok(()) } + +pub fn did_ethr_xmtp_regex(wallet: &LocalWallet, index: usize) -> Regex { + let regexr = format!( + r"did:ethr:mainnet:0x{}\?meta=installation×tamp=\d+#xmtp-{}", + hex::encode(wallet.address()), + index + ); + Regex::new(®exr).unwrap() +} diff --git a/registry/src/lib.rs b/registry/src/lib.rs index 5140756..19afc61 100644 --- a/registry/src/lib.rs +++ b/registry/src/lib.rs @@ -70,7 +70,7 @@ where id: method.verification_properties?.try_into().ok()?, timestamp_ns: method .id - .get_query_value("timestmap")? + .get_query_value("timestamp")? .parse::() .ok()?, })