From 80c8a33f74a3560065b4d0a27ab31afdfbff910e Mon Sep 17 00:00:00 2001 From: Kartik Chopra Date: Mon, 8 Jul 2024 15:27:30 -0400 Subject: [PATCH] feat: fix all issues with hash mismatch --- .../contracts/PreConfCommitmentStore.sol | 51 ++++++++++++++- contracts/test/PreConfirmationConfTest.sol | 62 +++++++++++++++++++ p2p/pkg/signer/preconfencryptor/encryptor.go | 2 + .../signer/preconfencryptor/encryptor_test.go | 10 +-- 4 files changed, 120 insertions(+), 5 deletions(-) diff --git a/contracts/contracts/PreConfCommitmentStore.sol b/contracts/contracts/PreConfCommitmentStore.sol index 958530429..d2c51959d 100644 --- a/contracts/contracts/PreConfCommitmentStore.sol +++ b/contracts/contracts/PreConfCommitmentStore.sol @@ -279,6 +279,56 @@ contract PreConfCommitmentStore is OwnableUpgradeable, UUPSUpgradeable { string memory _bidSignature, string memory _sharedSecretKey ) public view returns (bytes32) { + // console.log("EIP712_COMMITMENT_TYPEHASH:"); + // console.logBytes32(EIP712_COMMITMENT_TYPEHASH); + + // console.log("Transaction Hash:"); + // console.logBytes32(keccak256(abi.encodePacked(_txnHash))); + + // console.log("Reverting Transaction Hashes:"); + // console.logBytes32(keccak256(abi.encodePacked(_revertingTxHashes))); + + // console.log("Bid:"); + // console.logUint(_bid); + + // console.log("Block Number:"); + // console.logUint(_blockNumber); + + // console.log("Decay Start Timestamp:"); + // console.logUint(_decayStartTimeStamp); + + // console.log("Decay End Timestamp:"); + // console.logUint(_decayEndTimeStamp); + + // console.log("Reverting Transaction Hashes (again):"); + // console.logBytes32(keccak256(abi.encodePacked(_revertingTxHashes))); + + // console.log("Bid Hash:"); + // console.logBytes32(keccak256(abi.encodePacked(_bytes32ToHexString(_bidHash)))); + + // console.log("Bid Signature:"); + // console.logBytes32(keccak256(abi.encodePacked(_bidSignature))); + + // console.log("Shared Secret Key:"); + // console.logBytes32(keccak256(abi.encodePacked(_sharedSecretKey))); + // console.log("whole thing"); + // console.logBytes( + // abi.encode( + // EIP712_COMMITMENT_TYPEHASH, + // keccak256(abi.encodePacked(_txnHash)), + // keccak256(abi.encodePacked(_revertingTxHashes)), + // _bid, + // _blockNumber, + // _decayStartTimeStamp, + // _decayEndTimeStamp, + // _revertingTxHashes, + // keccak256( + // abi.encodePacked(_bytes32ToHexString(_bidHash)) + // ), + // keccak256(abi.encodePacked(_bidSignature)), + // keccak256(abi.encodePacked(_sharedSecretKey)) + // ) + // ); return ECDSA.toTypedDataHash( DOMAIN_SEPARATOR_PRECONF, @@ -291,7 +341,6 @@ contract PreConfCommitmentStore is OwnableUpgradeable, UUPSUpgradeable { _blockNumber, _decayStartTimeStamp, _decayEndTimeStamp, - _revertingTxHashes, keccak256( abi.encodePacked(_bytes32ToHexString(_bidHash)) ), diff --git a/contracts/test/PreConfirmationConfTest.sol b/contracts/test/PreConfirmationConfTest.sol index a2adcc2eb..46f019a06 100644 --- a/contracts/test/PreConfirmationConfTest.sol +++ b/contracts/test/PreConfirmationConfTest.sol @@ -120,6 +120,68 @@ contract TestPreConfCommitmentStore is Test { ); } + function test_getBidHash() public { + // Step 1: Prepare the test commitment data + PreConfCommitmentStore.CommitmentParams memory testCommitment = PreConfCommitmentStore.CommitmentParams({ + txnHash: "0xkartik", + revertingTxHashes: "0xkartik", + bid: 2, + blockNumber: 2, + decayStartTimeStamp: 10, + decayEndTimeStamp: 20, + sharedSecretKey: bytes("0xsecret"), + bidHash: hex"9890bcda118cfabed02ff3b9d05a54dca5310e9ace3b05f259f4731f58ad0900", + bidSignature: hex"c2ab6e530f6b09337e53e1192857fa10017cdb488cf2a07e0aa4457571492b8c6bff93cbda4e003336656b4ecf8ff46bd1d408b310acdf07be4925a1a8fee4471c", + commitmentSignature: hex"5b3000290d4f347b94146eb37f66d5368aed18fb8713bf78620abe40ae3de7f635f7ed161801c31ea10e736d88e6fd2a2286bbd59385161dd24c9fefd2568f341b" + }); + // Step 2: Calculate the bid hash using the getBidHash function + bytes32 bidHash = preConfCommitmentStore.getBidHash( + testCommitment.txnHash, + testCommitment.revertingTxHashes, + testCommitment.bid, + testCommitment.blockNumber, + testCommitment.decayStartTimeStamp, + testCommitment.decayEndTimeStamp + ); + + // Add a bob private key and console log the key + (address bob, uint256 bobPk) = makeAddrAndKey("bob"); + console.log("Bob's Private Key:", bobPk); + + // Make a signature on the bid hash + (uint8 v, bytes32 r, bytes32 s) = vm.sign(bobPk, bidHash); + bytes memory bidSignature = abi.encodePacked(r, s, v); + console.logBytes(bidSignature); + + // Step 3: Calculate the commitment hash using the getPreConfHash function + bytes32 commitmentHash = preConfCommitmentStore.getPreConfHash( + testCommitment.txnHash, + testCommitment.revertingTxHashes, + testCommitment.bid, + testCommitment.blockNumber, + testCommitment.decayStartTimeStamp, + testCommitment.decayEndTimeStamp, + bidHash, + _bytesToHexString(bidSignature), + _bytesToHexString(testCommitment.sharedSecretKey) + ); + + // Step 4: Verify the bid hash is correctly generated and not zero + assert(bidHash != bytes32(0)); + + // Optional: Log the bid hash for debugging purposes + console.logBytes32(bidHash); + + // Step 5: Verify the commitment hash is correctly generated and not zero + assert(commitmentHash != bytes32(0)); + // Log the commitment hash for debugging purposes + console.log("Logging commitment hash:"); + console.logBytes32(commitmentHash); + + // Optional: Log the commitment hash for debugging purposes + console.logBytes32(commitmentHash); + } + function test_Initialize() public view { assertEq(preConfCommitmentStore.oracle(), feeRecipient); assertEq( diff --git a/p2p/pkg/signer/preconfencryptor/encryptor.go b/p2p/pkg/signer/preconfencryptor/encryptor.go index 9614345e4..98afd0e15 100644 --- a/p2p/pkg/signer/preconfencryptor/encryptor.go +++ b/p2p/pkg/signer/preconfencryptor/encryptor.go @@ -6,6 +6,7 @@ import ( "crypto/rand" "encoding/hex" "errors" + "fmt" "math/big" "github.com/ethereum/go-ethereum/common" @@ -374,6 +375,7 @@ func GetPreConfirmationHash(c *preconfpb.PreConfirmation) ([]byte, error) { data = append(data, bidDigestHash.Bytes()...) data = append(data, bidSigHash.Bytes()...) data = append(data, sharedSecretHash.Bytes()...) + fmt.Printf("Data to be hashed: %x\n", data) dataHash := crypto.Keccak256Hash(data) rawData := append([]byte("\x19\x01"), append(domainSeparatorBid.Bytes(), dataHash.Bytes()...)...) diff --git a/p2p/pkg/signer/preconfencryptor/encryptor_test.go b/p2p/pkg/signer/preconfencryptor/encryptor_test.go index ac600147c..08d6df573 100644 --- a/p2p/pkg/signer/preconfencryptor/encryptor_test.go +++ b/p2p/pkg/signer/preconfencryptor/encryptor_test.go @@ -152,6 +152,7 @@ func TestHashing(t *testing.T) { t.Run("bid", func(t *testing.T) { bid := &preconfpb.Bid{ TxHash: "0xkartik", + RevertingTxHashes: "0xkartik", BidAmount: "2", BlockNumber: 2, DecayStartTimestamp: 10, @@ -165,15 +166,15 @@ func TestHashing(t *testing.T) { hashStr := hex.EncodeToString(hash) // This hash is sourced from the solidity contract to ensure interoperability - expHash := "d794a5899d123c0bf474ca2a39b00a3b421e13575e6dde349fa7bef78356c9a2" + expHash := "9890bcda118cfabed02ff3b9d05a54dca5310e9ace3b05f259f4731f58ad0900" if hashStr != expHash { t.Fatalf("hash mismatch: %s != %s", hashStr, expHash) } }) t.Run("preConfirmation", func(t *testing.T) { - bidHash := "d794a5899d123c0bf474ca2a39b00a3b421e13575e6dde349fa7bef78356c9a2" - bidSignature := "2e7df27808c72d7d5b2543bb63b06c0ae2144e021593b8d2a7cca6a3fb2d9c4b1a82dd2a07266de9364d255bdb709476ad96b826ec855efb528eaff66682997e1c" + bidHash := "9890bcda118cfabed02ff3b9d05a54dca5310e9ace3b05f259f4731f58ad0900" + bidSignature := "c2ab6e530f6b09337e53e1192857fa10017cdb488cf2a07e0aa4457571492b8c6bff93cbda4e003336656b4ecf8ff46bd1d408b310acdf07be4925a1a8fee4471c" bidHashBytes, err := hex.DecodeString(bidHash) if err != nil { @@ -186,6 +187,7 @@ func TestHashing(t *testing.T) { bid := &preconfpb.Bid{ TxHash: "0xkartik", + RevertingTxHashes: "0xkartik", BidAmount: "2", BlockNumber: 2, DecayStartTimestamp: 10, @@ -207,7 +209,7 @@ func TestHashing(t *testing.T) { } hashStr := hex.EncodeToString(hash) - expHash := "fa013e066e60e39d4f20cbb08aaab09d98e9830716132f5e6ebbcd1b1119775b" + expHash := "e872828431f235bf75bebc05bd1a7d349e3b3d30be926c04502a674a05ef802d" if hashStr != expHash { t.Fatalf("hash mismatch: %s != %s", hashStr, expHash) }