Skip to content

Commit 3507b42

Browse files
authored
fix(contracts): new stricter typecast rules (#38)
New rules were enforced in SeismicSystems/seismic-solidity#204, such as not being allowed to abi.encode a shielded type. Fixed these mindlessly, but I think we should take some time to look at the devex related to our current rules. It looks like we force a lot of manual typecasts which might confuse more than help. For example, we disallow abi encoding shielded types; I think a more intuitive rule would be to allow abi encoding shielded types but forcing the output to be sbytes instead of bytes?
1 parent 978fb71 commit 3507b42

4 files changed

Lines changed: 9 additions & 9 deletions

File tree

contracts/src/directory/Directory.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ contract Directory is IDirectory {
2020
}
2121

2222
function checkHasKey(address _addr) public view returns (bool) {
23-
return keys[_addr] != suint256(0);
23+
return bool(keys[_addr] != suint256(0));
2424
}
2525

2626
function keyHash(address to) public view returns (bytes32) {
27-
return keccak256(abi.encodePacked(keys[to]));
27+
return keccak256(abi.encodePacked(uint256(keys[to])));
2828
}
2929

3030
function encrypt(address to, bytes memory _plaintext) public returns (bytes memory) {

contracts/src/seismic-std-lib/utils/precompiles/CryptoUtils.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ library CryptoUtils {
8080
view
8181
returns (bytes memory ciphertext)
8282
{
83-
bytes memory input = abi.encodePacked(key, nonce, plaintext);
83+
bytes memory input = abi.encodePacked(uint256(key), nonce, plaintext);
8484
(bool success, bytes memory output) = AES_ENCRYPT_PRECOMPILE.staticcall(input);
8585
if (!success) revert AESPrecompileCallFailed();
8686
if (output.length == 0) revert EncryptionReturnedNoOutput();
@@ -98,7 +98,7 @@ library CryptoUtils {
9898
returns (bytes memory plaintext)
9999
{
100100
if (ciphertext.length == 0) revert CiphertextCannotBeEmpty();
101-
bytes memory input = abi.encodePacked(key, nonce, ciphertext);
101+
bytes memory input = abi.encodePacked(uint256(key), nonce, ciphertext);
102102
(bool success, bytes memory output) = AES_DECRYPT_PRECOMPILE.staticcall(input);
103103
if (!success) revert AESPrecompileCallFailed();
104104
return output;

contracts/test/Intelligence.t.sol

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ contract IntelligenceTest is Test {
5252
assertEq(extractCT(encryptedData[1]), directCiphertext[1]);
5353

5454
bytes32[] memory directHashes = new bytes32[](2);
55-
directHashes[0] = keccak256(abi.encodePacked(aliceKey));
56-
directHashes[1] = keccak256(abi.encodePacked(bobKey));
55+
directHashes[0] = keccak256(abi.encodePacked(uint256(aliceKey)));
56+
directHashes[1] = keccak256(abi.encodePacked(uint256(bobKey)));
5757
assertEq(hashes[0], directHashes[0]);
5858
assertEq(hashes[1], directHashes[1]);
5959
}
@@ -73,7 +73,7 @@ contract IntelligenceTest is Test {
7373

7474
assertEq(intelligence.numProviders(), 3);
7575
assertEq(extractCT(encryptedData[2]), directCiphertext);
76-
assertEq(hashes[2], keccak256(abi.encodePacked(charlieKey)));
76+
assertEq(hashes[2], keccak256(abi.encodePacked(uint256(charlieKey))));
7777

7878
vm.prank(intelligence.owner());
7979
vm.expectRevert("DUPLICATE_PROVIDER");
@@ -93,7 +93,7 @@ contract IntelligenceTest is Test {
9393
assertEq(extractCT(encryptedData[0]), directCiphertext);
9494

9595
bytes32 h = directory.keyHash(bob);
96-
assertEq(h, keccak256(abi.encodePacked(bobKey)));
96+
assertEq(h, keccak256(abi.encodePacked(uint256(bobKey))));
9797
}
9898

9999
function test_RevertIfRemoveUnkownProvider() public {

contracts/test/ShieldedDelegationAccount.t.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ contract ShieldedDelegationAccountTest is Test, ShieldedDelegationAccount {
250250
function _createTokenTransferCall(address recipient, uint256 amount) internal view returns (bytes memory calls) {
251251
// Create the transfer function call data
252252
bytes memory transferData =
253-
abi.encodeWithSelector(SRC20.transfer.selector, saddress(recipient), suint256(amount));
253+
abi.encodeWithSelector(SRC20.transfer.selector, recipient, amount);
254254

255255
// Format it for MultiSend
256256
return abi.encodePacked(

0 commit comments

Comments
 (0)