Skip to content

Commit 69a05b9

Browse files
committed
Simplify DidDocument API
1 parent 74d34b5 commit 69a05b9

6 files changed

Lines changed: 90 additions & 30 deletions

File tree

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
# Carbon DID
1+
# Carbon Decentralized Identifiers
22
An implementation of the Decentralized Identifiers (DIDs) Primitives & API in Java.
33

44

55
[![Java 8 CI](https://github.com/filip26/carbon-did-core/actions/workflows/java8-build.yml/badge.svg)](https://github.com/filip26/carbon-did-core/actions/workflows/java8-build.yml)
6-
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/dd79aafc6eb14ed18f2217de62585ba7)](https://app.codacy.com/gh/filip26/carbon-decentralized-identifiers/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
7-
[![Codacy Badge](https://app.codacy.com/project/badge/Coverage/dd79aafc6eb14ed18f2217de62585ba7)](https://app.codacy.com/gh/filip26/carbon-decentralized-identifiers/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_coverage)
6+
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/dd79aafc6eb14ed18f2217de62585ba7)](https://app.codacy.com/gh/filip26/carbon-did-core/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
87
[![Maven Central](https://img.shields.io/maven-central/v/com.apicatalog/carbon-did.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:com.apicatalog%20AND%20a:carbon-did)
98
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
109

@@ -49,8 +48,10 @@ Fork and clone the project repository.
4948

5049
## Resources
5150

52-
- [Decentralized Identifiers (DIDs) v1.0](https://www.w3.org/TR/did-core/)
51+
- [W3C Decentralized Identifiers (DIDs) v1.0](https://www.w3.org/TR/did-core/)
52+
- [W3C Controlled Identifiers v1.0](https://www.w3.org/TR/cid-1.0/)
5353
- [Carbon DID Key Method](https://github.com/filip26/carbon-did-key)
54+
- [Carbon Controlled Identifiers](https://github.com/filip26/carbon-cid)
5455

5556
## Sponsors
5657

SECURITY.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
## Supported Versions
44

5-
| Version | Supported |
6-
| ------- |:------------------:|
5+
| Version | Supported |
6+
| ------- |:---------:|
77
| 0.x.x ||
8+
| 1.x.x | |
89

910
## Reporting a Vulnerability
1011

src/main/java/com/apicatalog/did/document/DidDocument.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.apicatalog.did.document;
22

33
import java.net.URI;
4+
import java.util.Collection;
45
import java.util.Collections;
5-
import java.util.Set;
66

77
import com.apicatalog.did.Did;
88

@@ -28,7 +28,7 @@ public interface DidDocument {
2828
*
2929
* @return controller set, possibly empty
3030
*/
31-
default Set<Did> controller() {
31+
default Collection<Did> controller() {
3232
return Collections.emptySet();
3333
}
3434

@@ -38,7 +38,7 @@ default Set<Did> controller() {
3838
*
3939
* @return verification methods, possibly empty
4040
*/
41-
default Set<DidVerificationMethod> verification() {
41+
default Collection<DidVerificationMethod> verification() {
4242
return Collections.emptySet();
4343
}
4444

@@ -48,7 +48,7 @@ default Set<DidVerificationMethod> verification() {
4848
*
4949
* @return URIs, possibly empty
5050
*/
51-
default Set<URI> alsoKnownAs() {
51+
default Collection<URI> alsoKnownAs() {
5252
return Collections.emptySet();
5353
}
5454

@@ -58,7 +58,7 @@ default Set<URI> alsoKnownAs() {
5858
*
5959
* @return authentication methods, possibly empty
6060
*/
61-
default Set<DidVerificationMethod> authentication() {
61+
default Collection<DidVerificationMethod> authentication() {
6262
return Collections.emptySet();
6363
}
6464

@@ -67,7 +67,7 @@ default Set<DidVerificationMethod> authentication() {
6767
*
6868
* @return assertion methods, possibly empty
6969
*/
70-
default Set<DidVerificationMethod> assertion() {
70+
default Collection<DidVerificationMethod> assertion() {
7171
return Collections.emptySet();
7272
}
7373

@@ -76,7 +76,7 @@ default Set<DidVerificationMethod> assertion() {
7676
*
7777
* @return key agreement methods, possibly empty
7878
*/
79-
default Set<DidVerificationMethod> keyAgreement() {
79+
default Collection<DidVerificationMethod> keyAgreement() {
8080
return Collections.emptySet();
8181
}
8282

@@ -86,7 +86,7 @@ default Set<DidVerificationMethod> keyAgreement() {
8686
*
8787
* @return invocation methods, possibly empty
8888
*/
89-
default Set<DidVerificationMethod> capabilityInvocation() {
89+
default Collection<DidVerificationMethod> capabilityInvocation() {
9090
return Collections.emptySet();
9191
}
9292

@@ -96,7 +96,7 @@ default Set<DidVerificationMethod> capabilityInvocation() {
9696
*
9797
* @return delegation methods, possibly empty
9898
*/
99-
default Set<DidVerificationMethod> capabilityDelegation() {
99+
default Collection<DidVerificationMethod> capabilityDelegation() {
100100
return Collections.emptySet();
101101
}
102102

@@ -105,7 +105,7 @@ default Set<DidVerificationMethod> capabilityDelegation() {
105105
*
106106
* @return service definitions, possibly empty
107107
*/
108-
default Set<DidService> service() {
108+
default Collection<DidService> service() {
109109
return Collections.emptySet();
110110
}
111111

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.apicatalog.did.resolver;
2+
3+
import java.util.Collections;
4+
import java.util.LinkedHashMap;
5+
import java.util.Map;
6+
import java.util.Objects;
7+
8+
import com.apicatalog.did.Did;
9+
import com.apicatalog.did.resolver.DidResolutionException.Code;
10+
11+
public class DidMethodResolver implements DidResolver {
12+
13+
protected final Map<String, DidResolver> resolvers;
14+
15+
protected DidMethodResolver(final Map<String, DidResolver> resolvers) {
16+
this.resolvers = resolvers;
17+
}
18+
19+
@Override
20+
public ResolvedDidDocument resolve(Did did) throws DidResolutionException {
21+
22+
Objects.requireNonNull(did);
23+
24+
final DidResolver resolver = resolvers.get(did.getMethod());
25+
26+
if (resolver == null) {
27+
throw new DidResolutionException(did, Code.UnsupportedMethod);
28+
}
29+
return resolver.resolve(did);
30+
}
31+
32+
public static Builder with(String method, DidResolver resolver) {
33+
return (new Builder()).with(method, resolver);
34+
}
35+
36+
public static class Builder {
37+
38+
final Map<String, DidResolver> resolvers;
39+
40+
Builder() {
41+
this.resolvers = new LinkedHashMap<>();
42+
}
43+
44+
public Builder with(String method, DidResolver resolver) {
45+
resolvers.put(method, resolver);
46+
return this;
47+
}
48+
49+
public DidResolver build() {
50+
if (resolvers.size() == 1) {
51+
return resolvers.values().iterator().next();
52+
}
53+
return new DidMethodResolver(Collections.unmodifiableMap(resolvers));
54+
}
55+
}
56+
57+
}

src/main/java/com/apicatalog/did/resolver/DidResolver.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* A <a href="https://www.w3.org/TR/did-core/#dfn-did-resolvers">DID
88
* Resolver</a> expands a {@link Did} into a corresponding {@link DidDocument}.
99
*/
10+
@FunctionalInterface
1011
public interface DidResolver {
1112

1213
/**

src/test/java/com/apicatalog/did/DidTest.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,6 @@ void ofString(String uri, String method, String specificId) {
3232
assertEquals(specificId, did.getMethodSpecificId());
3333
}
3434

35-
@DisplayName("!of(String)")
36-
@ParameterizedTest()
37-
@MethodSource({ "negativeVectors" })
38-
void ofStringNegative(String uri) {
39-
try {
40-
Did.of(uri);
41-
fail();
42-
} catch (IllegalArgumentException e) {
43-
/* expected */ }
44-
}
45-
4635
@DisplayName("of(URI)")
4736
@ParameterizedTest(name = "{0}")
4837
@MethodSource({ "positiveVectors" })
@@ -65,7 +54,18 @@ void toString(String input, String method, String specificId) {
6554
assertEquals(input, did.toString());
6655
}
6756

68-
@DisplayName("!of(URI)")
57+
@DisplayName("negative: of(String)")
58+
@ParameterizedTest()
59+
@MethodSource({ "negativeVectors" })
60+
void ofStringNegative(String uri) {
61+
try {
62+
Did.of(uri);
63+
fail();
64+
} catch (IllegalArgumentException e) {
65+
/* expected */ }
66+
}
67+
68+
@DisplayName("negative: of(URI)")
6969
@ParameterizedTest()
7070
@MethodSource({ "negativeVectors" })
7171
void ofUriNegative(String uri) {
@@ -92,8 +92,8 @@ void toUri(String input, String method, String specificId) {
9292
void stringIsDid(String uri) {
9393
assertTrue(Did.isDid(uri));
9494
}
95-
96-
@DisplayName("isNotDid(String)")
95+
96+
@DisplayName("negative: isDid(String)")
9797
@ParameterizedTest()
9898
@MethodSource({ "negativeVectors" })
9999
void stringIsNotDid(String uri) {

0 commit comments

Comments
 (0)