Skip to content

Commit

Permalink
fix: return "0x00" for normalize(0)
Browse files Browse the repository at this point in the history
  • Loading branch information
legobeat committed Apr 29, 2023
1 parent 90fecaf commit 8f9c3a2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ describe('normalize', function () {
expect(result).toBe(`0x${initial.toLowerCase()}`);
});

it('should normalize 0 to a byte-pair hex string', function () {
const initial = 0;
const result = normalize(initial);
expect(result).toBe('0x00');
});

it('should normalize an integer to a byte-pair hex string', function () {
const initial = 1;
const result = normalize(initial);
Expand Down
8 changes: 4 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,6 @@ export function recoverPublicKey(
* @returns The normalized value.
*/
export function normalize(input: number | string): string | undefined {
if (!input) {
return undefined;
}

if (typeof input === 'number') {
if (input < 0) {
return '0x';
Expand All @@ -115,6 +111,10 @@ export function normalize(input: number | string): string | undefined {
input = bufferToHex(buffer);
}

if (!input) {
return undefined;
}

if (typeof input !== 'string') {
let msg = 'eth-sig-util.normalize() requires hex string or integer input.';
msg += ` received ${typeof input}: ${input as any as string}`;
Expand Down

0 comments on commit 8f9c3a2

Please sign in to comment.