Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
insipx committed Feb 22, 2024
1 parent d379d57 commit 5ba7191
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 80 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.

1 change: 1 addition & 0 deletions lib-xps/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ 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" }
139 changes: 63 additions & 76 deletions lib-xps/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ 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::{
did_registry::RegistrySignerExt,
types::{DidUrl, KeyEncoding, XmtpAttribute, XmtpKeyPurpose, NULL_ADDRESS},
types::{string_to_bytes32, DidUrl, KeyEncoding, XmtpAttribute, XmtpKeyPurpose, NULL_ADDRESS},
};
use lib_xps::rpc::{XpsClient, DEFAULT_ATTRIBUTE_VALIDITY};
use messaging::ConversationSignerExt;
Expand Down Expand Up @@ -125,7 +126,8 @@ async fn test_grant_revoke() -> Result<(), Error> {
let wallet: LocalWallet = key.clone().into();
let me = get_user(&anvil, key_index).await;
let name = *b"xmtp/installation/hex ";
let value = b"02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71";
let value = "02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71";
let value_bytes = Bytes::from_hex(value).unwrap();

let attribute = XmtpAttribute {
purpose: XmtpKeyPurpose::Installation,
Expand All @@ -135,7 +137,7 @@ async fn test_grant_revoke() -> Result<(), Error> {
.sign_attribute(
&context.registry,
name,
value.to_vec(),
value_bytes.to_vec().clone(),
U256::from(DEFAULT_ATTRIBUTE_VALIDITY),
)
.await?;
Expand All @@ -144,7 +146,7 @@ async fn test_grant_revoke() -> Result<(), Error> {
.grant_installation(
format!("0x{}", hex::encode(me.address())),
attribute.clone(),
value.to_vec(),
value_bytes.to_vec(),
signature,
)
.await?;
Expand All @@ -164,24 +166,19 @@ 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 test = did_ethr_xmtp_regex(me.signer(), 0);
assert!(test.is_match(&doc.verification_method[1].id.to_string()));

let signature = wallet
.sign_revoke_attribute(&context.registry, name, value.to_vec())
.sign_revoke_attribute(&context.registry, name, value_bytes.to_vec())
.await?;

client
.revoke_installation(
format!("0x{}", hex::encode(me.address())),
attribute,
value.to_vec(),
value_bytes.to_vec(),
signature,
)
.await?;
Expand Down Expand Up @@ -256,14 +253,9 @@ async fn test_grant_installation() -> 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 test = did_ethr_xmtp_regex(me.signer(), 0);
assert!(test.is_match(&doc.verification_method[1].id.to_string()));

// now, try to grant again, and ensure it fails as expected:
// (due to bad signature)
Expand Down Expand Up @@ -315,14 +307,9 @@ async fn test_grant_installation() -> Result<(), Error> {
))
.unwrap()
);
assert_eq!(
doc.verification_method[1].id,
DidUrl::parse(format!(
"did:ethr:0x{}?meta=installation#xmtp-1",
hex::encode(me.address())
))
.unwrap()
);

let test = did_ethr_xmtp_regex(me.signer(), 1);
assert!(test.is_match(&doc.verification_method[1].id.to_string()));

Ok(())
})
Expand All @@ -333,27 +320,27 @@ async fn test_grant_installation() -> Result<(), Error> {
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 name = "xmtp/installation/hex";
let value = "02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71";
let value_bytes = Bytes::from_hex(value).unwrap();

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,
string_to_bytes32(name),
value_bytes.to_vec(),
)
.await?;

let attribute = XmtpAttribute {
Expand All @@ -365,7 +352,7 @@ async fn test_revoke_installation() -> Result<(), Error> {
.revoke_installation(
format!("0x{}", hex::encode(me.address())),
attribute,
value.to_vec(),
value_bytes.to_vec(),
signature,
)
.await?;
Expand Down Expand Up @@ -429,27 +416,27 @@ async fn test_balance() -> Result<(), Error> {
async fn test_get_identity_updates() -> 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 name = "xmtp/installation/hex ";
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
.get_identity_updates(format!("0x{}", hex::encode(me.address())), 0)
.await?;

assert_eq!(res.status, Status::Success);
assert_eq!(&res.message, "Key packages retrieved");
assert_eq!(&res.message, "Identities retrieved");
assert_eq!(res.installations.len(), 2);
assert_eq!(
res.installations,
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(())
})
Expand All @@ -460,12 +447,13 @@ async fn test_get_identity_updates() -> Result<(), Error> {
async fn test_get_identity_updates_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 name = "xmtp/installation/hex";
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";
let value_bytes = Bytes::from_hex(value).unwrap().to_vec();
set_attribute(name, value, &me, &context.registry).await?;

client
.revoke_installation(
Expand All @@ -474,8 +462,8 @@ async fn test_get_identity_updates_revoke() -> Result<(), Error> {
purpose: XmtpKeyPurpose::Installation,
encoding: KeyEncoding::Hex,
},
value.to_vec(),
me.sign_revoke_attribute(&context.registry, name, value.to_vec())
value_bytes.clone(),
me.sign_revoke_attribute(&context.registry, string_to_bytes32(name), value_bytes)
.await?,
)
.await?;
Expand All @@ -485,13 +473,12 @@ async fn test_get_identity_updates_revoke() -> Result<(), Error> {
.await?;

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.installations,
vec![hex::decode(
b"000000000000000000000000000000000000000000000000000000000000000000"
)
.unwrap()]
res.installations[0].id,
Bytes::from_hex("000000000000000000000000000000000000000000000000000000000000000000")
.unwrap()
);

Ok(())
Expand All @@ -507,7 +494,9 @@ async fn test_get_identity_updates_client() -> Result<(), Error> {
purpose: XmtpKeyPurpose::Installation,
encoding: KeyEncoding::Hex,
};
let value = b"000000000000000000000000000000000000000000000000000000000000000000";
let value =
Bytes::from_hex("000000000000000000000000000000000000000000000000000000000000000000")
.unwrap();

client
.grant_installation(
Expand All @@ -528,13 +517,11 @@ async fn test_get_identity_updates_client() -> Result<(), Error> {
.await?;

assert_eq!(res.status, Status::Success);
assert_eq!(&res.message, "Key packages retrieved");
assert_eq!(&res.message, "Identities retrieved");
assert_eq!(
res.installations,
vec![hex::decode(
b"000000000000000000000000000000000000000000000000000000000000000000"
)
.unwrap()]
res.installations[0].id,
Bytes::from_hex("000000000000000000000000000000000000000000000000000000000000000000")
.unwrap()
);

Ok(())
Expand Down
23 changes: 19 additions & 4 deletions lib-xps/tests/integration_util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@ use ethers::{
middleware::SignerMiddleware,
providers::{Provider, Ws},
signers::{LocalWallet, Signer as _},
types::U256,
types::{Bytes, U256},
utils::AnvilInstance,
};
use futures::future::FutureExt;
use hex::FromHex;
use lib_didethresolver::{
did_registry::{DIDRegistry, RegistrySignerExt},
types::string_to_bytes32,
Resolver,
};
use messaging::Conversation;
use regex::Regex;
use std::{
future::Future,
sync::{Arc, Once},
Expand Down Expand Up @@ -169,12 +172,14 @@ fn init_test_logging() {
}

pub async fn set_attribute(
name: [u8; 32],
value: Vec<u8>,
name: &str,
value: &str,
wallet: &LocalWallet,
registry: &DIDRegistry<GatewaySigner<Provider<Ws>>>,
) -> Result<(), Error> {
let validity = U256::from(604_800);
let name = string_to_bytes32(name);
let value = Bytes::from_hex(value)?;
let signature = wallet
.sign_attribute(registry, name, value.to_vec(), validity)
.await?;
Expand All @@ -185,9 +190,19 @@ pub async fn set_attribute(
signature.r.into(),
signature.s.into(),
name,
value.into(),
value,
validity,
);
attr.send().await?.await?;
Ok(())
}

/// Returns a regex that matches the DID URL for the given index and wallet address
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()
}

0 comments on commit 5ba7191

Please sign in to comment.