-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Adding Electra fork support for AggregateAndProof
-- Handle Remoting API version 1.2.0 changes related to Electra -- Updated Acceptance tests
- Loading branch information
1 parent
bf9f357
commit 9883f4f
Showing
11 changed files
with
355 additions
and
17 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
.../test/java/tech/pegasys/web3signer/dsl/utils/Eth2AggregateAndProofSigningRequestUtil.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,64 @@ | ||
/* | ||
* Copyright 2021 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.dsl.utils; | ||
|
||
import tech.pegasys.teku.api.schema.Fork; | ||
import tech.pegasys.teku.spec.Spec; | ||
import tech.pegasys.teku.spec.SpecMilestone; | ||
import tech.pegasys.teku.spec.TestSpecFactory; | ||
import tech.pegasys.teku.spec.datastructures.operations.AggregateAndProof; | ||
import tech.pegasys.teku.spec.datastructures.state.ForkInfo; | ||
import tech.pegasys.teku.spec.signatures.SigningRootUtil; | ||
import tech.pegasys.teku.spec.util.DataStructureUtil; | ||
import tech.pegasys.web3signer.core.service.http.ArtifactType; | ||
import tech.pegasys.web3signer.core.service.http.handlers.signing.eth2.AggregateAndProofV2; | ||
import tech.pegasys.web3signer.core.service.http.handlers.signing.eth2.Eth2SigningRequestBody; | ||
|
||
import org.apache.tuweni.bytes.Bytes; | ||
|
||
public class Eth2AggregateAndProofSigningRequestUtil { | ||
private final SpecMilestone specMilestone; | ||
private final DataStructureUtil dataStructureUtil; | ||
private final SigningRootUtil signingRootUtil; | ||
private final ForkInfo tekuForkInfo; | ||
private final Fork tekuFork; | ||
private final tech.pegasys.web3signer.core.service.http.handlers.signing.eth2.ForkInfo forkInfo; | ||
private final Bytes signingRoot; | ||
private final AggregateAndProof aggregateAndProof; | ||
|
||
public Eth2AggregateAndProofSigningRequestUtil(final SpecMilestone specMilestone) { | ||
final Spec spec = TestSpecFactory.createMinimal(specMilestone); | ||
this.specMilestone = specMilestone; | ||
dataStructureUtil = new DataStructureUtil(spec); | ||
signingRootUtil = new SigningRootUtil(spec); | ||
tekuForkInfo = Eth2RequestUtils.forkInfo().asInternalForkInfo(); | ||
tekuFork = new Fork(tekuForkInfo.getFork()); | ||
forkInfo = | ||
new tech.pegasys.web3signer.core.service.http.handlers.signing.eth2.ForkInfo( | ||
tekuFork, tekuForkInfo.getGenesisValidatorsRoot()); | ||
aggregateAndProof = dataStructureUtil.randomAggregateAndProof(); | ||
signingRoot = | ||
signingRootUtil.signingRootForSignAggregateAndProof(aggregateAndProof, tekuForkInfo); | ||
} | ||
|
||
public Eth2SigningRequestBody createAggregateAndProofV2Request() { | ||
final tech.pegasys.teku.api.schema.AggregateAndProof aggregateAndProof = | ||
new tech.pegasys.teku.api.schema.AggregateAndProof(this.aggregateAndProof); | ||
return Eth2SigningRequestBodyBuilder.anEth2SigningRequestBody() | ||
.withType(ArtifactType.AGGREGATE_AND_PROOF_V2) | ||
.withSigningRoot(signingRoot) | ||
.withForkInfo(forkInfo) | ||
.withAggregateAndProofV2(new AggregateAndProofV2(specMilestone, aggregateAndProof)) | ||
.build(); | ||
} | ||
} |
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
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
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
74 changes: 74 additions & 0 deletions
74
...ava/tech/pegasys/web3signer/tests/signing/Eth2AggregateAndProofSigningAcceptanceTest.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,74 @@ | ||
/* | ||
* Copyright 2021 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.tests.signing; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import tech.pegasys.teku.bls.BLS; | ||
import tech.pegasys.teku.bls.BLSKeyPair; | ||
import tech.pegasys.teku.bls.BLSPublicKey; | ||
import tech.pegasys.teku.bls.BLSSecretKey; | ||
import tech.pegasys.teku.bls.BLSSignature; | ||
import tech.pegasys.teku.spec.SpecMilestone; | ||
import tech.pegasys.teku.spec.networks.Eth2Network; | ||
import tech.pegasys.web3signer.core.service.http.handlers.signing.eth2.Eth2SigningRequestBody; | ||
import tech.pegasys.web3signer.dsl.utils.Eth2AggregateAndProofSigningRequestUtil; | ||
import tech.pegasys.web3signer.dsl.utils.MetadataFileHelpers; | ||
import tech.pegasys.web3signer.signing.KeyType; | ||
|
||
import java.nio.file.Path; | ||
|
||
import io.restassured.http.ContentType; | ||
import io.restassured.response.Response; | ||
import org.apache.tuweni.bytes.Bytes; | ||
import org.apache.tuweni.bytes.Bytes32; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.EnumSource; | ||
|
||
public class Eth2AggregateAndProofSigningAcceptanceTest extends SigningAcceptanceTestBase { | ||
private static final String PRIVATE_KEY = | ||
"3ee2224386c82ffea477e2adf28a2929f5c349165a4196158c7f3a2ecca40f35"; | ||
|
||
private static final MetadataFileHelpers METADATA_FILE_HELPERS = new MetadataFileHelpers(); | ||
private static final BLSSecretKey KEY = | ||
BLSSecretKey.fromBytes(Bytes32.fromHexString(PRIVATE_KEY)); | ||
private static final BLSKeyPair KEY_PAIR = new BLSKeyPair(KEY); | ||
private static final BLSPublicKey PUBLIC_KEY = KEY_PAIR.getPublicKey(); | ||
|
||
@BeforeEach | ||
void setup() { | ||
final String configFilename = PUBLIC_KEY.toString().substring(2); | ||
final Path keyConfigFile = testDirectory.resolve(configFilename + ".yaml"); | ||
METADATA_FILE_HELPERS.createUnencryptedYamlFileAt(keyConfigFile, PRIVATE_KEY, KeyType.BLS); | ||
} | ||
|
||
@ParameterizedTest(name = "#{index} - {0}: Sign and verify AggregateAndProofV2 Signature") | ||
@EnumSource( | ||
value = SpecMilestone.class, | ||
names = {"PHASE0", "ALTAIR", "BELLATRIX", "CAPELLA", "DENEB", "ELECTRA"}) | ||
void signAndVerifyAggregateAndProofV2Signature(final SpecMilestone specMilestone) | ||
throws Exception { | ||
final Eth2AggregateAndProofSigningRequestUtil util = | ||
new Eth2AggregateAndProofSigningRequestUtil(specMilestone); | ||
|
||
setupEth2Signer(Eth2Network.MINIMAL, specMilestone); | ||
|
||
final Eth2SigningRequestBody request = util.createAggregateAndProofV2Request(); | ||
final Response response = | ||
signer.eth2Sign(KEY_PAIR.getPublicKey().toString(), request, ContentType.JSON); | ||
final Bytes signature = verifyAndGetSignatureResponse(response, ContentType.JSON); | ||
final BLSSignature expectedSignature = BLS.sign(KEY_PAIR.getSecretKey(), request.signingRoot()); | ||
assertThat(signature).isEqualTo(expectedSignature.toBytesCompressed()); | ||
} | ||
} |
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
60 changes: 60 additions & 0 deletions
60
.../tech/pegasys/web3signer/core/service/http/handlers/signing/eth2/AggregateAndProofV2.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,60 @@ | ||
/* | ||
* Copyright 2020 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.signing.eth2; | ||
|
||
import tech.pegasys.teku.api.schema.AggregateAndProof; | ||
import tech.pegasys.teku.spec.SpecMilestone; | ||
import tech.pegasys.web3signer.core.service.http.handlers.signing.eth2.json.AggregateAndProofV2Deserializer; | ||
import tech.pegasys.web3signer.core.service.http.handlers.signing.eth2.json.AggregateAndProofV2Serializer; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
import com.fasterxml.jackson.databind.annotation.JsonSerialize; | ||
|
||
/** | ||
* Represents an AGGREGATE_AND_PROOF_V2 signing request. | ||
* | ||
* <p>This class is designed to handle both the new format (with explicit version and data fields) | ||
* and the legacy format of AGGREGATE_AND_PROOF signing requests. The aggregate property (i.e., | ||
* Attestation) has introduced a new field 'committee_bits' in the Electra spec. | ||
* | ||
* <p>The Teku signing utility is able to utilize the same Attestation object for all changes, | ||
* ensuring compatibility across different versions. | ||
* | ||
* <p>Deserialization is handled by {@link AggregateAndProofV2Deserializer}, which supports both: | ||
* | ||
* <ul> | ||
* <li>New format: JSON with explicit "version" and "data" fields | ||
* <li>Legacy format: JSON without "version" and "data" fields (treated as legacy | ||
* AggregateAndProof) | ||
* </ul> | ||
* | ||
* <p>For legacy format, the deserializer sets the version to null and treats the entire JSON as the | ||
* data field. | ||
* | ||
* <p>Serialization is handled by {@link AggregateAndProofV2Serializer}, which: * | ||
* | ||
* <ul> | ||
* * | ||
* <li>For new format (version is not null): Serializes with "version" and "data" fields * | ||
* <li>For legacy format (version is null): Serializes all data fields at the top level * | ||
* <li>Throws a JsonMappingException if the data field is null * | ||
* </ul> | ||
* | ||
* * | ||
*/ | ||
@JsonDeserialize(using = AggregateAndProofV2Deserializer.class) | ||
@JsonSerialize(using = AggregateAndProofV2Serializer.class) | ||
public record AggregateAndProofV2( | ||
@JsonProperty(value = "version") SpecMilestone version, | ||
@JsonProperty(value = "data") AggregateAndProof data) {} |
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
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
Oops, something went wrong.