Skip to content

Commit

Permalink
chore: move PacketV2 to channel/v2 proto and rename it to Packet (#7357)
Browse files Browse the repository at this point in the history
* chore: move PacketV2 to channel/v2

* remove file accidentally added

* chore: created packet.go for v2

* rename
  • Loading branch information
bznein authored Oct 1, 2024
1 parent f2f46bc commit a42e1c7
Show file tree
Hide file tree
Showing 6 changed files with 1,236 additions and 1,099 deletions.
1,068 changes: 71 additions & 997 deletions modules/core/04-channel/types/channel.pb.go

Large diffs are not rendered by default.

66 changes: 0 additions & 66 deletions modules/core/04-channel/types/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,42 +101,6 @@ func commitV2Packet(packet Packet) []byte {
return hash[:]
}

// CommitPacketV2 returns the V2 packet commitment bytes. The commitment consists of:
// sha256_hash(timeout) + sha256_hash(destinationID) + sha256_hash(packetData) 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 CommitPacketV2(packet PacketV2) []byte {
buf := sdk.Uint64ToBigEndian(packet.GetTimeoutTimestamp())

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

for _, data := range packet.Data {
buf = append(buf, hashPacketData(data)...)
}

hash := sha256.Sum256(buf)
return hash[:]
}

// hashPacketData returns the hash of the packet data.
func hashPacketData(data PacketData) []byte {
var buf []byte
sourceHash := sha256.Sum256([]byte(data.SourcePort))
buf = append(buf, sourceHash[:]...)
destHash := sha256.Sum256([]byte(data.DestinationPort))
buf = append(buf, destHash[:]...)
payloadValueHash := sha256.Sum256(data.Payload.Value)
buf = append(buf, payloadValueHash[:]...)
payloadEncodingHash := sha256.Sum256([]byte(data.Payload.Encoding))
buf = append(buf, payloadEncodingHash[:]...)
payloadVersionHash := sha256.Sum256([]byte(data.Payload.Version))
buf = append(buf, payloadVersionHash[:]...)
hash := sha256.Sum256(buf)
return hash[:]
}

// CommitAcknowledgement returns the hash of commitment bytes
func CommitAcknowledgement(data []byte) []byte {
hash := sha256.Sum256(data)
Expand Down Expand Up @@ -264,33 +228,3 @@ func (p PacketId) Validate() error {
func NewPacketID(portID, channelID string, seq uint64) PacketId {
return PacketId{PortId: portID, ChannelId: channelID, Sequence: seq}
}

// ConvertPacketV1toV2 constructs a PacketV2 from a Packet.
func ConvertPacketV1toV2(packet Packet) (PacketV2, error) {
if packet.ProtocolVersion != IBC_VERSION_2 {
return PacketV2{}, errorsmod.Wrapf(ErrInvalidPacket, "expected protocol version %s, got %s instead", IBC_VERSION_2, packet.ProtocolVersion)
}

encoding := strings.TrimSpace(packet.Encoding)
if encoding == "" {
encoding = "json"
}

return PacketV2{
Sequence: packet.Sequence,
SourceId: packet.SourceChannel,
DestinationId: packet.DestinationChannel,
TimeoutTimestamp: packet.TimeoutTimestamp,
Data: []PacketData{
{
SourcePort: packet.SourcePort,
DestinationPort: packet.DestinationPort,
Payload: Payload{
Version: packet.AppVersion,
Encoding: encoding,
Value: packet.Data,
},
},
},
}, nil
}
Loading

0 comments on commit a42e1c7

Please sign in to comment.