Skip to content

Commit

Permalink
fix: add Ethereum address check in createBeneficiary for ParaToPara d…
Browse files Browse the repository at this point in the history
…irection (#315)
  • Loading branch information
marshacb authored Nov 6, 2023
1 parent 6fbd6c4 commit 2cb2d13
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 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

0 comments on commit 2cb2d13

Please sign in to comment.