Skip to content

Commit

Permalink
Match commitment to specification (#7544)
Browse files Browse the repository at this point in the history
* fix hash

* prepend hash with byte 0x02

* fix hash to spec

* chore: update function documentation to align with implementation.

---------

Co-authored-by: DimitrisJim <[email protected]>
  • Loading branch information
AdityaSripal and DimitrisJim authored Nov 12, 2024
1 parent e769eeb commit 5b3ce55
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion modules/core/04-channel/v2/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (k *Keeper) HasPacketReceipt(ctx context.Context, channelID string, sequenc
// This is a public path that is standardized by the IBC V2 specification.
func (k *Keeper) SetPacketReceipt(ctx context.Context, channelID string, sequence uint64) {
store := k.storeService.OpenKVStore(ctx)
if err := store.Set(hostv2.PacketReceiptKey(channelID, sequence), []byte{byte(1)}); err != nil {
if err := store.Set(hostv2.PacketReceiptKey(channelID, sequence), []byte{byte(2)}); err != nil {
panic(err)
}
}
Expand Down
26 changes: 16 additions & 10 deletions modules/core/04-channel/v2/types/commitment.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,28 @@ import (
)

// CommitPacket returns the V2 packet commitment bytes. The commitment consists of:
// sha256_hash(timeout) + sha256_hash(destinationChannel) + sha256_hash(payload) from a given packet.
// 0x02 + sha256_hash(destinationChannel) + sha256_hash(timeout) + sha256_hash(payload) from a given packet.
// This results in a fixed length preimage.
// NOTE: A fixed length preimage is ESSENTIAL to prevent relayers from being able
// to malleate the packet fields and create a commitment hash that matches the original packet.
func CommitPacket(packet Packet) []byte {
buf := sdk.Uint64ToBigEndian(packet.GetTimeoutTimestamp())

var buf []byte
destIDHash := sha256.Sum256([]byte(packet.DestinationChannel))
buf = append(buf, destIDHash[:]...)

timeoutBytes := sdk.Uint64ToBigEndian(packet.GetTimeoutTimestamp())
timeoutHash := sha256.Sum256(timeoutBytes)
buf = append(buf, timeoutHash[:]...)

var appBytes []byte
for _, payload := range packet.Payloads {
buf = append(buf, hashPayload(payload)...)
appBytes = append(appBytes, hashPayload(payload)...)
}
appHash := sha256.Sum256(appBytes)
buf = append(buf, appHash[:]...)

hash := sha256.Sum256(buf)
return hash[:]
return append([]byte{byte(2)}, hash[:]...)
}

// hashPayload returns the hash of the payload.
Expand All @@ -32,12 +38,12 @@ func hashPayload(data Payload) []byte {
buf = append(buf, sourceHash[:]...)
destHash := sha256.Sum256([]byte(data.DestinationPort))
buf = append(buf, destHash[:]...)
payloadValueHash := sha256.Sum256(data.Value)
buf = append(buf, payloadValueHash[:]...)
payloadEncodingHash := sha256.Sum256([]byte(data.Encoding))
buf = append(buf, payloadEncodingHash[:]...)
payloadVersionHash := sha256.Sum256([]byte(data.Version))
buf = append(buf, payloadVersionHash[:]...)
payloadEncodingHash := sha256.Sum256([]byte(data.Encoding))
buf = append(buf, payloadEncodingHash[:]...)
payloadValueHash := sha256.Sum256(data.Value)
buf = append(buf, payloadValueHash[:]...)
hash := sha256.Sum256(buf)
return hash[:]
}
Expand All @@ -51,5 +57,5 @@ func CommitAcknowledgement(acknowledgement Acknowledgement) []byte {
}

hash := sha256.Sum256(buf)
return hash[:]
return append([]byte{byte(2)}, hash[:]...)
}

0 comments on commit 5b3ce55

Please sign in to comment.