(hexString: string)
| 35 | * @throws InvalidParameterError If the input is not a hexadecimal string. |
| 36 | */ |
| 37 | export function hexStringToBigInt(hexString: string): bigint { |
| 38 | if (!isHexString(hexString)) { |
| 39 | throw new InvalidParameterError( |
| 40 | `Expected a valid hexadecimal string. Received: ${hexString}`, |
| 41 | ); |
| 42 | } |
| 43 | // Prefix the string as it is required to make BigInt interpret it as a |
| 44 | // hexadecimal number. |
| 45 | let prefixedHexString = getPrefixedHexString(hexString); |
| 46 | // BigInt does not support "0x" as a valid hexadecimal number, so we need to |
| 47 | // add a zero after the prefix if the string is "0x". |
| 48 | prefixedHexString = prefixedHexString === "0x" ? "0x0" : prefixedHexString; |
| 49 | |
| 50 | const bigInt = BigInt(prefixedHexString); |
| 51 | |
| 52 | return bigInt; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Converts a hexadecimal string to a number. The string must be a valid |
no test coverage detected