Skip to content

Commit

Permalink
refactor: Use typed-builder instead of custom one for `Verification…
Browse files Browse the repository at this point in the history
…Method` (#1162)
  • Loading branch information
Patrik-Stas authored Mar 21, 2024
1 parent 05c92f2 commit 413b183
Show file tree
Hide file tree
Showing 15 changed files with 179 additions and 657 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.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use did_doc::schema::{
did_doc::DidDocument,
service::{service_key_kind::ServiceKeyKind, typed::didcommv1::ServiceDidCommV1, Service},
types::uri::Uri,
verification_method::{VerificationMethod, VerificationMethodType},
verification_method::{PublicKeyField, VerificationMethod, VerificationMethodType},
};
use did_key::DidKey;
use did_parser_nom::{Did, DidUrl};
Expand Down Expand Up @@ -99,13 +99,14 @@ fn did_doc_from_keys(
service: Service,
) -> Result<DidDocument, AriesVcxError> {
let vm_ka_id = DidUrl::from_fragment(key_enc.short_prefixless_fingerprint())?;
let vm_ka = VerificationMethod::builder(
vm_ka_id,
did.clone(),
VerificationMethodType::Ed25519VerificationKey2020,
)
.add_public_key_base58(key_enc.base58())
.build();
let vm_ka = VerificationMethod::builder()
.id(vm_ka_id)
.controller(did.clone())
.verification_method_type(VerificationMethodType::Ed25519VerificationKey2020)
.public_key(PublicKeyField::Base58 {
public_key_base58: key_enc.base58(),
})
.build();
Ok(DidDocument::builder(did)
.add_service(service)
.add_key_agreement(vm_ka)
Expand Down
89 changes: 44 additions & 45 deletions did_core/did_doc/src/schema/did_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,21 +280,24 @@ mod tests {
let controller = Did::parse("did:example:controller".to_string()).unwrap();

let vm1_id = DidUrl::parse("did:example:vm1#vm1".to_string()).unwrap();
let verification_method = VerificationMethod::builder(
vm1_id.clone(),
Did::parse("did:example:vm1".to_string()).unwrap(),
VerificationMethodType::Ed25519VerificationKey2018,
)
.add_public_key_base58("H3C2AVvLMv6gmMNam3uVAjZpfkcJCwDwnZn6z3wXmqPV".to_string())
.build();
let verification_method = VerificationMethod::builder()
.id(vm1_id.clone())
.controller(Did::parse("did:example:vm1".to_string()).unwrap())
.verification_method_type(VerificationMethodType::Ed25519VerificationKey2018)
.public_key(PublicKeyField::Base58 {
public_key_base58: "H3C2AVvLMv6gmMNam3uVAjZpfkcJCwDwnZn6z3wXmqPV".to_string(),
})
.build();

let authentication_reference = DidUrl::parse("did:example:authref".to_string()).unwrap();
let assertion_method = VerificationMethod::builder(
DidUrl::parse("did:example:am1".to_string()).unwrap(),
Did::parse("did:example:am2".to_string()).unwrap(),
VerificationMethodType::Ed25519VerificationKey2018,
)
.add_public_key_base58("H3C2AVvLMv6gmMNam3uVAjZpfkcJCwDwnZn6z3wXmqPV".to_string())
.build();
let assertion_method = VerificationMethod::builder()
.id(DidUrl::parse("did:example:am1".to_string()).unwrap())
.controller(Did::parse("did:example:am2".to_string()).unwrap())
.verification_method_type(VerificationMethodType::Ed25519VerificationKey2018)
.public_key(PublicKeyField::Base58 {
public_key_base58: "H3C2AVvLMv6gmMNam3uVAjZpfkcJCwDwnZn6z3wXmqPV".to_string(),
})
.build();

let service_id = Uri::new("did:example:123456789abcdefghi;service-1").unwrap();
let service_endpoint = "https://example.com/service";
Expand Down Expand Up @@ -473,39 +476,34 @@ mod tests {
.unwrap();

let vm1_id = DidUrl::parse("#g1".to_string()).unwrap();
let vm1 = VerificationMethod::builder(
vm1_id,
controller.clone(),
VerificationMethodType::JsonWebKey2020,
)
.add_public_key_jwk(
JsonWebKey::from_str(
r#"{
let vm1 = VerificationMethod::builder()
.id(vm1_id)
.controller(controller.clone())
.verification_method_type(VerificationMethodType::JsonWebKey2020)
.public_key(PublicKeyField::Jwk {
public_key_jwk: JsonWebKey::from_str(
r#"{
"kty": "EC",
"crv": "BLS12381_G1",
"x": "hxF12gtsn9ju4-kJq2-nUjZQKVVWpcBAYX5VHnUZMDilClZsGuOaDjlXS8pFE1GG"
}"#,
)
.unwrap(),
)
.build();
}"#,
)
.unwrap(),
})
.build();

let vm2_id = DidUrl::parse("#g2".to_string()).unwrap();
let vm2 = VerificationMethod::builder(
vm2_id,
controller.clone(),
VerificationMethodType::JsonWebKey2020,
)
.add_public_key_jwk(
JsonWebKey::from_str(
r#"{
let vm2 = VerificationMethod::builder()
.id(vm2_id)
.controller(controller.clone())
.verification_method_type(VerificationMethodType::JsonWebKey2020)
.public_key(PublicKeyField::Jwk { public_key_jwk: JsonWebKey::from_str(
r#"{
"kty": "EC",
"crv": "BLS12381_G2",
"x": "l4MeBsn_OGa2OEDtHeHdq0TBC8sYh6QwoI7QsNtZk9oAru1OnGClaAPlMbvvs73EABDB6GjjzybbOHarkBmP6pon8H1VuMna0nkEYihZi8OodgdbwReDiDvWzZuXXMl-"
}"#,
)
.unwrap(),
)
).unwrap()})
.build();

assert_eq!(did_doc.verification_method().first().unwrap().clone(), vm1);
Expand Down Expand Up @@ -550,13 +548,14 @@ mod tests {
"did:web:did-actor-alice#zC8GybikEfyNaausDA4mkT4egP7SNLx2T1d1kujLQbcP6h".to_string(),
)
.unwrap();
let ka1 = VerificationMethod::builder(
ka1_id,
controller,
VerificationMethodType::X25519KeyAgreementKey2019,
)
.add_public_key_base58("CaSHXEvLKS6SfN9aBfkVGBpp15jSnaHazqHgLHp8KZ3Y".to_string())
.build();
let ka1 = VerificationMethod::builder()
.id(ka1_id)
.controller(controller.clone())
.verification_method_type(VerificationMethodType::X25519KeyAgreementKey2019)
.public_key(PublicKeyField::Base58 {
public_key_base58: "CaSHXEvLKS6SfN9aBfkVGBpp15jSnaHazqHgLHp8KZ3Y".to_string(),
})
.build();

assert_eq!(
did_doc.key_agreement(),
Expand Down
Loading

0 comments on commit 413b183

Please sign in to comment.