Skip to content

Commit

Permalink
fix: msg as bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
gcranju committed Dec 10, 2024
1 parent 3baf010 commit 6d7e2e3
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 21 deletions.
2 changes: 1 addition & 1 deletion contracts/evm/contracts/adapters/ClusterConnection.sol
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ contract ClusterConnection is Initializable, IConnection {
.encodePacked(
srcNetwork,
_connSn.toString(),
_msg.bytesToHex(),
_msg,
dstNetwork
);
return keccak256(encoded);
Expand Down
2 changes: 1 addition & 1 deletion contracts/evm/test/adapters/ClusterConnection.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ contract ClusterConnectionTest is Test {
.encodePacked(
srcNetwork,
_connSn.toString(),
_msg.bytesToHex(),
_msg,
dstNetwork
);
return keccak256(encoded);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
}


}

0 comments on commit 6d7e2e3

Please sign in to comment.