Usage Examples

Initialize SDK Client

Depending on your use-case and specific needs, constructing a LearnCard is likely as simple as the following code:

import { initLearnCard } from '@learncard/init';

const learnCard = await initLearnCard({ seed: 'abc123' });

The initLearnCard function

While there are many init functions that are exposed and that can be used, we recommend instead sticking to the initLearnCard function.

initLearnCard is a config-driven, heavily overloaded function, that allows you to construct a wallet flexibly, without sacrificing type safety.

Under the hood, it is simply a map between the config you provide and the init function you would normally need to call, meaning calls like initLearnCard() and emptyLearnCard() are identical.

Example Usage

import { initLearnCard } from '@learncard/init';

// Constructs an empty LearnCard without key material (can not sign VCs). 
// Useful for Verifying Credentials only in a light-weight form.
const emptyLearncard = await initLearnCard();

// Constructs a LearnCard from a deterministic seed. 
const learncard = await initLearnCard({ seed: 'abc123' });

// Constructs a LearnCard default connected to LearnCard Network hosted at https://network.learncard.com
const networkLearnCard = await initLearnCard({ seed: 'abc123', network: true });

// Constructs a LearnCard default connected to VC-API at https://bridge.learncard.com for handling signing
const defaultApi = await initLearnCard({ vcApi: true });

// Constructs a LearnCard connected to a custom VC-API, with Issuer DID specified.
const customApi = await initLearnCard({ vcApi: 'vc-api.com', did: 'did:key:123' });

// Constructs a LearnCard connected to a custom VC-API that implements /did discovery endpoint.
const customApiWithDIDDiscovery = await initLearnCard({ vcApi: 'https://bridge.learncard.com' });

// Constructs a LearnCard with no plugins. Useful for building your own bespoke LearnCard
const customLearnCard = await initLearnCard({ custom: true });

The examples above are not exhaustive of possible ways to instantiate a LearnCard:

  • For more on initialization with a VC-API, check out the VC-API Plugin.

The learnCardFromSeed function

Helper function to quickly create a LearnCard from seed.

The emptyLearnCard function

For a bare-bones LearnCard wallet without any plugins.

Key Generation

How to generate and store keys is left to you, the consumer. However, if you'd like to simply generate a random key, you can do so with the following code:

To speed up instantiation of the wallet, you can host our didkit wasm binary yourself.

If you're curious about what the above code is doing, read more here.

Create Credentials

Test Credential

One of the easiestโ€”and fastestโ€”ways to create a credential is to generate a test credential:

Basic Boost Credential

In it's most basic form, you can create a Boost credential using the following schema:

Credentials from Template

But sometimes the test credential is too basic for most use cases. That's why LearnCard has out-of-the-box support for some basic types of credentials, and a simple function to create new credentials:

Issue Credentials

First, make an unsigned Verifiable Credential. You can do this yourself if you already have that set up for your app, or, if you just need to test out working with this library, you can use the newCredential method to easily create a test VC.

To sign (or "issue") that VC, simply call issueCredential

Verify Credentials

After a credential is signed, the credential may be transferred via an exchange mechanism, where a receiving party can verify it! To verify a signed Verifiable Credential, you can use verifyCredential

Issue/Verify Presentations

Similar to Verifiable Credentials, LearnCard has methods for verifying and issuing Verifiable Presentations:

What is a Verifiable Presentation, and why would I use one?

Verifiable Presentations enable trusted sharing of one or more claims in a single, verifiable package. Claims may be bundled from one or multiple issuers, and they may consist of the original Verifiable Credential, or a derived "zero-knowledge proof."

As a general principle, you should use Verifiable Presentations when presenting Verifiable Credentials to a verifying party because it proves the relationship between the user or entity presenting the credential, and the credential itself.

Storing/Retrieving Credentials

Credentials can be converted back and forth to URIs, which can be stored per holder using Control Planes. URIs simplify complex processes, such as indexing and caching, over credentials stored in many different locations, such as in IPFS, device storage, or a Decentralized Web Node.

The above example uses LearnCloud storage, but there are many ways to store and retrieve a credential! Check out the Store control plane for more info and options.

LearnCloud Network

Create a Profile

To create a new profile, use the createProfile method. This method accepts an object containing the profile information, excluding the did and isServiceProfile properties.

These examples demonstrate creating, retrieving, updating, and deleting different types of profiles and profile managers.

Retrieving & Searching Profiles

To search for profiles, use the searchProfiles method. This method accepts an optional profileId parameter and an options object. The options object can contain the following properties:

  • limit: Maximum number of profiles to return.

  • includeSelf: Whether to include the current user's profile in the results.

  • includeConnectionStatus: Whether to include connection status in the results.

More advanced usage:

Updating & Deleting Profiles

Connection Management

Connect with a Profile

To send a connection request to another profile, use the connectWith method. This method accepts a profileId parameter.

Generate an Invite

To generate an invite, use the generateInvite method. This method now accepts two parameters: a challenge and an expiration parameter. The challenge parameter is optional and will be automatically generated if not provided. The expiration parameter sets the duration (in seconds) for which the invite remains valid and defaults to 30 days if not specified.

Connect with a Profile using an Invite

To connect with another profile using an invite, use the connectWithInvite method. This method requires a profileId and a challenge parameter.

Accept a Connection Request

To accept a connection request from another profile, use the acceptConnectionRequest method. This method accepts a profileId parameter.

Advanced examples for establishing, managing, and retrieving connections between profiles.

Credential & Presentation Exchange

Demonstrates sending, receiving, and managing Verifiable Credentials (VCs) and Verifiable Presentations (VPs).

Send a Credential

To send a credential to another profile, use the sendCredential method. This method accepts a profileId, a vc object (which can be an UnsignedVC or VC), and an optional encrypt parameter.

Get Received Credentials

To retrieve all received credentials, use the getReceivedCredentials method. This method accepts an optional from parameter.

Accept a Credential

To accept a credential, use the acceptCredential method. This method accepts a uri parameter.

Send a Presentation

To send a presentation to another profile, use the sendPresentation method. This method accepts a profileId, a vp object, and an optional encrypt parameter.

Advanced Examples:

Boost Management

Examples covering the lifecycle of Boosts, including creation, retrieval, hierarchy, recipients, permissions, and sending.

Create a Boost

To create a boost, use the createBoost method. This method accepts a credential object (which can be an UnsignedVC or VC) and an optional metadata object.

Get a Boost

To get a boost, use the getBoost method. This method accepts a uri parameter.

Send a Boost

To send a boost to another profile, use the sendBoost method. This method accepts a profileId, a boostUri parameter, and an optional encrypt parameteropconst profileId = 'janesmith';

These are the API calls related to boosts management in the LearnCard Network API. Use these methods to create, update, retrieve, and delete boosts, as well as send boosts to other profiles.

Advanced Examples:

Signing Authorities & Claim Links

Examples for registering and using signing authorities, and generating/claiming boosts via links.

ConsentFlow Contracts

Examples for creating and managing Consent Flow Contracts, user consent, and data access.

DID Metadata Management

Examples for adding, retrieving, updating, and deleting DID metadata.

Claim Hooks

Examples for managing claim hooks for Boosts.

Authorization Grants & API Tokens

Examples for managing authorization grants and generating API tokens.

Click here for more information on Auth Grants and Scopes.

Create an AuthGrant

Use the LearnCard SDK's addAuthGrant method to create a new AuthGrant:

AuthGrant Properties

  • id: Unique identifier (auto-generated if not provided)

  • name: Name of the AuthGrant

  • description: (Optional) Description of the purpose or use case

  • challenge: Security challenge string (must start with AuthGrant prefix)

  • status: Either 'active' or 'revoked'

  • scope: Permission scope string

  • createdAt: ISO 8601 datetime string of creation (auto-generated if not provided)

  • expiresAt: (Optional) ISO 8601 datetime string for expiration

Generating an API Token

Once you have an AuthGrant, you can generate an API token using the getAPITokenForAuthGrant method:

This token encapsulates the permissions defined in the AuthGrant and should be used for authentication in API requests.

Using the API Token for HTTP Requests

Use the generated API token in the Authorization header with the Bearer scheme:

Generate API Token End-to-End Example

Here's a complete example showing how to:

  1. Create an AuthGrant

  2. Generate an API token

  3. Use the token to send a boost via the HTTP API

Retrieving AuthGrants

Updating AuthGrants

Revoking AuthGrants

General Utilities

Last updated

Was this helpful?