Skip to content

Commit

Permalink
fix: add check for ethereum accounts to ParaToPara and ParaToSystem d…
Browse files Browse the repository at this point in the history
…irections
  • Loading branch information
marshacb committed Nov 6, 2023
1 parent 7d302d5 commit af7fc3f
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 4 deletions.
37 changes: 37 additions & 0 deletions src/createXcmTypes/ParaToPara.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,25 @@ describe('ParaToPara', () => {

expect(beneficiary).toStrictEqual(expectedRes);
});
it('Should work for V2 for an Ethereum Address', () => {
const beneficiary = ParaToPara.createBeneficiary('0x96Bd611EbE3Af39544104e26764F4939924F6Ece', 2);

const expectedRes = {
V2: {
parents: 0,
interior: {
X1: {
AccountKey20: {
key: '0x96Bd611EbE3Af39544104e26764F4939924F6Ece',
network: 'Any',
},
},
},
},
};

expect(beneficiary).toStrictEqual(expectedRes);
});
it('Should work for V3', () => {
const beneficiary = ParaToPara.createBeneficiary(
'0xf5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b',
Expand All @@ -48,6 +67,24 @@ describe('ParaToPara', () => {
},
};

expect(beneficiary).toStrictEqual(expectedRes);
});
it('Should work for V3 for an Ethereum Address', () => {
const beneficiary = ParaToPara.createBeneficiary('0x96Bd611EbE3Af39544104e26764F4939924F6Ece', 3);

const expectedRes = {
V3: {
parents: 0,
interior: {
X1: {
AccountKey20: {
key: '0x96Bd611EbE3Af39544104e26764F4939924F6Ece',
},
},
},
},
};

expect(beneficiary).toStrictEqual(expectedRes);
});
});
Expand Down
11 changes: 9 additions & 2 deletions src/createXcmTypes/ParaToPara.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import type { ApiPromise } from '@polkadot/api';
import type { AnyJson } from '@polkadot/types/types';
import { isEthereumAddress } from '@polkadot/util-crypto';

import { BaseError, BaseErrorsEnum } from '../errors';
import { Registry } from '../registry';
Expand Down Expand Up @@ -44,21 +45,27 @@ export const ParaToPara: ICreateXcmType = {
*/
createBeneficiary: (accountId: string, xcmVersion?: number): XcmDestBenificiary => {
if (xcmVersion == 2) {
const X1 = isEthereumAddress(accountId)
? { AccountKey20: { network: 'Any', key: accountId } }
: { AccountId32: { network: 'Any', id: accountId } };

return {
V2: {
parents: 0,
interior: {
X1: { AccountId32: { network: 'Any', id: accountId } },
X1,
},
},
};
}

const X1 = isEthereumAddress(accountId) ? { AccountKey20: { key: accountId } } : { AccountId32: { id: accountId } };

return {
V3: {
parents: 0,
interior: {
X1: { AccountId32: { id: accountId } },
X1,
},
},
};
Expand Down
37 changes: 37 additions & 0 deletions src/createXcmTypes/ParaToSystem.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,25 @@ describe('ParaToSystem', () => {

expect(beneficiary).toStrictEqual(expectedRes);
});
it('Should work for V2 for an Ethereum Address', () => {
const beneficiary = ParaToSystem.createBeneficiary('0x96Bd611EbE3Af39544104e26764F4939924F6Ece', 2);

const expectedRes = {
V2: {
parents: 0,
interior: {
X1: {
AccountKey20: {
key: '0x96Bd611EbE3Af39544104e26764F4939924F6Ece',
network: 'Any',
},
},
},
},
};

expect(beneficiary).toStrictEqual(expectedRes);
});
it('Should work for V3', () => {
const beneficiary = ParaToSystem.createBeneficiary(
'0xf5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b',
Expand All @@ -48,6 +67,24 @@ describe('ParaToSystem', () => {
},
};

expect(beneficiary).toStrictEqual(expectedRes);
});
it('Should work for V3 for an Ethereum Address', () => {
const beneficiary = ParaToSystem.createBeneficiary('0x96Bd611EbE3Af39544104e26764F4939924F6Ece', 3);

const expectedRes = {
V3: {
parents: 0,
interior: {
X1: {
AccountKey20: {
key: '0x96Bd611EbE3Af39544104e26764F4939924F6Ece',
},
},
},
},
};

expect(beneficiary).toStrictEqual(expectedRes);
});
});
Expand Down
11 changes: 9 additions & 2 deletions src/createXcmTypes/ParaToSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import type { ApiPromise } from '@polkadot/api';
import type { AnyJson } from '@polkadot/types/types';
import { isEthereumAddress } from '@polkadot/util-crypto';

import { BaseError, BaseErrorsEnum } from '../errors';
import { Registry } from '../registry';
Expand Down Expand Up @@ -44,21 +45,27 @@ export const ParaToSystem: ICreateXcmType = {
*/
createBeneficiary: (accountId: string, xcmVersion?: number): XcmDestBenificiary => {
if (xcmVersion == 2) {
const X1 = isEthereumAddress(accountId)
? { AccountKey20: { network: 'Any', key: accountId } }
: { AccountId32: { network: 'Any', id: accountId } };

return {
V2: {
parents: 0,
interior: {
X1: { AccountId32: { network: 'Any', id: accountId } },
X1,
},
},
};
}

const X1 = isEthereumAddress(accountId) ? { AccountKey20: { key: accountId } } : { AccountId32: { id: accountId } };

return {
V3: {
parents: 0,
interior: {
X1: { AccountId32: { id: accountId } },
X1,
},
},
};
Expand Down

0 comments on commit af7fc3f

Please sign in to comment.