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 5835b3e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 40 deletions.
1 change: 1 addition & 0 deletions scripts/transfer_domains.ts
Original file line number Diff line number Diff line change
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
73 changes: 33 additions & 40 deletions src/utils/transfer-domains.ts
Original file line number Diff line number Diff line change
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,43 @@ 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');
const 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('.'));
}
}
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) {
console.group();
const label = nodeName.split('.')[0];
const labelHash = labelhash(label);
console.log(`${dryRun ? 'Would transfer' : 'Transferring'} ${nodeName}`);
console.log(
`${
dryRun ? 'Would transfer' : 'Transferring'
} ${nodeName} to root owner`
);
if (!dryRun) {
// transferring nodeName to root owner to be able to transfer to him subnodes of nodeName
await (
await ensRegistry.setSubnodeOwner(
domainHash,
Expand All @@ -76,12 +66,15 @@ export const transferDomain = async ({
await transfer(nodeName);
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 5835b3e

Please sign in to comment.