Skip to content

Commit

Permalink
feat: adds reverting txns to hashing
Browse files Browse the repository at this point in the history
  • Loading branch information
ckartik committed Jul 8, 2024
1 parent 8bc062a commit 533f387
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions p2p/pkg/signer/preconfencryptor/encryptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,18 +307,20 @@ func GetBidHash(bid *preconfpb.Bid) ([]byte, error) {
return nil, ErrInvalidBidAmt
}

// EIP712_MESSAGE_TYPEHASH
// EIP712_BID_TYPEHASH
eip712MessageTypeHash := crypto.Keccak256Hash(
[]byte("PreConfBid(string txnHash,uint256 bid,uint64 blockNumber,uint64 decayStartTimeStamp,uint64 decayEndTimeStamp)"),
[]byte("PreConfBid(string txnHash,string revertingTxHashes,uint256 bid,uint64 blockNumber,uint64 decayStartTimeStamp,uint64 decayEndTimeStamp)"),
)

// Convert the txnHash to a byte array and hash it
txnHashHash := crypto.Keccak256Hash([]byte(bid.TxHash))
revertingTxHashesHash := crypto.Keccak256Hash([]byte(bid.RevertingTxHashes))

// Encode values similar to Solidity's abi.encode
// The reason we use math.U256Bytes is because we want to encode the uint64 as a 32 byte array
// The EVM does this for values due via padding to 32 bytes, as that's the base size of a word in the EVM
data := append(eip712MessageTypeHash.Bytes(), txnHashHash.Bytes()...)
data = append(data, revertingTxHashesHash.Bytes()...)
data = append(data, math.U256Bytes(bidAmt)...)
data = append(data, math.U256Bytes(big.NewInt(bid.BlockNumber))...)
data = append(data, math.U256Bytes(big.NewInt(bid.DecayStartTimestamp))...)
Expand Down Expand Up @@ -350,19 +352,21 @@ func GetPreConfirmationHash(c *preconfpb.PreConfirmation) ([]byte, error) {
return nil, ErrInvalidBidAmt
}

// EIP712_MESSAGE_TYPEHASH
// EIP712_COMMITMENT_TYPEHASH
eip712MessageTypeHash := crypto.Keccak256Hash(
[]byte("PreConfCommitment(string txnHash,uint256 bid,uint64 blockNumber,uint64 decayStartTimeStamp,uint64 decayEndTimeStamp,bytes32 bidHash,string signature,string sharedSecretKey)"),
[]byte("PreConfCommitment(string txnHash,string revertingTxHashes,uint256 bid,uint64 blockNumber,uint64 decayStartTimeStamp,uint64 decayEndTimeStamp,bytes32 bidHash,string signature,string sharedSecretKey)"),
)

// Convert the txnHash to a byte array and hash it
txnHashHash := crypto.Keccak256Hash([]byte(c.Bid.TxHash))
revertingTxHashesHash := crypto.Keccak256Hash([]byte(c.Bid.RevertingTxHashes))
bidDigestHash := crypto.Keccak256Hash([]byte(hex.EncodeToString(c.Bid.Digest)))
bidSigHash := crypto.Keccak256Hash([]byte(hex.EncodeToString(c.Bid.Signature)))
sharedSecretHash := crypto.Keccak256Hash([]byte(hex.EncodeToString(c.SharedSecret)))

// Encode values similar to Solidity's abi.encode
data := append(eip712MessageTypeHash.Bytes(), txnHashHash.Bytes()...)
data = append(data, revertingTxHashesHash.Bytes()...)
data = append(data, math.U256Bytes(bidAmt)...)
data = append(data, math.U256Bytes(big.NewInt(c.Bid.BlockNumber))...)
data = append(data, math.U256Bytes(big.NewInt(c.Bid.DecayStartTimestamp))...)
Expand Down

0 comments on commit 533f387

Please sign in to comment.