diff --git a/README.md b/README.md index c8405ae..570fc5a 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,9 @@ An implementation of the [Decentralized Identifiers (DIDs) v1.0](https://www.w3. ## Features -* [did:key method v0.7](https://w3c-ccg.github.io/did-method-key/) +* DID, DID URL, DID Document +* Methods + * [did:key method v0.7](https://w3c-ccg.github.io/did-method-key/) ## Installation diff --git a/pom.xml b/pom.xml index 3c94eef..6f00230 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ com.apicatalog carbon-did - 0.1.1 + 0.2.0 jar https://github.com/filip26/carbon-decentralized-identifiers diff --git a/src/main/java/com/apicatalog/did/key/DidKey.java b/src/main/java/com/apicatalog/did/key/DidKey.java index b3a0ca3..98dcb8c 100644 --- a/src/main/java/com/apicatalog/did/key/DidKey.java +++ b/src/main/java/com/apicatalog/did/key/DidKey.java @@ -6,7 +6,6 @@ import com.apicatalog.multibase.Multibase; import com.apicatalog.multibase.MultibaseDecoder; import com.apicatalog.multicodec.Multicodec; -import com.apicatalog.multicodec.Multicodec.Tag; import com.apicatalog.multicodec.MulticodecDecoder; /** @@ -27,9 +26,6 @@ public class DidKey extends Did { public static final String METHOD_KEY = "key"; - protected static final MulticodecDecoder MULTICODEC = MulticodecDecoder.getInstance(Tag.Key); - protected static final MultibaseDecoder MULTIBASE = MultibaseDecoder.getInstance(); - private final Multicodec codec; private final byte[] rawKey; @@ -51,7 +47,7 @@ protected DidKey(Did did, Multicodec codec, byte[] rawValue) { * @throws IllegalArgumentException If the given {@code uri} is not valid DID * key */ - public static final DidKey from(final URI uri) { + public static final DidKey from(final URI uri, final MultibaseDecoder bases, final MulticodecDecoder codecs) { final Did did = Did.from(uri); @@ -59,20 +55,20 @@ public static final DidKey from(final URI uri) { throw new IllegalArgumentException("The given URI [" + uri + "] is not valid DID key, does not start with 'did:key'."); } - return from(did); + return from(did, bases, codecs); } - public static final DidKey from(final Did did) { + public static final DidKey from(final Did did, final MultibaseDecoder bases, final MulticodecDecoder codecs) { if (!METHOD_KEY.equalsIgnoreCase(did.getMethod())) { throw new IllegalArgumentException("The given DID method [" + did.getMethod() + "] is not 'key'. DID [" + did.toString() + "]."); } - final Multibase base = MULTIBASE.getBase(did.getMethodSpecificId()).orElseThrow(() -> new IllegalArgumentException("Cannot detect did:key base encoding.")); + final Multibase base = bases.getBase(did.getMethodSpecificId()).orElseThrow(() -> new IllegalArgumentException("Unsupported did:key base encoding. DID [" + did.toString() + "].")); final byte[] decoded = base.decode(did.getMethodSpecificId()); - final Multicodec codec = MULTICODEC.getCodec(decoded).orElseThrow(() -> new IllegalArgumentException("Cannot detect did:key codec.")); + final Multicodec codec = codecs.getCodec(decoded).orElseThrow(() -> new IllegalArgumentException("Unsupported did:key codec. DID [" + did.toString() + "].")); final byte[] rawKey = codec.decode(decoded); diff --git a/src/main/java/com/apicatalog/did/key/DidKeyResolver.java b/src/main/java/com/apicatalog/did/key/DidKeyResolver.java index 2509e0e..eb60453 100644 --- a/src/main/java/com/apicatalog/did/key/DidKeyResolver.java +++ b/src/main/java/com/apicatalog/did/key/DidKeyResolver.java @@ -5,9 +5,19 @@ import com.apicatalog.did.DidUrl; import com.apicatalog.did.document.DidDocument; import com.apicatalog.did.document.DidVerificationMethod; +import com.apicatalog.multibase.MultibaseDecoder; +import com.apicatalog.multicodec.MulticodecDecoder; public class DidKeyResolver implements DidResolver { + protected final MultibaseDecoder bases; + protected final MulticodecDecoder codecs; + + public DidKeyResolver(final MultibaseDecoder bases, final MulticodecDecoder codecs) { + this.bases = bases; + this.codecs = codecs; + } + @Override public DidDocument resolve(final Did did) { @@ -15,7 +25,7 @@ public DidDocument resolve(final Did did) { throw new IllegalArgumentException(); } - final DidKey didKey = DidKey.from(did); + final DidKey didKey = DidKey.from(did, bases, codecs); final DidDocumentBuilder builder = DidDocumentBuilder.create(); diff --git a/src/test/java/com/apicatalog/did/DidKeyTest.java b/src/test/java/com/apicatalog/did/DidKeyTest.java index fadff31..6c22e64 100644 --- a/src/test/java/com/apicatalog/did/DidKeyTest.java +++ b/src/test/java/com/apicatalog/did/DidKeyTest.java @@ -14,6 +14,9 @@ import org.junit.jupiter.params.provider.MethodSource; import com.apicatalog.did.key.DidKey; +import com.apicatalog.multibase.MultibaseDecoder; +import com.apicatalog.multicodec.Multicodec.Tag; +import com.apicatalog.multicodec.MulticodecDecoder; import com.apicatalog.multicodec.codec.KeyCodec; @DisplayName("DID Key") @@ -26,7 +29,7 @@ class DidKeyTest { void fromString(DidKeyTestCase testCase) { try { - final DidKey didKey = DidKey.from(testCase.uri); + final DidKey didKey = DidKey.from(testCase.uri, MultibaseDecoder.getInstance(), MulticodecDecoder.getInstance(Tag.Key)); if (testCase.negative) { fail("Expected failure but got " + didKey);