From af7fc3f401579f3daae8a7183965952804e496e7 Mon Sep 17 00:00:00 2001 From: marshacb Date: Mon, 6 Nov 2023 09:38:34 -0500 Subject: [PATCH 1/3] 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, }, }, }; From 9cbc4983b2ba159d5d19cb524501629c9445be95 Mon Sep 17 00:00:00 2001 From: marshacb Date: Mon, 6 Nov 2023 09:52:10 -0500 Subject: [PATCH 2/3] remove ethereum address check for paratosystem --- src/createXcmTypes/ParaToSystem.spec.ts | 37 ---------------------- src/createXcmTypes/ParaToSystem.ts | 42 ++++++++++++------------- 2 files changed, 20 insertions(+), 59 deletions(-) diff --git a/src/createXcmTypes/ParaToSystem.spec.ts b/src/createXcmTypes/ParaToSystem.spec.ts index 685cc81a..8eead2eb 100644 --- a/src/createXcmTypes/ParaToSystem.spec.ts +++ b/src/createXcmTypes/ParaToSystem.spec.ts @@ -29,25 +29,6 @@ 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', @@ -67,24 +48,6 @@ 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 f9367504..e5d7e915 100644 --- a/src/createXcmTypes/ParaToSystem.ts +++ b/src/createXcmTypes/ParaToSystem.ts @@ -1,8 +1,9 @@ // Copyright 2023 Parity Technologies (UK) Ltd. import type { ApiPromise } from '@polkadot/api'; +import type { u32 } from '@polkadot/types'; +import type { WeightLimitV2 } from '@polkadot/types/interfaces'; import type { AnyJson } from '@polkadot/types/types'; -import { isEthereumAddress } from '@polkadot/util-crypto'; import { BaseError, BaseErrorsEnum } from '../errors'; import { Registry } from '../registry'; @@ -19,6 +20,7 @@ import type { FungibleObjMultiAsset, FungibleStrMultiAsset, ICreateXcmType, + IWeightLimit, UnionXcAssetsMultiAsset, UnionXcAssetsMultiAssets, UnionXcAssetsMultiLocation, @@ -26,7 +28,6 @@ import type { XcmDestBenificiary, XcmDestBenificiaryXcAssets, XcmV3MultiLocation, - XcmWeight, } from './types'; import { constructForeignAssetMultiLocationFromAssetId } from './util/constructForeignAssetMultiLocationFromAssetId'; import { dedupeMultiAssets } from './util/dedupeMultiAssets'; @@ -45,27 +46,21 @@ 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, + X1: { AccountId32: { network: 'Any', id: accountId } }, }, }, }; } - const X1 = isEthereumAddress(accountId) ? { AccountKey20: { key: accountId } } : { AccountId32: { id: accountId } }; - return { V3: { parents: 0, interior: { - X1, + X1: { AccountId32: { id: accountId } }, }, }, }; @@ -147,15 +142,18 @@ export const ParaToSystem: ICreateXcmType = { * @param refTime amount of computation time * @param proofSize amount of storage to be used */ - createWeightLimit: (opts: CreateWeightLimitOpts): XcmWeight => { - return opts.isLimited && opts.weightLimit?.refTime && opts.weightLimit?.proofSize - ? { - Limited: { - refTime: opts.weightLimit.refTime, - proofSize: opts.weightLimit.proofSize, - }, - } - : { Unlimited: null }; + createWeightLimit: (api: ApiPromise, opts: CreateWeightLimitOpts): WeightLimitV2 => { + const limit: IWeightLimit = + opts.isLimited && opts.weightLimit?.refTime && opts.weightLimit?.proofSize + ? { + Limited: { + refTime: opts.weightLimit.refTime, + proofSize: opts.weightLimit.proofSize, + }, + } + : { Unlimited: null }; + + return api.registry.createType('XcmV3WeightLimit', limit); }, /** * returns the correct feeAssetItem based on XCM direction. @@ -168,7 +166,7 @@ export const ParaToSystem: ICreateXcmType = { * @xcmVersion number * */ - createFeeAssetItem: async (api: ApiPromise, opts: CreateFeeAssetItemOpts): Promise => { + createFeeAssetItem: async (api: ApiPromise, opts: CreateFeeAssetItemOpts): Promise => { const { registry, paysWithFeeDest, specName, assetIds, amounts, xcmVersion } = opts; if (xcmVersion && xcmVersion === 3 && specName && amounts && assetIds && paysWithFeeDest) { const multiAssets = await createParaToSystemMultiAssets( @@ -191,10 +189,10 @@ export const ParaToSystem: ICreateXcmType = { opts.isForeignAssetsTransfer ); - return assetIndex; + return api.registry.createType('u32', assetIndex); } - return 0; + return api.registry.createType('u32', 0); }, createXTokensBeneficiary: ( destChainId: string, From 57f3f8f63a8be8a0e6d1a3e3a807c271a23d0ec3 Mon Sep 17 00:00:00 2001 From: marshacb Date: Mon, 6 Nov 2023 09:56:47 -0500 Subject: [PATCH 3/3] remove paratosystem ethereum address checks --- src/createXcmTypes/ParaToSystem.ts | 31 +++++++++++++----------------- 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/src/createXcmTypes/ParaToSystem.ts b/src/createXcmTypes/ParaToSystem.ts index e5d7e915..4fea2006 100644 --- a/src/createXcmTypes/ParaToSystem.ts +++ b/src/createXcmTypes/ParaToSystem.ts @@ -1,8 +1,6 @@ // Copyright 2023 Parity Technologies (UK) Ltd. import type { ApiPromise } from '@polkadot/api'; -import type { u32 } from '@polkadot/types'; -import type { WeightLimitV2 } from '@polkadot/types/interfaces'; import type { AnyJson } from '@polkadot/types/types'; import { BaseError, BaseErrorsEnum } from '../errors'; @@ -20,7 +18,6 @@ import type { FungibleObjMultiAsset, FungibleStrMultiAsset, ICreateXcmType, - IWeightLimit, UnionXcAssetsMultiAsset, UnionXcAssetsMultiAssets, UnionXcAssetsMultiLocation, @@ -28,6 +25,7 @@ import type { XcmDestBenificiary, XcmDestBenificiaryXcAssets, XcmV3MultiLocation, + XcmWeight, } from './types'; import { constructForeignAssetMultiLocationFromAssetId } from './util/constructForeignAssetMultiLocationFromAssetId'; import { dedupeMultiAssets } from './util/dedupeMultiAssets'; @@ -142,18 +140,15 @@ export const ParaToSystem: ICreateXcmType = { * @param refTime amount of computation time * @param proofSize amount of storage to be used */ - createWeightLimit: (api: ApiPromise, opts: CreateWeightLimitOpts): WeightLimitV2 => { - const limit: IWeightLimit = - opts.isLimited && opts.weightLimit?.refTime && opts.weightLimit?.proofSize - ? { - Limited: { - refTime: opts.weightLimit.refTime, - proofSize: opts.weightLimit.proofSize, - }, - } - : { Unlimited: null }; - - return api.registry.createType('XcmV3WeightLimit', limit); + createWeightLimit: (opts: CreateWeightLimitOpts): XcmWeight => { + return opts.isLimited && opts.weightLimit?.refTime && opts.weightLimit?.proofSize + ? { + Limited: { + refTime: opts.weightLimit.refTime, + proofSize: opts.weightLimit.proofSize, + }, + } + : { Unlimited: null }; }, /** * returns the correct feeAssetItem based on XCM direction. @@ -166,7 +161,7 @@ export const ParaToSystem: ICreateXcmType = { * @xcmVersion number * */ - createFeeAssetItem: async (api: ApiPromise, opts: CreateFeeAssetItemOpts): Promise => { + createFeeAssetItem: async (api: ApiPromise, opts: CreateFeeAssetItemOpts): Promise => { const { registry, paysWithFeeDest, specName, assetIds, amounts, xcmVersion } = opts; if (xcmVersion && xcmVersion === 3 && specName && amounts && assetIds && paysWithFeeDest) { const multiAssets = await createParaToSystemMultiAssets( @@ -189,10 +184,10 @@ export const ParaToSystem: ICreateXcmType = { opts.isForeignAssetsTransfer ); - return api.registry.createType('u32', assetIndex); + return assetIndex; } - return api.registry.createType('u32', 0); + return 0; }, createXTokensBeneficiary: ( destChainId: string,