Skip to content
Merged
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
Ensure functions that should return Hex, do
Some functions that return hex strings satisfying the `Hex` type return
a `string` type instead. This makes it slightly inconvenient to use
these functions in conjunction with others, and it is common in practice
to use a type assertion as a workaround. This commit rectifies this
issue by assigning these functions proper return types:

- `getChecksumAddress`
- `numberToHex`
- `bigIntToHex`

As such this change is **breaking**.
  • Loading branch information
mcmire committed Jun 21, 2024
commit 11e638f7937ebf94ee5d3560bfdec3ee768a7a2e
2 changes: 1 addition & 1 deletion src/hex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export function isValidHexAddress(possibleAddress: Hex) {
* @returns The address encoded according to ERC-55.
* @see https://eips.ethereum.org/EIPS/eip-55
*/
export function getChecksumAddress(address: Hex) {
export function getChecksumAddress(address: Hex): Hex {
assert(is(address, HexChecksumAddressStruct), 'Invalid hex address.');
const unPrefixed = remove0x(address.toLowerCase());
const unPrefixedHash = remove0x(bytesToHex(keccak256(unPrefixed)));
Expand Down
5 changes: 3 additions & 2 deletions src/number.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { assert } from './assert';
import type { Hex } from './hex';
import { add0x, assertIsHexString } from './hex';

/**
Expand All @@ -18,7 +19,7 @@ import { add0x, assertIsHexString } from './hex';
* @returns The hexadecimal string, with the "0x"-prefix.
* @throws If the number is not a non-negative safe integer.
*/
export const numberToHex = (value: number): string => {
export const numberToHex = (value: number): Hex => {
assert(typeof value === 'number', 'Value must be a number.');
assert(value >= 0, 'Value must be a non-negative number.');
assert(
Expand All @@ -45,7 +46,7 @@ export const numberToHex = (value: number): string => {
* @returns The hexadecimal string, with the "0x"-prefix.
* @throws If the `bigint` is not a non-negative integer.
*/
export const bigIntToHex = (value: bigint): string => {
export const bigIntToHex = (value: bigint): Hex => {
assert(typeof value === 'bigint', 'Value must be a bigint.');
assert(value >= 0, 'Value must be a non-negative bigint.');

Expand Down