|
| 1 | +import type { Network } from "../types/network"; |
| 2 | +import { ExampleFactory } from "./factory"; |
| 3 | + |
| 4 | +export class PeopleChainExample extends ExampleFactory { |
| 5 | + constructor() { |
| 6 | + super({ |
| 7 | + id: "people-chain-identity", |
| 8 | + name: "People Chain Identity", |
| 9 | + description: "Demonstrate People Chain identity operations: DID creation, verification, and management", |
| 10 | + level: "advanced", |
| 11 | + categories: ["identity", "people-chain", "did", "verification"], |
| 12 | + }); |
| 13 | + } |
| 14 | + |
| 15 | + generateCode(network: Network): string { |
| 16 | + return `// People Chain Identity Operations Example on ${network.name} |
| 17 | +${this.getImports(network, true)} |
| 18 | +
|
| 19 | +// Connect to ${network.name} People Chain |
| 20 | +const client = createClient( |
| 21 | + withPolkadotSdkCompat( |
| 22 | + getWsProvider("${network.endpoint}") |
| 23 | + ) |
| 24 | +); |
| 25 | +
|
| 26 | +// Get the typed API using the descriptors |
| 27 | +const typedApi = client.getTypedApi(${network.descriptorKey}); |
| 28 | +
|
| 29 | +// People Chain identity operations demonstration |
| 30 | +const demonstratePeopleChainIdentity = async () => { |
| 31 | + try { |
| 32 | + console.log("🆔 Starting People Chain identity operations demonstration..."); |
| 33 | +
|
| 34 | + // 1. Query identity information |
| 35 | + console.log("\\n🔍 Querying identity information:"); |
| 36 | + const aliceAddress = "${this.getTestAccount("alice")}"; |
| 37 | + const bobAddress = "${this.getTestAccount("bob")}"; |
| 38 | +
|
| 39 | + try { |
| 40 | + const aliceIdentity = await typedApi.query.Identity.IdentityOf.getValue(aliceAddress); |
| 41 | + console.log("Alice's identity:", aliceIdentity); |
| 42 | +
|
| 43 | + const bobIdentity = await typedApi.query.Identity.IdentityOf.getValue(bobAddress); |
| 44 | + console.log("Bob's identity:", bobIdentity); |
| 45 | + } catch (error) { |
| 46 | + console.log("Identity queries not available or different structure"); |
| 47 | + } |
| 48 | +
|
| 49 | + // 2. Query super identities |
| 50 | + console.log("\\n👑 Querying super identities:"); |
| 51 | + try { |
| 52 | + const superOf = await typedApi.query.Identity.SuperOf.getValue(aliceAddress); |
| 53 | + console.log("Alice's super identity:", superOf); |
| 54 | + } catch (error) { |
| 55 | + console.log("Super identity queries not available"); |
| 56 | + } |
| 57 | +
|
| 58 | + // 3. Query subs (sub-accounts) |
| 59 | + console.log("\\n👥 Querying sub-accounts:"); |
| 60 | + try { |
| 61 | + const subsOf = await typedApi.query.Identity.SubsOf.getValue(aliceAddress); |
| 62 | + console.log("Alice's sub-accounts:", subsOf); |
| 63 | + } catch (error) { |
| 64 | + console.log("Sub-account queries not available"); |
| 65 | + } |
| 66 | +
|
| 67 | + // 4. Demonstrate identity registration |
| 68 | + console.log("\\n📝 Identity Registration Example:"); |
| 69 | + console.log("This would register a new identity:"); |
| 70 | + const registerTx = typedApi.tx.Identity.setIdentity({ |
| 71 | + display: { |
| 72 | + Raw: "Alice Developer" |
| 73 | + }, |
| 74 | + legal: { |
| 75 | + Raw: "Alice Developer Ltd" |
| 76 | + }, |
| 77 | + web: { |
| 78 | + Raw: "https://alice.dev" |
| 79 | + }, |
| 80 | + email: { |
| 81 | + |
| 82 | + }, |
| 83 | + twitter: { |
| 84 | + Raw: "@alice_dev" |
| 85 | + }, |
| 86 | + matrix: { |
| 87 | + Raw: "@alice:matrix.org" |
| 88 | + } |
| 89 | + }); |
| 90 | + console.log("Identity registration transaction created (not submitted in simulator)"); |
| 91 | +
|
| 92 | + // 5. Demonstrate identity verification request |
| 93 | + console.log("\\n✅ Identity Verification Request Example:"); |
| 94 | + console.log("This would request verification for identity fields:"); |
| 95 | + const requestVerifyTx = typedApi.tx.Identity.requestJudgement({ |
| 96 | + reg_index: 0, // Registration index |
| 97 | + max_fee: 1000000000000n // Maximum fee for verification |
| 98 | + }); |
| 99 | + console.log("Judgement request transaction created (not submitted in simulator)"); |
| 100 | +
|
| 101 | + // 6. Query registrars |
| 102 | + console.log("\\n🏛️ Querying registrars:"); |
| 103 | + try { |
| 104 | + const registrars = await typedApi.query.Identity.Registrars.getValue(); |
| 105 | + console.log("Available registrars:", registrars?.map((reg, index) => ({ |
| 106 | + index, |
| 107 | + account: reg?.account, |
| 108 | + fee: reg?.fee?.toString(), |
| 109 | + fields: reg?.fields?.toString() |
| 110 | + }))); |
| 111 | + } catch (error) { |
| 112 | + console.log("Registrar queries not available"); |
| 113 | + } |
| 114 | +
|
| 115 | + // 7. Demonstrate sub-account addition |
| 116 | + console.log("\\n👶 Sub-Account Addition Example:"); |
| 117 | + console.log("This would add a sub-account to an identity:"); |
| 118 | + const addSubTx = typedApi.tx.Identity.addSub({ |
| 119 | + sub: MultiAddress.Id(bobAddress), |
| 120 | + data: { |
| 121 | + Raw: "Bob Assistant" |
| 122 | + } |
| 123 | + }); |
| 124 | + console.log("Add sub-account transaction created (not submitted in simulator)"); |
| 125 | +
|
| 126 | + // 8. Demonstrate sub-account removal |
| 127 | + console.log("\\n👋 Sub-Account Removal Example:"); |
| 128 | + console.log("This would remove a sub-account from an identity:"); |
| 129 | + const removeSubTx = typedApi.tx.Identity.removeSub({ |
| 130 | + sub: MultiAddress.Id(bobAddress) |
| 131 | + }); |
| 132 | + console.log("Remove sub-account transaction created (not submitted in simulator)"); |
| 133 | +
|
| 134 | + // 9. Demonstrate identity clearing |
| 135 | + console.log("\\n🗑️ Identity Clearing Example:"); |
| 136 | + console.log("This would clear the entire identity:"); |
| 137 | + const clearTx = typedApi.tx.Identity.clearIdentity(); |
| 138 | + console.log("Clear identity transaction created (not submitted in simulator)"); |
| 139 | +
|
| 140 | + // 10. Query identity judgments |
| 141 | + console.log("\\n⚖️ Querying identity judgments:"); |
| 142 | + try { |
| 143 | + const judgements = await typedApi.query.Identity.Judgements.getValue(aliceAddress); |
| 144 | + console.log("Alice's judgements:", judgements); |
| 145 | + } catch (error) { |
| 146 | + console.log("Judgement queries not available"); |
| 147 | + } |
| 148 | +
|
| 149 | + // 11. Demonstrate username registration (if available) |
| 150 | + console.log("\\n📛 Username Registration Example:"); |
| 151 | + console.log("This would register a username:"); |
| 152 | + try { |
| 153 | + const usernameTx = typedApi.tx.Identity.setUsername({ |
| 154 | + username: "alice_dev" |
| 155 | + }); |
| 156 | + console.log("Username registration transaction created (not submitted in simulator)"); |
| 157 | + } catch (error) { |
| 158 | + console.log("Username registration not available on this network"); |
| 159 | + } |
| 160 | +
|
| 161 | + // 12. Query username authorities |
| 162 | + console.log("\\n🔐 Querying username authorities:"); |
| 163 | + try { |
| 164 | + const authorities = await typedApi.query.Identity.UsernameAuthorities.getValue(); |
| 165 | + console.log("Username authorities:", authorities); |
| 166 | + } catch (error) { |
| 167 | + console.log("Username authorities not available"); |
| 168 | + } |
| 169 | +
|
| 170 | + // 13. Demonstrate DID operations (if available) |
| 171 | + console.log("\\n🆔 DID Operations Example:"); |
| 172 | + console.log("This would perform DID operations:"); |
| 173 | + try { |
| 174 | + // Query DID document |
| 175 | + const didDoc = await typedApi.query.Did.DidDocuments.getValue(aliceAddress); |
| 176 | + console.log("DID document for Alice:", didDoc); |
| 177 | +
|
| 178 | + // DID update example |
| 179 | + const didUpdateTx = typedApi.tx.Did.setAttribute({ |
| 180 | + did: aliceAddress, |
| 181 | + name: { |
| 182 | + Raw: "email" |
| 183 | + }, |
| 184 | + value: { |
| 185 | + |
| 186 | + }, |
| 187 | + valid_for: null |
| 188 | + }); |
| 189 | + console.log("DID attribute update transaction created (not submitted in simulator)"); |
| 190 | + } catch (error) { |
| 191 | + console.log("DID operations not available on this network"); |
| 192 | + } |
| 193 | +
|
| 194 | + console.log("\\n✅ People Chain identity operations demonstration completed!"); |
| 195 | + console.log("Note: Identity operations require:"); |
| 196 | + console.log("- Understanding of registrar fees and requirements"); |
| 197 | + console.log("- Proper field validation and formatting"); |
| 198 | + console.log("- Sufficient balance for registration and verification fees"); |
| 199 | + console.log("- In a real application, you would sign and submit these transactions"); |
| 200 | + console.log("- Identity verification may require third-party registrars"); |
| 201 | +
|
| 202 | + } catch (error) { |
| 203 | + console.error("❌ Error in People Chain identity operations:", error); |
| 204 | + } |
| 205 | +}; |
| 206 | +
|
| 207 | +demonstratePeopleChainIdentity().catch(console.error); |
| 208 | +`; |
| 209 | + } |
| 210 | +} |
0 commit comments