-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Commit boost API - Generate Proxy Key
-- Added routes and handler
- Loading branch information
1 parent
3433f75
commit 9277a3e
Showing
7 changed files
with
252 additions
and
1 deletion.
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
.../main/java/tech/pegasys/web3signer/core/routes/eth2/CommitBoostGenerateProxyKeyRoute.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...web3signer/core/service/http/handlers/commitboost/CommitBoostGenerateProxyKeyHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
.../pegasys/web3signer/core/service/http/handlers/commitboost/json/GenerateProxyKeyBody.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} |
19 changes: 19 additions & 0 deletions
19
...asys/web3signer/core/service/http/handlers/commitboost/json/GenerateProxyKeyResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} |
19 changes: 19 additions & 0 deletions
19
.../tech/pegasys/web3signer/core/service/http/handlers/commitboost/json/ProxyKeyMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} |
18 changes: 18 additions & 0 deletions
18
...gasys/web3signer/core/service/http/handlers/commitboost/json/ProxyKeySignatureScheme.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |