Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues/263 - Merge in com.syntifi.crypto sources #264

Merged
merged 7 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ apply plugin: 'java'

group = 'network.casper'
// Version number update for release
version='2.5.3'
version='2.5.4'
sourceCompatibility = 1.8
targetCompatibility = 1.8

Expand All @@ -26,9 +26,11 @@ repositories {
dependencies {
implementation "dev.oak3:sbs4j:${sbs4jVersion}"
implementation "io.github.oak:jsonrpc4j:${jsonrpc4jVersion}"
implementation "com.syntifi.crypto:crypto-key-common:${cryptokeyVersion}"
implementation "com.syntifi.crypto:crypto-key-ed25519:${cryptokeyVersion}"
implementation "com.syntifi.crypto:crypto-key-secp256k1:${cryptokeyVersion}"
// implementation "com.syntifi.crypto:crypto-key-common:${cryptokeyVersion}"
// implementation "com.syntifi.crypto:crypto-key-ed25519:${cryptokeyVersion}"
// implementation "com.syntifi.crypto:crypto-key-secp256k1:${cryptokeyVersion}"
implementation "org.bouncycastle:bcpkix-jdk15on:${bouncyCastleVersion}"
implementation "org.web3j:core:${web3jVersion}"
implementation "com.fasterxml.jackson.core:jackson-core:${jacksonVersion}"
implementation "com.fasterxml.jackson.core:jackson-databind:${jacksonVersion}"
implementation "com.fasterxml.jackson.core:jackson-annotations:${jacksonVersion}"
Expand Down
14 changes: 8 additions & 6 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
sbs4jVersion=0.1.8
bouncyCastleVersion=1.69
commonsIoVersion=2.11.0
cryptokeyVersion=0.4.0
cxfRtRsSseVersion=3.5.5
lombokPluginVersion=6.2.0
jupiterVersion=5.9.0
jsonrpc4jVersion=1.6.1-oak
jacksonVersion=2.13.4
javaTuplesVersion=1.2
jodaTimeVersion=2.11.1
jsonassertVersion=1.5.1
jsonPathAssertVersion=2.7.0
jsonrpc4jVersion=1.6.1-oak
jupiterVersion=5.9.0
mockwebserverVersion=4.10.0
log4jVersion=2.18.0
lombokPluginVersion=6.2.0
sbs4jVersion=0.1.8
slf4jApiVersion=2.0.0
javaTuplesVersion=1.2
jodaTimeVersion=2.11.1
web3jVersion=5.0.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.casper.sdk.exception;

/**
* @author [email protected]
*/
public class InvalidKeyBytesException extends RuntimeException {

public InvalidKeyBytesException(Throwable cause) {
super(cause);
}
}
Original file line number Diff line number Diff line change
@@ -1,36 +1,31 @@
package com.casper.sdk.jackson.deserializer;

import java.io.IOException;

import com.casper.sdk.exception.NoSuchTypeException;
import com.casper.sdk.jackson.resolver.CLValueResolver;
import com.casper.sdk.model.clvalue.AbstractCLValue;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.jsontype.TypeIdResolver;
import com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer;
import com.fasterxml.jackson.databind.node.TreeTraversingParser;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.casper.sdk.jackson.resolver.CLValueResolver;
import com.casper.sdk.model.clvalue.AbstractCLValue;

import java.io.IOException;

/**
* Core Deserializer for the CLValue property. This deserializer is used by the
* {@link CLValueResolver} to return the correct CLType object in Java depending
* on the cl_type sent over json
*
*
* @author Alexandre Carvalho
* @author Andre Bertolace
* @since 0.0.1
* @see AbstractCLValue
* @since 0.0.1
*/
public abstract class AbstractAnyOfDeserializer extends AsPropertyTypeDeserializer {

protected AbstractAnyOfDeserializer(final JavaType bt, final TypeIdResolver idRes, final String typePropertyName,
final boolean typeIdVisible, JavaType defaultImpl) {
final boolean typeIdVisible, JavaType defaultImpl) {
super(bt, idRes, typePropertyName, typeIdVisible, defaultImpl);
}

Expand All @@ -50,7 +45,7 @@ public Object deserializeTypedFromObject(final JsonParser jp, final Deserializat
} catch (NoSuchTypeException e) {
throw new IOException("Parse error", e);
}
TypeFactory factory = new ObjectMapper().getTypeFactory();
TypeFactory factory = ctxt.getTypeFactory();
JavaType type = factory.constructType(subType);

try (JsonParser jsonParser = new TreeTraversingParser(node, jp.getCodec())) {
Expand All @@ -64,9 +59,9 @@ public Object deserializeTypedFromObject(final JsonParser jp, final Deserializat

/**
* Returns the node which contains the type key.
*
* <p>
* Override if you have a child node which holds the type information.
*
*
* @param currentNode the current deserialization node
* @return node which contains the type key.
*/
Expand All @@ -76,7 +71,7 @@ protected JsonNode getTypeNode(JsonNode currentNode) {

/**
* Method that returns the instance of the found type
*
*
* @param classType the name of the class type
* @return {@link Class} of the type
* @throws NoSuchTypeException thrown if no type for the given classType String
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/syntifi/crypto/key/ASN1Identifiers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.syntifi.crypto.key;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;

/**
* ASN1 identifiers for working with key cryptography
*
* @author Alexandre Carvalho
* @author Andre Bertolace
* @since 0.1.0
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ASN1Identifiers {

public static final ASN1ObjectIdentifier Secp256k1OIDCurve = new ASN1ObjectIdentifier("1.3.132.0.10");
public static final ASN1ObjectIdentifier Secp256k1OIDkey = new ASN1ObjectIdentifier("1.2.840.10045.2.1");

public static final ASN1ObjectIdentifier Ed25519OID = new ASN1ObjectIdentifier("1.3.101.112");

public static final String PUBLIC_KEY_DER_HEADER = "PUBLIC KEY";
public static final String PRIVATE_KEY_DER_HEADER = "PRIVATE KEY";
public static final String EC_PRIVATE_KEY_DER_HEADER = "EC PRIVATE KEY";
}
86 changes: 86 additions & 0 deletions src/main/java/com/syntifi/crypto/key/AbstractPrivateKey.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.syntifi.crypto.key;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.*;
import java.security.GeneralSecurityException;

/**
* Abstract class for needed shared functionalities
*
* @author Alexandre Carvalho
* @author Andre Bertolace
* @since 0.1.0
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public abstract class AbstractPrivateKey {

private byte[] key;

/**
* Loads a private key from a byte array
*
* @param privateKey the private key bytes
*/
public abstract void loadPrivateKey(final byte[] privateKey) throws IOException;

/**
* Reads the private key from a file
*
* @param filename the source filename
* @throws IOException thrown if an error occurs reading the file
*/
public final void readPrivateKey(final String filename) throws IOException {
try (final Reader fileReader = new FileReader(filename)) {
readPrivateKey(fileReader);
}
}

/**
* Reads the private key from a stream
*
* @param reader the source of the private key
* @throws IOException thrown if an error occurs reading the file
*/
public abstract void readPrivateKey(final Reader reader) throws IOException;

/**
* Writes the private key to a file
*
* @param filename the target filename
* @throws IOException thrown if an error occurs writing the file
*/
public final void writePrivateKey(final String filename) throws IOException {
try (final Writer fileWriter = new FileWriter(filename)) {
writePrivateKey(fileWriter);
}
}

/**
* Writes the private key to a file
*
* @param writer the target writer
* @throws IOException thrown if an error occurs writing the file
*/
public abstract void writePrivateKey(final Writer writer) throws IOException;

/**
* Signs a message with the loaded key
*
* @param message message to sign
* @return signed message
* @throws GeneralSecurityException thrown if an error occurs processing message or signature
*/
public abstract byte[] sign(final byte[] message) throws GeneralSecurityException;

/**
* Derives the public key from the loaded private key
*
* @return the derived {@link AbstractPublicKey}
*/
public abstract AbstractPublicKey derivePublicKey();
}
80 changes: 80 additions & 0 deletions src/main/java/com/syntifi/crypto/key/AbstractPublicKey.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.syntifi.crypto.key;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.*;
import java.security.GeneralSecurityException;

/**
* Abstract class for needed shared functionalities
*
* @author Alexandre Carvalho
* @author Andre Bertolace
* @since 0.1.0
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public abstract class AbstractPublicKey {

private byte[] key;

/**
* Loads a public key from a byte array
*
* @param publicKey the public key bytes
*/
public abstract void loadPublicKey(final byte[] publicKey) throws IOException;

/**
* Reads the public key from a file
*
* @param filename the source filename
* @throws IOException thrown if an error occurs reading the file
*/
public final void readPublicKey(final String filename) throws IOException {
try (final Reader fileReader = new FileReader(filename)) {
readPublicKey(fileReader);
}
}

/**
* Reads the public key from a file
*
* @param reader the source filename
* @throws IOException thrown if an error occurs reading the file
*/
public abstract void readPublicKey(final Reader reader) throws IOException;

/**
* Writes the public key to a file
*
* @param filename the target filename
* @throws IOException thrown if an error occurs writing the file
*/
public final void writePublicKey(final String filename) throws IOException {
try (final Writer fileWriter = new FileWriter(filename)) {
writePublicKey(fileWriter);
}
}

/**
* Writes the public key to a file
*
* @param writer the target to write the public key
* @throws IOException thrown if an error occurs writing the file
*/
public abstract void writePublicKey(final Writer writer) throws IOException;

/**
* Verifies message with given signature
*
* @param message the signed message
* @param signature the signature to check against
* @return true if matches, false otherwise
* @throws GeneralSecurityException thrown if an error occurs processing message and signature
*/
public abstract Boolean verify(final byte[] message, final byte[] signature) throws GeneralSecurityException;
}
Loading
Loading