1
1
package ibc .ics04 .channel ;
2
2
3
- import ibc .icon .interfaces .ILightClient ;
4
3
import ibc .icon .score .util .ByteUtil ;
5
4
import ibc .icon .score .util .Proto ;
6
- import ibc .icon .structs .messages .MsgRequestTimeoutPacket ;
7
5
import ibc .ics24 .host .IBCCommitment ;
8
- import icon .proto .core .channel .Channel ;
9
- import icon .proto .core .channel .Packet ;
10
- import icon .proto .core .client .Height ;
11
- import icon .proto .core .connection .ConnectionEnd ;
6
+ import ibc .ics24 .host .IBCHost ;
7
+ import icon .ibc .interfaces .ILightClient ;
8
+ import icon .ibc .structs .messages .MsgRequestTimeoutPacket ;
12
9
import score .Context ;
13
10
import score .DictDB ;
14
11
15
12
import java .math .BigInteger ;
16
13
import java .util .Arrays ;
17
14
15
+ import icon .proto .core .channel .Channel ;
16
+ import icon .proto .core .channel .Packet ;
17
+ import icon .proto .core .client .Height ;
18
+ import icon .proto .core .connection .ConnectionEnd ;
19
+
18
20
public class IBCPacket extends IBCChannelHandshake {
19
21
20
22
public void _sendPacket (Packet packet ) {
@@ -57,7 +59,7 @@ public void _sendPacket(Packet packet) {
57
59
packet .getSourceChannel (),
58
60
packet .getSequence ());
59
61
60
- byte [] packetCommitment = createPacketCommitment (packet );
62
+ byte [] packetCommitment = createPacketCommitment (connection . getClientId (), packet );
61
63
commitments .set (packetCommitmentKey , packetCommitment );
62
64
packetHeights .at (packet .getSourcePort ()).at (packet .getSourceChannel ()).set (packet .getSequence (),
63
65
Context .getBlockHeight ());
@@ -98,7 +100,7 @@ public void _recvPacket(Packet packet, byte[] proof, byte[] proofHeight) {
98
100
99
101
byte [] commitmentPath = IBCCommitment .packetCommitmentPath (packet .getSourcePort (),
100
102
packet .getSourceChannel (), packet .getSequence ());
101
- byte [] commitmentBytes = createPacketCommitmentBytes (packet );
103
+ byte [] commitmentBytes = createPacketCommitmentBytes (connection . getClientId (), packet );
102
104
103
105
verifyPacketCommitment (
104
106
connection ,
@@ -144,7 +146,7 @@ public void _writeAcknowledgement(String destinationPortId, String destinationCh
144
146
byte [] ackCommitmentKey = IBCCommitment .packetAcknowledgementCommitmentKey (destinationPortId ,
145
147
destinationChannel , sequence );
146
148
Context .require (commitments .get (ackCommitmentKey ) == null , "acknowledgement for packet already exists" );
147
- byte [] ackCommitment = IBCCommitment . keccak256 ( acknowledgement );
149
+ byte [] ackCommitment = createAcknowledgmentCommitment ( connection . getClientId (), acknowledgement );
148
150
commitments .set (ackCommitmentKey , ackCommitment );
149
151
ackHeights .at (destinationPortId ).at (destinationChannel ).set (sequence ,
150
152
Context .getBlockHeight ());
@@ -173,18 +175,19 @@ public void _acknowledgePacket(Packet packet, byte[] acknowledgement, byte[] pro
173
175
packet .getSourceChannel (), packet .getSequence ());
174
176
byte [] packetCommitment = commitments .get (packetCommitmentKey );
175
177
Context .require (packetCommitment != null , "packet commitment not found" );
176
- byte [] commitment = createPacketCommitment (packet );
178
+ byte [] commitment = createPacketCommitment (connection . getClientId (), packet );
177
179
178
180
Context .require (Arrays .equals (packetCommitment , commitment ), "commitment byte[] are not equal" );
179
181
180
182
byte [] packetAckPath = IBCCommitment .packetAcknowledgementCommitmentPath (packet .getDestinationPort (),
181
183
packet .getDestinationChannel (), packet .getSequence ());
184
+ byte [] commitmentBytes = createAcknowledgmentCommitmentBytes (connection .getClientId (), acknowledgement );
182
185
verifyPacketAcknowledgement (
183
186
connection ,
184
187
proofHeight ,
185
188
proof ,
186
189
packetAckPath ,
187
- acknowledgement );
190
+ commitmentBytes );
188
191
189
192
if (channel .getOrdering () == Channel .Order .ORDER_ORDERED ) {
190
193
DictDB <String , BigInteger > nextSequenceAckSourcePort = nextSequenceAcknowledgements
@@ -235,7 +238,7 @@ public void _requestTimeout(MsgRequestTimeoutPacket msg) {
235
238
236
239
byte [] commitmentPath = IBCCommitment .packetCommitmentPath (packet .getSourcePort (),
237
240
packet .getSourceChannel (), packet .getSequence ());
238
- byte [] commitmentBytes = createPacketCommitmentBytes (packet );
241
+ byte [] commitmentBytes = createPacketCommitmentBytes (connection . getClientId (), packet );
239
242
verifyPacketCommitment (
240
243
connection ,
241
244
proofHeight ,
@@ -299,7 +302,7 @@ public void _timeoutPacket(Packet packet, byte[] proofHeight, byte[] proof, BigI
299
302
packet .getSourceChannel (), packet .getSequence ());
300
303
byte [] packetCommitment = commitments .get (packetCommitmentKey );
301
304
Context .require (packetCommitment != null , "packet commitment not found" );
302
- byte [] commitment = createPacketCommitment (packet );
305
+ byte [] commitment = createPacketCommitment (connection . getClientId (), packet );
303
306
304
307
Context .require (Arrays .equals (packetCommitment , commitment ), "commitment byte[] are not equal" );
305
308
@@ -431,11 +434,20 @@ private BigInteger calcBlockDelay(BigInteger timeDelay) {
431
434
return blockDelay ;
432
435
}
433
436
434
- private byte [] createPacketCommitment (Packet packet ) {
435
- return IBCCommitment .keccak256 (createPacketCommitmentBytes (packet ));
437
+ private byte [] createPacketCommitment (String clientId , Packet packet ) {
438
+ return IBCCommitment .keccak256 (createPacketCommitmentBytes (clientId , packet ));
439
+ }
440
+
441
+ private byte [] createPacketCommitmentBytes (String clientId , Packet packet ) {
442
+ int hashType = IBCCommitment .getHashType (clientId );
443
+ if (hashType == IBCHost .HashType .ICS08 .type ) {
444
+ return createIBCPacketCommitmentBytes (packet );
445
+ }
446
+
447
+ return createWasmPacketCommitmentBytes (packet );
436
448
}
437
449
438
- public static byte [] createPacketCommitmentBytes (Packet packet ) {
450
+ public static byte [] createWasmPacketCommitmentBytes (Packet packet ) {
439
451
return ByteUtil .join (
440
452
Proto .encodeFixed64 (packet .getTimeoutTimestamp (), false ),
441
453
Proto .encodeFixed64 (packet .getTimeoutHeight ().getRevisionNumber (),
@@ -445,6 +457,29 @@ public static byte[] createPacketCommitmentBytes(Packet packet) {
445
457
IBCCommitment .keccak256 (packet .getData ()));
446
458
}
447
459
460
+ public static byte [] createIBCPacketCommitmentBytes (Packet packet ) {
461
+ return IBCCommitment .sha256 (ByteUtil .join (
462
+ Proto .encodeFixed64 (packet .getTimeoutTimestamp (), false ),
463
+ Proto .encodeFixed64 (packet .getTimeoutHeight ().getRevisionNumber (),
464
+ false ),
465
+ Proto .encodeFixed64 (packet .getTimeoutHeight ().getRevisionHeight (),
466
+ false ),
467
+ IBCCommitment .sha256 (packet .getData ())));
468
+ }
469
+
470
+ private byte [] createAcknowledgmentCommitment (String clientId , byte [] ack ) {
471
+ return IBCCommitment .keccak256 (createAcknowledgmentCommitmentBytes (clientId , ack ));
472
+ }
473
+
474
+ public static byte [] createAcknowledgmentCommitmentBytes (String clientId , byte [] ack ) {
475
+ int hashType = IBCCommitment .getHashType (clientId );
476
+ if (hashType == IBCHost .HashType .ICS08 .type ) {
477
+ return IBCCommitment .sha256 (ack );
478
+ }
479
+
480
+ return ack ;
481
+ }
482
+
448
483
private boolean lt (Height h1 , Height h2 ) {
449
484
return h1 .getRevisionNumber ().compareTo (h2 .getRevisionNumber ()) < 0
450
485
|| (h1 .getRevisionNumber ().equals (h2 .getRevisionNumber ())
0 commit comments