Skip to content

Commit

Permalink
feat: Commit boost API - Generate Proxy Key
Browse files Browse the repository at this point in the history
 -- Added routes and handler
  • Loading branch information
usmansaleem committed Oct 21, 2024
1 parent 3433f75 commit 9277a3e
Show file tree
Hide file tree
Showing 7 changed files with 252 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright 2024 ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package tech.pegasys.web3signer.core.routes.eth2;

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

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.DefaultArtifactSignerProvider;

import io.vertx.core.http.HttpMethod;
import io.vertx.core.json.JsonObject;

public class CommitBoostGenerateProxyKeyRoute implements Web3SignerRoute {
private static final String PATH = "/signer/v1/generate_proxy_key";
private final Context context;
private final SignerForIdentifier<BlsArtifactSignature> blsSigner;

public CommitBoostGenerateProxyKeyRoute(final Context context) {
this.context = context;

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

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

@Override
public void register() {
context
.getRouter()
.route(HttpMethod.POST, PATH)
.blockingHandler(new CommitBoostGenerateProxyKeyHandler(blsSigner), false)
.failureHandler(context.getErrorHandler())
.failureHandler(
ctx -> {
final int statusCode = ctx.statusCode();
if (statusCode == 400) {
ctx.response()
.setStatusCode(statusCode)
.end(
new JsonObject()
.put("code", statusCode)
.put("message", "Bad Request")
.encode());
} else if (statusCode == 404) {
ctx.response()
.setStatusCode(statusCode)
.end(
new JsonObject()
.put("code", statusCode)
.put("message", "Identifier not found.")
.encode());
} else if (statusCode == 500) {
ctx.response()
.setStatusCode(statusCode)
.end(
new JsonObject()
.put("code", statusCode)
.put("message", "Internal Server Error")
.encode());
} else {
ctx.next(); // go to global failure handler
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ public void register() {
if (statusCode == 500) {
ctx.response()
.setStatusCode(statusCode)
.end(new JsonObject().put("error", "Internal Server Error").encode());
.end(
new JsonObject()
.put("code", statusCode)
.put("message", "Internal Server Error")
.encode());
} else {
ctx.next(); // go to global failure handler
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright 2024 ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package tech.pegasys.web3signer.core.service.http.handlers.commitboost;

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

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.GenerateProxyKeyResponse;
import tech.pegasys.web3signer.core.service.http.handlers.commitboost.json.ProxyKeyMessage;
import tech.pegasys.web3signer.core.service.http.handlers.signing.SignerForIdentifier;

import com.fasterxml.jackson.core.JsonProcessingException;
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;

public class CommitBoostGenerateProxyKeyHandler implements Handler<RoutingContext> {
private static final Logger LOG = LogManager.getLogger();

private static final ObjectMapper JSON_MAPPER = SigningObjectMapperFactory.createObjectMapper();
public static final int NOT_FOUND = 404;
public static final int BAD_REQUEST = 400;
private static final int INTERNAL_ERROR = 500;
private final SignerForIdentifier<?> signerForIdentifier;

public CommitBoostGenerateProxyKeyHandler(final SignerForIdentifier<?> signerForIdentifier) {
this.signerForIdentifier = signerForIdentifier;
}

@Override
public void handle(final RoutingContext context) {
final String body = context.body().asString();

// read and validate incoming json body
final GenerateProxyKeyBody proxyKeyBody;
try {
proxyKeyBody = JSON_MAPPER.readValue(body, GenerateProxyKeyBody.class);
} catch (final JsonProcessingException | IllegalArgumentException e) {
context.fail(BAD_REQUEST);
return;
}

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

// TODO: Generate actual proxy key based on signature scheme
// TODO: Add generated proxy key to DefaultArtifactSignerProvider

final ProxyKeyMessage proxyKeyMessage = new ProxyKeyMessage(identifier, "");

// TODO: Generate actual signature. This involves custom domain and zzs classes
final String signature = ""; // need tree-hash-root of ProxyKeyMessage

final GenerateProxyKeyResponse generateProxyKeyResponse =
new GenerateProxyKeyResponse(proxyKeyMessage, signature);

try {
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);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright 2024 ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package tech.pegasys.web3signer.core.service.http.handlers.commitboost.json;

import com.fasterxml.jackson.annotation.JsonProperty;

public record GenerateProxyKeyBody(
@JsonProperty(value = "pubkey", required = true) String blsPublicKey,
@JsonProperty(value = "scheme", required = true) String ProxyKeySignatureScheme) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright 2024 ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package tech.pegasys.web3signer.core.service.http.handlers.commitboost.json;

import com.fasterxml.jackson.annotation.JsonProperty;

public record GenerateProxyKeyResponse(
@JsonProperty(value = "message") ProxyKeyMessage proxyKeyMessage,
@JsonProperty(value = "signature") String signature) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright 2024 ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package tech.pegasys.web3signer.core.service.http.handlers.commitboost.json;

import com.fasterxml.jackson.annotation.JsonProperty;

public record ProxyKeyMessage(
@JsonProperty(value = "delegator", required = true) String blsPublicKey,
@JsonProperty(value = "proxy", required = true) String proxyPublicKey) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright 2024 ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package tech.pegasys.web3signer.core.service.http.handlers.commitboost.json;

public enum ProxyKeySignatureScheme {
BLS,
ECDSA
}

0 comments on commit 9277a3e

Please sign in to comment.