Skip to main content

Quick start guide

This guide walks you through the process of creating and verifying a verifiable credential (VC) using the Truvity SDK. You utilize user-defined types (UDTs) to represent the credential claims schema, enhancing type safety and simplifying the credential lifecycle.

Prerequisites​

Before you begin, make sure you have the following:

  • Installed and configured Truvity SDK. If you do not have the installed SDK, follow the Installation guide.
  • A basic understanding of Self-Sovereign Identity (SSI) concepts, such as Decentralized Identifiers (DIDs), verifiable credentials (VCs), and verifiable presentations (VPs). For more information, refer to the Core Concepts section.

Step 1: Define a simple credential type​

Begin by defining a simple credential type using UDTs. This credential type contains two required claims: name and age.

import { Truvity, VcSchema, VcClaim } from '@truvity/sdk';

// Define a simple credential type using UDT
@VcSchema()
class SimpleCredential {
@VcClaim
name?: string;

@VcClaim
age?: number;
}

The @VcClaim decorator marks the fields as claims in the credential.

Step 2: Generate a key for signing​

To issue a credential, you first need to generate a cryptographic key. The following example shows how generate a ED25519 key type.

const key = await client.keys.keyGenerate({ data: { type: 'ED25519' } });

You use this key to sign the credential later in the process.

Step 3: Create a draft​

Now, create a draft using the SimpleCredential UDT that you defined in Step 1. This draft includes claims (name and age) to embed in the credential.

info

Draft is a concept introduced by Truvity that represents the initial state of credential's content. To learn more about drafts, check the Drafts vs credentials page.

const simpleCredDecorator = client.createVcDecorator(SimpleCredential);

const draft = await simpleCredDecorator.create({
claims: {
name: "Alice",
age: 30
}
});

In this step, the credential is not yet issued or signed - it is just a draft that contains the claims data.

Step 4: Issue the credential​

Next, issue the credential by signing it with the previously generated key. This transforms the draft into a verifiable credential (VC).

const issuedVC = await draft.issue(key.id);

The issued credential is now cryptographically signed and can be shared with other parties.

Step 5: Verify the credential​

Finally, verify the authenticity of the issued credential. Verification ensures that the credential has not been tampered with and that it was signed by a valid key.

const verificationResult = await issuedVC.verify();
console.log("Verification result:", verificationResult.verified);

The verify() function checks the integrity and validity of the credential, and the result is printed to the console.

Putting it all together​

Now that each step is covered, here is the full code that puts everything together. This code defines a credential, generates a key, creates a draft, issues the credential, and finally verifies it.

import { TruvityClient, VcSchema, VcClaim } from '@truvity/sdk';

// Define a simple credential type using UDT
@VcSchema()
class SimpleCredential {
@VcClaim
name!: string;

@VcClaim
age!: number;
}

async function createAndVerifyVC() {
// Initialize the SDK client
const client = new Truvity.TruvityClient({
environment: "https://api.truvity.com",
apiKey: "YOUR_API_KEY",
});

// Generate a key for signing
const key = await client.keys.keyGenerate({ data: { type: 'ED25519' } });

// Create a credential decorator
const simpleCredDecorator = client.createVcDecorator(SimpleCredential);

// Create a draft
const draft = await simpleCredDecorator.create({
claims: {
name: "Alice",
age: 30
}
});

// Issue the credential
const issuedVC = await draft.issue(key.id);

// Verify the credential
const verificationResult = await issuedVC.verify();
console.log("Verification result:", verificationResult.verified);
}

createAndVerifyVC();

Congratulations! You have just issued your first Truvity-powered verifiable credential and is now ready to explore more topics on working with the SDK.

Further reading​