Skip to content

Commit abc7958

Browse files
committed
fix: preserve value for numeric arguments within address padding utility
1 parent bdec7f0 commit abc7958

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

__tests__/utils/address.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,22 @@ describe('addAddressPadding', () => {
2020

2121
return expect(addAddressPadding(addr)).toBe(padded);
2222
});
23+
24+
test('should pad a number value', () => {
25+
const addr1 = 31;
26+
const addr2 = 0x1f;
27+
const padded = '0x000000000000000000000000000000000000000000000000000000000000001f';
28+
29+
expect(addAddressPadding(addr1)).toBe(padded);
30+
expect(addAddressPadding(addr2)).toBe(padded);
31+
});
32+
33+
test('should pad a bigint value', () => {
34+
const addr = constants.ADDR_BOUND;
35+
const padded = '0x07ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00';
36+
37+
expect(addAddressPadding(addr)).toBe(padded);
38+
});
2339
});
2440

2541
describe('validateAndParseAddress', () => {

src/utils/address.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,47 @@ import { BigNumberish } from '../types';
66
import { addHexPrefix, removeHexPrefix } from './encode';
77
import { keccakBn } from './hash';
88
import { assertInRange, toHex } from './num';
9+
import { isString } from './typed';
910

1011
/**
1112
* Format a hex number to '0x' and 64 characters, adding leading zeros if necessary.
1213
*
1314
* @param {BigNumberish} address
14-
* @returns {string} Hex string : 0x followed by 64 characters. No upper case characters in the response.
15+
* @returns {string} Hex string: 0x followed by 64 characters. No upper case characters in the response.
1516
* @example
1617
* ```typescript
17-
* const address = "0x90591d9fa3efc87067d95a643f8455e0b8190eb8cb7bfd39e4fb7571fdf";
18-
* const result = addAddressPadding(address);
19-
* // result = "0x0000090591d9fa3efc87067d95a643f8455e0b8190eb8cb7bfd39e4fb7571fdf"
18+
* const result = [31, 0x1f, '31', '0x1f', '0x90591d9fa3efc87067d95a643f8455e0b8190eb8cb7bfd39e4fb7571fdf'].map(addAddressPadding);
19+
* // result = [
20+
* // '0x000000000000000000000000000000000000000000000000000000000000001f',
21+
* // '0x000000000000000000000000000000000000000000000000000000000000001f',
22+
* // '0x0000000000000000000000000000000000000000000000000000000000000031',
23+
* // '0x000000000000000000000000000000000000000000000000000000000000001f',
24+
* // '0x0000090591d9fa3efc87067d95a643f8455e0b8190eb8cb7bfd39e4fb7571fdf'
25+
* // ]
2026
* ```
2127
*/
2228
export function addAddressPadding(address: BigNumberish): string {
23-
const hex = toHex(addHexPrefix(address.toString()));
29+
const hex = toHex(isString(address) ? addHexPrefix(address) : address);
2430
const padded = removeHexPrefix(hex).padStart(64, '0');
2531
return addHexPrefix(padded);
2632
}
2733

2834
/**
29-
* Check the validity of a Starknet address, and format it as a hex number : '0x' and 64 characters, adding leading zeros if necessary.
35+
* Check the validity of a Starknet address, and format it as a hex number: '0x' and 64 characters, adding leading zeros if necessary.
3036
*
3137
* @param {BigNumberish} address
32-
* @returns {string} Hex string : 0x followed by 64 characters. No upper case characters in the response.
38+
* @returns {string} Hex string: 0x followed by 64 characters. No upper case characters in the response.
3339
* @throws address argument must be a valid address inside the address range bound
3440
* @example
3541
* ```typescript
36-
* const address = "0x90591d9fa3efc87067d95a643f8455e0b8190eb8cb7bfd39e4fb7571fdf";
37-
* const result = validateAndParseAddress(address);
38-
* // result = "0x0000090591d9fa3efc87067d95a643f8455e0b8190eb8cb7bfd39e4fb7571fdf"
42+
* const result = [31, 0x1f, '31', '0x1f', '0x90591d9fa3efc87067d95a643f8455e0b8190eb8cb7bfd39e4fb7571fdf'].map(addAddressPadding);
43+
* // result = [
44+
* // '0x000000000000000000000000000000000000000000000000000000000000001f',
45+
* // '0x000000000000000000000000000000000000000000000000000000000000001f',
46+
* // '0x0000000000000000000000000000000000000000000000000000000000000031',
47+
* // '0x000000000000000000000000000000000000000000000000000000000000001f',
48+
* // '0x0000090591d9fa3efc87067d95a643f8455e0b8190eb8cb7bfd39e4fb7571fdf'
49+
* // ]
3950
* ```
4051
*/
4152
export function validateAndParseAddress(address: BigNumberish): string {

0 commit comments

Comments
 (0)