git.delta.rocks / unique-network / refs/commits / 651b8ff28764

difftreelog

source

tests/src/util/identitySetter.ts3.7 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// SPDX-License-Identifier: Apache-2.034import {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}2223// This is a utility for pulling24const forceInsertIdentities = async (): Promise<void> => {25  const identitiesOnRelay: any[] = [];26  const identitiesToRemove: string[] = [];27  await usingPlaygrounds(async helper => {28    try {29      // iterate over every identity30      for(const [key, v] of await helper.getApi().query.identity.identityOf.entries()) {31        const value = v as any;32        if (value.isNone) {33          // in the nigh-impossible case that storage map would actually give None for a value, might as well delete it34          identitiesToRemove.push((key as any).toHuman()[0]);35          continue;36        }3738        // if any of the judgements resulted in a good confirmed outcome, keep this identity39        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      // cross-reference every account for changes57      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          // only update if the identity info does not exist or is changed63          if (value.toString() === identity[1].toString()) {64            continue;65          }66        }67        identitiesToAdd.push([key, value]);68        // exercise caution - in case we have an identity and the realy doesn't, it might mean one of two things:69        // 1) it was deleted on the relay;70        // 2) it is our own identity, we don't want to delete it.71        // identitiesToRemove.push((key as any).toHuman()[0]);72      }7374      // await helper.getSudo().executeExtrinsic(superuser, 'api.tx.identity.forceRemoveIdentities', [identitiesToRemove]);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));