Skip to content

Commit 13ebaba

Browse files
Add examples
1 parent 2ae8e13 commit 13ebaba

28 files changed

+1376
-5
lines changed

examples/README.md

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,46 +15,108 @@ Additionally, there is an E2E demo for recently added Hedera AnonCreds support.
1515

1616
The following examples are available in this directory:
1717

18-
### resolveDID
18+
### Resolver package
19+
20+
#### resolveDID
1921

2022
- **[`resolveDID-as-cbor.ts`](./resolveDID-as-cbor.ts)**: Demonstrates how to resolve a DID and retrieve its corresponding DID Document in CBOR format.
2123
- **[`resolveDID-as-json-ld.ts`](./resolveDID-as-json-ld.ts)**: Demonstrates how to resolve a DID and retrieve its corresponding DID Document in JSON-LD format.
2224
- **[`resolveDID-as-json.ts`](./resolveDID-as-json.ts)**: Demonstrates how to resolve a DID and retrieve its corresponding DID Document in JSON format.
2325
- **[`resolveDID-with-full-metadata.ts`](./resolveDID-with-full-metadata.ts)**: Demonstrates how to resolve a DID and retrieve its corresponding DID Document with full DID Resolution metadata.
2426
- **[`resolveDID-with-topic-reader.ts`](./resolveDID-with-topic-reader.ts)**: Demonstrates how to resolve a DID using a custom topic reader.
27+
- **[`resolveDID-with-hcs-topic-reader.ts`](./resolveDID-with-topic-reader.ts)**: Demonstrates how to resolve a DID using a HCS Topic Reader from resolver package.
2528
- **[`resolveDID-with-rest-api-topic-reader.ts`](./resolveDID-with-rest-api-topic-reader.ts)**: Demonstrates how to resolve a DID using a Hedera REST API Topic Reader from resolver package.
2629
- **[`resolveDID-with-verifier.ts`](./resolveDID-with-verifier.ts)**: Demonstrates how to resolve a DID using a custom verifier.
2730

28-
### dereferenceDID
31+
#### dereferenceDID
2932

3033
- **[`dereferenceDID-fragment.ts`](./dereferenceDID-fragment.ts)**: Demonstrates how to dereference a fragment from a DID document.
3134
- **[`dereferenceDID-service-endpoint.ts`](./dereferenceDID-service-endpoint.ts)**: Demonstrates how to dereference a service endpoint from a DID document.
3235
- **[`dereferenceDID-with-full-metadata.ts`](./dereferenceDID-with-full-metadata.ts)**: Demonstrates how to dereference a DID fragment with full DID Resolution metadata.
3336
- **[`dereferenceDID-with-topic-reader.ts`](./dereferenceDID-with-topic-reader.ts)**: Demonstrates how to dereference a DID fragment using a custom topic reader.
3437
- - **[`dereferenceDID-with-verifier.ts`](./dereferenceDID-with-verifier.ts)**: Demonstrates how to dereference a DID fragment using a custom verifier.
3538

36-
### createDID
39+
### Registrar package
40+
41+
#### createDID
3742

3843
- **[`createDID-with-client-options.ts`](./createDID-with-client-options.ts)**: Demonstrates how to create a DID with custom `client-options`.
3944
- **[`createDID-with-a-client.ts`](./createDID-with-a-client.ts)**: Shows how to create a DID using a pre-configured Hedera `client` instance.
4045
- **[`createDID-with-a-custom-controller.ts`](./createDID-with-a-custom-controller.ts)**: Demonstrates how to create a DID with a custom `controller`.
4146
- **[`createDID-with-a-topic-specific-did.ts`](./createDID-with-a-topic-specific-did.ts)**: Shows how to create a DID associated with a specific Hedera topic ID.
4247
- **[`createDID-using-client-secret-mode.ts`](./createDID-using-client-secret-mode.ts)**: Shows how to create a DID in a Client Managed Secret Mode.
4348

44-
### updateDID
49+
#### updateDID
4550

4651
- **[`updateDID-with-client-options.ts`](./updateDID-with-client-options.ts)**: Demonstrates how to update a DID with custom `client-options`.
4752
- **[`updateDID-with-a-client.ts`](./updateDID-with-a-client.ts)**: Shows how to update a DID using a pre-configured Hedera `client` instance.
4853
- **[`updateDID-with-multiple-properties.ts`](./updateDID-with-multiple-properties.ts)**: Demonstrates how to update multiple properties of a DID document simultaneously.
4954
- **[`updateDID-with-DID-update-builder.ts`](./updateDID-with-DID-update-builder.ts)**: Demonstrates how to use the `DIDUpdateBuilder` class to construct and execute DID update operations.
5055
- **[`updateDID-using-client-secret-mode.ts`](./updateDID-using-client-secret-mode.ts)**: Demonstrates how to update a DID in a Client Managed Secret Mode.
5156

52-
### deactivateDID
57+
#### deactivateDID
5358

5459
- **[`deactivateDID-with-client-options.ts`](./deactivateDID-with-client-options.ts)**: Demonstrates how to deactivate a DID with custom `client-options`.
5560
- **[`deactivateDID-with-a-client.ts`](./deactivateDID-with-a-client.ts)**: Shows how to deactivate a DID using a pre-configured Hedera `client` instance.
5661
- **[`deactivateDID-using-client-secret-mode.ts`](./deactivateDID-using-client-secret-mode.ts)**: Demonstrates how to deactivate a DID in a Client Managed Secret Mode.
5762

63+
64+
### HCS package
65+
66+
#### hcsService
67+
68+
- **[`hedara-hcs-service-comprehensive-example.ts`](./hedara-hcs-service-comprehensive-example.ts)**: Comprehensive example demonstrating creation, update, info retrieval of HCS topic, message submission and retrieval, file submission and resolution using `HederaHcsService`.
69+
70+
#### HcsTopicService
71+
72+
- **[`hcs-topic-service-create-topic.ts`](./hcs-topic-service-create-topic.ts)**: Demonstrates how to create a new Hedera Consensus Service (HCS) topic using `HcsTopicService.createTopic`.
73+
- **[`hcs-topic-service-update-topic.ts`](./hcs-topic-service-update-topic.ts)**: Demonstrates how to update an existing HCS topic using `HcsTopicService.updateTopic`.
74+
- **[`hcs-topic-service-delete-topic.ts`](./hcs-topic-service-delete-topic.ts)**: Demonstrates how to delete an HCS topic using `HcsTopicService.deleteTopic`.
75+
- **[`hcs-topic-service-get-topic-info.ts`](./hcs-topic-service-get-topic-info.ts)**: Demonstrates how to retrieve information about an HCS topic using `HcsTopicService.getTopicInfo`.
76+
77+
#### hcsMessageService
78+
79+
- **[`hcs-message-service-submit-message.ts`](./hcs-message-service-submit-message.ts)**: Demonstrates how to submit a message to a Hedera Consensus Service (HCS) topic using `HcsMessageService.submitMessage`.
80+
- **[`hcs-message-service-get-topic-messages.ts`](./hcs-message-service-get-topic-messages.ts)**: Demonstrates how to retrieve messages from an HCS topic using `HcsMessageService.getTopicMessages`.
81+
82+
#### hcsFileService
83+
84+
- **[`hcs-file-service-submit-file.ts`](./hcs-file-service-submit-file.ts)**: Demonstrates how to submit a file to Hedera Consensus Service (HCS) using `HcsFileService.submitFile`.
85+
- **[`hcs-file-service-resolve-file.ts`](./hcs-file-service-resolve-file.ts)**: Demonstrates how to resolve and retrieve a file from HCS using `HcsFileService.resolveFile`.
86+
87+
### HederaAnoncredsRegistry package
88+
89+
- **[`anoncreds-register-schema.ts`](./anoncreds-register-schema.ts)**: Demonstrates how to register a schema using `HederaAnoncredsRegistry.registerSchema`.
90+
- **[`anoncreds-get-schema.ts`](./anoncreds-get-schema.ts)**: Demonstrates how to retrieve a registered schema using `HederaAnoncredsRegistry.getSchema`.
91+
- **[`anoncreds-register-credential-definition.ts`](./anoncreds-register-credential-definition.ts)**: Demonstrates how to register a credential definition with `HederaAnoncredsRegistry.registerCredentialDefinition`.
92+
- **[`anoncreds-get-credential-definition.ts`](./anoncreds-get-credential-definition.ts)**: Demonstrates how to obtain a credential definition using `HederaAnoncredsRegistry.getCredentialDefinition`.
93+
- **[`anoncreds-register-revocation-registry.ts`](./anoncreds-register-revocation-registry.ts)**: Demonstrates how to register a revocation registry definition via `HederaAnoncredsRegistry.registerRevocationRegistryDefinition`.
94+
- **[`anoncreds-get-revocation-registry.ts`](./anoncreds-get-revocation-registry.ts)**: Demonstrates how to fetch a revocation registry definition using `HederaAnoncredsRegistry.getRevocationRegistryDefinition`.
95+
- **[`anoncreds-register-revocation-status-list.ts`](./anoncreds-register-revocation-status-list.ts)**: Demonstrates how to register a revocation status list using `HederaAnoncredsRegistry.registerRevocationStatusList`.
96+
- **[`anoncreds-get-revocation-status-list.ts`](./anoncreds-get-revocation-status-list.ts)**: Demonstrates how to get a revocation status list with `HederaAnoncredsRegistry.getRevocationStatusList`.
97+
98+
### HederaClientService package
99+
100+
- **[`get-client.ts`](./get-client.ts)**: Demonstrates how to create a Hedera client instance for a specific network using `HederaClientService.getClient`.
101+
- **[`with-client-operation.ts`](./with-client-operation.ts)**: Demonstrates usage of `HederaClientService.withClient` to safely perform operations with automatic client lifecycle management.
102+
103+
### LRUMemoryCache package
104+
105+
- **[`cache-set-get.ts`](./cache-set-get.ts)**: Demonstrates how to set, get, and remove cache entries using `LRUMemoryCache`.
106+
- **[`cache-expiry-cleanup.ts`](./cache-expiry-cleanup.ts)**: Demonstrates how to use expiration for cache entries and clean up expired items.
107+
- **[`cache-clear-getall.ts`](./cache-clear-getall.ts)**: Demonstrates how to clear the cache and retrieve all current cache entries.
108+
109+
### Crypto package
110+
111+
- **[`crypto-sha256-string.ts`](./crypto-sha256-string.ts)**: Demonstrates how to compute the SHA-256 hash of a string using the `Crypto.sha256` method.
112+
- **[`crypto-sha256-uint8array.ts`](./crypto-sha256-uint8array.ts)**: Demonstrates how to compute the SHA-256 hash of a Uint8Array using the `Crypto.sha256` method.
113+
114+
### Zstd package
115+
116+
- **[`zstd-compress.ts`](./zstd-compress.ts)**: Demonstrates how to compress a Uint8Array using the `Zstd.compress` static method.
117+
- **[`zstd-decompress.ts`](./zstd-decompress.ts)**: Demonstrates how to decompress a Uint8Array using the `Zstd.decompress` static method.
118+
119+
58120
## Running the Examples
59121

60122
To run the examples, follow these steps:
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* This example demonstrates how to fetch a credential definition
3+
* from the registry by its ID.
4+
*/
5+
import { HederaAnoncredsRegistry } from '@hiero-did-sdk/anoncreds';
6+
import { HederaClientConfiguration } from '@hiero-did-sdk/client';
7+
8+
const operatorId = process.env.HEDERA_TESTNET_OPERATOR_ID;
9+
const operatorKey = process.env.HEDERA_TESTNET_OPERATOR_KEY;
10+
11+
const config: HederaClientConfiguration = {
12+
networks: [
13+
{
14+
network: 'testnet',
15+
operatorId,
16+
operatorKey
17+
},
18+
],
19+
};
20+
21+
// Specify the existing credentialDefinitionId on the testnet here
22+
const credentialDefinitionId = 'did:hedera:testnet:zFAeKMsqnNc2bwEsC8oqENBvGqjpGu9tpUi3VWaFEBXBo_0.0.5896419/anoncreds/v0/PUBLIC_CRED_DEF/0.0.6557868';
23+
24+
async function main() {
25+
const registry = new HederaAnoncredsRegistry(config);
26+
27+
try {
28+
const result = await registry.getCredentialDefinition(credentialDefinitionId);
29+
console.log('Credential definition result:', result);
30+
} catch (error) {
31+
console.error('Failed to get credential definition:', error);
32+
}
33+
}
34+
35+
main();
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* This example demonstrates how to get a revocation registry definition
3+
* by its identifier from the registry.
4+
*/
5+
import { HederaAnoncredsRegistry } from '@hiero-did-sdk/anoncreds';
6+
import { HederaClientConfiguration } from '@hiero-did-sdk/client';
7+
8+
const operatorId = process.env.HEDERA_TESTNET_OPERATOR_ID;
9+
const operatorKey = process.env.HEDERA_TESTNET_OPERATOR_KEY;
10+
11+
const config: HederaClientConfiguration = {
12+
networks: [
13+
{
14+
network: 'testnet',
15+
operatorId,
16+
operatorKey
17+
},
18+
],
19+
};
20+
21+
// Specify the existing revocationRegistryDefinitionId on the testnet here
22+
const revocationRegistryDefinitionId = 'did:hedera:testnet:zFAeKMsqnNc2bwEsC8oqENBvGqjpGu9tpUi3VWaFEBXBo_0.0.5896419/anoncreds/v0/REV_REG/0.0.6557893';
23+
24+
async function main() {
25+
const registry = new HederaAnoncredsRegistry(config);
26+
27+
try {
28+
const result = await registry.getRevocationRegistryDefinition(revocationRegistryDefinitionId);
29+
console.log('Revocation registry definition result:', result);
30+
} catch (error) {
31+
console.error('Failed to get revocation registry definition:', error);
32+
}
33+
}
34+
35+
main();
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* This example demonstrates how to get a revocation status list
3+
* by revocation registry ID and timestamp using `getRevocationStatusList`.
4+
*/
5+
import { HederaAnoncredsRegistry } from '@hiero-did-sdk/anoncreds';
6+
import { HederaClientConfiguration } from '@hiero-did-sdk/client';
7+
8+
const operatorId = process.env.HEDERA_TESTNET_OPERATOR_ID;
9+
const operatorKey = process.env.HEDERA_TESTNET_OPERATOR_KEY;
10+
11+
const config: HederaClientConfiguration = {
12+
networks: [
13+
{
14+
network: 'testnet',
15+
operatorId,
16+
operatorKey
17+
},
18+
],
19+
};
20+
21+
// Specify the existing revocationRegistryId on the testnet here
22+
const revocationRegistryId = 'did:hedera:testnet:zFAeKMsqnNc2bwEsC8oqENBvGqjpGu9tpUi3VWaFEBXBo_0.0.5896419/anoncreds/v0/REV_REG/0.0.6557949';
23+
const timestamp = Date.now();
24+
25+
async function main() {
26+
const registry = new HederaAnoncredsRegistry(config);
27+
28+
try {
29+
const result = await registry.getRevocationStatusList(revocationRegistryId, timestamp);
30+
console.log('Revocation status list result:', result);
31+
} catch (error) {
32+
console.error('Failed to get revocation status list:', error);
33+
}
34+
}
35+
36+
main();

examples/anoncreds-get-schema.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* This example demonstrates how to get a schema
3+
* from HederaAnoncredsRegistry by schema identifier using `getSchema`.
4+
*/
5+
import { HederaAnoncredsRegistry } from '@hiero-did-sdk/anoncreds';
6+
import { HederaClientConfiguration } from '@hiero-did-sdk/client';
7+
8+
const operatorId = process.env.HEDERA_TESTNET_OPERATOR_ID;
9+
const operatorKey = process.env.HEDERA_TESTNET_OPERATOR_KEY;
10+
11+
const config: HederaClientConfiguration = {
12+
networks: [
13+
{
14+
network: 'testnet',
15+
operatorId,
16+
operatorKey
17+
},
18+
],
19+
};
20+
21+
// Specify the existing schemaId on the testnet here
22+
const schemaId = 'did:hedera:testnet:zFAeKMsqnNc2bwEsC8oqENBvGqjpGu9tpUi3VWaFEBXBo_0.0.5896419/anoncreds/v0/SCHEMA/0.0.6557796';
23+
24+
async function main() {
25+
const registry = new HederaAnoncredsRegistry(config);
26+
27+
try {
28+
const result = await registry.getSchema(schemaId);
29+
console.log('Schema resolution result:', result);
30+
} catch (error) {
31+
console.error('Failed to get schema:', error);
32+
}
33+
}
34+
35+
main();
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* This example demonstrates how to register a credential definition
3+
* using `registerCredentialDefinition` method.
4+
*/
5+
import { HederaAnoncredsRegistry } from '@hiero-did-sdk/anoncreds';
6+
import { HederaClientConfiguration } from '@hiero-did-sdk/client';
7+
import { AnonCredsCredentialDefinition, AnonCredsSchema } from '../packages/anoncreds/src/specification';
8+
9+
const operatorId = process.env.HEDERA_TESTNET_OPERATOR_ID;
10+
const operatorKey = process.env.HEDERA_TESTNET_OPERATOR_KEY;
11+
12+
const config: HederaClientConfiguration = {
13+
networks: [
14+
{
15+
network: 'testnet',
16+
operatorId,
17+
operatorKey
18+
},
19+
],
20+
};
21+
22+
const issuerDid = 'did:hedera:testnet:zFAeKMsqnNc2bwEsC8oqENBvGqjpGu9tpUi3VWaFEBXBo_0.0.5896419';
23+
24+
const schemaPayload: AnonCredsSchema = {
25+
issuerId: '',
26+
name: `Schema-${new Date().getTime()}`,
27+
version: '1',
28+
attrNames: ['field1', 'field2'],
29+
};
30+
31+
const credentialDefinitionPayload: AnonCredsCredentialDefinition = {
32+
issuerId: '',
33+
schemaId: '',
34+
tag: 'TAG',
35+
type: 'CL',
36+
value: {
37+
primary: {
38+
n: '92511867718854414868106363741369833735017762038454769060600859608405811709675033445666654908195955460485998711087020152978597220168927505650092431295783175164390266561239892662085428655566792056852960599485298025843840058914610127716620252006466964070280255168745873592143068949458568751438337748294055976926080232538440619420568859737673474560851456027625679328271511966332808025880807996449998057729417608399774744254122385012832309402226532031122728445959276178939234308090390331654445053482963947804769291501664200141562885660084823885847247231002821472258218384342423605116504024514572826071246440130942849549441',
39+
s: '80388543865249952799447792504739237616187770512259677275061283897050980768551818104137338144380636412773836688624071360386172349725818126495487584981520630638409717065318132420766896092370913800616033623618952639023946750307405126873476182540669638841562357523429245685476919178722373320218824590869735129801004394337640642997250464303104754942997839179333543643110326022824394934965538190976474473353762308333205671176627192797138375084260446324344637548455228161138089974447059481109651156379803576163576511072261388342837813901850712083922506433336723723235701670225584863772222447543742649328218950436824219992164',
40+
r: {
41+
age: '676933340341980399002624386891134393471002096508227567343731826159610079436978196421307099268754545293545727546242372579987825752872485684085629459107300175443328323289748793060894500514926703654606851666031895448970879827423190730510730624784665299646624113512701254199984520803796529034094958026048762178753193812250643294518237843809104055653333871102658177900702978008644780459400512716361564897282969982554031820285585105004870317861287847206222714589633178648982299799311192432563797220854755882933052881306804544233529886513105815543097685128456041780804442879272476590077760678785460726492895806240870944398',
42+
master_secret:
43+
'57770757113548032970308439965749734133430520933173186296299026579579930337912607419798836831937319372744879560676750427054135869214212225572618340088847222727882935159356459822445182287686057012197046378986248048722180093079919306125315662058290895629438767985427829790980355162853804522854494960613869765167538645624719923127052541372069255024631093663068055100579264049925388231368871107383977060590248865498902704546409806115171120555709438784189721957301548212242748685629860268468247494986146122636455769804467583612610341632602695197189514316033637331733820369170763954604394734655429769801516997967996980978751',
44+
},
45+
rctxt:
46+
'19574881057684356733946284215946569464410211018678168661028327420122678446653210056362495902735819742274128834330867933095119512313591151219353395069123546495720010325822330866859140765940839241212947354612836044244554152389691282543839111284006009168728161183863936810142428875817934316327118674532328892591410224676539770085459540786747902789677759379901079898127879301595929571621032704093287675668250862222728331030586585586110859977896767318814398026750215625180255041545607499673023585546720788973882263863911222208020438685873501025545464213035270207099419236974668665979962146355749687924650853489277747454993',
47+
z: '18569464356833363098514177097771727133940629758890641648661259687745137028161881113251218061243607037717553708179509640909238773964066423807945164288256211132195919975343578956381001087353353060599758005375631247614777454313440511375923345538396573548499287265163879524050255226779884271432737062283353279122281220812931572456820130441114446870167673796490210349453498315913599982158253821945225264065364670730546176140788405935081171854642125236557475395879246419105888077042924382595999612137336915304205628167917473420377397118829734604949103124514367857266518654728464539418834291071874052392799652266418817991437',
48+
},
49+
revocation: true,
50+
},
51+
};
52+
53+
async function main() {
54+
const registry = new HederaAnoncredsRegistry(config);
55+
56+
try {
57+
// Register a schema
58+
console.log(`Schema registering...`);
59+
const schemaResult = await registry.registerSchema({
60+
schema: { ...schemaPayload, issuerId: issuerDid },
61+
});
62+
const schemaId = schemaResult.schemaState.schemaId;
63+
console.log(`Schema registered (schemaId = ${schemaId})`);
64+
65+
// Register a credential definition
66+
console.log(`Credential definition registering...`);
67+
const result = await registry.registerCredentialDefinition({
68+
credentialDefinition: {
69+
...credentialDefinitionPayload,
70+
issuerId: issuerDid,
71+
schemaId,
72+
},
73+
options: {
74+
supportRevocation: true,
75+
},
76+
});
77+
console.log('Credential definition register result:', result);
78+
} catch (error) {
79+
console.error('Failed to register credential definition:', error);
80+
}
81+
}
82+
83+
main();

0 commit comments

Comments
 (0)