Skip to content

Commit

Permalink
Ensure functions that should return Hex, do (#193)
Browse files Browse the repository at this point in the history
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 authored Jun 27, 2024
1 parent 6000435 commit 369e5b2
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
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

0 comments on commit 369e5b2

Please sign in to comment.