|
| 1 | +// Copyright 2020-2024 IOTA Stiftung |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +use anyhow::Context; |
| 5 | +use examples::create_did; |
| 6 | +use examples::random_stronghold_path; |
| 7 | +use examples::MemStorage; |
| 8 | +use examples::API_ENDPOINT; |
| 9 | +use identity_eddsa_verifier::EdDSAJwsVerifier; |
| 10 | +use identity_iota::core::FromJson; |
| 11 | +use identity_iota::core::Object; |
| 12 | +use identity_iota::core::OrderedSet; |
| 13 | +use identity_iota::core::Url; |
| 14 | +use identity_iota::credential::CompoundJwtPresentationValidationError; |
| 15 | +use identity_iota::credential::CredentialBuilder; |
| 16 | +use identity_iota::credential::DecodedJwtPresentation; |
| 17 | +use identity_iota::credential::Jwt; |
| 18 | +use identity_iota::credential::JwtPresentationOptions; |
| 19 | +use identity_iota::credential::JwtPresentationValidationOptions; |
| 20 | +use identity_iota::credential::JwtPresentationValidator; |
| 21 | +use identity_iota::credential::JwtPresentationValidatorUtils; |
| 22 | +use identity_iota::credential::LinkedVerifiablePresentationService; |
| 23 | +use identity_iota::credential::PresentationBuilder; |
| 24 | +use identity_iota::credential::Subject; |
| 25 | +use identity_iota::did::CoreDID; |
| 26 | +use identity_iota::did::DIDUrl; |
| 27 | +use identity_iota::did::DID; |
| 28 | +use identity_iota::document::verifiable::JwsVerificationOptions; |
| 29 | +use identity_iota::iota::IotaClientExt; |
| 30 | +use identity_iota::iota::IotaDID; |
| 31 | +use identity_iota::iota::IotaDocument; |
| 32 | +use identity_iota::iota::IotaIdentityClientExt; |
| 33 | +use identity_iota::resolver::Resolver; |
| 34 | +use identity_iota::storage::JwkDocumentExt; |
| 35 | +use identity_iota::storage::JwkMemStore; |
| 36 | +use identity_iota::storage::JwsSignatureOptions; |
| 37 | +use identity_iota::storage::KeyIdMemstore; |
| 38 | +use iota_sdk::client::secret::stronghold::StrongholdSecretManager; |
| 39 | +use iota_sdk::client::secret::SecretManager; |
| 40 | +use iota_sdk::client::Client; |
| 41 | +use iota_sdk::client::Password; |
| 42 | +use iota_sdk::types::block::address::Address; |
| 43 | +use iota_sdk::types::block::output::AliasOutput; |
| 44 | +use iota_sdk::types::block::output::AliasOutputBuilder; |
| 45 | +use iota_sdk::types::block::output::RentStructure; |
| 46 | + |
| 47 | +#[tokio::main] |
| 48 | +async fn main() -> anyhow::Result<()> { |
| 49 | + // Create a new client to interact with the IOTA ledger. |
| 50 | + let client: Client = Client::builder() |
| 51 | + .with_primary_node(API_ENDPOINT, None)? |
| 52 | + .finish() |
| 53 | + .await?; |
| 54 | + let stronghold_path = random_stronghold_path(); |
| 55 | + |
| 56 | + println!("Using stronghold path: {stronghold_path:?}"); |
| 57 | + // Create a new secret manager backed by a Stronghold. |
| 58 | + let mut secret_manager: SecretManager = SecretManager::Stronghold( |
| 59 | + StrongholdSecretManager::builder() |
| 60 | + .password(Password::from("secure_password".to_owned())) |
| 61 | + .build(stronghold_path)?, |
| 62 | + ); |
| 63 | + |
| 64 | + // Create a DID for the entity that will be the holder of the Verifiable Presentation. |
| 65 | + let storage: MemStorage = MemStorage::new(JwkMemStore::new(), KeyIdMemstore::new()); |
| 66 | + let (_, mut did_document, fragment): (Address, IotaDocument, String) = |
| 67 | + create_did(&client, &mut secret_manager, &storage).await?; |
| 68 | + let did: IotaDID = did_document.id().clone(); |
| 69 | + |
| 70 | + // ===================================================== |
| 71 | + // Create Linked Verifiable Presentation service |
| 72 | + // ===================================================== |
| 73 | + |
| 74 | + // The DID should link to the following VPs. |
| 75 | + let verifiable_presentation_url_1: Url = Url::parse("https://foo.example.com/verifiable-presentation.jwt")?; |
| 76 | + let verifiable_presentation_url_2: Url = Url::parse("https://bar.example.com/verifiable-presentation.jsonld")?; |
| 77 | + |
| 78 | + let mut verifiable_presentation_urls: OrderedSet<Url> = OrderedSet::new(); |
| 79 | + verifiable_presentation_urls.append(verifiable_presentation_url_1.clone()); |
| 80 | + verifiable_presentation_urls.append(verifiable_presentation_url_2.clone()); |
| 81 | + |
| 82 | + // Create a Linked Verifiable Presentation Service to enable the discovery of the linked VPs through the DID Document. |
| 83 | + // This is optional since it is not a hard requirement by the specs. |
| 84 | + let service_url: DIDUrl = did.clone().join("#linked-vp")?; |
| 85 | + let linked_verifiable_presentation_service = |
| 86 | + LinkedVerifiablePresentationService::new(service_url, verifiable_presentation_urls, Object::new())?; |
| 87 | + did_document.insert_service(linked_verifiable_presentation_service.into())?; |
| 88 | + let updated_did_document: IotaDocument = publish_document(client.clone(), secret_manager, did_document).await?; |
| 89 | + |
| 90 | + println!("DID document with linked verifiable presentation service: {updated_did_document:#}"); |
| 91 | + |
| 92 | + // ===================================================== |
| 93 | + // Verification |
| 94 | + // ===================================================== |
| 95 | + |
| 96 | + // Init a resolver for resolving DID Documents. |
| 97 | + let mut resolver: Resolver<IotaDocument> = Resolver::new(); |
| 98 | + resolver.attach_iota_handler(client.clone()); |
| 99 | + |
| 100 | + // Resolve the DID Document of the DID that issued the credential. |
| 101 | + let did_document: IotaDocument = resolver.resolve(&did).await?; |
| 102 | + |
| 103 | + // Get the Linked Verifiable Presentation Services from the DID Document. |
| 104 | + let linked_verifiable_presentation_services: Vec<LinkedVerifiablePresentationService> = did_document |
| 105 | + .service() |
| 106 | + .iter() |
| 107 | + .cloned() |
| 108 | + .filter_map(|service| LinkedVerifiablePresentationService::try_from(service).ok()) |
| 109 | + .collect(); |
| 110 | + assert_eq!(linked_verifiable_presentation_services.len(), 1); |
| 111 | + |
| 112 | + // Get the VPs included in the service. |
| 113 | + let _verifiable_presentation_urls: &[Url] = linked_verifiable_presentation_services |
| 114 | + .first() |
| 115 | + .ok_or_else(|| anyhow::anyhow!("expected verifiable presentation urls"))? |
| 116 | + .verifiable_presentation_urls(); |
| 117 | + |
| 118 | + // Fetch the verifiable presentation from the URL (for example using `reqwest`). |
| 119 | + // But since the URLs do not point to actual online resource, we will simply create an example JWT. |
| 120 | + let presentation_jwt: Jwt = make_vp_jwt(&did_document, &storage, &fragment).await?; |
| 121 | + |
| 122 | + // Resolve the holder's document. |
| 123 | + let holder_did: CoreDID = JwtPresentationValidatorUtils::extract_holder(&presentation_jwt)?; |
| 124 | + let holder: IotaDocument = resolver.resolve(&holder_did).await?; |
| 125 | + |
| 126 | + // Validate linked presentation. Note that this doesn't validate the included credentials. |
| 127 | + let presentation_verifier_options: JwsVerificationOptions = JwsVerificationOptions::default(); |
| 128 | + let presentation_validation_options = |
| 129 | + JwtPresentationValidationOptions::default().presentation_verifier_options(presentation_verifier_options); |
| 130 | + let validation_result: Result<DecodedJwtPresentation<Jwt>, CompoundJwtPresentationValidationError> = |
| 131 | + JwtPresentationValidator::with_signature_verifier(EdDSAJwsVerifier::default()).validate( |
| 132 | + &presentation_jwt, |
| 133 | + &holder, |
| 134 | + &presentation_validation_options, |
| 135 | + ); |
| 136 | + |
| 137 | + assert!(validation_result.is_ok()); |
| 138 | + |
| 139 | + Ok(()) |
| 140 | +} |
| 141 | + |
| 142 | +async fn publish_document( |
| 143 | + client: Client, |
| 144 | + secret_manager: SecretManager, |
| 145 | + document: IotaDocument, |
| 146 | +) -> anyhow::Result<IotaDocument> { |
| 147 | + // Resolve the latest output and update it with the given document. |
| 148 | + let alias_output: AliasOutput = client.update_did_output(document.clone()).await?; |
| 149 | + |
| 150 | + // Because the size of the DID document increased, we have to increase the allocated storage deposit. |
| 151 | + // This increases the deposit amount to the new minimum. |
| 152 | + let rent_structure: RentStructure = client.get_rent_structure().await?; |
| 153 | + let alias_output: AliasOutput = AliasOutputBuilder::from(&alias_output) |
| 154 | + .with_minimum_storage_deposit(rent_structure) |
| 155 | + .finish()?; |
| 156 | + |
| 157 | + // Publish the updated Alias Output. |
| 158 | + Ok(client.publish_did_output(&secret_manager, alias_output).await?) |
| 159 | +} |
| 160 | + |
| 161 | +async fn make_vp_jwt(did_doc: &IotaDocument, storage: &MemStorage, fragment: &str) -> anyhow::Result<Jwt> { |
| 162 | + // first we create a credential encoding it as jwt |
| 163 | + let credential = CredentialBuilder::new(Object::default()) |
| 164 | + .id(Url::parse("https://example.edu/credentials/3732")?) |
| 165 | + .issuer(Url::parse(did_doc.id().as_str())?) |
| 166 | + .type_("UniversityDegreeCredential") |
| 167 | + .subject(Subject::from_json_value(serde_json::json!({ |
| 168 | + "id": did_doc.id().as_str(), |
| 169 | + "name": "Alice", |
| 170 | + "degree": { |
| 171 | + "type": "BachelorDegree", |
| 172 | + "name": "Bachelor of Science and Arts", |
| 173 | + }, |
| 174 | + "GPA": "4.0", |
| 175 | + }))?) |
| 176 | + .build()?; |
| 177 | + let credential = did_doc |
| 178 | + .create_credential_jwt(&credential, storage, fragment, &JwsSignatureOptions::default(), None) |
| 179 | + .await?; |
| 180 | + // then we create a presentation including the just created JWT encoded credential. |
| 181 | + let presentation = PresentationBuilder::new(Url::parse(did_doc.id().as_str())?, Object::default()) |
| 182 | + .credential(credential) |
| 183 | + .build()?; |
| 184 | + // we encode the presentation as JWT |
| 185 | + did_doc |
| 186 | + .create_presentation_jwt( |
| 187 | + &presentation, |
| 188 | + storage, |
| 189 | + fragment, |
| 190 | + &JwsSignatureOptions::default(), |
| 191 | + &JwtPresentationOptions::default(), |
| 192 | + ) |
| 193 | + .await |
| 194 | + .context("jwt presentation failed") |
| 195 | +} |
0 commit comments