Skip to content

Commit 06d1ea8

Browse files
committed
feat: Add support for DataIntegrityProof
1 parent de18bac commit 06d1ea8

18 files changed

+77
-130
lines changed

src/main/java/com/danubetech/dataintegrity/canonicalizer/Canonicalizer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.danubetech.dataintegrity.canonicalizer;
22

3+
import com.danubetech.dataintegrity.DataIntegrityProof;
34
import foundation.identity.jsonld.JsonLDException;
45
import foundation.identity.jsonld.JsonLDObject;
5-
import com.danubetech.dataintegrity.DataIntegrityProof;
66

77
import java.io.IOException;
88
import java.security.GeneralSecurityException;
@@ -13,6 +13,7 @@ public abstract class Canonicalizer {
1313

1414
private final List<String> algorithms;
1515

16+
public abstract String canonicalize(JsonLDObject jsonLdObject) throws IOException, GeneralSecurityException, JsonLDException;
1617
public abstract byte[] canonicalize(DataIntegrityProof dataIntegrityProof, JsonLDObject jsonLdObject) throws IOException, GeneralSecurityException, JsonLDException;
1718

1819
public Canonicalizer(List<String> algorithms) {

src/main/java/com/danubetech/dataintegrity/canonicalizer/JCSCanonicalizer.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ public static JCSCanonicalizer getInstance() {
2222
return INSTANCE;
2323
}
2424

25+
@Override
26+
public String canonicalize(JsonLDObject jsonLDObject) throws JsonLDException, IOException {
27+
28+
return new JsonCanonicalizer(jsonLDObject.toJson()).getEncodedString();
29+
}
30+
2531
@Override
2632
public byte[] canonicalize(DataIntegrityProof dataIntegrityProof, JsonLDObject jsonLdObject) throws IOException, GeneralSecurityException, JsonLDException {
2733

@@ -51,8 +57,4 @@ public byte[] canonicalize(DataIntegrityProof dataIntegrityProof, JsonLDObject j
5157
byte[] canonicalizationResult = SHAUtil.sha256(canonicalizedJsonLdObjectWithProofWithoutProofValues);
5258
return canonicalizationResult;
5359
}
54-
55-
public String canonicalize(JsonLDObject jsonLDObject) throws JsonLDException, IOException {
56-
return new JsonCanonicalizer(jsonLDObject.toJson()).getEncodedString();
57-
}
5860
}

src/main/java/com/danubetech/dataintegrity/canonicalizer/RDFC10Canonicalizer.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@
88
import com.danubetech.dataintegrity.util.SHAUtil;
99
import foundation.identity.jsonld.JsonLDException;
1010
import foundation.identity.jsonld.JsonLDObject;
11-
import io.setl.rdf.normalization.RdfNormalize;
1211

1312
import java.io.IOException;
1413
import java.io.StringWriter;
1514
import java.security.GeneralSecurityException;
16-
import java.security.NoSuchAlgorithmException;
1715
import java.util.Collection;
1816
import java.util.List;
1917

@@ -29,6 +27,17 @@ public static RDFC10Canonicalizer getInstance() {
2927
return INSTANCE;
3028
}
3129

30+
@Override
31+
public String canonicalize(JsonLDObject jsonLDObject) throws JsonLDException, IOException {
32+
33+
RdfDataset rdfDataset = jsonLDObject.toDataset();
34+
Collection<RdfNQuad> rdfNQuads = RdfCanonicalizer.canonicalize(rdfDataset.toList());
35+
StringWriter stringWriter = new StringWriter();
36+
NQuadsWriter nQuadsWriter = new NQuadsWriter(stringWriter);
37+
for (RdfNQuad rdfNQuad : rdfNQuads) nQuadsWriter.write(rdfNQuad);
38+
return stringWriter.getBuffer().toString();
39+
}
40+
3241
@Override
3342
public byte[] canonicalize(DataIntegrityProof dataIntegrityProof, JsonLDObject jsonLdObject) throws IOException, GeneralSecurityException, JsonLDException {
3443

@@ -61,13 +70,4 @@ public byte[] canonicalize(DataIntegrityProof dataIntegrityProof, JsonLDObject j
6170

6271
return canonicalizationResult;
6372
}
64-
65-
public String canonicalize(JsonLDObject jsonLDObject) throws JsonLDException, IOException {
66-
RdfDataset rdfDataset = jsonLDObject.toDataset();
67-
Collection<RdfNQuad> rdfNQuads = RdfCanonicalizer.canonicalize(rdfDataset.toList());
68-
StringWriter stringWriter = new StringWriter();
69-
NQuadsWriter nQuadsWriter = new NQuadsWriter(stringWriter);
70-
for (RdfNQuad rdfNQuad : rdfNQuads) nQuadsWriter.write(rdfNQuad);
71-
return stringWriter.getBuffer().toString();
72-
}
7373
}

src/main/java/com/danubetech/dataintegrity/canonicalizer/URDNA2015Canonicalizer.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ public static URDNA2015Canonicalizer getInstance() {
2626
return INSTANCE;
2727
}
2828

29+
@Override
30+
public String canonicalize(JsonLDObject jsonLDObject) throws JsonLDException, IOException, NoSuchAlgorithmException {
31+
32+
RdfDataset rdfDataset = jsonLDObject.toDataset();
33+
rdfDataset = RdfNormalize.normalize(rdfDataset, "urdna2015");
34+
StringWriter stringWriter = new StringWriter();
35+
NQuadsWriter nQuadsWriter = new NQuadsWriter(stringWriter);
36+
nQuadsWriter.write(rdfDataset);
37+
return stringWriter.getBuffer().toString();
38+
}
39+
2940
@Override
3041
public byte[] canonicalize(DataIntegrityProof dataIntegrityProof, JsonLDObject jsonLdObject) throws IOException, GeneralSecurityException, JsonLDException {
3142

@@ -58,13 +69,4 @@ public byte[] canonicalize(DataIntegrityProof dataIntegrityProof, JsonLDObject j
5869

5970
return canonicalizationResult;
6071
}
61-
62-
public String canonicalize(JsonLDObject jsonLDObject) throws JsonLDException, IOException, NoSuchAlgorithmException {
63-
RdfDataset rdfDataset = jsonLDObject.toDataset();
64-
rdfDataset = RdfNormalize.normalize(rdfDataset, "urdna2015");
65-
StringWriter stringWriter = new StringWriter();
66-
NQuadsWriter nQuadsWriter = new NQuadsWriter(stringWriter);
67-
nQuadsWriter.write(rdfDataset);
68-
return stringWriter.getBuffer().toString();
69-
}
7072
}

src/main/java/com/danubetech/dataintegrity/signer/DataIntegrityProofLdSigner.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ public static void sign(DataIntegrityProof.Builder<? extends DataIntegrityProof.
4949

5050
@Override
5151
public void sign(DataIntegrityProof.Builder<? extends DataIntegrityProof.Builder<?>> ldProofBuilder, byte[] signingInput) throws GeneralSecurityException {
52-
5352
sign(ldProofBuilder, signingInput, this.getSigner());
5453
}
5554
}

src/main/java/com/danubetech/dataintegrity/suites/BbsBlsSignature2020DataIntegritySuite.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,9 @@
1111
public class BbsBlsSignature2020DataIntegritySuite extends DataIntegritySuite {
1212

1313
BbsBlsSignature2020DataIntegritySuite() {
14-
1514
super(
1615
"BbsBlsSignature2020",
1716
URI.create("https://w3id.org/security#BbsBlsSignature2020"),
18-
URI.create("https://w3id.org/security#URDNA2015"),
19-
URI.create("https://www.blake2.net/"),
20-
URI.create("https://electriccoin.co/blog/new-snark-curve/"),
21-
List.of(KeyTypeName.Bls12381G1,
22-
KeyTypeName.Bls12381G2),
2317
Map.of(KeyTypeName.Bls12381G1, List.of(JWSAlgorithm.BBSPlus),
2418
KeyTypeName.Bls12381G2, List.of(JWSAlgorithm.BBSPlus)),
2519
List.of(LDSecurityContexts.JSONLD_CONTEXT_W3ID_SECURITY_BBS_V1, LDSecurityContexts.JSONLD_CONTEXT_W3ID_SECURITY_V3));
Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.danubetech.dataintegrity.suites;
22

3+
import com.danubetech.dataintegrity.canonicalizer.Canonicalizer;
4+
import com.danubetech.dataintegrity.canonicalizer.JCSCanonicalizer;
5+
import com.danubetech.dataintegrity.canonicalizer.RDFC10Canonicalizer;
36
import com.danubetech.dataintegrity.jsonld.LDSecurityContexts;
47
import com.danubetech.keyformats.jose.JWSAlgorithm;
58
import com.danubetech.keyformats.jose.KeyTypeName;
@@ -10,22 +13,43 @@
1013

1114
public class DataIntegrityProofDataIntegritySuite extends DataIntegritySuite {
1215

13-
DataIntegrityProofDataIntegritySuite() {
16+
private static final Map<String, Canonicalizer> CANONICALIZERS_BY_CRYPTOSUITE = Map.of(
17+
"ecdsa-rdfc-2019", RDFC10Canonicalizer.getInstance(),
18+
"ecdsa-jcs-2019", JCSCanonicalizer.getInstance(),
19+
"eddsa-rdfc-2022", RDFC10Canonicalizer.getInstance(),
20+
"eddsa-jcs-2022", JCSCanonicalizer.getInstance()
21+
);
22+
23+
private static final Map<String, Map<KeyTypeName, String>> JWS_ALGORITHM_BY_CRYPTOSUITE_AND_KEY_TYPE_NAME = Map.of(
24+
"ecdsa-rdfc-2019", Map.of(
25+
KeyTypeName.secp256k1, JWSAlgorithm.ES256K,
26+
KeyTypeName.P_256, JWSAlgorithm.ES256,
27+
KeyTypeName.P_384, JWSAlgorithm.ES384,
28+
KeyTypeName.P_521, JWSAlgorithm.ES512
29+
),
30+
"ecdsa-jcs-2019", Map.of(
31+
KeyTypeName.Ed25519, JWSAlgorithm.EdDSA
32+
),
33+
"eddsa-rdfc-2022", Map.of(
34+
KeyTypeName.secp256k1, JWSAlgorithm.ES256K,
35+
KeyTypeName.P_256, JWSAlgorithm.ES256,
36+
KeyTypeName.P_384, JWSAlgorithm.ES384,
37+
KeyTypeName.P_521, JWSAlgorithm.ES512
38+
),
39+
"eddsa-jcs-2022", Map.of(
40+
KeyTypeName.Ed25519, JWSAlgorithm.EdDSA
41+
)
42+
);
1443

44+
DataIntegrityProofDataIntegritySuite() {
1545
super(
1646
"DataIntegrityProof",
17-
URI.create("https://w3id.org/security#JsonWebSignature2020"),
18-
URI.create("https://w3id.org/security#URDNA2015"),
19-
URI.create("https://registry.ietf.org/ietf-digest-algorithms#SHA256"),
20-
null,
21-
List.of(KeyTypeName.Ed25519,
22-
KeyTypeName.secp256k1,
23-
KeyTypeName.P_256,
24-
KeyTypeName.P_384),
47+
URI.create("https://w3id.org/security#DataIntegrityProof"),
2548
Map.of(KeyTypeName.Ed25519, List.of(JWSAlgorithm.EdDSA),
2649
KeyTypeName.secp256k1, List.of(JWSAlgorithm.ES256K),
2750
KeyTypeName.P_256, List.of(JWSAlgorithm.ES256),
28-
KeyTypeName.P_384, List.of(JWSAlgorithm.ES384)),
51+
KeyTypeName.P_384, List.of(JWSAlgorithm.ES384),
52+
KeyTypeName.P_521, List.of(JWSAlgorithm.ES512)),
2953
List.of(LDSecurityContexts.JSONLD_CONTEXT_W3ID_DATAINTEGRITY_V2));
3054
}
3155
}

src/main/java/com/danubetech/dataintegrity/suites/DataIntegritySuite.java

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,19 @@ public abstract class DataIntegritySuite {
1414
private final String term;
1515
private final URI id;
1616
private final URI type;
17-
private final URI canonicalizationAlgorithm;
18-
private final URI digestAlgorithm;
19-
private final URI proofAlgorithm;
20-
private final List<KeyTypeName> keyTypeNames;
21-
private final Map<KeyTypeName, List<String>> jwsAlgorithmsForKeyTypeName;
17+
private final Map<KeyTypeName, List<String>> keyTypeNamesAndJwsAlgorithms;
2218
private final List<URI> supportedJsonLDContexts;
2319

24-
public DataIntegritySuite(String term, URI id, URI canonicalizationAlgorithm, URI digestAlgorithm, URI proofAlgorithm, List<KeyTypeName> keyTypeNames, Map<KeyTypeName, List<String>> jwsAlgorithmsForKeyTypeName, List<URI> supportedJsonLDContexts) {
20+
public DataIntegritySuite(String term, URI id, Map<KeyTypeName, List<String>> keyTypeNamesAndJwsAlgorithms, List<URI> supportedJsonLDContexts) {
2521
this.term = term;
2622
this.id = id;
2723
this.type = URI_TYPE_SIGNATURESUITE;
28-
this.canonicalizationAlgorithm = canonicalizationAlgorithm;
29-
this.digestAlgorithm = digestAlgorithm;
30-
this.proofAlgorithm = proofAlgorithm;
31-
this.keyTypeNames = keyTypeNames;
32-
this.jwsAlgorithmsForKeyTypeName = jwsAlgorithmsForKeyTypeName;
24+
this.keyTypeNamesAndJwsAlgorithms = keyTypeNamesAndJwsAlgorithms;
3325
this.supportedJsonLDContexts = supportedJsonLDContexts;
3426
}
3527

3628
public List<String> findJwsAlgorithmsForKeyTypeName(KeyTypeName keyTypeName) {
37-
return this.getJwsAlgorithmsForKeyTypeName().get(keyTypeName);
29+
return this.getKeyTypeNamesAndJwsAlgorithms().get(keyTypeName);
3830
}
3931

4032
public String findDefaultJwsAlgorithmForKeyTypeName(KeyTypeName keyTypeName) {
@@ -59,24 +51,8 @@ public URI getType() {
5951
return type;
6052
}
6153

62-
public URI getCanonicalizationAlgorithm() {
63-
return canonicalizationAlgorithm;
64-
}
65-
66-
public URI getDigestAlgorithm() {
67-
return digestAlgorithm;
68-
}
69-
70-
public URI getProofAlgorithm() {
71-
return proofAlgorithm;
72-
}
73-
74-
public List<KeyTypeName> getKeyTypeNames() {
75-
return keyTypeNames;
76-
}
77-
78-
public Map<KeyTypeName, List<String>> getJwsAlgorithmsForKeyTypeName() {
79-
return jwsAlgorithmsForKeyTypeName;
54+
public Map<KeyTypeName, List<String>> getKeyTypeNamesAndJwsAlgorithms() {
55+
return keyTypeNamesAndJwsAlgorithms;
8056
}
8157

8258
public List<URI> getSupportedJsonLDContexts() {
@@ -88,12 +64,12 @@ public boolean equals(Object o) {
8864
if (this == o) return true;
8965
if (o == null || getClass() != o.getClass()) return false;
9066
DataIntegritySuite that = (DataIntegritySuite) o;
91-
return Objects.equals(term, that.term) && Objects.equals(id, that.id) && Objects.equals(type, that.type) && Objects.equals(canonicalizationAlgorithm, that.canonicalizationAlgorithm) && Objects.equals(digestAlgorithm, that.digestAlgorithm) && Objects.equals(proofAlgorithm, that.proofAlgorithm) && Objects.equals(keyTypeNames, that.keyTypeNames) && Objects.equals(jwsAlgorithmsForKeyTypeName, that.jwsAlgorithmsForKeyTypeName) && Objects.equals(supportedJsonLDContexts, that.supportedJsonLDContexts);
67+
return Objects.equals(term, that.term) && Objects.equals(id, that.id) && Objects.equals(type, that.type) && Objects.equals(keyTypeNamesAndJwsAlgorithms, that.keyTypeNamesAndJwsAlgorithms) && Objects.equals(supportedJsonLDContexts, that.supportedJsonLDContexts);
9268
}
9369

9470
@Override
9571
public int hashCode() {
96-
return Objects.hash(term, id, type, canonicalizationAlgorithm, digestAlgorithm, proofAlgorithm, keyTypeNames, jwsAlgorithmsForKeyTypeName, supportedJsonLDContexts);
72+
return Objects.hash(term, id, type, keyTypeNamesAndJwsAlgorithms, supportedJsonLDContexts);
9773
}
9874

9975
@Override
@@ -102,11 +78,7 @@ public String toString() {
10278
"term='" + term + '\'' +
10379
", id=" + id +
10480
", type=" + type +
105-
", canonicalizationAlgorithm=" + canonicalizationAlgorithm +
106-
", digestAlgorithm=" + digestAlgorithm +
107-
", proofAlgorithm=" + proofAlgorithm +
108-
", keyTypeNames=" + keyTypeNames +
109-
", jwsAlgorithmForKeyTypeName=" + jwsAlgorithmsForKeyTypeName +
81+
", keyTypeNamesAndJwsAlgorithms=" + keyTypeNamesAndJwsAlgorithms +
11082
", supportedJsonLDContexts=" + supportedJsonLDContexts +
11183
'}';
11284
}

src/main/java/com/danubetech/dataintegrity/suites/DataIntegritySuites.java

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

33
import com.danubetech.keyformats.jose.KeyTypeName;
44

5-
import java.util.ArrayList;
6-
import java.util.HashMap;
7-
import java.util.List;
8-
import java.util.Map;
5+
import java.util.*;
96

107
public class DataIntegritySuites {
118

@@ -56,7 +53,7 @@ public class DataIntegritySuites {
5653
static {
5754
DATA_INTEGRITY_SUITES_BY_KEY_TYPE_NAME = new HashMap<>();
5855
for (DataIntegritySuite dataIntegritySuite : DATA_INTEGRITY_SUITES) {
59-
List<KeyTypeName> keyTypeNames = dataIntegritySuite.getKeyTypeNames();
56+
Set<KeyTypeName> keyTypeNames = dataIntegritySuite.getKeyTypeNamesAndJwsAlgorithms().keySet();
6057
for (KeyTypeName keyTypeName : keyTypeNames) {
6158
List<DataIntegritySuite> dataIntegritySuitesList = DATA_INTEGRITY_SUITES_BY_KEY_TYPE_NAME.computeIfAbsent(keyTypeName, k -> new ArrayList<>());
6259
dataIntegritySuitesList.add(dataIntegritySuite);

src/main/java/com/danubetech/dataintegrity/suites/EcdsaKoblitzSignature2016DataIntegritySuite.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,9 @@
1111
public class EcdsaKoblitzSignature2016DataIntegritySuite extends DataIntegritySuite {
1212

1313
EcdsaKoblitzSignature2016DataIntegritySuite() {
14-
1514
super(
1615
"EcdsaKoblitzSignature2016",
1716
URI.create("https://w3id.org/security#EcdsaKoblitzSignature2016"),
18-
URI.create("https://w3id.org/security#URDNA2015"),
19-
URI.create("http://w3id.org/digests#sha256"),
20-
URI.create("http://w3id.org/security#koblitz"),
21-
List.of(KeyTypeName.secp256k1),
2217
Map.of(KeyTypeName.secp256k1, List.of(JWSAlgorithm.ES256K)),
2318
List.of(LDSecurityContexts.JSONLD_CONTEXT_W3ID_SECURITY_V1, LDSecurityContexts.JSONLD_CONTEXT_W3ID_SECURITY_V3));
2419
}

0 commit comments

Comments
 (0)