Skip to content

Commit b23f456

Browse files
committed
Fix issue #54
1 parent fc999d9 commit b23f456

6 files changed

Lines changed: 41 additions & 98 deletions

File tree

pom.xml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858

5959
<jakarta.json.version>2.0.1</jakarta.json.version>
6060

61-
<copper.multicodec.version>1.0.0</copper.multicodec.version>
6261
<copper.multibase.version>0.5.0</copper.multibase.version>
6362

6463
<!-- test resources -->
@@ -73,13 +72,6 @@
7372
<scope>provided</scope>
7473
</dependency>
7574

76-
<dependency>
77-
<groupId>com.apicatalog</groupId>
78-
<artifactId>copper-multicodec</artifactId>
79-
<version>${copper.multicodec.version}</version>
80-
<scope>provided</scope>
81-
</dependency>
82-
8375
<dependency>
8476
<groupId>com.apicatalog</groupId>
8577
<artifactId>copper-multibase</artifactId>

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,20 @@
22

33
import com.apicatalog.did.Did;
44
import com.apicatalog.did.DidUrl;
5-
import com.apicatalog.multicodec.Multicodec;
65

76
public class DidVerificationMethod {
87

98
final DidUrl id;
109
final Did controller;
11-
final Multicodec codec;
1210
final byte[] publicKey;
1311

1412
public DidVerificationMethod(
1513
DidUrl id,
1614
Did controller,
17-
Multicodec codec,
1815
byte[] publicKey
1916
) {
2017
this.id = id;
2118
this.controller = controller;
22-
this.codec = codec;
2319
this.publicKey = publicKey;
2420
}
2521

@@ -34,8 +30,4 @@ public Did controller() {
3430
public byte[] publicKey() {
3531
return publicKey;
3632
}
37-
38-
public Multicodec codec() {
39-
return codec;
40-
}
4133
}

src/main/java/com/apicatalog/did/key/DidKey.java

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
import com.apicatalog.did.Did;
66
import com.apicatalog.multibase.Multibase;
77
import com.apicatalog.multibase.MultibaseDecoder;
8-
import com.apicatalog.multicodec.Multicodec;
9-
import com.apicatalog.multicodec.MulticodecDecoder;
108

119
/**
1210
* Immutable DID Key
@@ -27,58 +25,53 @@ public class DidKey extends Did {
2725
public static final String METHOD_KEY = "key";
2826

2927
protected final Multibase base;
30-
protected final Multicodec codec;
3128

32-
protected final byte[] rawKey;
29+
protected final byte[] encodedKey;
3330

34-
protected DidKey(String version, String encoded, Multibase base, Multicodec codec, byte[] rawValue) {
31+
protected DidKey(String version, String encoded, Multibase base, byte[] debased) {
3532
super(METHOD_KEY, version, encoded);
3633
this.base = base;
37-
this.codec = codec;
38-
this.rawKey = rawValue;
34+
this.encodedKey = debased;
3935
}
4036

4137
/**
4238
* Creates a new DID key instance from the given {@link URI}.
4339
*
44-
* @param uri The source URI to be transformed into DID key
40+
* @param uri The source URI to be transformed into DID key
41+
* @param bases
4542
* @return The new DID key
4643
*
4744
* @throws NullPointerException If {@code uri} is {@code null}
4845
*
4946
* @throws IllegalArgumentException If the given {@code uri} is not valid DID
5047
* key
5148
*/
52-
public static final DidKey from(final URI uri, final MultibaseDecoder bases, final MulticodecDecoder codecs) {
49+
public static final DidKey from(final URI uri, final MultibaseDecoder bases) {
5350

5451
final Did did = Did.from(uri);
5552

5653
if (!METHOD_KEY.equalsIgnoreCase(did.getMethod())) {
5754
throw new IllegalArgumentException("The given URI [" + uri + "] is not valid DID key, does not start with 'did:key'.");
5855
}
5956

60-
return from(did, bases, codecs);
57+
return from(did, bases);
6158
}
6259

63-
public static final DidKey from(final Did did, final MultibaseDecoder bases, final MulticodecDecoder codecs) {
60+
public static final DidKey from(final Did did, final MultibaseDecoder bases) {
6461

6562
if (!METHOD_KEY.equalsIgnoreCase(did.getMethod())) {
6663
throw new IllegalArgumentException("The given DID method [" + did.getMethod() + "] is not 'key'. DID [" + did.toString() + "].");
6764
}
6865

6966
final Multibase base = bases.getBase(did.getMethodSpecificId()).orElseThrow(() -> new IllegalArgumentException("Unsupported did:key base encoding. DID [" + did.toString() + "]."));
7067

71-
final byte[] decoded = base.decode(did.getMethodSpecificId());
68+
final byte[] debased = base.decode(did.getMethodSpecificId());
7269

73-
final Multicodec codec = codecs.getCodec(decoded).orElseThrow(() -> new IllegalArgumentException("Unsupported did:key codec. DID [" + did.toString() + "]."));
74-
75-
final byte[] rawKey = codec.decode(decoded);
76-
77-
return new DidKey(did.getVersion(), did.getMethodSpecificId(), base, codec, rawKey);
70+
return new DidKey(did.getVersion(), did.getMethodSpecificId(), base, debased);
7871
}
7972

80-
public static final DidKey create(Multibase base, Multicodec codec, byte[] rawKey) {
81-
return new DidKey(null, base.encode(codec.encode(rawKey)), base, codec, rawKey);
73+
public static final DidKey create(Multibase base, byte[] key) {
74+
return new DidKey(null, base.encode(key), base, key);
8275
}
8376

8477
public static boolean isDidKey(final Did did) {
@@ -95,15 +88,11 @@ public static boolean isDidKey(final String uri) {
9588
&& uri.toLowerCase().startsWith(SCHEME + ":" + METHOD_KEY + ":");
9689
}
9790

98-
public Multicodec getCodec() {
99-
return codec;
100-
}
101-
10291
public Multibase getBase() {
10392
return base;
10493
}
10594

106-
public byte[] getRawKey() {
107-
return rawKey;
95+
public byte[] getKey() {
96+
return encodedKey;
10897
}
10998
}

src/main/java/com/apicatalog/did/key/DidKeyResolver.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,13 @@
66
import com.apicatalog.did.document.DidDocument;
77
import com.apicatalog.did.document.DidVerificationMethod;
88
import com.apicatalog.multibase.MultibaseDecoder;
9-
import com.apicatalog.multicodec.MulticodecDecoder;
109

1110
public class DidKeyResolver implements DidResolver {
1211

1312
protected final MultibaseDecoder bases;
14-
protected final MulticodecDecoder codecs;
1513

16-
public DidKeyResolver(final MultibaseDecoder bases, final MulticodecDecoder codecs) {
14+
public DidKeyResolver(final MultibaseDecoder bases) {
1715
this.bases = bases;
18-
this.codecs = codecs;
1916
}
2017

2118
@Override
@@ -25,7 +22,7 @@ public DidDocument resolve(final Did did) {
2522
throw new IllegalArgumentException();
2623
}
2724

28-
final DidKey didKey = DidKey.from(did, bases, codecs);
25+
final DidKey didKey = DidKey.from(did, bases);
2926

3027
final DidDocumentBuilder builder = DidDocumentBuilder.create();
3128

@@ -61,8 +58,7 @@ public static DidVerificationMethod createSignatureMethod(DidKey didKey) {
6158
return new DidVerificationMethod(
6259
DidUrl.from(didKey, null, null, didKey.getMethodSpecificId()),
6360
DidUrl.from(didKey, null, null, didKey.getMethodSpecificId()),
64-
didKey.getCodec(),
65-
didKey.getRawKey()
61+
didKey.getKey()
6662
);
6763
}
6864

@@ -81,8 +77,7 @@ public static DidVerificationMethod createEncryptionMethod(final DidKey didKey)
8177
return new DidVerificationMethod(
8278
DidUrl.from(didKey, null, null, didKey.getMethodSpecificId()),
8379
DidUrl.from(didKey, null, null, didKey.getMethodSpecificId()),
84-
didKey.getCodec(),
85-
didKey.getRawKey()
80+
didKey.getKey()
8681
);
8782
}
8883
}

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

Lines changed: 20 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515

1616
import com.apicatalog.did.key.DidKey;
1717
import com.apicatalog.multibase.MultibaseDecoder;
18-
import com.apicatalog.multicodec.Multicodec.Tag;
19-
import com.apicatalog.multicodec.MulticodecDecoder;
20-
import com.apicatalog.multicodec.codec.KeyCodec;
2118

2219
@DisplayName("DID Key")
2320
@TestMethodOrder(OrderAnnotation.class)
@@ -29,18 +26,17 @@ class DidKeyTest {
2926
void fromString(DidKeyTestCase testCase) {
3027
try {
3128

32-
final DidKey didKey = DidKey.from(testCase.uri, MultibaseDecoder.getInstance(), MulticodecDecoder.getInstance(Tag.Key));
29+
final DidKey didKey = DidKey.from(testCase.uri, MultibaseDecoder.getInstance());
3330

3431
if (testCase.negative) {
3532
fail("Expected failure but got " + didKey);
3633
return;
3734
}
3835

3936
assertNotNull(didKey);
40-
assertNotNull(didKey.getRawKey());
37+
assertNotNull(didKey.getKey());
4138
assertEquals(testCase.version, didKey.getVersion());
42-
assertEquals(testCase.codec, didKey.getCodec());
43-
assertEquals(testCase.keyLength, didKey.getRawKey().length);
39+
assertEquals(testCase.keyLength, didKey.getKey().length);
4440

4541
} catch (IllegalArgumentException | NullPointerException e) {
4642
if (!testCase.negative) {
@@ -57,78 +53,63 @@ static Stream<DidKeyTestCase> testVectors() {
5753
static final DidKeyTestCase testCases[] = new DidKeyTestCase[] {
5854
DidKeyTestCase.create(
5955
"did:key:z6MkpTHR8VNsBxYAAWHut2Geadd9jSwuBV8xRoAnwWsdvktH",
60-
KeyCodec.ED25519_PUBLIC_KEY,
61-
32
56+
34
6257
),
6358
DidKeyTestCase.create(
6459
"did:key:z6MkiTBz1ymuepAQ4HEHYSF1H8quG5GLVVQR3djdX3mDooWp",
65-
KeyCodec.ED25519_PUBLIC_KEY,
66-
32
60+
34
6761
),
6862
DidKeyTestCase.create(
6963
"did:key:z6MkjchhfUsD6mmvni8mCdXHw216Xrm9bQe2mBH1P5RDjVJG",
70-
KeyCodec.ED25519_PUBLIC_KEY,
71-
32
64+
34
7265
),
7366
DidKeyTestCase.create(
7467
"did:key:z6MknGc3ocHs3zdPiJbnaaqDi58NGb4pk1Sp9WxWufuXSdxf",
75-
KeyCodec.ED25519_PUBLIC_KEY,
76-
32
68+
34
7769
),
7870
DidKeyTestCase.create(
7971
"did:key:z6MkicdicToW5HbxPP7zZV1H7RHvXgRMhoujWAF2n5WQkdd2",
80-
KeyCodec.ED25519_PUBLIC_KEY,
81-
32
72+
34
8273
),
8374
DidKeyTestCase.create(
8475
"did:key:z6MkiVQTYk3L2XKY6yg6MyeN2QLE5QkKcXByUeY1dkdiLx4j",
85-
KeyCodec.ED25519_PUBLIC_KEY,
86-
32
76+
34
8777
),
8878
DidKeyTestCase.create(
8979
"did:key:zQ3shokFTS3brHcDQrn82RUDfCZESWL1ZdCEJwekUDPQiYBme",
90-
KeyCodec.SECP256K1_PUBLIC_KEY,
91-
33
80+
35
9281
),
9382
DidKeyTestCase.create(
9483
"did:key:zQ3shtxV1FrJfhqE1dvxYRcCknWNjHc3c5X1y3ZSoPDi2aur2",
95-
KeyCodec.SECP256K1_PUBLIC_KEY,
96-
33
84+
35
9785
),
9886
DidKeyTestCase.create(
9987
"did:key:zQ3shZc2QzApp2oymGvQbzP8eKheVshBHbU4ZYjeXqwSKEn6N",
100-
KeyCodec.SECP256K1_PUBLIC_KEY,
101-
33
88+
35
10289
),
10390
DidKeyTestCase.create(
10491
"did:key:zDnaerDaTF5BXEavCrfRZEk316dpbLsfPDZ3WJ5hRTPFU2169",
105-
KeyCodec.P256_PUBLIC_KEY,
106-
33
92+
35
10793
),
10894
DidKeyTestCase.create(
10995
"did:key:zDnaerx9CtbPJ1q36T5Ln5wYt3MQYeGRG5ehnPAmxcf5mDZpv",
110-
KeyCodec.P256_PUBLIC_KEY,
111-
33
96+
35
11297
),
11398
DidKeyTestCase.create(
11499
"did:key:z82Lm1MpAkeJcix9K8TMiLd5NMAhnwkjjCBeWHXyu3U4oT2MVJJKXkcVBgjGhnLBn2Kaau9",
115-
KeyCodec.P384_PUBLIC_KEY,
116-
49
100+
51
117101
),
118102
DidKeyTestCase.create(
119103
"did:key:z82LkvCwHNreneWpsgPEbV3gu1C6NFJEBg4srfJ5gdxEsMGRJUz2sG9FE42shbn2xkZJh54",
120-
KeyCodec.P384_PUBLIC_KEY,
121-
49
104+
51
122105
),
123106
DidKeyTestCase.create(
124107
"did:key:z2J9gaYxrKVpdoG9A4gRnmpnRCcxU6agDtFVVBVdn1JedouoZN7SzcyREXXzWgt3gGiwpoHq7K68X4m32D8HgzG8wv3sY5j7",
125-
KeyCodec.P521_PUBLIC_KEY,
126-
67
108+
69
127109
),
128110
DidKeyTestCase.create(
129111
"did:key:z2J9gcGdb2nEyMDmzQYv2QZQcM1vXktvy1Pw4MduSWxGabLZ9XESSWLQgbuPhwnXN7zP7HpTzWqrMTzaY5zWe6hpzJ2jnw4f",
130-
KeyCodec.P521_PUBLIC_KEY,
131-
67
112+
69
132113
),
133114

134115
// invalid keys
@@ -139,14 +120,12 @@ static Stream<DidKeyTestCase> testVectors() {
139120
// versioned keys
140121
DidKeyTestCase.create(
141122
"did:key:1.1:z6MkicdicToW5HbxPP7zZV1H7RHvXgRMhoujWAF2n5WQkdd2",
142-
KeyCodec.ED25519_PUBLIC_KEY,
143-
32,
123+
34,
144124
"1.1"
145125
),
146126
DidKeyTestCase.create(
147127
"did:key:0.7:z6MkicdicToW5HbxPP7zZV1H7RHvXgRMhoujWAF2n5WQkdd2",
148-
KeyCodec.ED25519_PUBLIC_KEY,
149-
32,
128+
34,
150129
"0.7"
151130
),
152131
};

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,21 @@
22

33
import java.net.URI;
44

5-
import com.apicatalog.multicodec.Multicodec;
6-
75
public class DidKeyTestCase {
86

97
URI uri;
108
boolean negative;
11-
Multicodec codec;
129
int keyLength;
1310
String version;
1411

15-
static DidKeyTestCase create(String uri, Multicodec codec, int length) {
16-
return create(uri, codec, length, "1");
12+
static DidKeyTestCase create(String uri, int length) {
13+
return create(uri, length, "1");
1714
}
1815

19-
static DidKeyTestCase create(String uri, Multicodec codec, int length, String version) {
16+
static DidKeyTestCase create(String uri, int length, String version) {
2017
DidKeyTestCase testCase = new DidKeyTestCase();
2118
testCase.uri = URI.create(uri);
2219
testCase.negative = false;
23-
testCase.codec = codec;
2420
testCase.keyLength = length;
2521
testCase.version = version;
2622

0 commit comments

Comments
 (0)