From af7fc3f401579f3daae8a7183965952804e496e7 Mon Sep 17 00:00:00 2001 From: marshacb Date: Mon, 6 Nov 2023 09:38:34 -0500 Subject: [PATCH] fix: add check for ethereum accounts to ParaToPara and ParaToSystem directions --- src/createXcmTypes/ParaToPara.spec.ts | 37 +++++++++++++++++++++++++ src/createXcmTypes/ParaToPara.ts | 11 ++++++-- src/createXcmTypes/ParaToSystem.spec.ts | 37 +++++++++++++++++++++++++ src/createXcmTypes/ParaToSystem.ts | 11 ++++++-- 4 files changed, 92 insertions(+), 4 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, }, }, }; diff --git a/src/createXcmTypes/ParaToSystem.spec.ts b/src/createXcmTypes/ParaToSystem.spec.ts index 8eead2eb..685cc81a 100644 --- a/src/createXcmTypes/ParaToSystem.spec.ts +++ b/src/createXcmTypes/ParaToSystem.spec.ts @@ -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', @@ -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); }); }); diff --git a/src/createXcmTypes/ParaToSystem.ts b/src/createXcmTypes/ParaToSystem.ts index 4fea2006..f9367504 100644 --- a/src/createXcmTypes/ParaToSystem.ts +++ b/src/createXcmTypes/ParaToSystem.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 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, }, }, };