Skip to content

Commit

Permalink
refactor: get all subdomains in one req
Browse files Browse the repository at this point in the history
  • Loading branch information
JGiter committed Jul 3, 2023
1 parent c747db3 commit 81d5bb9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 43 deletions.
1 change: 1 addition & 0 deletions scripts/transfer_domains.ts
Expand Up @@ -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) {
Expand Down
78 changes: 35 additions & 43 deletions src/utils/transfer-domains.ts
Expand Up @@ -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,
Expand All @@ -24,47 +18,42 @@ export const transferDomain = async ({
dryRun?: boolean;
}) => {
const logger = getLogger();
const { domainHierarchy, domainReader, ensRegistry } = await initDomains(
signer,
chainId
);
const transferred: Record<string, unknown>[] = [];
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<string> = [];
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<string> = [];
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}: ${(<Error>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,
Expand All @@ -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 });
};

0 comments on commit 81d5bb9

Please sign in to comment.