Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add did:cheqd example #4

Merged
merged 1 commit into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions EXAMPLES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
> [!TIP]
> You can run an example by executing `pnpm example <example-id>`. The example id is included for each example below in `(<example-id>)`.

- [Create Did Cheqd](./examples/create-did-cheqd) (`create-did-cheqd`) - Create a did using the `did:cheqd` method
- [Create Did Jwk](./examples/create-did-jwk) (`create-did-jwk`) - Create a did using the `did:jwk` method
- [Create Did Key](./examples/create-did-key) (`create-did-key`) - Create a did using the `did:key` method
- [Endorse Indy Did](./examples/endorse-indy-did) (`endorse-indy-did`) - Endorse an Indy DID
Expand Down
6 changes: 6 additions & 0 deletions examples.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
[
{
"id": "create-did-cheqd",
"name": "Create Did Cheqd",
"path": "examples/create-did-cheqd",
"description": "Create a did using the `did:cheqd` method"
},
{
"id": "create-did-jwk",
"name": "Create Did Jwk",
Expand Down
100 changes: 100 additions & 0 deletions examples/create-did-cheqd/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { AskarModule } from "@credo-ts/askar";
import {
CheqdDidCreateOptions,
CheqdDidRegistrar,
CheqdDidResolver,
CheqdModule,
} from "@credo-ts/cheqd";
import {
Agent,
ConsoleLogger,
DidsModule,
LogLevel,
TypedArrayEncoder,
} from "@credo-ts/core";
import { agentDependencies } from "@credo-ts/node";
import { ariesAskar } from "@hyperledger/aries-askar-nodejs";

async function example() {
const agent = new Agent({
config: {
label: "Create did:cheqd",
logger: new ConsoleLogger(LogLevel.debug),
walletConfig: {
id: "create-did-cheqd-wallet",
key: "create-did-cheqd-wallet",
},
},
dependencies: agentDependencies,
modules: {
askar: new AskarModule({
ariesAskar,
}),

// CheqdModule
// Testnet Wallet address used cheqd1f9azjjmq8w5sq7dpsyl8pgw0672gp326v3h9gz
cheqdSdk: new CheqdModule({
networks: [
{
rpcUrl: "https://rpc.cheqd.network",
network: "testnet",
cosmosPayerSeed:
"truck exist skin trial divert master vintage ridge can ginger clap ugly",
},
],
}),

// Dids Module with CheqdDidRegistrar and CheqdDidResolver
dids: new DidsModule({
registrars: [new CheqdDidRegistrar()],
resolvers: [new CheqdDidResolver()],
}),
},
});

await agent.initialize();

// Generate a seed and get private key from it
const privateKey = TypedArrayEncoder.fromString(
Array(32 + 1)
.join(`${Math.random().toString(36)}00000000000000000`.slice(2, 18))
.slice(0, 32),
);

// CheqdDidCreateOptions provided the required interface for creating
// a did:cheqd DID.
const didResult = await agent.dids.create<CheqdDidCreateOptions>({
method: "cheqd",
secret: {
verificationMethod: {
id: "key-1",
type: "Ed25519VerificationKey2018",
privateKey,
},
},
options: {
network: "testnet",
methodSpecificIdAlgo: "base58btc",
},
});

// Some dids require multi-step processes to create and register a did, but for did:cheqd, the DID
// should always immediately reach state 'finished'. If this is not the case, something went wrong
if (didResult.didState.state !== "finished") {
throw new Error(
`Expected did state to be finished, but got ${
didResult.didState.state
}. ${
didResult.didState.state === "failed"
? didResult.didState.reason
: "Unknown error"
}`,
);
}

agent.config.logger.info(`Created did:cheqd DID: ${didResult.didState.did}`, {
didDocument: didResult.didState.didDocument.toJSON(),
});
}

example();
3 changes: 3 additions & 0 deletions examples/create-did-cheqd/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"description": "Create a did using the `did:cheqd` method"
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"dependencies": {
"@credo-ts/anoncreds": "0.5.0-alpha.114",
"@credo-ts/askar": "0.5.0-alpha.114",
"@credo-ts/cheqd": "0.5.0-alpha.114",
"@credo-ts/core": "0.5.0-alpha.114",
"@credo-ts/indy-vdr": "0.5.0-alpha.114",
"@credo-ts/node": "0.5.0-alpha.114",
Expand Down
Loading
Loading