Skip to content

Commit

Permalink
hex::decode bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
insipx committed Feb 6, 2024
2 parents 4de0284 + 0580a61 commit 33d8dce
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 19 deletions.
15 changes: 11 additions & 4 deletions xps-gateway/src/rpc/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ use thiserror::Error;
use gateway_types::Message;
use registry::{error::ContactOperationError, ContactOperations};

// DEFAULT_ATTRIBUTE_VALIDITY is the hard-coded value we use for the validity of the attributes we set.
// This value is interpeted as number of seconds starting from the block where the attribute is being set.
pub const DEFAULT_ATTRIBUTE_VALIDITY: u64 = 60 * 60 * 24 * 365;

/// Gateway Methods for XPS
pub struct XpsMethods<P: Middleware + 'static> {
contact_operations: ContactOperations<GatewaySigner<P>>,
Expand Down Expand Up @@ -56,13 +60,16 @@ impl<P: Middleware + 'static> XpsServer for XpsMethods<P> {
signature: Signature,
) -> Result<GrantInstallationResult, ErrorObjectOwned> {
log::debug!("xps_grantInstallation called");
let block_number = self.signer.get_block_number().await.unwrap();
let validity_period: U64 = U64::from(60 * 60 * 24 * 365 / 5); // number of round in one year, assuming 5-second round.
let validity = block_number + validity_period;

let result = self
.contact_operations
.grant_installation(did, name, value, signature, U256::from(validity.as_u64()))
.grant_installation(
did,
name,
value,
signature,
U256::from(DEFAULT_ATTRIBUTE_VALIDITY),
)
.await
.map_err(RpcError::from)?;

Expand Down
181 changes: 166 additions & 15 deletions xps-gateway/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ use lib_didethresolver::{
did_registry::RegistrySignerExt,
types::{DidUrl, KeyEncoding, XmtpAttribute, XmtpKeyPurpose},
};
use xps_gateway::rpc::XpsClient;
use xps_gateway::rpc::*;

use ethers::middleware::Middleware;
use ethers::types::{Address, U256, U64};
use ethers::types::{Address, U256};
use gateway_types::{Message, Status};

use integration_util::*;
Expand Down Expand Up @@ -53,10 +52,102 @@ async fn test_wallet_address() -> Result<(), Error> {
.await
}

#[tokio::test]
async fn test_grant_revoke() -> Result<(), Error> {
with_xps_client(None, |client, context, resolver, anvil| async move {
for (key_index, key) in anvil.keys().iter().enumerate() {
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 attribute = XmtpAttribute {
purpose: XmtpKeyPurpose::Installation,
encoding: KeyEncoding::Hex,
};
let signature = wallet
.sign_attribute(
&context.registry,
name,
value.to_vec(),
U256::from(DEFAULT_ATTRIBUTE_VALIDITY),
)
.await?;

client
.grant_installation(
format!("0x{}", hex::encode(me.address())),
attribute.clone(),
value.to_vec(),
signature,
)
.await?;

let doc = resolver
.resolve_did(me.address(), None)
.await
.unwrap()
.document;

assert_eq!(doc.verification_method.len(), 2);
assert_eq!(
doc.verification_method[0].id,
DidUrl::parse(format!(
"did:ethr:0x{}#controller",
hex::encode(me.address())
))
.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
.sign_revoke_attribute(&context.registry, name, value.to_vec())
.await?;

client
.revoke_installation(
format!("0x{}", hex::encode(me.address())),
attribute,
value.to_vec(),
signature,
)
.await?;

let doc = resolver
.resolve_did(me.address(), None)
.await
.unwrap()
.document;

log::debug!("{}", serde_json::to_string_pretty(&doc).unwrap());

assert_eq!(
doc.verification_method[0].id,
DidUrl::parse(format!(
"did:ethr:0x{}#controller",
hex::encode(me.address())
))
.unwrap()
);
assert_eq!(doc.verification_method.len(), 1);
}
Ok(())
})
.await
}

#[tokio::test]
async fn test_grant_installation() -> Result<(), Error> {
with_xps_client(None, |client, context, resolver, anvil| async move {
let wallet: LocalWallet = anvil.keys()[3].clone().into();
let keys = anvil.keys();
let wallet: LocalWallet = keys[3].clone().into();
let me = get_user(&anvil, 3).await;
let name = *b"xmtp/installation/hex ";
let value = b"02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71";
Expand All @@ -66,23 +157,19 @@ async fn test_grant_installation() -> Result<(), Error> {
encoding: KeyEncoding::Hex,
};

let block_number = context.signer.get_block_number().await.unwrap();
let validity_period: U64 = U64::from(60 * 60 * 24 * 365 / 5); // number of round in one year, assuming 5-second round.
let validity = block_number + validity_period;

let signature = wallet
.sign_attribute(
&context.registry,
name,
value.to_vec(),
U256::from(validity.as_u64()),
U256::from(DEFAULT_ATTRIBUTE_VALIDITY),
)
.await?;

client
.grant_installation(
format!("0x{}", hex::encode(me.address())),
attribute,
attribute.clone(),
value.to_vec(),
signature,
)
Expand Down Expand Up @@ -111,6 +198,66 @@ async fn test_grant_installation() -> Result<(), Error> {
))
.unwrap()
);

// now, try to grant again, and ensure it fails as expected:
// (due to bad signature)
match client
.grant_installation(
format!("0x{}", hex::encode(me.address())),
attribute.clone(),
value.to_vec(),
signature,
)
.await
{
Err(jsonrpsee::core::client::error::Error::Call(e)) => assert_eq!(e.code(), -31999),
_ => panic!("grant_installation call was expected to fail on the second invocation"),
};

// calculate the signature again.
let signature = wallet
.sign_attribute(
&context.registry,
name,
value.to_vec(),
U256::from(DEFAULT_ATTRIBUTE_VALIDITY),
)
.await?;

// and invoke again.
client
.grant_installation(
format!("0x{}", hex::encode(me.address())),
attribute.clone(),
value.to_vec(),
signature,
)
.await?;

let doc = resolver
.resolve_did(me.address(), None)
.await
.unwrap()
.document;

assert_eq!(doc.verification_method.len(), 2);
assert_eq!(
doc.verification_method[0].id,
DidUrl::parse(format!(
"did:ethr:0x{}#controller",
hex::encode(me.address())
))
.unwrap()
);
assert_eq!(
doc.verification_method[1].id,
DidUrl::parse(format!(
"did:ethr:0x{}?meta=installation#xmtp-1",
hex::encode(me.address())
))
.unwrap()
);

Ok(())
})
.await
Expand Down Expand Up @@ -188,8 +335,8 @@ async fn test_fetch_key_packages() -> Result<(), Error> {
let value = b"000000000000000000000000000000000000000000000000000000000000000000";
set_attribute(name, value.to_vec(), &me, &context.registry).await?;

// let value = b"111111111111111111111111111111111111111111111111111111111111111111";
// set_attribute(name, value.to_vec(), &me, &context.registry).await?;
let value = b"111111111111111111111111111111111111111111111111111111111111111111";
set_attribute(name, value.to_vec(), &me, &context.registry).await?;

let res = client
.fetch_key_packages(format!("0x{}", hex::encode(me.address())))
Expand All @@ -202,7 +349,8 @@ async fn test_fetch_key_packages() -> Result<(), Error> {
vec![
hex::decode(b"000000000000000000000000000000000000000000000000000000000000000000")
.unwrap(),
// b"111111111111111111111111111111111111111111111111111111111111111111"
hex::decode(b"111111111111111111111111111111111111111111111111111111111111111111")
.unwrap()
]
);

Expand Down Expand Up @@ -273,7 +421,7 @@ async fn test_fetch_key_packages_client() -> Result<(), Error> {
&context.registry,
attribute.into(),
value.to_vec(),
U256::from(604_800),
U256::from(DEFAULT_ATTRIBUTE_VALIDITY),
)
.await?,
)
Expand All @@ -286,7 +434,10 @@ async fn test_fetch_key_packages_client() -> Result<(), Error> {
assert_eq!(&res.message, "Key packages retrieved");
assert_eq!(
res.installation,
vec![b"000000000000000000000000000000000000000000000000000000000000000000"]
vec![hex::decode(
b"000000000000000000000000000000000000000000000000000000000000000000"
)
.unwrap()]
);

Ok(())
Expand Down

0 comments on commit 33d8dce

Please sign in to comment.