git.delta.rocks / unique-network / refs/commits / e7f049d5aecf

difftreelog

util(identity-setter): compose pulled identities into preimages

Fahrrader2023-02-21parent: #1fd2934.patch.diff
in: master

1 file changed

modifiedtests/src/util/identitySetter.tsdiffbeforeafterboth
1// 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.0
3//3//
4// Pulls identities and sub-identities from a chain and then uses sudo to force upload them into another.4// Pulls identities and sub-identities from a chain and then makes a preimage to later force upload them into another.
5// Only changed or previously non-existent data are inserted.5// Only changed or previously non-existent data are inserted.
6//6//
7// Usage: `yarn setIdentities [relay WS URL] [parachain WS URL] [sudo key]`7// Usage: `yarn setIdentities [relay WS URL] [parachain WS URL] [user key]
8// Example: `yarn setIdentities wss://polkadot-rpc.dwellir.com ws://localhost:9944 escape pattern miracle train sudden cart adapt embark wedding alien lamp mesh`8// Example: `yarn setIdentities wss://polkadot-rpc.dwellir.com ws://localhost:9944 escape pattern miracle train sudden cart adapt embark wedding alien lamp mesh`
99
10import {encodeAddress} from '@polkadot/keyring';10import {encodeAddress} from '@polkadot/keyring';
11import {IKeyringPair} from '@polkadot/types/types';
11import {usingPlaygrounds, Pallets} from './index';12import {usingPlaygrounds, Pallets} from './index';
12import {ChainHelperBase} from './playgrounds/unique';13import {ChainHelperBase} from './playgrounds/unique';
1314
81 return (await helper.getApi().query.identity.superOf.entries()).map(([key, value]) => [extractAccountId(key), value as any]);82 return (await helper.getApi().query.identity.superOf.entries()).map(([key, value]) => [extractAccountId(key), value as any]);
82}83}
84
85async function uploadPreimage(helper: ChainHelperBase, preimageMaker: IKeyringPair, preimage: string) {
86 try {
87 await helper.executeExtrinsic(preimageMaker, 'api.tx.preimage.notePreimage', [preimage]);
88 } catch(e: any) {
89 if (e.message.includes('AlreadyNoted')) {
90 console.warn('Warning: The same preimage already exists on the chain. Nothing was uploaded.');
91 } else {
92 console.error(e);
93 }
94 }
95}
8396
84// The utility for pulling identity and sub-identity data97// The utility for pulling identity and sub-identity data
85const forceInsertIdentities = async (): Promise<void> => {98const forceInsertIdentities = async (): Promise<void> => {
128141
129 await usingPlaygrounds(async (helper, privateKey) => {142 await usingPlaygrounds(async (helper, privateKey) => {
130 if (helper.fetchMissingPalletNames([Pallets.Identity]).length != 0) console.error('pallet-identity is not included in parachain.');143 if (helper.fetchMissingPalletNames([Pallets.Identity]).length != 0) console.error('pallet-identity is not included in parachain.');
144 if (helper.fetchMissingPalletNames([Pallets.Preimage]).length != 0) console.error('pallet-preimage is not included in parachain.');
145
131 try {146 try {
132 const superuser = await privateKey(key);147 const preimageMaker = await privateKey(key);
133 const ss58Format = helper.chain.getChainProperties().ss58Format;148 const ss58Format = helper.chain.getChainProperties().ss58Format;
134 const paraIdentities = await getIdentities(helper);149 const paraIdentities = await getIdentities(helper);
135 const identitiesToAdd: any[] = [];150 const identitiesToAdd: any[] = [];
157 // identitiesToRemove.push((key as any).toHuman()[0]);172 // identitiesToRemove.push((key as any).toHuman()[0]);
158 }173 }
174
175 console.log(identitiesToAdd[0][1]);
159176
160 if (identitiesToRemove.length != 0)177 if (identitiesToRemove.length != 0)
161 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.identity.forceRemoveIdentities', [identitiesToRemove]);178 await uploadPreimage(
179 helper,
180 preimageMaker,
181 helper.constructApiCall('api.tx.identity.forceRemoveIdentities', [identitiesToRemove]).method.toHex(),
182 );
162 if (identitiesToAdd.length != 0)183 if (identitiesToAdd.length != 0)
163 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.identity.forceInsertIdentities', [identitiesToAdd]);184 await uploadPreimage(
185 helper,
186 preimageMaker,
187 helper.constructApiCall('api.tx.identity.forceInsertIdentities', [identitiesToAdd]).method.toHex(),
188 );
164189
165 console.log(`Tried to upload ${identitiesToAdd.length} identities`190 console.log(`Tried to push ${identitiesToAdd.length} identities`
166 + ` and found ${identitiesToRemove.length} identities for potential removal.`191 + ` and found ${identitiesToRemove.length} identities for potential removal.`
167 + ` Now there are ${(await helper.getApi().query.identity.identityOf.keys()).length}.`);192 + ` Currently there are ${(await helper.getApi().query.identity.identityOf.keys()).length} identities on the chain.`);
168193
169 // fill sub-identities194 // fill sub-identities
170 const paraSubs = await getSubs(helper);195 const paraSubs = await getSubs(helper);
187 }212 }
188213
189 if (subsToUpdate.length != 0)214 if (subsToUpdate.length != 0)
190 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.identity.forceSetSubs', [subsToUpdate]);215 await uploadPreimage(
216 helper,
217 preimageMaker,
218 helper.constructApiCall('api.tx.identity.forceSetSubs', [subsToUpdate]).method.toHex(),
219 );
191220
192 console.log(`Also tried to update ${subsToUpdate.length} identities with their sub-identities.`221 console.log(`Also tried to push ${subsToUpdate.length} identities with their sub-identities.`
193 + ` Now there are ${(await helper.getApi().query.identity.subsOf.keys()).length} identities with subs.`);222 + ` Currently there are ${(await helper.getApi().query.identity.subsOf.keys()).length} identities with subs.`);
194 } catch (error) {223 } catch (error) {
195 console.error(error);224 console.error(error);
196 throw Error('Error during setting identities');225 throw Error('Error during setting identities');