|
| 1 | +import { PrivateKey } from '@hashgraph/sdk'; |
| 2 | +import { createDID, deactivateDID, updateDID } from '@hiero-did-sdk/registrar'; |
| 3 | +import { resolveDID } from '@hiero-did-sdk/resolver'; |
| 4 | +import * as readline from 'node:readline'; |
| 5 | + |
| 6 | +const readLineInterface = readline.createInterface({ |
| 7 | + input: process.stdin, |
| 8 | + output: process.stdout, |
| 9 | +}); |
| 10 | + |
| 11 | +const prompt = (text: string) => new Promise((resolve) => readLineInterface.question(text, resolve)); |
| 12 | + |
| 13 | +const operatorId = process.env.HEDERA_TESTNET_OPERATOR_ID; |
| 14 | +const operatorKey = process.env.HEDERA_TESTNET_OPERATOR_KEY; |
| 15 | + |
| 16 | +const clientOptions = { |
| 17 | + network: 'testnet' as const, |
| 18 | + accountId: operatorId, |
| 19 | + privateKey: operatorKey, |
| 20 | +}; |
| 21 | + |
| 22 | +async function main() { |
| 23 | + const privateKey = await PrivateKey.generateED25519Async(); |
| 24 | + |
| 25 | + console.log('Creating new Hedera DID...'); |
| 26 | + |
| 27 | + const { did } = await createDID( |
| 28 | + { privateKey }, |
| 29 | + { |
| 30 | + clientOptions, |
| 31 | + } |
| 32 | + ); |
| 33 | + |
| 34 | + console.log(`Hedera DID has been created: ${did}`); |
| 35 | + |
| 36 | + console.log(`Resolving the DID...`); |
| 37 | + |
| 38 | + let didDocument = await resolveDID(did); |
| 39 | + |
| 40 | + console.log('Resolved DID Document:'); |
| 41 | + console.log(JSON.stringify(didDocument, null, 2)); |
| 42 | + |
| 43 | + await prompt('Press enter to proceed with updating the DID...'); |
| 44 | + const serviceEndpoint = await prompt('Please enter DID service endpoint to add: '); |
| 45 | + |
| 46 | + console.log(`Updating the DID...`); |
| 47 | + |
| 48 | + await updateDID( |
| 49 | + { |
| 50 | + did, |
| 51 | + privateKey, |
| 52 | + updates: [ |
| 53 | + { |
| 54 | + operation: 'add-service', |
| 55 | + id: '#service-1', |
| 56 | + type: 'VerifiableCredentialService', |
| 57 | + serviceEndpoint: serviceEndpoint as string, |
| 58 | + }, |
| 59 | + ], |
| 60 | + }, |
| 61 | + { |
| 62 | + clientOptions, |
| 63 | + } |
| 64 | + ); |
| 65 | + |
| 66 | + console.log('Resolving the updated DID...'); |
| 67 | + |
| 68 | + didDocument = await resolveDID(did); |
| 69 | + |
| 70 | + console.log('Resolved updated DID Document:'); |
| 71 | + console.log(JSON.stringify(didDocument, null, 2)); |
| 72 | + |
| 73 | + await prompt('Press enter to proceed with deactivating the DID...'); |
| 74 | + |
| 75 | + console.log('Deactivating the DID...'); |
| 76 | + |
| 77 | + await deactivateDID({ did, privateKey }, { clientOptions }); |
| 78 | + |
| 79 | + console.log('Resolving the deactivated DID...'); |
| 80 | + |
| 81 | + didDocument = await resolveDID(did); |
| 82 | + |
| 83 | + console.log('Resolved deactivated DID Document:'); |
| 84 | + console.log(JSON.stringify(didDocument, null, 2)); |
| 85 | +} |
| 86 | + |
| 87 | +main().finally(() => readLineInterface.close()); |
0 commit comments