(hexString: string)
| 109 | * @throws InvalidParameterError If the input is not a hexadecimal string. |
| 110 | */ |
| 111 | export function hexStringToBytes(hexString: string): Uint8Array { |
| 112 | if (!isHexString(hexString)) { |
| 113 | throw new InvalidParameterError( |
| 114 | `Expected a valid hexadecimal string. Received: ${hexString}`, |
| 115 | ); |
| 116 | } |
| 117 | |
| 118 | // Pad the hex string if it's odd, as Buffer.from will truncate it |
| 119 | // the last character if it's not a full byte. |
| 120 | // See: https://nodejs.org/api/buffer.html#buffers-and-character-encodings |
| 121 | const unprefixedHexString = getUnprefixedHexString(padToEven(hexString)); |
| 122 | return Uint8Array.from(Buffer.from(unprefixedHexString, "hex")); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Normalizes and validates a string that represents a hexadecimal number. |
no test coverage detected