Skip to content

Commit e220db2

Browse files
committed
fix linked vp example
1 parent 5a2dd53 commit e220db2

File tree

1 file changed

+45
-8
lines changed

1 file changed

+45
-8
lines changed

examples/1_advanced/11_linked_verifiable_presentation.rs

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
// Copyright 2020-2024 IOTA Stiftung
22
// SPDX-License-Identifier: Apache-2.0
33

4+
use anyhow::Context;
45
use examples::create_did;
56
use examples::random_stronghold_path;
67
use examples::MemStorage;
78
use examples::API_ENDPOINT;
89
use identity_eddsa_verifier::EdDSAJwsVerifier;
10+
use identity_iota::core::FromJson;
911
use identity_iota::core::Object;
1012
use identity_iota::core::OrderedSet;
1113
use identity_iota::core::Url;
1214
use identity_iota::credential::CompoundJwtPresentationValidationError;
15+
use identity_iota::credential::CredentialBuilder;
1316
use identity_iota::credential::DecodedJwtPresentation;
1417
use identity_iota::credential::Jwt;
18+
use identity_iota::credential::JwtPresentationOptions;
1519
use identity_iota::credential::JwtPresentationValidationOptions;
1620
use identity_iota::credential::JwtPresentationValidator;
1721
use identity_iota::credential::JwtPresentationValidatorUtils;
1822
use identity_iota::credential::LinkedVerifiablePresentationService;
23+
use identity_iota::credential::PresentationBuilder;
24+
use identity_iota::credential::Subject;
1925
use identity_iota::did::CoreDID;
2026
use identity_iota::did::DIDUrl;
2127
use identity_iota::did::DID;
@@ -25,7 +31,9 @@ use identity_iota::iota::IotaDID;
2531
use identity_iota::iota::IotaDocument;
2632
use identity_iota::iota::IotaIdentityClientExt;
2733
use identity_iota::resolver::Resolver;
34+
use identity_iota::storage::JwkDocumentExt;
2835
use identity_iota::storage::JwkMemStore;
36+
use identity_iota::storage::JwsSignatureOptions;
2937
use identity_iota::storage::KeyIdMemstore;
3038
use iota_sdk::client::secret::stronghold::StrongholdSecretManager;
3139
use iota_sdk::client::secret::SecretManager;
@@ -36,8 +44,6 @@ use iota_sdk::types::block::output::AliasOutput;
3644
use iota_sdk::types::block::output::AliasOut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ldSMUwyTnlaV1JsYm5ScFlXeHpMek0zTXpJaUxDSnpkV0lpT2lKa2FXUTZhVzkwWVRweWJYTTZNSGcyWTJJNU1XVXlNR014TXpoaE1UUTFNVFV6TURZNFpURXdPRE5oTUdFeVlUVXdZalUyWkRJMU1HSTNZalV6WXpZd1ltRXpPVEk0TkdKa01XUmpOelF4SWl3aWRtTWlPbnNpUUdOdmJuUmxlSFFpT2lKb2RIUndjem92TDNkM2R5NTNNeTV2Y21jdk1qQXhPQzlqY21Wa1pXNTBhV0ZzY3k5Mk1TSXNJblI1Y0dVaU9sc2lWbVZ5YVdacFlXSnNaVU55WldSbGJuUnBZV3dpTENKVmJtbDJaWEp6YVhSNVJHVm5jbVZsUTNKbFpHVnVkR2xoYkNKZExDSmpjbVZrWlc1MGFXRnNVM1ZpYW1WamRDSTZleUpqWlhKMGFXWnBZMkYwWlNJNmV5SjBlWEJsSWpvaVFXTmpjbVZrYVhSaGRHbHZiaUlzSW14bGRtVnNJam96ZlgxOWZRLm5ldEpyMkZEaWlPYmRFVWVaaTkwcW90dG9BcFlYLVhacmxMQ0ZwOTA2RHZCMlJUbEw2WDVWb3JhYy1reFpNUThwMkpIUEZMbUk5ZzM5c3NuSG1MWkNnIl19fQ.vcpp_imMMv6inSOy9L-IsvF_WPfEYsuTpcPfEAHQfrBJ_O_zhZxZ0pzcbbvwJqh-wcmMgas0DuR_0NGcZK8CAw";
40-
4147
#[tokio::main]
4248
async fn main() -> anyhow::Result<()> {
4349
// Create a new client to interact with the IOTA ledger.
@@ -57,7 +63,7 @@ async fn main() -> anyhow::Result<()> {
5763

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

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

112118
// Fetch the verifiable presentation from the URL (for example using `reqwest`).
113-
// But since the URLs are not actually online in this example, we will simply create an example JWT.
114-
let presentation_jwt: Jwt = example_vp();
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?;
115121

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

155-
/// A static VP, without nonce and expiry (created using basic example `6_create_vp.rs`).
156-
fn example_vp() -> Jwt {
157-
Jwt::from(VP_JWT.to_string())
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")
158195
}

0 commit comments

Comments
 (0)