Skip to content

Commit ca3e2a8

Browse files
authored
fix: prevent 'invalid Uri' error (#689)
* fix: prevent 'invalid Uri' error when creating verificationMethod for proofDraft * test: add test for 'invalid Uri' error when creating verificationMethod for proofDraft * fix: add variable to compose controller and keyId
1 parent a3f4879 commit ca3e2a8

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

core/identity-hub-core/src/main/java/org/eclipse/edc/identityhub/core/services/verifiablepresentation/generators/LdpPresentationGenerator.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,12 @@ private JsonArray toJsonArray(List<VerifiableCredentialContainer> credentials) {
159159
}
160160

161161
private JsonObject signPresentation(JsonObject presentationObject, SignatureSuite suite, String suiteIdentifier, PrivateKey pk, String publicKeyId, String controller) {
162-
var keyIdUri = URI.create(publicKeyId);
162+
var composedKeyId = publicKeyId;
163+
if (!publicKeyId.startsWith(controller)) {
164+
composedKeyId = controller + "#" + publicKeyId;
165+
}
166+
167+
var keyIdUri = URI.create(composedKeyId);
163168
var controllerUri = URI.create(controller);
164169
var verificationMethodType = URI.create(suiteIdentifier);
165170

@@ -169,7 +174,7 @@ private JsonObject signPresentation(JsonObject presentationObject, SignatureSuit
169174

170175
var proofDraft = Jws2020ProofDraft.Builder.newInstance()
171176
.proofPurpose(ASSERTION_METHOD)
172-
.verificationMethod(new JsonWebKeyPair(URI.create(controller + "#" + publicKeyId), verificationMethodType, controllerUri, null))
177+
.verificationMethod(new JsonWebKeyPair(keyIdUri, verificationMethodType, controllerUri, null))
173178
.created(Instant.now())
174179
.mapper(typeManager.getMapper(typeContext))
175180
.build();

core/identity-hub-core/src/test/java/org/eclipse/edc/identityhub/core/services/verifiablepresentation/generators/LdpPresentationGeneratorTest.java

+11
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,17 @@ public void create_whenVcsEmpty_shouldReturnEmptyVp() {
154154
assertThat(result).isNotNull();
155155
}
156156

157+
@Test
158+
public void create_whenPublicKeyContainsController() {
159+
var ldpVc = TestData.LDP_VC_WITH_PROOF;
160+
var vcc = new VerifiableCredentialContainer(ldpVc, CredentialFormat.VC1_0_LD, createDummyCredential());
161+
var publicKeyIdWithController = ADDITIONAL_DATA.get("controller").toString() + "#" + PUBLIC_KEY_ID;
162+
163+
var result = creator.generatePresentation(List.of(vcc), PRIVATE_KEY_ALIAS, publicKeyIdWithController, issuerId, ADDITIONAL_DATA);
164+
assertThat(result).isNotNull();
165+
assertThat(result.get("https://w3id.org/security#proof")).isNotNull();
166+
}
167+
157168
@NotNull
158169
private TitaniumJsonLd initializeJsonLd() {
159170
var jld = new TitaniumJsonLd(mock());

0 commit comments

Comments
 (0)