Skip to content

Commit

Permalink
fix linked vp example
Browse files Browse the repository at this point in the history
  • Loading branch information
UMR1352 committed Aug 30, 2024
1 parent 5a2dd53 commit e220db2
Showing 1 changed file with 45 additions and 8 deletions.
53 changes: 45 additions & 8 deletions examples/1_advanced/11_linked_verifiable_presentation.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
// Copyright 2020-2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use anyhow::Context;
use examples::create_did;
use examples::random_stronghold_path;
use examples::MemStorage;
use examples::API_ENDPOINT;
use identity_eddsa_verifier::EdDSAJwsVerifier;
use identity_iota::core::FromJson;
use identity_iota::core::Object;
use identity_iota::core::OrderedSet;
use identity_iota::core::Url;
use identity_iota::credential::CompoundJwtPresentationValidationError;
use identity_iota::credential::CredentialBuilder;
use identity_iota::credential::DecodedJwtPresentation;
use identity_iota::credential::Jwt;
use identity_iota::credential::JwtPresentationOptions;
use identity_iota::credential::JwtPresentationValidationOptions;
use identity_iota::credential::JwtPresentationValidator;
use identity_iota::credential::JwtPresentationValidatorUtils;
use identity_iota::credential::LinkedVerifiablePresentationService;
use identity_iota::credential::PresentationBuilder;
use identity_iota::credential::Subject;
use identity_iota::did::CoreDID;
use identity_iota::did::DIDUrl;
use identity_iota::did::DID;
Expand All @@ -25,7 +31,9 @@ use identity_iota::iota::IotaDID;
use identity_iota::iota::IotaDocument;
use identity_iota::iota::IotaIdentityClientExt;
use identity_iota::resolver::Resolver;
use identity_iota::storage::JwkDocumentExt;
use identity_iota::storage::JwkMemStore;
use identity_iota::storage::JwsSignatureOptions;
use identity_iota::storage::KeyIdMemstore;
use iota_sdk::client::secret::stronghold::StrongholdSecretManager;
use iota_sdk::client::secret::SecretManager;
Expand All @@ -36,8 +44,6 @@ use iota_sdk::types::block::output::AliasOutput;
use iota_sdk::types::block::output::AliasOutputBuilder;
use iota_sdk::types::block::output::RentStructure;

const VP_JWT: &str = "eyJraWQiOiJkaWQ6aW90YTpybXM6MHg2Y2I5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VXlNR014TXpoaE1UUTFNVFV6TURZNFpURXdPRE5oTUdFeVlUVXdZalUyWkRJMU1HSTNZalV6WXpZd1ltRXpPVEk0TkdKa01XUmpOelF4SWl3aWRtTWlPbnNpUUdOdmJuUmxlSFFpT2lKb2RIUndjem92TDNkM2R5NTNNeTV2Y21jdk1qQXhPQzlqY21Wa1pXNTBhV0ZzY3k5Mk1TSXNJblI1Y0dVaU9sc2lWbVZ5YVdacFlXSnNaVU55WldSbGJuUnBZV3dpTENKVmJtbDJaWEp6YVhSNVJHVm5jbVZsUTNKbFpHVnVkR2xoYkNKZExDSmpjbVZrWlc1MGFXRnNVM1ZpYW1WamRDSTZleUpqWlhKMGFXWnBZMkYwWlNJNmV5SjBlWEJsSWpvaVFXTmpjbVZrYVhSaGRHbHZiaUlzSW14bGRtVnNJam96ZlgxOWZRLm5ldEpyMkZEaWlPYmRFVWVaaTkwcW90dG9BcFlYLVhacmxMQ0ZwOTA2RHZCMlJUbEw2WDVWb3JhYy1reFpNUThwMkpIUEZMbUk5ZzM5c3NuSG1MWkNnIl19fQ.vcpp_imMMv6inSOy9L-IsvF_WPfEYsuTpcPfEAHQfrBJ_O_zhZxZ0pzcbbvwJqh-wcmMgas0DuR_0NGcZK8CAw";

#[tokio::main]
async fn main() -> anyhow::Result<()> {
// Create a new client to interact with the IOTA ledger.
Expand All @@ -57,7 +63,7 @@ async fn main() -> anyhow::Result<()> {

// Create a DID for the entity that will be the holder of the Verifiable Presentation.
let storage: MemStorage = MemStorage::new(JwkMemStore::new(), KeyIdMemstore::new());
let (_, mut did_document, _): (Address, IotaDocument, String) =
let (_, mut did_document, fragment): (Address, IotaDocument, String) =
create_did(&client, &mut secret_manager, &storage).await?;
let did: IotaDID = did_document.id().clone();

Expand Down Expand Up @@ -110,8 +116,8 @@ async fn main() -> anyhow::Result<()> {
.verifiable_presentation_urls();

// Fetch the verifiable presentation from the URL (for example using `reqwest`).
// But since the URLs are not actually online in this example, we will simply create an example JWT.
let presentation_jwt: Jwt = example_vp();
// But since the URLs do not point to actual online resource, we will simply create an example JWT.
let presentation_jwt: Jwt = make_vp_jwt(&did_document, &storage, &fragment).await?;

// Resolve the holder's document.
let holder_did: CoreDID = JwtPresentationValidatorUtils::extract_holder(&presentation_jwt)?;
Expand Down Expand Up @@ -152,7 +158,38 @@ async fn publish_document(
Ok(client.publish_did_output(&secret_manager, alias_output).await?)
}

/// A static VP, without nonce and expiry (created using basic example `6_create_vp.rs`).
fn example_vp() -> Jwt {
Jwt::from(VP_JWT.to_string())
async fn make_vp_jwt(did_doc: &IotaDocument, storage: &MemStorage, fragment: &str) -> anyhow::Result<Jwt> {
// first we create a credential encoding it as jwt
let credential = CredentialBuilder::new(Object::default())
.id(Url::parse("https://example.edu/credentials/3732")?)
.issuer(Url::parse(did_doc.id().as_str())?)
.type_("UniversityDegreeCredential")
.subject(Subject::from_json_value(serde_json::json!({
"id": did_doc.id().as_str(),
"name": "Alice",
"degree": {
"type": "BachelorDegree",
"name": "Bachelor of Science and Arts",
},
"GPA": "4.0",
}))?)
.build()?;
let credential = did_doc
.create_credential_jwt(&credential, storage, fragment, &JwsSignatureOptions::default(), None)
.await?;
// then we create a presentation including the just created JWT encoded credential.
let presentation = PresentationBuilder::new(Url::parse(did_doc.id().as_str())?, Object::default())
.credential(credential)
.build()?;
// we encode the presentation as JWT
did_doc
.create_presentation_jwt(
&presentation,
storage,
fragment,
&JwsSignatureOptions::default(),
&JwtPresentationOptions::default(),
)
.await
.context("jwt presentation failed")
}

0 comments on commit e220db2

Please sign in to comment.