1234import {encodeAddress} from '@polkadot/keyring';5import {usingPlaygrounds, Pallets} from './index';6import {ChainHelperBase} from './playgrounds/unique';78const relayUrl = process.argv[2] ?? 'ws://localhost:9844';9const paraUrl = process.argv[3] ?? 'ws://localhost:9944';10const key = process.argv.length > 4 ? process.argv.slice(4).join(' ') : '//Alice';1112function extractIdentity(key: any, value: any): [string, any] {13 return [(key as any).toHuman()[0], (value as any).unwrap()];14}1516async function getIdentities(helper: ChainHelperBase) {17 const identities: [string, any][] = [];18 for(const [key, value] of await helper.getApi().query.identity.identityOf.entries())19 identities.push(extractIdentity(key, value));20 return identities;21}222324const forceInsertIdentities = async (): Promise<void> => {25 const identitiesOnRelay: any[] = [];26 const identitiesToRemove: string[] = [];27 await usingPlaygrounds(async helper => {28 try {29 30 for(const [key, v] of await helper.getApi().query.identity.identityOf.entries()) {31 const value = v as any;32 if (value.isNone) {33 34 identitiesToRemove.push((key as any).toHuman()[0]);35 continue;36 }3738 39 if (value.unwrap().toHuman().judgements.filter((x: any) => x[1] == 'Reasonable' || x[1] == 'KnownGood').length == 0) continue;40 identitiesOnRelay.push(extractIdentity(key, value));41 }42 } catch (error) {43 console.error(error);44 throw Error('Error during fetching identities');45 }46 }, relayUrl);4748 await usingPlaygrounds(async (helper, privateKey) => {49 if (helper.fetchMissingPalletNames([Pallets.Identity]).length != 0) console.error('pallet-identity is not included in parachain.');50 try {51 const superuser = await privateKey(key);52 const ss58Format = helper.chain.getChainProperties().ss58Format;53 const paraIdentities = await getIdentities(helper);54 const identitiesToAdd: any[] = [];5556 57 for (const [key, value] of identitiesOnRelay) {58 const encodedKey = encodeAddress(key, ss58Format);5960 const identity = paraIdentities.find(i => i[0] === encodedKey);61 if (identity) {62 63 if (value.toString() === identity[1].toString()) {64 continue;65 }66 }67 identitiesToAdd.push([key, value]);68 69 70 71 72 }7374 75 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.identity.forceInsertIdentities', [identitiesToAdd]);76 console.log(`Tried to upload ${identitiesToAdd.length} identities `77 + `and found ${identitiesToRemove.length} identities for potential removal. `78 + `Now there are ${(await helper.getApi().query.identity.identityOf.keys()).length}.`);79 } catch (error) {80 console.error(error);81 throw Error('Error during setting identities');82 }83 }, paraUrl);84};8586forceInsertIdentities().catch(() => process.exit(1));