Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Match commitment to specification #7544

Merged
merged 4 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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[:]...)
}
Loading