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(internal): ensure all sanitizeKeys int values are sanitized to str #346

Merged
merged 2 commits into from
Jan 17, 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
14 changes: 7 additions & 7 deletions src/createXcmTypes/ParaToPara.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ describe('ParaToPara', () => {
{
id: {
Concrete: {
Parents: 1,
Parents: '1',
Interior: {
Here: null,
},
Expand All @@ -157,9 +157,9 @@ describe('ParaToPara', () => {
{
id: {
Concrete: {
Parents: 1,
Parents: '1',
Interior: {
X3: [{ Parachain: 1000 }, { PalletInstance: 50 }, { GeneralIndex: 8 }],
X3: [{ Parachain: '1000' }, { PalletInstance: '50' }, { GeneralIndex: '8' }],
},
},
},
Expand Down Expand Up @@ -191,9 +191,9 @@ describe('ParaToPara', () => {
{
id: {
Concrete: {
Parents: 1,
Parents: '1',
Interior: {
X3: [{ Parachain: 1000 }, { PalletInstance: 50 }, { GeneralIndex: 8 }],
X3: [{ Parachain: '1000' }, { PalletInstance: '50' }, { GeneralIndex: '8' }],
},
},
},
Expand All @@ -204,9 +204,9 @@ describe('ParaToPara', () => {
{
id: {
Concrete: {
Parents: 1,
Parents: '1',
Interior: {
X3: [{ Parachain: 1000 }, { PalletInstance: 50 }, { GeneralIndex: 1984 }],
X3: [{ Parachain: '1000' }, { PalletInstance: '50' }, { GeneralIndex: '1984' }],
},
},
},
Expand Down
14 changes: 7 additions & 7 deletions src/createXcmTypes/ParaToSystem.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ describe('ParaToSystem', () => {
{
id: {
Concrete: {
Parents: 1,
Parents: '1',
Interior: {
Here: null,
},
Expand All @@ -120,9 +120,9 @@ describe('ParaToSystem', () => {
{
id: {
Concrete: {
Parents: 1,
Parents: '1',
Interior: {
X3: [{ Parachain: 1000 }, { PalletInstance: 50 }, { GeneralIndex: 8 }],
X3: [{ Parachain: '1000' }, { PalletInstance: '50' }, { GeneralIndex: '8' }],
},
},
},
Expand Down Expand Up @@ -154,9 +154,9 @@ describe('ParaToSystem', () => {
{
id: {
Concrete: {
Parents: 1,
Parents: '1',
Interior: {
X3: [{ Parachain: 1000 }, { PalletInstance: 50 }, { GeneralIndex: 8 }],
X3: [{ Parachain: '1000' }, { PalletInstance: '50' }, { GeneralIndex: '8' }],
},
},
},
Expand All @@ -167,9 +167,9 @@ describe('ParaToSystem', () => {
{
id: {
Concrete: {
Parents: 1,
Parents: '1',
Interior: {
X3: [{ Parachain: 1000 }, { PalletInstance: 50 }, { GeneralIndex: 1984 }],
X3: [{ Parachain: '1000' }, { PalletInstance: '50' }, { GeneralIndex: '1984' }],
},
},
},
Expand Down
6 changes: 3 additions & 3 deletions src/createXcmTypes/SystemToSystem.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ describe('SystemToSystem XcmVersioned Generation', () => {
{
id: {
Concrete: {
Parents: 0,
Parents: '0',
Interior: {
X2: [{ PalletInstance: '50' }, { GeneralIndex: '11' }],
},
Expand Down Expand Up @@ -132,7 +132,7 @@ describe('SystemToSystem XcmVersioned Generation', () => {
{
id: {
Concrete: {
Parents: 1,
Parents: '1',
Interior: {
Here: '',
},
Expand Down Expand Up @@ -173,7 +173,7 @@ describe('SystemToSystem XcmVersioned Generation', () => {
{
id: {
Concrete: {
Parents: 0,
Parents: '0',
Interior: {
X2: [{ PalletInstance: '55' }, { GeneralIndex: '11' }],
},
Expand Down
2 changes: 1 addition & 1 deletion src/util/resolveMultiLocation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('resolveMultiLocation', () => {
});
it('Should correctly return a resolved multilocation object given a correct value', () => {
const str = `{"parents":1,"interior":{"x2":[{"parachain":2001},{"generalKey":"0x0001"}]}}`;
const exp = { Parents: 1, Interior: { X2: [{ Parachain: 2001 }, { GeneralKey: '0x0001' }] } };
const exp = { Parents: '1', Interior: { X2: [{ Parachain: '2001' }, { GeneralKey: '0x0001' }] } };

expect(resolveMultiLocation(str, 2)).toStrictEqual(exp);
});
Expand Down
9 changes: 9 additions & 0 deletions src/util/sanitizeKeys.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,13 @@ describe('sanitizeKeys', () => {
const exp = { OnlyChild: { PalletInstance: '' }, GlobalConsensus: { Key1: '' } };
expect(sanitizeKeys(obj)).toStrictEqual(exp);
});
it('Should correctly sanitize values that are integers', () => {
const obj = { OnlyChild: { PalletInstance: 10 }, GlobalConsensus: { Key1: '' } };
const exp = { OnlyChild: { PalletInstance: '10' }, GlobalConsensus: { Key1: '' } };
expect(sanitizeKeys(obj)).toStrictEqual(exp);

const obj2 = { key1: { key2: { key3: [{ key6: 111, key7: 222 }, { key8: null }], key4: 333 } }, key5: 444 };
const exp2 = { Key1: { Key2: { Key3: [{ Key6: '111', Key7: '222' }, { Key8: null }], Key4: '333' } }, Key5: '444' };
expect(sanitizeKeys(obj2)).toStrictEqual(exp2);
});
});
7 changes: 5 additions & 2 deletions src/util/sanitizeKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ const isPlainObject = (input: unknown) => {
};

/**
* Set all keys in an object with the first key being capitalized
* Set all keys in an object with the first key being capitalized.
* When a keys value is an integer it will convert that integer into a string.
*
* @param xcmObj
*/
Expand All @@ -30,7 +31,9 @@ export const sanitizeKeys = <T extends AnyObj>(xcmObj: T): T => {
if (Array.isArray(value)) {
final[mapKey(key)] = value.map(sanitizeKeys);
} else if (!isPlainObject(value)) {
final[mapKey(key)] = value;
// Ensure when the value is an integer that it is sanitized to a string.
const sanitizedValue = Number.isInteger(value) ? Number(value).toString() : value;
final[mapKey(key)] = sanitizedValue;
} else {
final[mapKey(key)] = sanitizeKeys(value as AnyObj);
}
Expand Down
5 changes: 5 additions & 0 deletions src/validate/validateNumber.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { MAX_NUM_LENGTH } from '../consts';

/**
* Check if a given string input is a valid integer.
*
* @param val
*/
export const validateNumber = (val: string): boolean => {
if (val.length < MAX_NUM_LENGTH) {
const isNum = Number.parseInt(val);
Expand Down