Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: preserve value for numeric arguments within address padding utility #1295

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
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
16 changes: 16 additions & 0 deletions __tests__/utils/address.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ describe('addAddressPadding', () => {

return expect(addAddressPadding(addr)).toBe(padded);
});

test('should pad a number value', () => {
const addr1 = 31;
const addr2 = 0x1f;
const padded = '0x000000000000000000000000000000000000000000000000000000000000001f';

expect(addAddressPadding(addr1)).toBe(padded);
expect(addAddressPadding(addr2)).toBe(padded);
});

test('should pad a bigint value', () => {
const addr = constants.ADDR_BOUND;
const padded = '0x07ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00';

expect(addAddressPadding(addr)).toBe(padded);
});
});

describe('validateAndParseAddress', () => {
Expand Down
31 changes: 21 additions & 10 deletions src/utils/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,47 @@ import { BigNumberish } from '../types';
import { addHexPrefix, removeHexPrefix } from './encode';
import { keccakBn } from './hash';
import { assertInRange, toHex } from './num';
import { isString } from './typed';

/**
* Format a hex number to '0x' and 64 characters, adding leading zeros if necessary.
*
* @param {BigNumberish} address
* @returns {string} Hex string : 0x followed by 64 characters. No upper case characters in the response.
* @returns {string} Hex string: 0x followed by 64 characters. No upper case characters in the response.
* @example
* ```typescript
* const address = "0x90591d9fa3efc87067d95a643f8455e0b8190eb8cb7bfd39e4fb7571fdf";
* const result = addAddressPadding(address);
* // result = "0x0000090591d9fa3efc87067d95a643f8455e0b8190eb8cb7bfd39e4fb7571fdf"
* const result = [31, 0x1f, '31', '0x1f', '0x90591d9fa3efc87067d95a643f8455e0b8190eb8cb7bfd39e4fb7571fdf'].map(addAddressPadding);
* // result = [
* // '0x000000000000000000000000000000000000000000000000000000000000001f',
* // '0x000000000000000000000000000000000000000000000000000000000000001f',
* // '0x0000000000000000000000000000000000000000000000000000000000000031',
* // '0x000000000000000000000000000000000000000000000000000000000000001f',
* // '0x0000090591d9fa3efc87067d95a643f8455e0b8190eb8cb7bfd39e4fb7571fdf'
* // ]
* ```
*/
export function addAddressPadding(address: BigNumberish): string {
const hex = toHex(addHexPrefix(address.toString()));
const hex = toHex(isString(address) ? addHexPrefix(address) : address);
const padded = removeHexPrefix(hex).padStart(64, '0');
return addHexPrefix(padded);
}

/**
* Check the validity of a Starknet address, and format it as a hex number : '0x' and 64 characters, adding leading zeros if necessary.
* Check the validity of a Starknet address, and format it as a hex number: '0x' and 64 characters, adding leading zeros if necessary.
*
* @param {BigNumberish} address
* @returns {string} Hex string : 0x followed by 64 characters. No upper case characters in the response.
* @returns {string} Hex string: 0x followed by 64 characters. No upper case characters in the response.
* @throws address argument must be a valid address inside the address range bound
* @example
* ```typescript
* const address = "0x90591d9fa3efc87067d95a643f8455e0b8190eb8cb7bfd39e4fb7571fdf";
* const result = validateAndParseAddress(address);
* // result = "0x0000090591d9fa3efc87067d95a643f8455e0b8190eb8cb7bfd39e4fb7571fdf"
* const result = [31, 0x1f, '31', '0x1f', '0x90591d9fa3efc87067d95a643f8455e0b8190eb8cb7bfd39e4fb7571fdf'].map(addAddressPadding);
* // result = [
* // '0x000000000000000000000000000000000000000000000000000000000000001f',
* // '0x000000000000000000000000000000000000000000000000000000000000001f',
* // '0x0000000000000000000000000000000000000000000000000000000000000031',
* // '0x000000000000000000000000000000000000000000000000000000000000001f',
* // '0x0000090591d9fa3efc87067d95a643f8455e0b8190eb8cb7bfd39e4fb7571fdf'
* // ]
* ```
*/
export function validateAndParseAddress(address: BigNumberish): string {
Expand Down
Loading