difftreelog
feat(identity) script to pull and set subs from relay
in: master
1 file changed
tests/src/util/identitySetter.tsdiffbeforeafterboth1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// SPDX-License-Identifier: Apache-2.02// SPDX-License-Identifier: Apache-2.03//4// Usage: `yarn setIdentities [relay WS URL] [parachain WS URL] [sudo key]`5// Example: `yarn setIdentities wss://polkadot-rpc.dwellir.com ws://localhost:9944 escape pattern miracle train sudden cart adapt embark wedding alien lamp mesh`364import {encodeAddress} from '@polkadot/keyring';7import {encodeAddress} from '@polkadot/keyring';5import {usingPlaygrounds, Pallets} from './index';8import {usingPlaygrounds, Pallets} from './index';9const paraUrl = process.argv[3] ?? 'ws://localhost:9944';12const paraUrl = process.argv[3] ?? 'ws://localhost:9944';10const key = process.argv.length > 4 ? process.argv.slice(4).join(' ') : '//Alice';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}111812function extractIdentity(key: any, value: any): [string, any] {19function extractIdentity(key: any, value: any): [string, any] {13 return [(key as any).toHuman()[0], (value as any).unwrap()];20 return [extractAccountId(key), (value as any).unwrap()];14}21}152216async function getIdentities(helper: ChainHelperBase) {23async function getIdentities(helper: ChainHelperBase, noneCasePredicate?: (key: any, value: any) => void) {17 const identities: [string, any][] = [];24 const identities: [string, any][] = [];18 for(const [key, value] of await helper.getApi().query.identity.identityOf.entries())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 }19 identities.push(extractIdentity(key, value));31 identities.push(extractIdentity(key, value));32 }20 return identities;33 return identities;21}34}223523// This is a utility for pulling36function 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 // supers.find((x: any) => x[0] === subIdentities[0])![1].toHuman();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}5960// The utility for pulling identity and sub-identity data24const forceInsertIdentities = async (): Promise<void> => {61const forceInsertIdentities = async (): Promise<void> => {25 const identitiesOnRelay: any[] = [];62 const identitiesOnRelay: any[] = [];63 const subsOnRelay: any[] = [];26 const identitiesToRemove: string[] = [];64 const identitiesToRemove: string[] = [];27 await usingPlaygrounds(async helper => {65 await usingPlaygrounds(async helper => {28 try {66 try {29 // iterate over every identity67 // iterate over every identity30 for(const [key, v] of await helper.getApi().query.identity.identityOf.entries()) {68 for(const [key, value] of await getIdentities(helper, (key, _value) => identitiesToRemove.push((key as any).toHuman()[0]))) {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 identity69 // 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;70 if (value.toHuman().judgements.filter((x: any) => x[1] == 'Reasonable' || x[1] == 'KnownGood').length == 0) continue;40 identitiesOnRelay.push(extractIdentity(key, value));71 identitiesOnRelay.push([key, value]);41 }72 }7374 const sublessIdentities = [...identitiesOnRelay];75 const supersOfSubs = await getSupers(helper);7677 // iterate over every sub-identity78 for(const [key, value] of await getSubs(helper)) {79 // only get subs of the identities interesting to us80 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 // mark the rest of sub-identities for deletion with empty arrays87 /*for(const [account, _identity] of sublessIdentities) {88 subsOnRelay.push([account, ['0', []]]);89 }*/42 } catch (error) {90 } catch (error) {43 console.error(error);91 console.error(error);44 throw Error('Error during fetching identities');92 throw Error('Error during fetching identities');60 const identity = paraIdentities.find(i => i[0] === encodedKey);108 const identity = paraIdentities.find(i => i[0] === encodedKey);61 if (identity) {109 if (identity) {62 // only update if the identity info does not exist or is changed110 // only update if the identity info does not exist or is changed63 if (value.toString() === identity[1].toString()) {111 if (JSON.stringify(value) === JSON.stringify(identity[1])) {64 continue;112 continue;65 }113 }66 }114 }71 // identitiesToRemove.push((key as any).toHuman()[0]);119 // identitiesToRemove.push((key as any).toHuman()[0]);72 }120 }7312174 // await helper.getSudo().executeExtrinsic(superuser, 'api.tx.identity.forceRemoveIdentities', [identitiesToRemove]);122 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)75 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.identity.forceInsertIdentities', [identitiesToAdd]);129 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.identity.forceInsertIdentities', [identitiesToAdd]);13076 console.log(`Tried to upload ${identitiesToAdd.length} identities `131 console.log(`Tried to upload ${identitiesToAdd.length} identities`77 + `and found ${identitiesToRemove.length} identities for potential removal. `132 + ` and found ${identitiesToRemove.length} identities for potential removal.`78 + `Now there are ${(await helper.getApi().query.identity.identityOf.keys()).length}.`);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 // only update if the sub-identity info does not exist or is changed140 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.`);79 } catch (error) {152 } catch (error) {80 console.error(error);153 console.error(error);81 throw Error('Error during setting identities');154 throw Error('Error during setting identities');