From 2cb2d136019b50a78745df09892554a1f3a07342 Mon Sep 17 00:00:00 2001 From: Cameron Marshall Date: Mon, 6 Nov 2023 10:02:31 -0500 Subject: [PATCH] fix: add Ethereum address check in createBeneficiary for ParaToPara direction (#315) --- src/createXcmTypes/ParaToPara.spec.ts | 37 +++++++++++++++++++++++++++ src/createXcmTypes/ParaToPara.ts | 11 ++++++-- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/createXcmTypes/ParaToPara.spec.ts b/src/createXcmTypes/ParaToPara.spec.ts index 2d5d27fa..9c1abc40 100644 --- a/src/createXcmTypes/ParaToPara.spec.ts +++ b/src/createXcmTypes/ParaToPara.spec.ts @@ -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', @@ -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); }); }); diff --git a/src/createXcmTypes/ParaToPara.ts b/src/createXcmTypes/ParaToPara.ts index d87e16a8..68638a39 100644 --- a/src/createXcmTypes/ParaToPara.ts +++ b/src/createXcmTypes/ParaToPara.ts @@ -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'; @@ -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, }, }, };