Skip to content

Commit

Permalink
feat: add model classes for credential service, add services to did d…
Browse files Browse the repository at this point in the history
…ocument
  • Loading branch information
aleksandra-bel committed Mar 4, 2024
1 parent 2f49428 commit dcea5c1
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public class DidDocument extends JsonLdObject {
public static final String ID = "id";
/** The constant VERIFICATION_METHOD. */
public static final String VERIFICATION_METHOD = "verificationMethod";

/** The constant SERVICE. */
public static final String SERVICE = "service";
/** The constant AUTHENTICATION. */
public static final String AUTHENTICATION = "authentication";

Expand Down Expand Up @@ -93,6 +94,27 @@ public List<VerificationMethod> getVerificationMethods() {
return result;
}

/**
* Gets services.
*
* @return the services
*/
public List<Service> getServices() {

List<Service> result = new ArrayList<>();

Object service = get(SERVICE);
if (service instanceof Map) {
result.add(new Service((Map<String, Object>) service));
}
if (service instanceof List) {
((List<Map<String, Object>>) service)
.forEach(s -> result.add(new Service(s)));
}

return result;
}

/**
* From json did document.
*
Expand Down
85 changes: 85 additions & 0 deletions src/main/java/org/eclipse/tractusx/ssi/lib/model/did/Service.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* ******************************************************************************
* Copyright (c) 2021,2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
* *******************************************************************************
*/

package org.eclipse.tractusx.ssi.lib.model.did;

import lombok.ToString;
import org.eclipse.tractusx.ssi.lib.serialization.SerializeUtil;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@ToString
public class Service extends HashMap<String, Object> {
/** The constant ID. */
public static final String ID = "id";
/** The constant TYPE. */
public static final String TYPE = "type";
/** The constant SERVICE_ENDPOINT. */
public static final String SERVICE_ENDPOINT = "serviceEndpoint";

/**
* Instantiates a new Service.
*
* @param json the json
*/
public Service(Map<String, Object> json) {
super(json);

try {
// validate getters
Objects.requireNonNull(this.getId(), "id is null");
Objects.requireNonNull(this.getType(), "type is null");
Objects.requireNonNull(this.getServiceEndpoint(), "serviceEndpoint is null");
} catch (Exception e) {
throw new IllegalArgumentException(
String.format("Invalid VerificationMethod: %s", SerializeUtil.toJson(json)), e);
}
}

/**
* Gets id.
*
* @return the id
*/
public URI getId() {
return SerializeUtil.asURI(this.get(ID));
}

/**
* Gets type.
*
* @return the type
*/
public String getType() {
return (String) this.get(TYPE);
}

/**
* Gets controller.
*
* @return the controller
*/
public URI getServiceEndpoint() {
return SerializeUtil.asURI(this.get(SERVICE_ENDPOINT));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* ******************************************************************************
* Copyright (c) 2021,2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
* *******************************************************************************
*/

package org.eclipse.tractusx.ssi.lib.model.did;

import java.net.URI;
import java.util.Map;
import lombok.ToString;

@ToString
public class ServiceBuilder {
private URI id;
private String type;
private URI serviceEndpoint;

/**
* Id service builder.
*
* @param id the id
* @return the service builder
*/
public ServiceBuilder id(URI id) {
this.id = id;
return this;
}

/**
* Type service builder.
*
* @param type the type
* @return the service builder
*/
public ServiceBuilder type(String type) {
this.type = type;
return this;
}

/**
* ServiceEndpoint service builder.
*
* @param serviceEndpoint the serviceEndpoint
* @return the service builder
*/
public ServiceBuilder serviceEndpoint(URI serviceEndpoint) {
this.serviceEndpoint = serviceEndpoint;
return this;
}

/**
* Build service.
*
* @return the service instance
*/
public Service build() {
return new Service(
Map.of(
Service.ID, id,
Service.TYPE, type,
Service.SERVICE_ENDPOINT, serviceEndpoint));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public final class SerializeUtil {
List.of(
JsonLdObject.CONTEXT,
DidDocument.ID,
DidDocument.SERVICE,
DidDocument.VERIFICATION_METHOD,
DidDocument.AUTHENTICATION),
VerifiableCredentialStatusList2021Entry.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
package org.eclipse.tractusx.ssi.lib.model.did;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.net.URI;
import java.util.List;
import java.util.Map;
import lombok.SneakyThrows;
Expand All @@ -44,6 +45,22 @@ public void canCreateDidDocument() {
}
}

/** Can create did document with service. */
@Test
public void canCreateDidDocumentWithService() {
final Map<String, Object> content = TestResourceUtil.getBPNDidDocument();
final URI id = URI.create("did:test:localhost:BPNL000000000000");
final String type = "CredentialService";
final URI serviceEndpoint = URI.create("https://cs.example.com");

DidDocument didDocument = new DidDocument(content);
Service service = didDocument.getServices().get(0);

Assertions.assertEquals(id, service.getId());
Assertions.assertEquals(type, service.getType());
Assertions.assertEquals(serviceEndpoint, service.getServiceEndpoint());
}

/** Can serialize did document. */
@Test
@SneakyThrows
Expand Down
9 changes: 8 additions & 1 deletion src/test/resources/did-document/document.ed25519.bpn.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
{
"id": "did:test:localhost:BPNL000000000000",
"service": [
{
"id": "did:test:localhost:BPNL000000000000",
"type": "CredentialService",
"serviceEndpoint": "https://cs.example.com"
}
],
"verificationMethod": [
{
"publicKeyMultibase": "zEHDkzHqUZipBCTJ7ysJ57zk2hKwuYjD47RhuM7qYNvQA",
Expand All @@ -9,4 +16,4 @@
}
],
"@context": "https://www.w3.org/ns/did/v1"
}
}

0 comments on commit dcea5c1

Please sign in to comment.