Skip to content

Commit

Permalink
unit test for signing root generator
Browse files Browse the repository at this point in the history
  • Loading branch information
usmansaleem committed Oct 29, 2024
1 parent 02562a0 commit 40d2c27
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,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;

public class CommitBoostGenerateProxyKeyHandler implements Handler<RoutingContext> {
private static final Logger LOG = LogManager.getLogger();
Expand Down Expand Up @@ -85,33 +86,25 @@ public void handle(final RoutingContext context) {
};
// Add generated proxy key to DefaultArtifactSignerProvider
signerForIdentifier.getSignerProvider().addProxySigner(artifactSigner, identifier).get();
} catch (final Exception e) {
context.fail(INTERNAL_ERROR, e);
return;
}

final ProxyKeyMessage proxyKeyMessage =
new ProxyKeyMessage(identifier, artifactSigner.getIdentifier());
final Optional<String> optionalSig =
signerForIdentifier.sign(
identifier,
signingRootGenerator.computeSigningRoot(proxyKeyMessage, proxyKeyBody.scheme()));
if (optionalSig.isEmpty()) {
context.fail(NOT_FOUND);
return;
}
final ProxyKeyMessage proxyKeyMessage =
new ProxyKeyMessage(identifier, artifactSigner.getIdentifier());
final Bytes signingRoot =
signingRootGenerator.computeSigningRoot(proxyKeyMessage, proxyKeyBody.scheme());
final Optional<String> optionalSig = signerForIdentifier.sign(identifier, signingRoot);
if (optionalSig.isEmpty()) {
context.fail(NOT_FOUND);
return;
}

final GenerateProxyKeyResponse generateProxyKeyResponse =
new GenerateProxyKeyResponse(proxyKeyMessage, optionalSig.get());
final GenerateProxyKeyResponse generateProxyKeyResponse =
new GenerateProxyKeyResponse(proxyKeyMessage, optionalSig.get());

// Encode and send response
try {
// Encode and send response
final String jsonEncoded = JSON_MAPPER.writeValueAsString(generateProxyKeyResponse);
context.response().putHeader(CONTENT_TYPE, JSON_UTF_8).end(jsonEncoded);
} catch (final JsonProcessingException e) {
// this is not meant to happen
LOG.error("Failed to encode GenerateProxyKeyResponse to JSON", e);
context.fail(INTERNAL_ERROR);
} catch (final Exception e) {
context.fail(INTERNAL_ERROR, e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,33 @@

import static org.assertj.core.api.Assertions.assertThat;

import tech.pegasys.teku.bls.BLSKeyPair;
import tech.pegasys.teku.bls.BLSPublicKey;
import tech.pegasys.teku.bls.BLSSecretKey;
import tech.pegasys.teku.networks.Eth2NetworkConfiguration;
import tech.pegasys.teku.spec.Spec;
import tech.pegasys.teku.spec.datastructures.util.ChainDataLoader;
import tech.pegasys.teku.spec.networks.Eth2Network;
import tech.pegasys.web3signer.core.service.http.handlers.commitboost.json.ProxyKeyMessage;
import tech.pegasys.web3signer.core.service.http.handlers.commitboost.json.ProxyKeySignatureScheme;

import java.io.IOException;
import java.util.Optional;

import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

class SigningRootGeneratorTest {
static Spec spec;
static Bytes32 genesisValidatorsRoot;
// constant generated and copied from Commit Boost Client
static final Bytes32 COMMIT_BOOST_COMPUTED_DOMAIN =
Bytes32.fromHexString("0x6d6d6f43b5303f2ad2010d699a76c8e62350947421a3e4a979779642cfdb0f66");
private static final String BLS_PRIVATE_KEY_1 =
"3ee2224386c82ffea477e2adf28a2929f5c349165a4196158c7f3a2ecca40f35";
private static final String BLS_PRIVATE_KEY_2 =
"32ae313afff2daa2ef7005a7f834bdf291855608fe82c24d30be6ac2017093a8";

@BeforeAll
static void initSpecAndGVR() {
Expand Down Expand Up @@ -65,7 +74,22 @@ void validComputedDomainForMAINNET() {

@Test
void computeSigningRootForBLSProxyKey() {
// TODO: Implement this test
final SigningRootGenerator signingRootGenerator =
new SigningRootGenerator(spec, genesisValidatorsRoot);
final BLSPublicKey delegator =
new BLSKeyPair(BLSSecretKey.fromBytes(Bytes32.fromHexString(BLS_PRIVATE_KEY_1)))
.getPublicKey();
final BLSPublicKey proxy =
new BLSKeyPair(BLSSecretKey.fromBytes(Bytes32.fromHexString(BLS_PRIVATE_KEY_2)))
.getPublicKey();
final ProxyKeyMessage proxyKeyMessage =
new ProxyKeyMessage(delegator.toHexString(), proxy.toHexString());
final Bytes signingRoot =
signingRootGenerator.computeSigningRoot(proxyKeyMessage, ProxyKeySignatureScheme.BLS);
assertThat(signingRoot)
.isEqualTo(
Bytes.fromHexString(
"0x148a095bf06bf227190b95dcc5c269e7d054d17c11d2a30499e0a41d2e200a05"));
}

@Test
Expand Down

0 comments on commit 40d2c27

Please sign in to comment.