Skip to content

Commit

Permalink
Merge pull request #52 from filip26/feat/codecs
Browse files Browse the repository at this point in the history
Extract multiformat decoders
  • Loading branch information
filip26 authored Feb 6, 2024
2 parents e7193c3 + dd51dd8 commit fbe6510
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 13 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>com.apicatalog</groupId>
<artifactId>carbon-did</artifactId>

<version>0.1.1</version>
<version>0.2.0</version>
<packaging>jar</packaging>

<url>https://github.com/filip26/carbon-decentralized-identifiers</url>
Expand Down
14 changes: 5 additions & 9 deletions src/main/java/com/apicatalog/did/key/DidKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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;
Expand All @@ -51,28 +47,28 @@ 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);

if (!METHOD_KEY.equalsIgnoreCase(did.getMethod())) {
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);

Expand Down
12 changes: 11 additions & 1 deletion src/main/java/com/apicatalog/did/key/DidKeyResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,27 @@
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) {

if (!DidKey.isDidKey(did)) {
throw new IllegalArgumentException();
}

final DidKey didKey = DidKey.from(did);
final DidKey didKey = DidKey.from(did, bases, codecs);

final DidDocumentBuilder builder = DidDocumentBuilder.create();

Expand Down
5 changes: 4 additions & 1 deletion src/test/java/com/apicatalog/did/DidKeyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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);
Expand Down

0 comments on commit fbe6510

Please sign in to comment.