From 378c38c729064ec1d1d2ba6edc49b6a959cdef11 Mon Sep 17 00:00:00 2001 From: Fahrrader Date: Fri, 23 Sep 2022 08:36:14 +0000 Subject: [PATCH] tests(util): altrefactorbstrate normalization --- --- a/tests/src/util/playgrounds/unique.ts +++ b/tests/src/util/playgrounds/unique.ts @@ -596,11 +596,7 @@ const admins = (await this.helper.callRpc('api.rpc.unique.adminlist', [collectionId])).toHuman(); return normalize - ? admins.map((address: any) => { - return address.Substrate - ? {Substrate: this.helper.address.normalizeSubstrate(address.Substrate)} - : address; - }) + ? admins.map((address: any) => this.helper.address.normalizeCrossAccountIfSubstrate(address)) : admins; } @@ -614,11 +610,7 @@ async getAllowList(collectionId: number, normalize = false): Promise { const allowListed = (await this.helper.callRpc('api.rpc.unique.allowlist', [collectionId])).toHuman(); return normalize - ? allowListed.map((address: any) => { - return address.Substrate - ? {Substrate: this.helper.address.normalizeSubstrate(address.Substrate)} - : address; - }) + ? allowListed.map((address: any) => this.helper.address.normalizeCrossAccountIfSubstrate(address)) : allowListed; } @@ -1122,7 +1114,7 @@ if (tokenData === null || tokenData.owner === null) return null; const owner = {} as any; for (const key of Object.keys(tokenData.owner)) { - owner[key.toLocaleLowerCase()] = key.toLocaleLowerCase() === 'substrate' ? this.helper.address.normalizeSubstrate(tokenData.owner[key]) : tokenData.owner[key]; + owner[key.toLocaleLowerCase()] = this.helper.address.normalizeCrossAccountIfSubstrate(tokenData.owner[key]); } tokenData.normalizedOwner = crossAccountIdFromLower(owner); return tokenData; @@ -1344,10 +1336,8 @@ } if (owner === null) return null; - - owner = owner.toHuman(); - return owner.Substrate ? {Substrate: this.helper.address.normalizeSubstrate(owner.Substrate)} : owner; + return owner.toHuman(); } /** @@ -2072,6 +2062,19 @@ } /** + * Normalizes the address of an account ONLY if it's Substrate to the specified ss58 format, by default ```42```. + * @param account account of either Substrate type or Ethereum, but only Substrate will be changed + * @param ss58Format format for address conversion, by default ```42``` + * @example normalizeCrossAccountIfSubstrate({Substrate: "unjKJQJrRd238pkUZZvzDQrfKuM39zBSnQ5zjAGAGcdRhaJTx"}) // returns 5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY + * @returns untouched ethereum account or substrate account converted to normalized (i.e., starting with 5) or specified explicitly representation + */ + normalizeCrossAccountIfSubstrate(account: ICrossAccountId, ss58Format = 42): ICrossAccountId { + return account.Substrate + ? {Substrate: this.normalizeSubstrate(account.Substrate, ss58Format)} + : account; + } + + /** * Get address in the connected chain format * @param address substrate address * @example normalizeSubstrateToChainFormat("5GrwvaEF5zXb26Fz...") // returns unjKJQJrRd238pkUZZ... for Unique Network -- gitstuff