From 8756074052fd7b2711bc89ab2124c7d7ba790c90 Mon Sep 17 00:00:00 2001 From: Gjermund Garaba Date: Thu, 14 Nov 2024 11:18:32 +0100 Subject: [PATCH] fix commit structure to 32 bytes --- .../core/04-channel/v2/types/commitment.go | 4 +++- .../04-channel/v2/types/commitment_test.go | 16 +++++++++++++ modules/core/24-host/v2/packet_keys_test.go | 24 +++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 modules/core/24-host/v2/packet_keys_test.go diff --git a/modules/core/04-channel/v2/types/commitment.go b/modules/core/04-channel/v2/types/commitment.go index 2350a032ab6..0ec99d6e3e7 100644 --- a/modules/core/04-channel/v2/types/commitment.go +++ b/modules/core/04-channel/v2/types/commitment.go @@ -58,6 +58,8 @@ func CommitAcknowledgement(acknowledgement Acknowledgement) []byte { buf = append(buf, hash[:]...) } + buf = append([]byte{byte(2)}, buf...) + hash := sha256.Sum256(buf) - return append([]byte{byte(2)}, hash[:]...) + return hash[:] } diff --git a/modules/core/04-channel/v2/types/commitment_test.go b/modules/core/04-channel/v2/types/commitment_test.go index 93c4811e2f2..393709c4bd7 100644 --- a/modules/core/04-channel/v2/types/commitment_test.go +++ b/modules/core/04-channel/v2/types/commitment_test.go @@ -44,3 +44,19 @@ func TestCommitPacket(t *testing.T) { require.Len(t, commitment, 32) } + +func TestCommitAcknowledgement(t *testing.T) { + ack := types.Acknowledgement{ + AcknowledgementResults: []types.AcknowledgementResult{ + { + AppName: "doesntmatter", + RecvPacketResult: types.RecvPacketResult{ + Status: 0, // doesnt matter + Acknowledgement: []byte("some bytes"), + }, + }, + }, + } + commitment := types.CommitAcknowledgement(ack) + require.Equal(t, "f03b4667413e56aaf086663267913e525c442b56fa1af4fa3f3dab9f37044c5b", hex.EncodeToString(commitment)) +} diff --git a/modules/core/24-host/v2/packet_keys_test.go b/modules/core/24-host/v2/packet_keys_test.go new file mode 100644 index 00000000000..bdd4d45a1c1 --- /dev/null +++ b/modules/core/24-host/v2/packet_keys_test.go @@ -0,0 +1,24 @@ +package v2_test + +import ( + "encoding/hex" + "testing" + + v2 "github.com/cosmos/ibc-go/v9/modules/core/24-host/v2" + "github.com/stretchr/testify/require" +) + +func TestPacketCommitmentKey(t *testing.T) { + actual := hex.EncodeToString(v2.PacketCommitmentKey("channel-0", 1)) + require.Equal(t, "6368616e6e656c2d30010000000000000001", actual) +} + +func TestPacketReceiptKey(t *testing.T) { + actual := hex.EncodeToString(v2.PacketReceiptKey("channel-0", 1)) + require.Equal(t, "6368616e6e656c2d30020000000000000001", actual) +} + +func TestPacketAcknowledgementKey(t *testing.T) { + actual := hex.EncodeToString(v2.PacketAcknowledgementKey("channel-0", 1)) + require.Equal(t, "6368616e6e656c2d30030000000000000001", actual) +}