Skip to content

Commit

Permalink
feat: fix all issues with hash mismatch
Browse files Browse the repository at this point in the history
  • Loading branch information
ckartik committed Jul 8, 2024
1 parent ff19199 commit 80c8a33
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 5 deletions.
51 changes: 50 additions & 1 deletion contracts/contracts/PreConfCommitmentStore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -291,7 +341,6 @@ contract PreConfCommitmentStore is OwnableUpgradeable, UUPSUpgradeable {
_blockNumber,
_decayStartTimeStamp,
_decayEndTimeStamp,
_revertingTxHashes,
keccak256(
abi.encodePacked(_bytes32ToHexString(_bidHash))
),
Expand Down
62 changes: 62 additions & 0 deletions contracts/test/PreConfirmationConfTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 2 additions & 0 deletions p2p/pkg/signer/preconfencryptor/encryptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/rand"
"encoding/hex"
"errors"
"fmt"
"math/big"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -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()...)...)
Expand Down
10 changes: 6 additions & 4 deletions p2p/pkg/signer/preconfencryptor/encryptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand All @@ -186,6 +187,7 @@ func TestHashing(t *testing.T) {

bid := &preconfpb.Bid{
TxHash: "0xkartik",
RevertingTxHashes: "0xkartik",
BidAmount: "2",
BlockNumber: 2,
DecayStartTimestamp: 10,
Expand All @@ -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)
}
Expand Down

0 comments on commit 80c8a33

Please sign in to comment.