@@ -6,36 +6,47 @@ import { BigNumberish } from '../types';
6
6
import { addHexPrefix , removeHexPrefix } from './encode' ;
7
7
import { keccakBn } from './hash' ;
8
8
import { assertInRange , toHex } from './num' ;
9
+ import { isString } from './typed' ;
9
10
10
11
/**
11
12
* Format a hex number to '0x' and 64 characters, adding leading zeros if necessary.
12
13
*
13
14
* @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.
15
16
* @example
16
17
* ```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
+ * // ]
20
26
* ```
21
27
*/
22
28
export function addAddressPadding ( address : BigNumberish ) : string {
23
- const hex = toHex ( addHexPrefix ( address . toString ( ) ) ) ;
29
+ const hex = toHex ( isString ( address ) ? addHexPrefix ( address ) : address ) ;
24
30
const padded = removeHexPrefix ( hex ) . padStart ( 64 , '0' ) ;
25
31
return addHexPrefix ( padded ) ;
26
32
}
27
33
28
34
/**
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.
30
36
*
31
37
* @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.
33
39
* @throws address argument must be a valid address inside the address range bound
34
40
* @example
35
41
* ```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
+ * // ]
39
50
* ```
40
51
*/
41
52
export function validateAndParseAddress ( address : BigNumberish ) : string {
0 commit comments