Skip to content

Commit

Permalink
simplifying code
Browse files Browse the repository at this point in the history
  • Loading branch information
usmansaleem committed Nov 7, 2024
1 parent 30c6f1b commit b8aa52d
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import io.vertx.core.Handler;
import io.vertx.ext.web.RoutingContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.tuweni.bytes.Bytes32;

public class CommitBoostGenerateProxyKeyHandler implements Handler<RoutingContext> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import static io.vertx.core.http.HttpHeaders.CONTENT_TYPE;
import static tech.pegasys.web3signer.core.service.http.handlers.ContentTypes.TEXT_PLAIN_UTF_8;
import static tech.pegasys.web3signer.core.service.http.handlers.signing.SignerForIdentifier.toBytes;
import static tech.pegasys.web3signer.signing.util.IdentifierUtils.normaliseIdentifier;

import tech.pegasys.web3signer.core.service.http.metrics.HttpApiMetrics;
Expand All @@ -23,6 +22,7 @@
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.RequestBody;
import io.vertx.ext.web.RoutingContext;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.tuweni.bytes.Bytes;
Expand All @@ -47,7 +47,7 @@ public void handle(final RoutingContext routingContext) {
final Bytes data;
try {
data = getDataToSign(routingContext.body());
} catch (final IllegalArgumentException e) {
} catch (final RuntimeException e) {
metrics.getMalformedRequestCounter().inc();
LOG.debug("Invalid signing request", e);
routingContext.fail(400);
Expand All @@ -72,6 +72,14 @@ private void respondWithSignature(final RoutingContext routingContext, final Str

private Bytes getDataToSign(final RequestBody requestBody) {
final JsonObject jsonObject = requestBody.asJsonObject();
return toBytes(jsonObject.getString("data"));

if (!jsonObject.containsKey("data")) {
throw new IllegalArgumentException("Request must contain a 'data' field");
}
if (StringUtils.isBlank(jsonObject.getString("data"))) {
throw new IllegalArgumentException("Data field must not be empty");
}

return Bytes.fromHexString(jsonObject.getString("data"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,13 @@

import java.util.Optional;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.tuweni.bytes.Bytes;

/**
* This class wraps the {@link ArtifactSignerProvider} and provides a way to check if a signer is
* available for a given identifier and to sign a message.
*/
public class SignerForIdentifier {
private static final Logger LOG = LogManager.getLogger();
private final ArtifactSignerProvider signerProvider;

public SignerForIdentifier(final ArtifactSignerProvider signerProvider) {
Expand All @@ -47,30 +43,16 @@ public Optional<String> sign(final String identifier, final Bytes data) {
return signerProvider.getSigner(identifier).map(signer -> signer.sign(data).asHex());
}

public Optional<ArtifactSignature> signAndGetArtifactSignature(
final String identifier, final Bytes data) {
return signerProvider.getSigner(identifier).map(signer -> signer.sign(data));
}

/**
* Converts hex string to bytes
* Sign data for given identifier and return ArtifactSignature. Useful for SECP signing.
*
* @param data hex string
* @return Bytes
* @throws IllegalArgumentException if data is invalid i.e. not a valid hex string, null or empty
* @param identifier The identifier for which to sign data.
* @param data Bytes which is signed
* @return Optional ArtifactSignature. Empty if no signer available for given identifier
*/
public static Bytes toBytes(final String data) {
final Bytes dataToSign;
try {
if (StringUtils.isBlank(data)) {
throw new IllegalArgumentException("Blank data");
}
dataToSign = Bytes.fromHexString(data);
} catch (final IllegalArgumentException e) {
LOG.debug("Invalid hex string {}", data, e);
throw e;
}
return dataToSign;
public Optional<ArtifactSignature> signAndGetArtifactSignature(
final String identifier, final Bytes data) {
return signerProvider.getSigner(identifier).map(signer -> signer.sign(data));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import tech.pegasys.web3signer.core.service.jsonrpc.JsonRpcRequest;
import tech.pegasys.web3signer.core.service.jsonrpc.exceptions.JsonRpcException;
import tech.pegasys.web3signer.core.service.jsonrpc.handlers.ResultProvider;
import tech.pegasys.web3signer.signing.SecpArtifactSignature;

import java.util.List;

Expand All @@ -35,8 +34,7 @@ public class EthSignResultProvider implements ResultProvider<String> {

private final SignerForIdentifier transactionSignerProvider;

public EthSignResultProvider(
final SignerForIdentifier transactionSignerProvider) {
public EthSignResultProvider(final SignerForIdentifier transactionSignerProvider) {
this.transactionSignerProvider = transactionSignerProvider;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import tech.pegasys.web3signer.core.service.jsonrpc.handlers.sendtransaction.transaction.EthTransaction;
import tech.pegasys.web3signer.core.service.jsonrpc.handlers.sendtransaction.transaction.Transaction;
import tech.pegasys.web3signer.core.service.jsonrpc.handlers.signing.TransactionSerializer;
import tech.pegasys.web3signer.signing.SecpArtifactSignature;

import java.util.List;

Expand All @@ -43,9 +42,7 @@ public class EthSignTransactionResultProvider implements ResultProvider<String>
private final SignerForIdentifier secpSigner;

public EthSignTransactionResultProvider(
final long chainId,
final SignerForIdentifier secpSigner,
final JsonDecoder decoder) {
final long chainId, final SignerForIdentifier secpSigner, final JsonDecoder decoder) {
this.chainId = chainId;
this.decoder = decoder;
this.secpSigner = secpSigner;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import tech.pegasys.web3signer.core.service.jsonrpc.JsonRpcRequest;
import tech.pegasys.web3signer.core.service.jsonrpc.exceptions.JsonRpcException;
import tech.pegasys.web3signer.core.service.jsonrpc.handlers.ResultProvider;
import tech.pegasys.web3signer.signing.SecpArtifactSignature;

import java.io.IOException;
import java.util.List;
Expand All @@ -36,8 +35,7 @@ public class EthSignTypedDataResultProvider implements ResultProvider<String> {

private final SignerForIdentifier transactionSignerProvider;

public EthSignTypedDataResultProvider(
final SignerForIdentifier transactionSignerProvider) {
public EthSignTypedDataResultProvider(final SignerForIdentifier transactionSignerProvider) {
this.transactionSignerProvider = transactionSignerProvider;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import tech.pegasys.web3signer.core.service.jsonrpc.handlers.sendtransaction.transaction.Transaction;
import tech.pegasys.web3signer.core.service.jsonrpc.handlers.sendtransaction.transaction.TransactionFactory;
import tech.pegasys.web3signer.core.service.jsonrpc.handlers.signing.TransactionSerializer;
import tech.pegasys.web3signer.signing.SecpArtifactSignature;

import io.vertx.core.json.DecodeException;
import io.vertx.ext.web.RoutingContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public class EthSignTransactionResultProviderTest {
private static JsonDecoder jsonDecoder;
private static long chainId;

@Mock SignerForIdentifier<SecpArtifactSignature> mockSignerForIdentifier;
@Mock SignerForIdentifier mockSignerForIdentifier;

@BeforeAll
static void beforeAll() {
Expand Down

0 comments on commit b8aa52d

Please sign in to comment.