Skip to content

Commit

Permalink
Merge pull request #53 from filip26/feat/didkey-create
Browse files Browse the repository at this point in the history
0.3.0 - DidKey.create
  • Loading branch information
filip26 authored Feb 15, 2024
2 parents 679ce0e + e484289 commit 330a16b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ An implementation of the [Decentralized Identifiers (DIDs) v1.0](https://www.w3.
<dependency>
<groupId>com.apicatalog</groupId>
<artifactId>carbon-did</artifactId>
<version>0.2.0</version>
<version>0.3.0</version>
</dependency>

```
Expand All @@ -32,7 +32,7 @@ An implementation of the [Decentralized Identifiers (DIDs) v1.0](https://www.w3.
Android API Level >=24

```gradle
implementation("com.apicatalog:carbon-did:0.2.0")
implementation("com.apicatalog:carbon-did:0.3.0")
```


Expand Down
4 changes: 2 additions & 2 deletions 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.2.0</version>
<version>0.3.0</version>
<packaging>jar</packaging>

<url>https://github.com/filip26/carbon-decentralized-identifiers</url>
Expand Down Expand Up @@ -59,7 +59,7 @@
<jakarta.json.version>2.0.1</jakarta.json.version>

<copper.multicodec.version>0.1.1</copper.multicodec.version>
<copper.multibase.version>0.4.0</copper.multibase.version>
<copper.multibase.version>0.5.0</copper.multibase.version>

<!-- test resources -->
<junit.version>5.10.2</junit.version>
Expand Down
22 changes: 16 additions & 6 deletions src/main/java/com/apicatalog/did/key/DidKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ public class DidKey extends Did {

public static final String METHOD_KEY = "key";

private final Multicodec codec;
protected final Multibase base;
protected final Multicodec codec;

private final byte[] rawKey;
protected final byte[] rawKey;

protected DidKey(Did did, Multicodec codec, byte[] rawValue) {
super(did.getMethod(), did.getVersion(), did.getMethodSpecificId());
protected DidKey(String version, String encoded, Multibase base, Multicodec codec, byte[] rawValue) {
super(METHOD_KEY, version, encoded);
this.base = base;
this.codec = codec;
this.rawKey = rawValue;
}
Expand Down Expand Up @@ -65,14 +67,18 @@ public static final DidKey from(final Did did, final MultibaseDecoder bases, fin
}

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 = codecs.getCodec(decoded).orElseThrow(() -> new IllegalArgumentException("Unsupported did:key codec. DID [" + did.toString() + "]."));

final byte[] rawKey = codec.decode(decoded);

return new DidKey(did, codec, rawKey);
return new DidKey(did.getVersion(), did.getMethodSpecificId(), base, codec, rawKey);
}

public static final DidKey create(Multibase base, Multicodec codec, byte[] rawKey) {
return new DidKey(null, base.encode(codec.encode(rawKey)), base, codec, rawKey);
}

public static boolean isDidKey(final Did did) {
Expand All @@ -93,6 +99,10 @@ public Multicodec getCodec() {
return codec;
}

public Multibase getBase() {
return base;
}

public byte[] getRawKey() {
return rawKey;
}
Expand Down

0 comments on commit 330a16b

Please sign in to comment.