Skip to content

Commit

Permalink
fix bugs with hex/byte conversion & timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
insipx committed Feb 14, 2024
1 parent 96e7c63 commit 1c0af2e
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 60 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions lib-xps/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }

107 changes: 49 additions & 58 deletions lib-xps/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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";
Expand All @@ -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,
Expand Down Expand Up @@ -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?;

Expand Down Expand Up @@ -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 {
Expand All @@ -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?;
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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(())
Expand All @@ -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(())
Expand Down Expand Up @@ -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();

Expand Down
15 changes: 14 additions & 1 deletion lib-xps/tests/integration_util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::Error;
use hex::FromHex;
use jsonrpsee::{
server::Server,
ws_client::{WsClient, WsClientBuilder},
Expand All @@ -20,13 +21,15 @@ use lib_didethresolver::{
Resolver,
};
use messaging::Conversation;
use regex::Regex;
use std::{
future::Future,
sync::{Arc, Once},
time::Duration,
};
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},
Expand Down Expand Up @@ -170,11 +173,12 @@ fn init_test_logging() {

pub async fn set_attribute(
name: [u8; 32],
value: Vec<u8>,
value: &str,
wallet: &LocalWallet,
registry: &DIDRegistry<GatewaySigner<Provider<Ws>>>,
) -> 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?;
Expand All @@ -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&timestamp=\d+#xmtp-{}",
hex::encode(wallet.address()),
index
);
Regex::new(&regexr).unwrap()
}
2 changes: 1 addition & 1 deletion registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<u64>()
.ok()?,
})
Expand Down

0 comments on commit 1c0af2e

Please sign in to comment.