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

feat: num.toHex64 ensure 0x(64 char) format #1222

Merged
merged 5 commits into from
Oct 16, 2024
Merged
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
74 changes: 65 additions & 9 deletions __tests__/utils/num.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { num } from '../../src';
import {
isHex,
toBigInt,
toHex,
hexToDecimalString,
cleanHex,
addPercent,
assertInRange,
bigNumberishArrayToDecimalStringArray,
bigNumberishArrayToHexadecimalStringArray,
isStringWholeNumber,
cleanHex,
getDecimalString,
getHexString,
getHexStringArray,
toCairoBool,
hexToBytes,
addPercent,
hexToDecimalString,
isHex,
isStringWholeNumber,
toBigInt,
toCairoBool,
toHex,
} from '../../src/utils/num';
import { num } from '../../src';

describe('isHex', () => {
test('should return true for valid hex strings', () => {
Expand Down Expand Up @@ -174,3 +174,59 @@ describe('isBigNumberish', () => {
expect(num.isBigNumberish('zero')).toBe(false);
});
});

describe('toStorageKey, toHex64', () => {
test('should convert to 0x + 64 hex unrestricted', () => {
expect(() => num.toStorageKey('monorepo')).toThrow();

const key1 = num.toStorageKey('0x123');
expect(key1).toEqual('0x0000000000000000000000000000000000000000000000000000000000000123');
expect(key1.length).toEqual(66);

const key11 = num.toStorageKey(
'0x000000000000000000000000000000000000000000000000000000000000000000000000000000000123'
);
expect(key11).toEqual('0x0000000000000000000000000000000000000000000000000000000000000123');
expect(key11.length).toEqual(66);

const key2 = num.toStorageKey(123);
expect(key2).toEqual('0x000000000000000000000000000000000000000000000000000000000000007b');
expect(key2.length).toEqual(66);

const key3 = num.toStorageKey(123n);
expect(key3).toEqual('0x000000000000000000000000000000000000000000000000000000000000007b');
expect(key3.length).toEqual(66);
});

test('should convert to 0x + 64 hex restricted', () => {
expect(() => num.toHex64('monorepo')).toThrow();

const key1 = num.toHex64('0x123');
expect(key1).toEqual('0x0000000000000000000000000000000000000000000000000000000000000123');
expect(key1.length).toEqual(66);

const key11 = num.toHex64(
'0x000000000000000000000000000000000000000000000000000000000000000000000000000000000123'
);
expect(key11).toEqual('0x0000000000000000000000000000000000000000000000000000000000000123');
expect(key11.length).toEqual(66);

expect(() =>
num.toHex64(
'0x123000000000000000000000000000000000000000000000000000000000000000000000000000000123'
)
).toThrow(TypeError);

const key2 = num.toHex64(123);
expect(key2).toEqual('0x000000000000000000000000000000000000000000000000000000000000007b');
expect(key2.length).toEqual(66);

const key3 = num.toHex64(123n);
expect(key3).toEqual('0x000000000000000000000000000000000000000000000000000000000000007b');
expect(key3.length).toEqual(66);

const key4 = num.toHex64('0x82bdafb0c4a2b03cd0f16ddcc3339da37f2cbb1aecb2a419764e35b7c3a8ec29');
expect(key4).toEqual('0x82bdafb0c4a2b03cd0f16ddcc3339da37f2cbb1aecb2a419764e35b7c3a8ec29');
expect(key4.length).toEqual(66);
});
});
Loading