Skip to content

Commit

Permalink
fix bugs w.r.t byte conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
insipx committed Feb 14, 2024
1 parent 2a563d1 commit 8a618d7
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 85 deletions.
7 changes: 5 additions & 2 deletions lib/src/rpc/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ impl<M: Middleware + 'static> DidRegistryServer for DidRegistryMethods<M> {
) -> Result<DidResolutionResult, ErrorObjectOwned> {
log::debug!("did_resolveDid called");

log::trace!("Resolving for key {}", public_key);

// parse the version_id
let parsed_version_id = version_id.map(|str| U64::from(u64::from_str(&str).unwrap()));

Expand All @@ -43,9 +45,10 @@ impl<M: Middleware + 'static> DidRegistryServer for DidRegistryMethods<M> {
H160::from_str(&public_key).map_err(RpcError::from)?,
parsed_version_id,
)
.await?;
.await;
log::debug!("Resolution Result {:?}", resolution_result);

Ok(resolution_result)
Ok(resolution_result?)
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ impl From<Attribute> for [u8; 32] {

// internal function to fill a [u8; 32] with bytes.
// anything over 32 bytes will be cutoff.
fn string_to_bytes32<S: AsRef<str>>(s: S) -> [u8; 32] {
pub fn string_to_bytes32<S: AsRef<str>>(s: S) -> [u8; 32] {
let s = s.as_ref();
let mut attr_bytes: [u8; 32] = [b' '; 32];
let length = std::cmp::min(s.as_bytes().len(), 32);
Expand Down
55 changes: 40 additions & 15 deletions lib/src/types/ethr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,13 @@ impl EthrBuilder {
value: V,
encoding: KeyEncoding,
) -> Result<Option<VerificationMethodProperties>, EthrBuilderError> {
let value = hex::decode(value.as_ref())?;
Ok(match encoding {
log::debug!(
"decoding attribute value {:?} with encoding: {}",
value.as_ref(),

Check warning on line 326 in lib/src/types/ethr.rs

View check run for this annotation

Codecov / codecov/patch

lib/src/types/ethr.rs#L325-L326

Added lines #L325 - L326 were not covered by tests
encoding
);

let enc = match encoding {
KeyEncoding::Hex => Some(VerificationMethodProperties::PublicKeyHex {
public_key_hex: hex::encode(value),
}),
Expand All @@ -332,7 +337,9 @@ impl EthrBuilder {
KeyEncoding::Base58 => Some(VerificationMethodProperties::PublicKeyBase58 {
public_key_base58: bs58::encode(value).into_string(),
}),
})
};
log::debug!("Encoded {:?}", enc);
Ok(enc)
}

/// Adds a delegate to the document
Expand Down Expand Up @@ -483,7 +490,11 @@ pub(crate) mod tests {

let event = DidattributeChangedFilter {
name: *b"did/pub/Secp256k1/veriKey/hex ",
value: b"02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71".into(),
value: hex::decode(
"02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71",
)
.unwrap()
.into(),
..base_attr_changed(identity, None)
};

Expand Down Expand Up @@ -514,7 +525,9 @@ pub(crate) mod tests {
let identity = address("0x7e575682a8e450e33eb0493f9972821ae333cd7f");
let event = DidattributeChangedFilter {
name: *b"did/pub/Ed25519/veriKey/base58 ",
value: b"b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71".into(),
value: hex::decode("b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71")
.unwrap()
.into(),
..base_attr_changed(identity, None)
};

Expand Down Expand Up @@ -545,7 +558,7 @@ pub(crate) mod tests {
let identity = address("0x7e575682a8e450e33eb0493f9972821ae333cd7f");
let event = DidattributeChangedFilter {
name: *b"did/pub/X25519/enc/base64 ",
value: b"302a300506032b656e032100118557777ffb078774371a52b00fed75561dcf975e61c47553e664a617661052".into(),
value: hex::decode("302a300506032b656e032100118557777ffb078774371a52b00fed75561dcf975e61c47553e664a617661052").unwrap().into(),
..base_attr_changed(identity, None)
};

Expand Down Expand Up @@ -606,17 +619,17 @@ pub(crate) mod tests {
let events = vec![
DidattributeChangedFilter {
name: *b"did/pub/Secp256k1/veriKey/hex ",
value: b"02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71".into(),
value: hex::decode("02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71").unwrap().into(),
..base_attr_changed(identity, None)
},
DidattributeChangedFilter {
name: *b"did/pub/Secp256k1/sigAuth/base58",
value: b"b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71".into(),
value: hex::decode("b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71").unwrap().into(),
..base_attr_changed(identity, None)
},
DidattributeChangedFilter {
name: *b"did/pub/X25519/enc/base64 ",
value: b"302a300506032b656e032100118557777ffb078774371a52b00fed75561dcf975e61c47553e664a617661052".into(),
value: hex::decode("302a300506032b656e032100118557777ffb078774371a52b00fed75561dcf975e61c47553e664a617661052").unwrap().into(),
..base_attr_changed(identity, None)
},
DidattributeChangedFilter {
Expand Down Expand Up @@ -664,17 +677,17 @@ pub(crate) mod tests {
let events = vec![
DidattributeChangedFilter {
name: *b"did/pub/Secp256k1/veriKey/hex ",
value: b"02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71".into(),
value: hex::decode("02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71").unwrap().into(),
..base_attr_changed(identity, None)
},
DidattributeChangedFilter {
name: *b"did/pub/Secp256k1/sigAuth/base58",
value: b"b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71".into(),
value: hex::decode("b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71").unwrap().into(),
..base_attr_changed(identity, Some(10))
},
DidattributeChangedFilter {
name: *b"did/pub/X25519/enc/base64 ",
value: b"302a300506032b656e032100118557777ffb078774371a52b00fed75561dcf975e61c47553e664a617661052".into(),
value: hex::decode("302a300506032b656e032100118557777ffb078774371a52b00fed75561dcf975e61c47553e664a617661052").unwrap().into(),
..base_attr_changed(identity, None)
},
DidattributeChangedFilter {
Expand Down Expand Up @@ -870,12 +883,20 @@ pub(crate) mod tests {
let attributes = vec![
DidattributeChangedFilter {
name: *b"did/pub/Secp256k1/veriKey/hex ",
value: b"02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71".into(),
value: hex::decode(
"02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71",
)
.unwrap()
.into(),
..base_attr_changed(identity, None)
},
DidattributeChangedFilter {
name: *b"did/pub/Secp256k1/sigAuth/base58",
value: b"b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71".into(),
value: hex::decode(
"b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71",
)
.unwrap()
.into(),
..base_attr_changed(identity, None)
},
];
Expand Down Expand Up @@ -977,7 +998,11 @@ pub(crate) mod tests {
let identity = address("0x7e575682a8e450e33eb0493f9972821ae333cd7f");
let event = DidattributeChangedFilter {
name: *b"test/random/attribute99999999 ",
value: b"02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71".into(),
value: hex::decode(
"02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71",
)
.unwrap()
.into(),
..base_attr_changed(identity, None)
};

Expand Down
79 changes: 15 additions & 64 deletions lib/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use ethers::{
signers::{LocalWallet, Signer as _},
types::{Address, U256},
};
use integration_util::{validate_document, with_client};
use integration_util::{set_attribute, revoke_attribute, validate_document, with_client};
use regex::Regex;

#[cfg(test)]
Expand All @@ -25,22 +25,9 @@ mod it {
pub async fn test_attributes() -> Result<()> {
with_client(None, |client, registry, signer, _| async move {
let me = signer.address();
let did = registry.set_attribute(
me,
*b"did/pub/Secp256k1/veriKey/hex ",
b"02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71".into(),
U256::from(604_800),
);
did.send().await?.await?;

let did = registry.set_attribute(
me,
*b"did/pub/Ed25519/veriKey/base64 ",
b"302a300506032b656e032100118557777ffb078774371a52b00fed75561dcf975e61c47553e664a617661052".into(),
U256::from(604_800),
);
did.send().await?.await?;

set_attribute(&registry, me, "did/pub/Secp256k1/veriKey/hex", "02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71", 604_800).await?;
set_attribute(&registry, me, "did/pub/Ed25519/veriKey/base64", "302a300506032b656e032100118557777ffb078774371a52b00fed75561dcf975e61c47553e664a617661052", 604_800).await?;

let resolution_response = client.resolve_did(hex::encode(me), None).await?;
validate_document(&resolution_response.document).await;
assert_eq!(
Expand All @@ -58,8 +45,7 @@ mod it {
assert_eq!(
resolution_response.document.verification_method[1].verification_properties,
Some(VerificationMethodProperties::PublicKeyHex {
public_key_hex:
"02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71".to_string()
public_key_hex: "02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71".to_string()
})
);

Expand Down Expand Up @@ -103,21 +89,8 @@ mod it {
pub async fn test_attributes_versions() -> Result<()> {
with_client(None, |client, registry, signer, _| async move {
let me = signer.address();
let did = registry.set_attribute(
me,
*b"did/pub/Secp256k1/veriKey/hex ",
b"02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71".into(),
U256::from(604_800),
);
did.send().await?.await?;

let did = registry.set_attribute(
me,
*b"did/pub/Ed25519/veriKey/base64 ",
b"302a300506032b656e032100118557777ffb078774371a52b00fed75561dcf975e61c47553e664a617661052".into(),
U256::from(604_800),
);
did.send().await?.await?;
set_attribute(&registry, me,"did/pub/Secp256k1/veriKey/hex", "02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71", 604_800).await?;
set_attribute(&registry, me, "did/pub/Ed25519/veriKey/base64", "302a300506032b656e032100118557777ffb078774371a52b00fed75561dcf975e61c47553e664a617661052", 604_800).await?;

let resolution_response = client.resolve_did(hex::encode(me), Some::<String>("2".to_string())).await?;
validate_document(&resolution_response.document).await;
Expand Down Expand Up @@ -225,20 +198,9 @@ mod it {
pub async fn test_attribute_revocation() -> Result<()> {
with_client(None, |client, registry, signer, _| async move {
let me = signer.address();
let did = registry.set_attribute(
me,
*b"did/pub/Secp256k1/veriKey/hex ",
b"02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71".into(),
U256::from(604_800),
);
did.send().await?.await?;

let did = registry.revoke_attribute(
me,
*b"did/pub/Secp256k1/veriKey/hex ",
b"02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71".into(),
);
did.send().await?.await?;

set_attribute(&registry, me, "did/pub/Secp256k1/veriKey/hex", "02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71", 604_800).await?;
revoke_attribute(&registry, me, "did/pub/Secp256k1/veriKey/hex", "02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71").await?;

let document = client.resolve_did(hex::encode(me), None).await?.document;
validate_document(&document).await;
Expand Down Expand Up @@ -322,14 +284,8 @@ mod it {
pub async fn test_xmtp_revocation() -> Result<()> {
with_client(None, |client, registry, signer, _| async move {
let me = signer.address();
let attribute_name = *b"xmtp/installation/hex ";
let did = registry.set_attribute(
me,
attribute_name,
b"02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71".into(),
U256::from(604_800),
);
did.send().await?.await?;
let attribute_name = "xmtp/installation/hex ";
set_attribute(&registry, me, attribute_name, "02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71", 604_800).await?;

let document = client.resolve_did(hex::encode(me), None).await?.document;
let regexr = format!(
Expand All @@ -338,14 +294,9 @@ mod it {
);
let test = Regex::new(&regexr).unwrap();
assert!(test.is_match(&document.verification_method[1].id.to_string()));

let did = registry.revoke_attribute(
me,
attribute_name,
b"02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71".into(),
);
did.send().await?.await?;


revoke_attribute(&registry, me, attribute_name, "02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71").await?;

let document = client.resolve_did(hex::encode(me), None).await?.document;
validate_document(&document).await;
assert_eq!(
Expand Down
37 changes: 34 additions & 3 deletions lib/tests/integration_util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Shared setup code for integration tests
use ethers::providers::Middleware;
use ethers::types::{Bytes, H160};
use hex::FromHex;
use std::sync::{Arc, Once};
use std::{future::Future, time::Duration};
use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Registry};
Expand All @@ -11,16 +13,16 @@ use ethers::{
middleware::SignerMiddleware,
providers::{Provider, Ws},
signers::{LocalWallet, Signer as _},
types::Address,
types::{Address, U256},
};
use futures::future::FutureExt;
use jsonrpsee::{
server::Server,
ws_client::{WsClient, WsClientBuilder},
};
use lib_didethresolver::{
did_registry::DIDRegistry, rpc::DidRegistryMethods, types::DidDocument, DidRegistryServer,
Resolver,
did_registry::DIDRegistry, rpc::DidRegistryMethods, types::string_to_bytes32,
types::DidDocument, DidRegistryServer, Resolver,
};
use serde::{Deserialize, Serialize};
use tokio::time::timeout as timeout_tokio;
Expand Down Expand Up @@ -164,3 +166,32 @@ pub async fn validate_document(document: &DidDocument) {
}
assert!(response.valid);
}

pub async fn set_attribute(
registry: &DIDRegistry<Signer>,
did: H160,
key: &str,
value: &str,
validity: u64,
) -> Result<()> {
let attribute = registry.set_attribute(
did,
string_to_bytes32(key),
Bytes::from_hex(value)?,
U256::from(validity),
);

attribute.send().await?.await?;
Ok(())
}

pub async fn revoke_attribute(
registry: &DIDRegistry<Signer>,
did: H160,
key: &str,
value: &str,
) -> Result<()> {
let attribute = registry.revoke_attribute(did, string_to_bytes32(key), Bytes::from_hex(value)?);
attribute.send().await?.await?;
Ok(())
}

0 comments on commit 8a618d7

Please sign in to comment.