Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added stringUtils contract and test #7

Open
wants to merge 1 commit into
base: matter-labs-audit-fixes
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions contracts/TestStringUtils.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

import { StringUtils } from "./libraries/stringUtils.sol";

contract TestStringUtils {
function test(bytes calldata data) public pure returns (bytes memory result) {
string memory key = abi.decode(
data,
(string)
);
result = StringUtils.hexToBytes(key);
}
}
59 changes: 59 additions & 0 deletions contracts/libraries/stringUtils.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

import { strings } from "./strings.sol";

// Extracted from https://github.com/zkemail/email-wallet-sdk/blob/main/src/helpers/StringUtils.sol
library StringUtils {
using strings for *;

function hexToBytes32(string calldata hexStr) public pure returns (bytes32 result) {
require(hexStr.toSlice().startsWith("0x".toSlice()), "invalid hex prefix");
hexStr = hexStr[2:];
require(bytes(hexStr).length == 64, "invalid hex string length");
uint256[] memory ints = hex2Ints(hexStr);
uint256 sum = 0;
for (uint256 i = 0; i < 32; i++) {
sum = (256 * sum + ints[i]);
}
return bytes32(sum);
}

function hexToBytes(string calldata hexStr) public pure returns (bytes memory) {
require(hexStr.toSlice().startsWith("0x".toSlice()), "invalid hex prefix");
string memory hexStrNoPrefix = hexStr[2:];
bytes memory hexBytes = bytes(hexStrNoPrefix);
require(hexBytes.length % 2 == 0, "invalid hex string length");

bytes memory result = new bytes(hexBytes.length / 2);

for (uint256 i = 0; i < hexBytes.length / 2; i++) {
result[i] = bytes1(
(hexChar2Int(hexBytes[2 * i]) << 4) + hexChar2Int(hexBytes[2 * i + 1])
);
}
return result;
}

function hex2Ints(string memory hexStr) private pure returns (uint256[] memory) {
uint256[] memory result = new uint256[](bytes(hexStr).length / 2);
for (uint256 i = 0; i < result.length; i++) {
result[i] =
16 * hexChar2Int(bytes(hexStr)[2 * i]) + hexChar2Int(bytes(hexStr)[2 * i + 1]);
}
return result;
}

function hexChar2Int(bytes1 char) private pure returns (uint8) {
uint8 charInt = uint8(char);
if (charInt >= 48 && charInt <= 57) {
return charInt - 48; // '0' - '9'
} else if (charInt >= 65 && charInt <= 70) {
return charInt - 55; // 'A' - 'F'
} else if (charInt >= 97 && charInt <= 102) {
return charInt - 87; // 'a' - 'f'
} else {
revert("invalid hex char");
}
}
}
Loading