1234567import {encodeAddress} from '@polkadot/keyring';8import {usingPlaygrounds, Pallets} from './index';9import {ChainHelperBase} from './playgrounds/unique';1011const relayUrl = process.argv[2] ?? 'ws://localhost:9844';12const paraUrl = process.argv[3] ?? 'ws://localhost:9944';13const key = process.argv.length > 4 ? process.argv.slice(4).join(' ') : '//Alice';1415function extractAccountId(key: any): string {16 return (key as any).toHuman()[0];17}1819function extractIdentity(key: any, value: any): [string, any] {20 return [extractAccountId(key), (value as any).unwrap()];21}2223async function getIdentities(helper: ChainHelperBase, noneCasePredicate?: (key: any, value: any) => void) {24 const identities: [string, any][] = [];25 for(const [key, v] of await helper.getApi().query.identity.identityOf.entries()) {26 const value = v as any;27 if (value.isNone) {28 if (noneCasePredicate) noneCasePredicate(key, value);29 continue;30 }31 identities.push(extractIdentity(key, value));32 }33 return identities;34}3536function constructSubInfo(identityAccount: string, subQuery: any, supers: any[], ss58?: number): [string, any] {37 const deposit = subQuery.toJSON()[0];38 const subIdentities = subQuery.toHuman()[1];39 subIdentities.map((sub: string) => supers.find((sup: any) => sup[0] === sub));40 41 return [42 encodeAddress(identityAccount, ss58), [43 deposit,44 subIdentities.map((sub: string) => [45 encodeAddress(sub, ss58),46 supers.find((sup: any) => sup[0] === sub)![1].toJSON()[1],47 ]),48 ],49 ];50}5152async function getSubs(helper: ChainHelperBase) {53 return (await helper.getApi().query.identity.subsOf.entries()).map(([key, value]) => [extractAccountId(key), value as any]);54}5556async function getSupers(helper: ChainHelperBase) {57 return (await helper.getApi().query.identity.superOf.entries()).map(([key, value]) => [extractAccountId(key), value as any]);58}596061const forceInsertIdentities = async (): Promise<void> => {62 const identitiesOnRelay: any[] = [];63 const subsOnRelay: any[] = [];64 const identitiesToRemove: string[] = [];65 await usingPlaygrounds(async helper => {66 try {67 68 for(const [key, value] of await getIdentities(helper, (key, _value) => identitiesToRemove.push((key as any).toHuman()[0]))) {69 70 if (value.toHuman().judgements.filter((x: any) => x[1] == 'Reasonable' || x[1] == 'KnownGood').length == 0) continue;71 identitiesOnRelay.push([key, value]);72 }7374 const sublessIdentities = [...identitiesOnRelay];75 const supersOfSubs = await getSupers(helper);7677 78 for(const [key, value] of await getSubs(helper)) {79 80 const identityIndex = sublessIdentities.findIndex((x: any) => x[0] == key);81 if (identityIndex == -1) continue;82 sublessIdentities.splice(identityIndex, 1);83 subsOnRelay.push(constructSubInfo(key, value, supersOfSubs));84 }8586 87 888990 } catch (error) {91 console.error(error);92 throw Error('Error during fetching identities');93 }94 }, relayUrl);9596 await usingPlaygrounds(async (helper, privateKey) => {97 if (helper.fetchMissingPalletNames([Pallets.Identity]).length != 0) console.error('pallet-identity is not included in parachain.');98 try {99 const superuser = await privateKey(key);100 const ss58Format = helper.chain.getChainProperties().ss58Format;101 const paraIdentities = await getIdentities(helper);102 const identitiesToAdd: any[] = [];103104 105 for (const [key, value] of identitiesOnRelay) {106 const encodedKey = encodeAddress(key, ss58Format);107108 const identity = paraIdentities.find(i => i[0] === encodedKey);109 if (identity) {110 111 if (JSON.stringify(value) === JSON.stringify(identity[1])) {112 continue;113 }114 }115 identitiesToAdd.push([key, value]);116 117 118 119 120 }121122 const paraSubs = await getSubs(helper);123 const supersOfSubs = await getSupers(helper);124 const subsToUpdate: any[] = [];125126 if (identitiesToRemove.length != 0)127 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.identity.forceRemoveIdentities', [identitiesToRemove]);128 if (identitiesToAdd.length != 0)129 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.identity.forceInsertIdentities', [identitiesToAdd]);130131 console.log(`Tried to upload ${identitiesToAdd.length} identities`132 + ` and found ${identitiesToRemove.length} identities for potential removal.`133 + ` Now there are ${(await helper.getApi().query.identity.identityOf.keys()).length}.`);134135 for (const [key, value] of subsOnRelay) {136 const encodedKey = encodeAddress(key, ss58Format);137 const sub = paraSubs.find(i => i[0] === encodedKey);138 if (sub) {139 140 if (JSON.stringify(value) === JSON.stringify(constructSubInfo(sub[0], sub[1], supersOfSubs)[1])) {141 continue;142 }143 }144 subsToUpdate.push([key, value]);145 }146147 if (subsToUpdate.length != 0)148 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.identity.forceSetSubs', [subsToUpdate]);149150 console.log(`Also tried to update ${subsToUpdate.length} identities with their sub-identities.`151 + ` Now there are ${(await helper.getApi().query.identity.subsOf.keys()).length} identities with subs.`);152 } catch (error) {153 console.error(error);154 throw Error('Error during setting identities');155 }156 }, paraUrl);157};158159forceInsertIdentities().catch(() => process.exit(1));