From 81d5bb9313837c13dda2114feccdcdff3c538880 Mon Sep 17 00:00:00 2001 From: JGiter Date: Fri, 30 Jun 2023 16:33:01 +0300 Subject: [PATCH] refactor: get all subdomains in one req --- scripts/transfer_domains.ts | 1 + src/utils/transfer-domains.ts | 78 ++++++++++++++++------------------- 2 files changed, 36 insertions(+), 43 deletions(-) diff --git a/scripts/transfer_domains.ts b/scripts/transfer_domains.ts index 4beb5c89..26bf936f 100644 --- a/scripts/transfer_domains.ts +++ b/scripts/transfer_domains.ts @@ -6,6 +6,7 @@ export const domains = ['energyweb.iam.ewc']; export const newOwner = ''; export const ownerPrivKey = Wallet.createRandom().privateKey; +// Fast rpc node is required to use this script (async function () { try { for await (const rootDomain of domains) { diff --git a/src/utils/transfer-domains.ts b/src/utils/transfer-domains.ts index 832c16ec..acd16469 100644 --- a/src/utils/transfer-domains.ts +++ b/src/utils/transfer-domains.ts @@ -2,14 +2,8 @@ import { ChainId } from '..'; import { Wallet } from 'ethers'; import { namehash, labelhash } from './ens-hash'; import { getLogger } from '../config/logger.config'; -import { DomainReader } from '@energyweb/credential-governance'; import { initDomains } from './init-domains'; -/** - * @description - Checks that role issuers of all roles under `rootDomain` contains method-specific-id and adds it if missing - * `signer` must own `rootDomain` on `targetChain` - */ - export const transferDomain = async ({ rootDomain, signer, @@ -24,47 +18,42 @@ export const transferDomain = async ({ dryRun?: boolean; }) => { const logger = getLogger(); - const { domainHierarchy, domainReader, ensRegistry } = await initDomains( - signer, - chainId - ); - const transferred: Record[] = []; + const { domainHierarchy, ensRegistry } = await initDomains(signer, chainId); + console.time('getSubdomains'); + let domains = await domainHierarchy.getSubdomainsUsingResolver({ + domain: rootDomain, + mode: 'ALL', + }); + console.timeEnd('getSubdomains'); + // domains which does not have definitions, like those which starts from `apps`, `orgs` and `roles` + const metadomains: Array = []; + for (let d of domains) { + while (![...domains, ...metadomains].includes(d)) { + metadomains.push(d); + d = d.slice(0, d.lastIndexOf('.')); + } + } + domains = [...domains, ...metadomains]; + console.dir([...domains].sort(), { depth: Infinity, colors: true }); + + const transferred: Array = []; const transfer = async (domain: string) => { const domainHash = namehash(domain); - let def; - try { - def = await domainReader.read({ node: domainHash }); - } catch (e) { - // 'apps' and 'roles' - logger.warn(`Unable to read ${domain}: ${(e).message}`); - } - - const subnodes = await domainHierarchy.getSubdomainsUsingResolver({ - domain, - mode: 'FIRSTLEVEL', - }); - if (def) { - if ( - DomainReader.isOrgDefinition(def) || - DomainReader.isAppDefinition(def) - ) { - subnodes.push(`roles.${domain}`); - } - if (DomainReader.isOrgDefinition(def)) { - subnodes.push(`apps.${domain}`); - } - if (domain === 'engie.auth.ewc') { - subnodes.push(`orgs.${domain}`); - } - } + const level = domain.split('.').length; + const subnodes = domains + .filter((d) => d.startsWith(domain)) + .filter((d) => d.split('.').length === level + 1); logger.info(`Subnodes of ${domain} are ${subnodes ?? 'not set'}`); - for await (const nodeName of subnodes) { + for await (const node of subnodes) { console.group(); - const label = nodeName.split('.')[0]; + const label = node.split('.')[0]; const labelHash = labelhash(label); - console.log(`${dryRun ? 'Would transfer' : 'Transferring'} ${nodeName}`); + console.log( + `${dryRun ? 'Would transfer' : 'Transferring'} ${node} to root owner` + ); if (!dryRun) { + // transferring node to root owner to be able to transfer to him subnodes of node. This transfer is needed, because not all nodes in tree can belong to tree owner await ( await ensRegistry.setSubnodeOwner( domainHash, @@ -73,15 +62,18 @@ export const transferDomain = async ({ ) ).wait(); } - await transfer(nodeName); + await transfer(node); console.groupEnd(); } - console.log(`${dryRun ? 'Would transfer' : 'Transferring'} ${domain}`); + console.log( + `${dryRun ? 'Would transfer' : 'Transferring'} ${domain} to new owner` + ); if (!dryRun) { await (await ensRegistry.setOwner(domainHash, newOwner)).wait(); + transferred.push(domain); } - return transferred; }; await transfer(rootDomain); + console.dir(transferred.sort(), { depth: Infinity, colors: true }); };