From 6d7e2e35d49c28b54141ee063b36a997c9d20d6e Mon Sep 17 00:00:00 2001 From: gcranju Date: Tue, 10 Dec 2024 14:52:22 +0545 Subject: [PATCH] fix: msg as bytes --- .../contracts/adapters/ClusterConnection.sol | 2 +- .../evm/test/adapters/ClusterConnection.t.sol | 2 +- .../adapter/cluster/ClusterConnection.java | 19 ++++++++-- .../cluster/ClusterConnectionTest.java | 36 ++++++++++--------- 4 files changed, 38 insertions(+), 21 deletions(-) diff --git a/contracts/evm/contracts/adapters/ClusterConnection.sol b/contracts/evm/contracts/adapters/ClusterConnection.sol index 69efae42..b5e76c1c 100644 --- a/contracts/evm/contracts/adapters/ClusterConnection.sol +++ b/contracts/evm/contracts/adapters/ClusterConnection.sol @@ -322,7 +322,7 @@ contract ClusterConnection is Initializable, IConnection { .encodePacked( srcNetwork, _connSn.toString(), - _msg.bytesToHex(), + _msg, dstNetwork ); return keccak256(encoded); diff --git a/contracts/evm/test/adapters/ClusterConnection.t.sol b/contracts/evm/test/adapters/ClusterConnection.t.sol index e301a8b2..dccef0cc 100644 --- a/contracts/evm/test/adapters/ClusterConnection.t.sol +++ b/contracts/evm/test/adapters/ClusterConnection.t.sol @@ -401,7 +401,7 @@ contract ClusterConnectionTest is Test { .encodePacked( srcNetwork, _connSn.toString(), - _msg.bytesToHex(), + _msg, dstNetwork ); return keccak256(encoded); diff --git a/contracts/javascore/cluster-connection/src/main/java/xcall/adapter/cluster/ClusterConnection.java b/contracts/javascore/cluster-connection/src/main/java/xcall/adapter/cluster/ClusterConnection.java index aff298dd..42bcbf88 100644 --- a/contracts/javascore/cluster-connection/src/main/java/xcall/adapter/cluster/ClusterConnection.java +++ b/contracts/javascore/cluster-connection/src/main/java/xcall/adapter/cluster/ClusterConnection.java @@ -359,8 +359,23 @@ private void OnlyAdmin() { * @return the hash of the message */ private byte[] getMessageHash(String srcNetwork, BigInteger _connSn, byte[] msg, String dstNetwork) { - String message = srcNetwork + String.valueOf(_connSn) + bytesToHex(msg) + dstNetwork; - return Context.hash("keccak-256", message.getBytes()); + byte[] result = concatBytes(srcNetwork.getBytes(), String.valueOf(_connSn).getBytes(), msg, dstNetwork.getBytes()); + return Context.hash("keccak-256", result); } + private static byte[] concatBytes(byte[]... arrays) { + int totalLength = 0; + for (byte[] array : arrays) { + totalLength += array.length; + } + byte[] result = new byte[totalLength]; + int currentIndex = 0; + for (byte[] array : arrays) { + System.arraycopy(array, 0, result, currentIndex, array.length); + currentIndex += array.length; + } + return result; + } + + } \ No newline at end of file diff --git a/contracts/javascore/cluster-connection/src/test/java/xcall/adapter/cluster/ClusterConnectionTest.java b/contracts/javascore/cluster-connection/src/test/java/xcall/adapter/cluster/ClusterConnectionTest.java index 6e29a92f..5d28e744 100644 --- a/contracts/javascore/cluster-connection/src/test/java/xcall/adapter/cluster/ClusterConnectionTest.java +++ b/contracts/javascore/cluster-connection/src/test/java/xcall/adapter/cluster/ClusterConnectionTest.java @@ -238,23 +238,6 @@ public void testRecvMessageWithSignaturesNotEnoughValidSignatures() throws Excep assertEquals("Reverted(0): Not enough valid signatures", e.getMessage()); } - private byte[] getMessageHash(String srcNetwork, BigInteger _connSn, byte[] msg, String dstNetwork) { - String message = srcNetwork + String.valueOf(_connSn) + bytesToHex(msg) + dstNetwork; - return Context.hash("keccak-256", message.getBytes()); - } - - private String bytesToHex(byte[] bytes) { - StringBuilder hexString = new StringBuilder(); - for (byte b : bytes) { - String hex = Integer.toHexString(0xff & b); // Mask with 0xff to handle negative values correctly - if (hex.length() == 1) { - hexString.append('0'); // Add a leading zero if hex length is 1 - } - hexString.append(hex); - } - return hexString.toString(); - } - @Test public void testAddSigners() throws Exception { KeyWallet wallet = KeyWallet.create(); @@ -268,5 +251,24 @@ public void testAddSigners() throws Exception { assertEquals(signers.length, 2); } + private byte[] getMessageHash(String srcNetwork, BigInteger _connSn, byte[] msg, String dstNetwork) { + byte[] result = concatBytes(srcNetwork.getBytes(), String.valueOf(_connSn).getBytes(), msg, dstNetwork.getBytes()); + return Context.hash("keccak-256", result); + } + + private static byte[] concatBytes(byte[]... arrays) { + int totalLength = 0; + for (byte[] array : arrays) { + totalLength += array.length; + } + byte[] result = new byte[totalLength]; + int currentIndex = 0; + for (byte[] array : arrays) { + System.arraycopy(array, 0, result, currentIndex, array.length); + currentIndex += array.length; + } + return result; + } + } \ No newline at end of file