Skip to content

Commit

Permalink
Use CommitBoostSignerProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
usmansaleem committed Nov 6, 2024
1 parent 0ddab6e commit 71435c9
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,11 @@
*/
package tech.pegasys.web3signer.core.routes.eth2;

import static tech.pegasys.web3signer.signing.KeyType.BLS;

import tech.pegasys.teku.spec.Spec;
import tech.pegasys.web3signer.core.Context;
import tech.pegasys.web3signer.core.routes.Web3SignerRoute;
import tech.pegasys.web3signer.core.service.http.handlers.commitboost.CommitBoostGenerateProxyKeyHandler;
import tech.pegasys.web3signer.core.service.http.handlers.signing.SignerForIdentifier;
import tech.pegasys.web3signer.signing.ArtifactSignerProvider;
import tech.pegasys.web3signer.signing.BlsArtifactSignature;
import tech.pegasys.web3signer.signing.config.CommitBoostParameters;
import tech.pegasys.web3signer.signing.config.DefaultArtifactSignerProvider;

Expand All @@ -30,7 +26,7 @@
public class CommitBoostGenerateProxyKeyRoute implements Web3SignerRoute {
private static final String PATH = "/signer/v1/generate_proxy_key";
private final Context context;
private final SignerForIdentifier<BlsArtifactSignature> blsSigner;
private final ArtifactSignerProvider artifactSignerProvider;
private final CommitBoostParameters commitBoostParameters;
private final Spec eth2Spec;

Expand All @@ -43,15 +39,11 @@ public CommitBoostGenerateProxyKeyRoute(
this.eth2Spec = eth2Spec;

// there should be only one DefaultArtifactSignerProvider in eth2 mode
final ArtifactSignerProvider artifactSignerProvider =
artifactSignerProvider =
context.getArtifactSignerProviders().stream()
.filter(p -> p instanceof DefaultArtifactSignerProvider)
.findFirst()
.orElseThrow();

blsSigner =
new SignerForIdentifier<>(
artifactSignerProvider, sig -> sig.getSignatureData().toString(), BLS);
}

@Override
Expand All @@ -60,7 +52,8 @@ public void register() {
.getRouter()
.route(HttpMethod.POST, PATH)
.blockingHandler(
new CommitBoostGenerateProxyKeyHandler(blsSigner, commitBoostParameters, eth2Spec),
new CommitBoostGenerateProxyKeyHandler(
artifactSignerProvider, commitBoostParameters, eth2Spec),
false)
.failureHandler(context.getErrorHandler())
.failureHandler(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
import tech.pegasys.web3signer.core.service.http.SigningObjectMapperFactory;
import tech.pegasys.web3signer.core.service.http.handlers.commitboost.json.GenerateProxyKeyBody;
import tech.pegasys.web3signer.core.service.http.handlers.commitboost.json.ProxyDelegation;
import tech.pegasys.web3signer.core.service.http.handlers.commitboost.json.SignRequestType;
import tech.pegasys.web3signer.core.service.http.handlers.commitboost.json.SignedProxyDelegation;
import tech.pegasys.web3signer.core.service.http.handlers.signing.SignerForIdentifier;
import tech.pegasys.web3signer.signing.ArtifactSigner;
import tech.pegasys.web3signer.signing.ArtifactSignerProvider;
import tech.pegasys.web3signer.signing.config.CommitBoostParameters;

import java.util.Optional;
Expand All @@ -33,7 +34,7 @@
import io.vertx.ext.web.RoutingContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;

public class CommitBoostGenerateProxyKeyHandler implements Handler<RoutingContext> {
private static final Logger LOG = LogManager.getLogger();
Expand All @@ -42,17 +43,17 @@ public class CommitBoostGenerateProxyKeyHandler implements Handler<RoutingContex
private static final int BAD_REQUEST = 400;
private static final int INTERNAL_ERROR = 500;

private final SignerForIdentifier<?> signerForIdentifier;
private final ProxyKeyGenerator proxyKeyGenerator;
private final SigningRootGenerator signingRootGenerator;
private final CommitBoostSignerProvider commitBoostSignerProvider;

public CommitBoostGenerateProxyKeyHandler(
final SignerForIdentifier<?> signerForIdentifier,
final ArtifactSignerProvider artifactSignerProvider,
final CommitBoostParameters commitBoostParameters,
final Spec eth2Spec) {
this.signerForIdentifier = signerForIdentifier;
this.proxyKeyGenerator = new ProxyKeyGenerator(commitBoostParameters);
this.signingRootGenerator =
commitBoostSignerProvider = new CommitBoostSignerProvider(artifactSignerProvider);
proxyKeyGenerator = new ProxyKeyGenerator(commitBoostParameters);
signingRootGenerator =
new SigningRootGenerator(eth2Spec, commitBoostParameters.getGenesisValidatorsRoot());
}

Expand All @@ -71,28 +72,31 @@ public void handle(final RoutingContext context) {

// Check for identifier, if not exist, fail with 404
final String identifier = normaliseIdentifier(proxyKeyBody.blsPublicKey());
if (!signerForIdentifier.isSignerAvailable(identifier)) {
final boolean signerAvailable =
commitBoostSignerProvider.isSignerAvailable(identifier, SignRequestType.CONSENSUS);
if (!signerAvailable) {
context.fail(NOT_FOUND);
return;
}

// Generate actual proxy key and encrypted keystore based on signature scheme
final ArtifactSigner artifactSigner;
try {
artifactSigner =
// Generate actual proxy key and encrypted keystore based on signature scheme
final ArtifactSigner proxyArtifactSigner =
switch (proxyKeyBody.scheme()) {
case BLS -> proxyKeyGenerator.generateBLSProxyKey(identifier);
case ECDSA -> proxyKeyGenerator.generateECProxyKey(identifier);
};
// Add generated proxy key to DefaultArtifactSignerProvider
signerForIdentifier.getSignerProvider().addProxySigner(artifactSigner, identifier).get();

// Add generated proxy ArtifactSigner to ArtifactSignerProvider
commitBoostSignerProvider.addProxySigner(proxyArtifactSigner, identifier);

final ProxyDelegation proxyDelegation =
new ProxyDelegation(identifier, artifactSigner.getIdentifier());
final Bytes signingRoot =
new ProxyDelegation(identifier, proxyArtifactSigner.getIdentifier());
final Bytes32 signingRoot =
signingRootGenerator.computeSigningRoot(
proxyDelegation.toMerkleizable(proxyKeyBody.scheme()).hashTreeRoot());
final Optional<String> optionalSig = signerForIdentifier.sign(identifier, signingRoot);
final Optional<String> optionalSig =
commitBoostSignerProvider.sign(identifier, SignRequestType.CONSENSUS, signingRoot);
if (optionalSig.isEmpty()) {
context.fail(NOT_FOUND);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,22 @@
public class CommitBoostSignerProvider {
private final ArtifactSignerProvider artifactSignerProvider;

/**
* Constructor for the CommitBoostSignerProvider
*
* @param artifactSignerProvider The {@link ArtifactSignerProvider} to use for signing
*/
public CommitBoostSignerProvider(final ArtifactSignerProvider artifactSignerProvider) {
this.artifactSignerProvider = artifactSignerProvider;
}

/**
* Check if a signer is available for the given identifier and type
*
* @param identifier The identifier to check
* @param type The type of signer to check
* @return true if a signer is available, false otherwise
*/
public boolean isSignerAvailable(final String identifier, final SignRequestType type) {
return switch (type) {
case CONSENSUS -> artifactSignerProvider.availableIdentifiers().contains(identifier);
Expand All @@ -55,6 +67,15 @@ public boolean isSignerAvailable(final String identifier, final SignRequestType
};
}

/**
* Sign a message with the given identifier and type
*
* @param identifier The identifier to sign with
* @param type The type of signer to use
* @param signingRoot The root to sign
* @return An optional string of the signature in hex format. Empty if no signer available for
* given identifier
*/
public Optional<String> sign(
final String identifier, final SignRequestType type, final Bytes32 signingRoot) {
final Optional<ArtifactSigner> optionalArtifactSigner =
Expand All @@ -78,4 +99,8 @@ public Optional<String> sign(
})
.orElse(Optional.empty());
}

public void addProxySigner(final ArtifactSigner artifactSigner, final String identifier) {
artifactSignerProvider.addProxySigner(artifactSigner, identifier);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,6 @@ public Optional<T> signAndGetArtifactSignature(final String identifier, final By
return signerProvider.getSigner(identifier).map(signer -> (T) signer.sign(data));
}

/**
* Get the signer provider
*
* @return signer provider
*/
public ArtifactSignerProvider getSignerProvider() {
return signerProvider;
}

/**
* Converts hex string to bytes
*
Expand Down

0 comments on commit 71435c9

Please sign in to comment.