Skip to content

Commit 5932d4c

Browse files
authored
chore: add helper view function (#1465)
**Motivation:** Offchain services need a way to encode `keyData` **Modifications:** Create a view function `encodeBN254KeyData` **Result:** Easier integration
1 parent fad8758 commit 5932d4c

File tree

10 files changed

+170
-8
lines changed

10 files changed

+170
-8
lines changed

pkg/bindings/BN254CertificateVerifier/binding.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/bindings/BN254TableCalculator/binding.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/bindings/CrossChainRegistry/binding.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/bindings/IKeyRegistrar/binding.go

Lines changed: 32 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/bindings/KeyRegistrar/binding.go

Lines changed: 33 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/bindings/KeyRegistrarStorage/binding.go

Lines changed: 32 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/bindings/OperatorTableUpdater/binding.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/contracts/interfaces/IKeyRegistrar.sol

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,15 @@ interface IKeyRegistrar is IKeyRegistrarErrors, IKeyRegistrarEvents, ISemVerMixi
173173
OperatorSet memory operatorSet,
174174
bytes calldata keyData
175175
) external view returns (bytes32);
176+
177+
/**
178+
* @notice Encodes the BN254 key data into a bytes array
179+
* @param g1Point The BN254 G1 public key
180+
* @param g2Point The BN254 G2 public key
181+
* @return The encoded key data
182+
*/
183+
function encodeBN254KeyData(
184+
BN254.G1Point memory g1Point,
185+
BN254.G2Point memory g2Point
186+
) external pure returns (bytes memory);
176187
}

src/contracts/permissions/KeyRegistrar.sol

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,4 +340,12 @@ contract KeyRegistrar is KeyRegistrarStorage, PermissionControllerMixin, Signatu
340340
);
341341
return _calculateSignableDigest(structHash);
342342
}
343+
344+
/// @inheritdoc IKeyRegistrar
345+
function encodeBN254KeyData(
346+
BN254.G1Point memory g1Point,
347+
BN254.G2Point memory g2Point
348+
) public pure returns (bytes memory) {
349+
return abi.encode(g1Point.X, g1Point.Y, g2Point.X[0], g2Point.X[1], g2Point.Y[0], g2Point.Y[1]);
350+
}
343351
}

src/test/unit/KeyRegistrarUnit.t.sol

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,56 @@ contract KeyRegistrarUnitTests is EigenLayerUnitTestSetup {
724724
keyRegistrar.registerKey(operator1, operatorSet, bn254Key1, wrongSignature);
725725
}
726726

727+
function testEncodeBN254KeyData() public {
728+
// Test that encodeBN254KeyData produces the expected encoding
729+
bytes memory encodedKey = keyRegistrar.encodeBN254KeyData(bn254G1Key1, bn254G2Key1);
730+
731+
// Verify the encoded data matches our manual encoding
732+
assertEq(encodedKey, bn254Key1);
733+
734+
// Decode to verify structure
735+
(uint g1X, uint g1Y, uint[2] memory g2X, uint[2] memory g2Y) = abi.decode(encodedKey, (uint, uint, uint[2], uint[2]));
736+
737+
assertEq(g1X, bn254G1Key1.X);
738+
assertEq(g1Y, bn254G1Key1.Y);
739+
assertEq(g2X[0], bn254G2Key1.X[0]);
740+
assertEq(g2X[1], bn254G2Key1.X[1]);
741+
assertEq(g2Y[0], bn254G2Key1.Y[0]);
742+
assertEq(g2Y[1], bn254G2Key1.Y[1]);
743+
}
744+
745+
function testEncodeBN254KeyData_RegisterKey() public {
746+
// Test that encoded data from encodeBN254KeyData can be used with registerKey
747+
OperatorSet memory operatorSet = _createOperatorSet(avs1, DEFAULT_OPERATOR_SET_ID);
748+
749+
vm.prank(avs1);
750+
keyRegistrar.configureOperatorSet(operatorSet, IKeyRegistrarTypes.CurveType.BN254);
751+
752+
// Use encodeBN254KeyData to prepare the key data
753+
bytes memory encodedKey = keyRegistrar.encodeBN254KeyData(bn254G1Key2, bn254G2Key2);
754+
755+
// Generate signature for the encoded key
756+
bytes memory signature = _generateBN254Signature(operator1, operatorSet, encodedKey, bn254PrivKey2);
757+
758+
// Register the key using the encoded data
759+
vm.prank(operator1);
760+
vm.expectEmit(true, true, true, true);
761+
emit KeyRegistered(operatorSet, operator1, IKeyRegistrarTypes.CurveType.BN254, encodedKey);
762+
keyRegistrar.registerKey(operator1, operatorSet, encodedKey, signature);
763+
764+
// Verify registration was successful
765+
assertTrue(keyRegistrar.isRegistered(operatorSet, operator1));
766+
767+
// Verify the stored key matches what we registered
768+
(BN254.G1Point memory storedG1, BN254.G2Point memory storedG2) = keyRegistrar.getBN254Key(operatorSet, operator1);
769+
assertEq(storedG1.X, bn254G1Key2.X);
770+
assertEq(storedG1.Y, bn254G1Key2.Y);
771+
assertEq(storedG2.X[0], bn254G2Key2.X[0]);
772+
assertEq(storedG2.X[1], bn254G2Key2.X[1]);
773+
assertEq(storedG2.Y[0], bn254G2Key2.Y[0]);
774+
assertEq(storedG2.Y[1], bn254G2Key2.Y[1]);
775+
}
776+
727777
// ============ Key Deregistration Tests ============
728778

729779
function testDeregisterKey_ECDSA() public {

0 commit comments

Comments
 (0)