Skip to content

Commit

Permalink
fetch key packages
Browse files Browse the repository at this point in the history
  • Loading branch information
insipx committed Feb 2, 2024
1 parent 9831bd4 commit 31162f3
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 40 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions gateway-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ pub type Bytes = Vec<u8>;
/// * `transaction` - A `String` representing the unique identifier of the transaction on the
/// blockchain. This can be used to track the transaction in a blockchain explorer.
///
#[derive(Serialize, Deserialize, Clone)]
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct GrantInstallationResult {
pub status: Status,
pub message: String,
pub transaction: String,
}

#[derive(Serialize, Deserialize, Clone)]
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct KeyPackageResult {
/// Status of the operation
pub status: Status,
Expand All @@ -56,7 +56,7 @@ pub struct KeyPackageResult {
pub key_packages: Vec<Bytes>,
}

#[derive(Serialize, Deserialize, Clone)]
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub enum Status {
Completed,
Failed,
Expand Down
2 changes: 2 additions & 0 deletions registry/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ pub enum ContactOperationError<M: Middleware> {
ResolutionError(lib_didethresolver::error::ResolverError<M>, String),
#[error("The DID has been deactivated, and no longer valid")]
DIDDeactivated,
#[error("Type failed to convert")]
Type(#[from] lib_didethresolver::error::TypeError),
}
34 changes: 22 additions & 12 deletions registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ use ethers::{core::types::Signature, providers::Middleware, types::Address};
use gateway_types::{GrantInstallationResult, KeyPackageResult, Status};
use lib_didethresolver::types::VerificationMethodProperties;
use lib_didethresolver::Resolver;
use lib_didethresolver::{
did_registry::DIDRegistry,
types::{Attribute, XmtpAttribute},
};
use lib_didethresolver::{did_registry::DIDRegistry, types::XmtpAttribute};

pub struct ContactOperations<Middleware> {
registry: DIDRegistry<Middleware>,
Expand Down Expand Up @@ -52,17 +49,30 @@ where
}

let document = resolution.document;
/*

let properties = document
.verification_method
.iter()
.filter_map(|method| method.id.fragment().filter(|f| f.starts_with("xmtp-")))
.collect::<Option<VerificationMethodProperties>>();
*/
.into_iter()
.filter(|method| {
method
.id
.fragment()
.map(|f| f.starts_with("xmtp-"))
.unwrap_or(false)
&& method
.id
.contains_query("meta".into(), "installation".into())
})
.filter_map(|method| method.verification_properties)
.collect::<Vec<VerificationMethodProperties>>();

Ok(KeyPackageResult {
status: Status::Completed,
message: "Key packages retrieved".to_string(),
key_packages: Vec::new(),
key_packages: properties
.into_iter()
.map(TryFrom::try_from)
.collect::<Result<_, _>>()?,
})
}

Expand All @@ -75,7 +85,7 @@ where
validity: U256,
) -> Result<GrantInstallationResult, ContactOperationError<M>> {
let address = self.resolve_did_address(did)?;
let attribute: [u8; 32] = Attribute::from(name).into();
let attribute: [u8; 32] = name.into();
log::debug!(
"setting attribute {:#?}",
String::from_utf8_lossy(&attribute)
Expand Down Expand Up @@ -110,7 +120,7 @@ where
signature: Signature,
) -> Result<(), ContactOperationError<M>> {
let address = self.resolve_did_address(did)?;
let attribute: [u8; 32] = Attribute::from(name).into();
let attribute: [u8; 32] = name.into();
log::debug!(
"Revoking attribute {:#?}",
String::from_utf8_lossy(&attribute)
Expand Down
7 changes: 5 additions & 2 deletions xps-gateway/src/rpc/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use ethers::core::types::Signature;
use ethers::prelude::*;
use jsonrpsee::{proc_macros::rpc, types::ErrorObjectOwned};

use gateway_types::GrantInstallationResult;
use gateway_types::Message;
use gateway_types::{GrantInstallationResult, KeyPackageResult};
use lib_didethresolver::types::XmtpAttribute;

/// XPS JSON-RPC Interface Methods
Expand Down Expand Up @@ -222,6 +222,9 @@ pub trait Xps {
signature: Signature,
) -> Result<(), ErrorObjectOwned>;

#[method(name = "fetchKeyPackages")]
async fn fetch_key_packages(&self, did: String) -> Result<KeyPackageResult, ErrorObjectOwned>;

/// # Documentation for JSON RPC Endpoint: `status`
/// ## Overview
Expand Down Expand Up @@ -304,7 +307,7 @@ pub trait Xps {
/// $ $ curl -H "Content-Type: application/json" -d '{"id":7000, "jsonrpc":"2.0", "method":"xps_status"}' http:///localhost:34695
/// {"jsonrpc":"2.0","result":"OK","id":7000}
/// ```
///
/// ### Notes
/// - The system should have proper error handling to deal with invalid requests, unauthorized access, and other potential issues.
#[method(name = "status")]
Expand Down
12 changes: 11 additions & 1 deletion xps-gateway/src/rpc/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use jsonrpsee::types::error::ErrorCode;
use async_trait::async_trait;
use ethers::prelude::*;
use ethers::{core::types::Signature, providers::Middleware};
use gateway_types::GrantInstallationResult;
use gateway_types::{GrantInstallationResult, KeyPackageResult};
use jsonrpsee::types::ErrorObjectOwned;
use lib_didethresolver::types::XmtpAttribute;
use rand::{rngs::StdRng, SeedableRng};
Expand Down Expand Up @@ -88,6 +88,16 @@ impl<P: Middleware + 'static> XpsServer for XpsMethods<P> {
async fn wallet_address(&self) -> Result<Address, ErrorObjectOwned> {
Ok(self.wallet.address())
}

async fn fetch_key_packages(&self, did: String) -> Result<KeyPackageResult, ErrorObjectOwned> {
log::debug!("xps_fetchKeyPackages called");
let result = self
.contact_operations
.fetch_key_packages(did)
.await
.map_err(RpcError::from)?;
Ok(result)
}
}

/// Error types for DID Registry JSON-RPC
Expand Down
138 changes: 119 additions & 19 deletions xps-gateway/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ mod integration_util;

use anyhow::Error;

use ethers::signers::LocalWallet;
use ethers::{signers::LocalWallet, signers::Signer};

use lib_didethresolver::{
did_registry::RegistrySignerExt,
types::{DidUrl, KeyEncoding, XmtpAttribute, XmtpKeyPurpose},
Expand All @@ -11,7 +12,7 @@ use xps_gateway::rpc::XpsClient;

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

use integration_util::*;

Expand Down Expand Up @@ -118,25 +119,11 @@ async fn test_grant_installation() -> Result<(), Error> {
#[tokio::test]
async fn test_revoke_installation() -> Result<(), Error> {
with_xps_client(None, |client, context, resolver, anvil| async move {
let wallet: LocalWallet = anvil.keys()[3].clone().into();
let me = get_user(&anvil, 3).await;
let me: LocalWallet = anvil.keys()[3].clone().into();
let name = *b"xmtp/installation/hex ";
let value = b"02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71";
let validity = U256::from(604_800);
let signature = wallet
.sign_attribute(&context.registry, name, value.to_vec(), validity)
.await?;

let attr = context.registry.set_attribute_signed(
me.address(),
signature.v.try_into().unwrap(),
signature.r.into(),
signature.s.into(),
name,
value.into(),
validity,
);
attr.send().await?.await?;
set_attribute(name, value.to_vec(), &me, &context.registry).await?;

let doc = resolver
.resolve_did(me.address(), None)
Expand All @@ -152,7 +139,7 @@ async fn test_revoke_installation() -> Result<(), Error> {
.unwrap()
);

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

Expand Down Expand Up @@ -192,3 +179,116 @@ async fn test_revoke_installation() -> Result<(), Error> {
})
.await
}

#[tokio::test]
async fn test_fetch_key_packages() -> Result<(), Error> {
with_xps_client(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 = b"111111111111111111111111111111111111111111111111111111111111111111";
set_attribute(name, value.to_vec(), &me, &context.registry).await?;

let res = client
.fetch_key_packages(format!("0x{}", hex::encode(me.address())))
.await?;

assert_eq!(res.status, Status::Completed);
assert_eq!(&res.message, "Key packages retrieved");
assert_eq!(
res.key_packages,
vec![
b"000000000000000000000000000000000000000000000000000000000000000000",
b"111111111111111111111111111111111111111111111111111111111111111111"
]
);

Ok(())
})
.await
}

#[tokio::test]
async fn test_fetch_key_packages_revoke() -> Result<(), Error> {
with_xps_client(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 = b"111111111111111111111111111111111111111111111111111111111111111111";
set_attribute(name, value.to_vec(), &me, &context.registry).await?;

client
.revoke_installation(
format!("0x{}", hex::encode(me.address())),
XmtpAttribute {
purpose: XmtpKeyPurpose::Installation,
encoding: KeyEncoding::Hex,
},
value.to_vec(),
me.sign_revoke_attribute(&context.registry, name, value.to_vec())
.await?,
)
.await?;

let res = client
.fetch_key_packages(format!("0x{}", hex::encode(me.address())))
.await?;

assert_eq!(res.status, Status::Completed);
assert_eq!(&res.message, "Key packages retrieved");
assert_eq!(
res.key_packages,
vec![hex::decode(
b"000000000000000000000000000000000000000000000000000000000000000000"
)
.unwrap()]
);

Ok(())
})
.await
}

#[tokio::test]
async fn test_fetch_key_packages_client() -> Result<(), Error> {
with_xps_client(None, |client, context, _, anvil| async move {
let me: LocalWallet = anvil.keys()[3].clone().into();
let attribute = XmtpAttribute {
purpose: XmtpKeyPurpose::Installation,
encoding: KeyEncoding::Hex,
};
let value = b"000000000000000000000000000000000000000000000000000000000000000000";

client
.grant_installation(
format!("0x{}", hex::encode(me.address())),
attribute.clone(),
value.to_vec(),
me.sign_attribute(
&context.registry,
attribute.into(),
value.to_vec(),
U256::from(604_800),
)
.await?,
)
.await?;
let res = client
.fetch_key_packages(format!("0x{}", hex::encode(me.address())))
.await?;

assert_eq!(res.status, Status::Completed);
assert_eq!(&res.message, "Key packages retrieved");
assert_eq!(
res.key_packages,
vec![b"000000000000000000000000000000000000000000000000000000000000000000"]
);

Ok(())
})
.await
}
30 changes: 29 additions & 1 deletion xps-gateway/tests/integration_util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ use ethers::{
middleware::SignerMiddleware,
providers::{Provider, Ws},
signers::{LocalWallet, Signer as _},
types::U256,
utils::AnvilInstance,
};
use futures::future::FutureExt;
use lib_didethresolver::{did_registry::DIDRegistry, Resolver};
use lib_didethresolver::{
did_registry::{DIDRegistry, RegistrySignerExt},
Resolver,
};
use std::{
future::Future,
sync::{Arc, Once},
Expand Down Expand Up @@ -145,3 +149,27 @@ fn init_test_logging() {
.init()
})
}

pub async fn set_attribute(
name: [u8; 32],
value: Vec<u8>,
wallet: &LocalWallet,
registry: &DIDRegistry<GatewaySigner<Provider<Ws>>>,
) -> Result<(), Error> {
let validity = U256::from(604_800);
let signature = wallet
.sign_attribute(registry, name, value.to_vec(), validity)
.await?;

let attr = registry.set_attribute_signed(
wallet.address(),
signature.v.try_into().unwrap(),
signature.r.into(),
signature.s.into(),
name,
value.into(),
validity,
);
attr.send().await?.await?;
Ok(())
}

0 comments on commit 31162f3

Please sign in to comment.