Skip to content

Commit

Permalink
chore: Various code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
peacekeeper committed Oct 22, 2023
1 parent 3356ae8 commit 4e9db7b
Show file tree
Hide file tree
Showing 35 changed files with 146 additions and 145 deletions.
2 changes: 1 addition & 1 deletion src/main/java/info/weboftrust/ldsignatures/LdProof.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public B jws(String jws) {
}

public static Builder<? extends Builder<?>> builder() {
return new Builder(new LdProof());
return new Builder<>(new LdProof());
}

public static LdProof fromJsonObject(Map<String, Object> jsonObject) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

public abstract class Canonicalizer {

private List<String> algorithms;
private final List<String> algorithms;

public abstract byte[] canonicalize(LdProof ldProof, JsonLDObject jsonLdObject) throws IOException, GeneralSecurityException, JsonLDException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class Canonicalizers {
public static final JCSCanonicalizer CANONICALIZER_JCSCANONICALIZER = new JCSCanonicalizer();
public static final URDNA2015Canonicalizer CANONICALIZER_URDNA2015CANONICALIZER = new URDNA2015Canonicalizer();

public static List<? extends Canonicalizer> CANONICALIZERS = List.of(
public static final List<? extends Canonicalizer> CANONICALIZERS = List.of(
CANONICALIZER_JCSCANONICALIZER,
CANONICALIZER_URDNA2015CANONICALIZER
);
Expand All @@ -31,12 +31,8 @@ public class Canonicalizers {
for (Canonicalizer canonicalizer : CANONICALIZERS) {
List<String> algorithms = canonicalizer.getAlgorithms();
for (String algorithm : algorithms) {
List<Canonicalizer> canonicalizersList = CANONICALIZERS_BY_ALGORITHM.get(algorithm);
if (canonicalizersList == null) {
canonicalizersList = new ArrayList<>();
CANONICALIZERS_BY_ALGORITHM.put(algorithm, canonicalizersList);
}
canonicalizersList.add(canonicalizer);
List<Canonicalizer> canonicalizersList = CANONICALIZERS_BY_ALGORITHM.computeIfAbsent(algorithm, k -> new ArrayList<>());
canonicalizersList.add(canonicalizer);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class LDSecurityContexts {

Expand All @@ -32,23 +33,23 @@ public class LDSecurityContexts {
CONTEXTS = new HashMap<>();

CONTEXTS.put(JSONLD_CONTEXT_W3ID_SECURITY_V1,
JsonDocument.of(MediaType.JSON_LD, LDSecurityContexts.class.getResourceAsStream("security-v1.jsonld")));
JsonDocument.of(MediaType.JSON_LD, Objects.requireNonNull(LDSecurityContexts.class.getResourceAsStream("security-v1.jsonld"))));
CONTEXTS.put(JSONLD_CONTEXT_W3ID_SECURITY_V2,
JsonDocument.of(MediaType.JSON_LD, LDSecurityContexts.class.getResourceAsStream("security-v2.jsonld")));
JsonDocument.of(MediaType.JSON_LD, Objects.requireNonNull(LDSecurityContexts.class.getResourceAsStream("security-v2.jsonld"))));
CONTEXTS.put(JSONLD_CONTEXT_W3ID_SECURITY_V3,
JsonDocument.of(MediaType.JSON_LD, LDSecurityContexts.class.getResourceAsStream("security-v3-unstable.jsonld")));
JsonDocument.of(MediaType.JSON_LD, Objects.requireNonNull(LDSecurityContexts.class.getResourceAsStream("security-v3-unstable.jsonld"))));
CONTEXTS.put(JSONLD_CONTEXT_W3ID_SECURITY_BBS_V1,
JsonDocument.of(MediaType.JSON_LD, LDSecurityContexts.class.getResourceAsStream("security-bbs-v1.jsonld")));
JsonDocument.of(MediaType.JSON_LD, Objects.requireNonNull(LDSecurityContexts.class.getResourceAsStream("security-bbs-v1.jsonld"))));
CONTEXTS.put(JSONLD_CONTEXT_W3ID_SUITES_SECP256K1_2019_V1,
JsonDocument.of(MediaType.JSON_LD, LDSecurityContexts.class.getResourceAsStream("suites-secp256k1-2019.jsonld")));
JsonDocument.of(MediaType.JSON_LD, Objects.requireNonNull(LDSecurityContexts.class.getResourceAsStream("suites-secp256k1-2019.jsonld"))));
CONTEXTS.put(JSONLD_CONTEXT_W3ID_SUITES_ED25519_2018_V1,
JsonDocument.of(MediaType.JSON_LD, LDSecurityContexts.class.getResourceAsStream("suites-ed25519-2018.jsonld")));
JsonDocument.of(MediaType.JSON_LD, Objects.requireNonNull(LDSecurityContexts.class.getResourceAsStream("suites-ed25519-2018.jsonld"))));
CONTEXTS.put(JSONLD_CONTEXT_W3ID_SUITES_ED25519_2020_V1,
JsonDocument.of(MediaType.JSON_LD, LDSecurityContexts.class.getResourceAsStream("suites-ed25519-2020.jsonld")));
JsonDocument.of(MediaType.JSON_LD, Objects.requireNonNull(LDSecurityContexts.class.getResourceAsStream("suites-ed25519-2020.jsonld"))));
CONTEXTS.put(JSONLD_CONTEXT_W3ID_SUITES_X25519_2019_V1,
JsonDocument.of(MediaType.JSON_LD, LDSecurityContexts.class.getResourceAsStream("suites-x25519-2019.jsonld")));
JsonDocument.of(MediaType.JSON_LD, Objects.requireNonNull(LDSecurityContexts.class.getResourceAsStream("suites-x25519-2019.jsonld"))));
CONTEXTS.put(JSONLD_CONTEXT_W3ID_SUITES_JWS_2020_V1,
JsonDocument.of(MediaType.JSON_LD, LDSecurityContexts.class.getResourceAsStream("suites-jws-2020.jsonld")));
JsonDocument.of(MediaType.JSON_LD, Objects.requireNonNull(LDSecurityContexts.class.getResourceAsStream("suites-jws-2020.jsonld"))));

for (Map.Entry<URI, JsonDocument> context : CONTEXTS.entrySet()) {
context.getValue().setDocumentUrl(context.getKey());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public BbsBlsSignature2020LdSigner() {
this((ByteSigner) null);
}

public static void sign(LdProof.Builder ldProofBuilder, byte[] signingInput, ByteSigner signer) throws GeneralSecurityException {
public static void sign(LdProof.Builder<? extends LdProof.Builder<?>> ldProofBuilder, byte[] signingInput, ByteSigner signer) throws GeneralSecurityException {

// sign

Expand All @@ -49,7 +49,7 @@ public static void sign(LdProof.Builder ldProofBuilder, byte[] signingInput, Byt
}

@Override
public void sign(LdProof.Builder ldProofBuilder, byte[] signingInput) throws GeneralSecurityException {
public void sign(LdProof.Builder<? extends LdProof.Builder<?>> ldProofBuilder, byte[] signingInput) throws GeneralSecurityException {

sign(ldProofBuilder, signingInput, this.getSigner());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public EcdsaKoblitzSignature2016LdSigner() {
this((ByteSigner) null);
}

public static void sign(LdProof.Builder ldProofBuilder, byte[] signingInput, ByteSigner signer) throws GeneralSecurityException {
public static void sign(LdProof.Builder<? extends LdProof.Builder<?>> ldProofBuilder, byte[] signingInput, ByteSigner signer) throws GeneralSecurityException {

// build the JWS and sign

Expand All @@ -60,7 +60,7 @@ public static void sign(LdProof.Builder ldProofBuilder, byte[] signingInput, Byt
}

@Override
public void sign(LdProof.Builder ldProofBuilder, byte[] signingInput) throws GeneralSecurityException {
public void sign(LdProof.Builder<? extends LdProof.Builder<?>> ldProofBuilder, byte[] signingInput) throws GeneralSecurityException {

sign(ldProofBuilder, signingInput, this.getSigner());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public EcdsaSecp256k1Signature2019LdSigner() {
this((ByteSigner) null);
}

public static void sign(LdProof.Builder ldProofBuilder, byte[] signingInput, ByteSigner signer) throws GeneralSecurityException {
public static void sign(LdProof.Builder<? extends LdProof.Builder<?>> ldProofBuilder, byte[] signingInput, ByteSigner signer) throws GeneralSecurityException {

// build the JWS and sign

Expand All @@ -60,7 +60,7 @@ public static void sign(LdProof.Builder ldProofBuilder, byte[] signingInput, Byt
}

@Override
public void sign(LdProof.Builder ldProofBuilder, byte[] signingInput) throws GeneralSecurityException {
public void sign(LdProof.Builder<? extends LdProof.Builder<?>> ldProofBuilder, byte[] signingInput) throws GeneralSecurityException {

sign(ldProofBuilder, signingInput, this.getSigner());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public Ed25519Signature2018LdSigner() {
this((ByteSigner) null);
}

public static void sign(LdProof.Builder ldProofBuilder, byte[] signingInput, ByteSigner signer) throws GeneralSecurityException {
public static void sign(LdProof.Builder<? extends LdProof.Builder<?>> ldProofBuilder, byte[] signingInput, ByteSigner signer) throws GeneralSecurityException {

// build the JWS and sign

Expand All @@ -59,7 +59,7 @@ public static void sign(LdProof.Builder ldProofBuilder, byte[] signingInput, Byt
}

@Override
public void sign(LdProof.Builder ldProofBuilder, byte[] signingInput) throws GeneralSecurityException {
public void sign(LdProof.Builder<? extends LdProof.Builder<?>> ldProofBuilder, byte[] signingInput) throws GeneralSecurityException {

sign(ldProofBuilder, signingInput, this.getSigner());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public Ed25519Signature2020LdSigner() {
this((ByteSigner) null);
}

public static void sign(LdProof.Builder ldProofBuilder, byte[] signingInput, ByteSigner signer) throws GeneralSecurityException {
public static void sign(LdProof.Builder<? extends LdProof.Builder<?>> ldProofBuilder, byte[] signingInput, ByteSigner signer) throws GeneralSecurityException {

// sign

Expand All @@ -43,7 +43,7 @@ public static void sign(LdProof.Builder ldProofBuilder, byte[] signingInput, Byt
}

@Override
public void sign(LdProof.Builder ldProofBuilder, byte[] signingInput) throws GeneralSecurityException {
public void sign(LdProof.Builder<? extends LdProof.Builder<?>> ldProofBuilder, byte[] signingInput) throws GeneralSecurityException {

sign(ldProofBuilder, signingInput, this.getSigner());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package info.weboftrust.ldsignatures.signer;

import com.danubetech.keyformats.crypto.ByteSigner;
import com.danubetech.keyformats.crypto.impl.Ed25519_EdDSA_PrivateKeySigner;
import com.danubetech.keyformats.crypto.impl.secp256k1_ES256K_PrivateKeySigner;
import com.danubetech.keyformats.jose.JWSAlgorithm;
import info.weboftrust.ldsignatures.LdProof;
import info.weboftrust.ldsignatures.canonicalizer.JCSCanonicalizer;
import info.weboftrust.ldsignatures.suites.JcsEcdsaSecp256k1Signature2019SignatureSuite;
import info.weboftrust.ldsignatures.suites.JcsEd25519Signature2020SignatureSuite;
import info.weboftrust.ldsignatures.suites.SignatureSuites;
import io.ipfs.multibase.Base58;
import org.bitcoinj.core.ECKey;
Expand All @@ -32,7 +30,7 @@ public JcsEcdsaSecp256k1Signature2019LdSigner() {
this((ByteSigner) null);
}

public static void sign(LdProof.Builder ldProofBuilder, byte[] signingInput, ByteSigner signer) throws GeneralSecurityException {
public static void sign(LdProof.Builder<? extends LdProof.Builder<?>> ldProofBuilder, byte[] signingInput, ByteSigner signer) throws GeneralSecurityException {

// sign

Expand All @@ -47,7 +45,7 @@ public static void sign(LdProof.Builder ldProofBuilder, byte[] signingInput, Byt
}

@Override
public void sign(LdProof.Builder ldProofBuilder, byte[] signingInput) throws GeneralSecurityException {
public void sign(LdProof.Builder<? extends LdProof.Builder<?>> ldProofBuilder, byte[] signingInput) throws GeneralSecurityException {

sign(ldProofBuilder, signingInput, this.getSigner());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import info.weboftrust.ldsignatures.suites.JcsEd25519Signature2020SignatureSuite;
import info.weboftrust.ldsignatures.suites.SignatureSuites;
import io.ipfs.multibase.Base58;
import io.ipfs.multibase.Multibase;

import java.security.GeneralSecurityException;
import java.util.Map;
Expand All @@ -30,7 +29,7 @@ public JcsEd25519Signature2020LdSigner() {
this((ByteSigner) null);
}

public static void sign(LdProof.Builder ldProofBuilder, byte[] signingInput, ByteSigner signer) throws GeneralSecurityException {
public static void sign(LdProof.Builder<? extends LdProof.Builder<?>> ldProofBuilder, byte[] signingInput, ByteSigner signer) throws GeneralSecurityException {

// sign

Expand All @@ -45,7 +44,7 @@ public static void sign(LdProof.Builder ldProofBuilder, byte[] signingInput, Byt
}

@Override
public void sign(LdProof.Builder ldProofBuilder, byte[] signingInput) throws GeneralSecurityException {
public void sign(LdProof.Builder<? extends LdProof.Builder<?>> ldProofBuilder, byte[] signingInput) throws GeneralSecurityException {

sign(ldProofBuilder, signingInput, this.getSigner());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ public JsonWebSignature2020LdSigner(ByteSigner signer) {

public JsonWebSignature2020LdSigner() {

this((ByteSigner) null);
this(null);
}

public static void sign(LdProof.Builder ldProofBuilder, byte[] signingInput, ByteSigner signer) throws GeneralSecurityException {
public static void sign(LdProof.Builder<? extends LdProof.Builder<?>> ldProofBuilder, byte[] signingInput, ByteSigner signer) throws GeneralSecurityException {

// build the JWS and sign

Expand All @@ -53,7 +53,7 @@ public static void sign(LdProof.Builder ldProofBuilder, byte[] signingInput, Byt
}

@Override
public void sign(LdProof.Builder ldProofBuilder, byte[] signingInput) throws GeneralSecurityException {
public void sign(LdProof.Builder<? extends LdProof.Builder<?>> ldProofBuilder, byte[] signingInput) throws GeneralSecurityException {

sign(ldProofBuilder, signingInput, this.getSigner());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static LdSigner<? extends SignatureSuite> ldSignerForSignatureSuite(Signa
return LdSignerRegistry.getLdSignerBySignatureSuite(signatureSuite);
}

public abstract void sign(LdProof.Builder ldProofBuilder, byte[] signingInput) throws GeneralSecurityException;
public abstract void sign(LdProof.Builder<? extends LdProof.Builder<?>> ldProofBuilder, byte[] signingInput) throws GeneralSecurityException;

public LdProof sign(JsonLDObject jsonLdObject, boolean addToJsonLdObject, boolean defaultContexts) throws IOException, GeneralSecurityException, JsonLDException {

Expand All @@ -93,7 +93,7 @@ public LdProof sign(JsonLDObject jsonLdObject, boolean addToJsonLdObject, boolea

// sign

LdProof.Builder ldProofBuilder = LdProof.builder()
LdProof.Builder<? extends LdProof.Builder<?>> ldProofBuilder = LdProof.builder()
.base(ldProof)
.defaultContexts(defaultContexts);
this.sign(ldProofBuilder, canonicalizationResult);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public RsaSignature2018LdSigner() {
this((ByteSigner) null);
}

public static void sign(LdProof.Builder ldProofBuilder, byte[] signingInput, ByteSigner signer) throws GeneralSecurityException {
public static void sign(LdProof.Builder<? extends LdProof.Builder<?>> ldProofBuilder, byte[] signingInput, ByteSigner signer) throws GeneralSecurityException {

// build the JWS and sign

Expand All @@ -60,7 +60,7 @@ public static void sign(LdProof.Builder ldProofBuilder, byte[] signingInput, Byt
}

@Override
public void sign(LdProof.Builder ldProofBuilder, byte[] signingInput) throws GeneralSecurityException {
public void sign(LdProof.Builder<? extends LdProof.Builder<?>> ldProofBuilder, byte[] signingInput) throws GeneralSecurityException {

sign(ldProofBuilder, signingInput, this.getSigner());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ public abstract class SignatureSuite {

public static final URI URI_TYPE_SIGNATURESUITE = URI.create("https://w3id.org/security#SignatureSuite");

private String term;
private URI id;
private URI type;
private URI canonicalizationAlgorithm;
private URI digestAlgorithm;
private URI proofAlgorithm;
private List<KeyTypeName> keyTypeNames;
private Map<KeyTypeName, List<String>> jwsAlgorithmForKeyTypeName;
private List<URI> supportedJsonLDContexts;
private final String term;
private final URI id;
private final URI type;
private final URI canonicalizationAlgorithm;
private final URI digestAlgorithm;
private final URI proofAlgorithm;
private final List<KeyTypeName> keyTypeNames;
private final Map<KeyTypeName, List<String>> jwsAlgorithmForKeyTypeName;
private final List<URI> supportedJsonLDContexts;

public SignatureSuite(String term, URI id, URI canonicalizationAlgorithm, URI digestAlgorithm, URI proofAlgorithm, List<KeyTypeName> keyTypeNames, Map<KeyTypeName, List<String>> jwsAlgorithmForKeyTypeName,List<URI> supportedJsonLDContexts) {
this.term = term;
Expand All @@ -39,12 +39,12 @@ public List<String> findJwsAlgorithmsForKeyTypeName(KeyTypeName keyTypeName) {

public String findDefaultJwsAlgorithmForKeyTypeName(KeyTypeName keyTypeName) {
List<String> foundAlgorithmsForKeyTypeName = this.findJwsAlgorithmsForKeyTypeName(keyTypeName);
return (foundAlgorithmsForKeyTypeName == null || foundAlgorithmsForKeyTypeName.size() < 1) ? null : foundAlgorithmsForKeyTypeName.get(0);
return (foundAlgorithmsForKeyTypeName == null || foundAlgorithmsForKeyTypeName.isEmpty()) ? null : foundAlgorithmsForKeyTypeName.get(0);
}

public URI getDefaultSupportedJsonLDContext() {
List<URI> supportedJsonLDContexts = this.getSupportedJsonLDContexts();
return (supportedJsonLDContexts == null || supportedJsonLDContexts.size() < 1) ? null : supportedJsonLDContexts.get(0);
return (supportedJsonLDContexts == null || supportedJsonLDContexts.isEmpty()) ? null : supportedJsonLDContexts.get(0);
}

public String getTerm() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class SignatureSuites {
public static final BbsBlsSignature2020SignatureSuite SIGNATURE_SUITE_BBSBLSSIGNATURE2020 = new BbsBlsSignature2020SignatureSuite();
public static final JsonWebSignature2020SignatureSuite SIGNATURE_SUITE_JSONWEBSIGNATURE2020 = new JsonWebSignature2020SignatureSuite();

public static List<? extends SignatureSuite> SIGNATURE_SUITES = List.of(
public static final List<? extends SignatureSuite> SIGNATURE_SUITES = List.of(
SIGNATURE_SUITE_RSASIGNATURE2018,
SIGNATURE_SUITE_ED25519SIGNATURE2018,
SIGNATURE_SUITE_ED25519SIGNATURE2020,
Expand Down Expand Up @@ -56,12 +56,8 @@ public class SignatureSuites {
for (SignatureSuite signatureSuite : SIGNATURE_SUITES) {
List<KeyTypeName> keyTypeNames = signatureSuite.getKeyTypeNames();
for (KeyTypeName keyTypeName : keyTypeNames) {
List<SignatureSuite> signatureSuitesList = SIGNATURE_SUITES_BY_KEY_TYPE_NAME.get(keyTypeName);
if (signatureSuitesList == null) {
signatureSuitesList = new ArrayList<>();
SIGNATURE_SUITES_BY_KEY_TYPE_NAME.put(keyTypeName, signatureSuitesList);
}
signatureSuitesList.add(signatureSuite);
List<SignatureSuite> signatureSuitesList = SIGNATURE_SUITES_BY_KEY_TYPE_NAME.computeIfAbsent(keyTypeName, k -> new ArrayList<>());
signatureSuitesList.add(signatureSuite);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@

public class DetachedJWSObject extends JWSObject {

private static final long serialVersionUID = 4931892811891099374L;

private Base64URL parsedSignature;
private final Base64URL parsedSignature;

public DetachedJWSObject(JWSHeader jwsHeader, Payload payload, Base64URL parsedSignature) throws ParseException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
import com.danubetech.keyformats.crypto.ByteVerifier;
import com.danubetech.keyformats.crypto.impl.Ed25519_EdDSA_PublicKeyVerifier;
import com.danubetech.keyformats.jose.JWSAlgorithm;
import foundation.identity.jsonld.JsonLDUtils;
import info.weboftrust.ldsignatures.LdProof;
import info.weboftrust.ldsignatures.canonicalizer.JCSCanonicalizer;
import info.weboftrust.ldsignatures.suites.JcsEd25519Signature2020SignatureSuite;
import info.weboftrust.ldsignatures.suites.SignatureSuites;
import io.ipfs.multibase.Base58;
import io.ipfs.multibase.Multibase;

import java.security.GeneralSecurityException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public JsonWebSignature2020LdVerifier(ByteVerifier verifier) {

public JsonWebSignature2020LdVerifier() {

this((ByteVerifier) null);
this(null);
}

public static boolean verify(byte[] signingInput, LdProof ldProof, ByteVerifier verifier) throws GeneralSecurityException {
Expand Down
Loading

0 comments on commit 4e9db7b

Please sign in to comment.