| 63 | } |
| 64 | |
| 65 | inline vector<uint8_t> fromHex(const string &hex, size_t expected_length = 0) { |
| 66 | string hex_str = hex; |
| 67 | if (hex.substr(0, 2) == "0x") { |
| 68 | hex_str = hex.substr(2); |
| 69 | } |
| 70 | |
| 71 | if (expected_length > 0 && hex_str.length() != expected_length * 2) { |
| 72 | throw runtime_error("Invalid hex string length: expected " + |
| 73 | std::to_string(expected_length) + " bytes, got " + |
| 74 | std::to_string(hex_str.length() / 2) + " bytes"); |
| 75 | } |
| 76 | |
| 77 | vector<uint8_t> result; |
| 78 | result.reserve(hex_str.length() / 2); |
| 79 | for (size_t i = 0; i < hex_str.length(); i += 2) { |
| 80 | string byteString = hex_str.substr(i, 2); |
| 81 | uint8_t byte = |
| 82 | static_cast<uint8_t>(strtol(byteString.c_str(), nullptr, 16)); |
| 83 | result.push_back(byte); |
| 84 | } |
| 85 | return result; |
| 86 | } |
| 87 | |
| 88 | inline array<uint8_t, 32> fromHex32(const string &hex) { |
| 89 | vector<uint8_t> bytes = fromHex(hex, 32); |
no outgoing calls
no test coverage detected