@litentry/vc-sdk
TypeScript icon, indicating that this package has built-in type declarations

1.1.1 • Public • Published

@litentry/vc-sdk

This SDK provides the common functionality to help dApps parse and validate Litentry issued Verifiable Credentials.

Currently, these functions included:

  1. parseVc method to parse VC JSON into a Verifiable Credential object.
  2. validateVc method to validate a given VC JSON by interacting with the Litentry parachain and TEE.

A live example of this SDK's consumer application is identity-hub (https://idhub.litentry.io) where users request and generate their VCs and reveal their obtained VCs and share the relevant VCs should they consent to 3rd party applications.

Install

npm install @litentry/vc-sdk

Examples

You can find more elaborated examples about the usage of this package on https://github.com/litentry/client-sdk-examples.

Below you will find some code snippets to guide you through the main functionalities.

Parse VC json

Parse a VC json to the Verifiable Credential object.

import { parseVc } from '@litentry/vc-sdk';

// Sample Verifiable Credential JSON string
const vcJson =
  '{"@context": "https://www.w3.org/2018/credentials/v1", "type": "VerifiableCredential", "issuer": "https://example.com/issuer", "subject": "did:example:123", "credentialStatus": "https://example.com/status"}';

try {
  // Parse the VC JSON string
  const parsedVc = parseVc(vcJson);

  // Use the parsed VerifiableCredential
  console.log('Parsed Verifiable Credential:', parsedVc);
} catch (error) {
  // Handle parsing errors
  console.error('Error parsing Verifiable Credential:', error);
}

Validate Verifiable Credential (VC)

Interaction with the Litentry parachain and TEE to validate whether the VC is an active VC issued by the Litentry.

How to find the wallet account address in VC

The id of VC's credentialSubject is the encoded wallet account address, using code below to encode:

import { decodeAddress } from '@polkadot/util-crypto';
import { u8aToHex } from '@polkadot/util';

const address = u8aToHex(decodeAddress(walletAccountAddress));
const credentialSubjectId = address.substring(2);

With the code above:

  • If your EVM account address is 0xC620b3e5BEBedA952A8AD18b83Dc4Cf3Dc9CAF4b, the credentialSubjectId will be c620b3e5bebeda952a8ad18b83dc4cf3dc9caf4b.
  • If your Substrate account address is 5CwPfqmormPx9wJ4ASq7ikwdJeRonoUZX9SxwUtm1px9L72W, the credentialSubjectId will be 26a84b380d8c3226d69f9ae6e482aa6669ed34c6371c52c4dfb48596913d6f28.

What the validateVc function do

  • The validateVc function can only verify that the VC was issued by Litentry.
  • The VC's credentialSubject can be Substrate or EVM account that is support by Litentry.

What the validateVc function can't do

  • The validateVc function cannot validate that the VC's credentialSubject is the current wallet account. It's SDK's consumer's responsibility to validate the id of VC's credentialSubject is equal to the wallet address.

Example

import { WsProvider, ApiPromise } from '@polkadot/api';
import { validateVc, NETWORKS } from '@litentry/vc-sdk';

const api: ApiPromise = await ApiPromise.create({
  provider: new WsProvider(NETWORKS['litentry-staging']),
});
// vc json string
const vcJson =
  '{"@context": "https://www.w3.org/2018/credentials/v1", "type": "VerifiableCredential", "issuer": "https://example.com/issuer", "subject": "did:example:123", "credentialStatus": "https://example.com/status"}';
const result = await validateVc(api, vcJson);

// isValid is false if any field value of the result.detail is not true
if (!result.isValid) {
  // true or error message
  console.log('vcJson: ', result.detail.vcJson);
  // true or error message
  console.log('vcSignature: ', result.detail.vcSignature);
  // true or error message
  console.log('enclaveRegistry: ', result.detail.enclaveRegistry);
}

Note for simplicity, in above example it's assumed that SDK consumer application has to provide the api instance of ApiPromise and manage its lifecycle as well.

Readme

Keywords

none

Package Sidebar

Install

npm i @litentry/vc-sdk

Weekly Downloads

388

Version

1.1.1

License

GPL-3.0-or-later

Unpacked Size

419 kB

Total Files

37

Last publish

Collaborators

  • i-trofimov
  • lit_louisian
  • yoshiyuki_asakura
  • jonalvarezz
  • litentry_engineering
  • fei_litentry