New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@digitalcredentials/credential-status-manager-db

Package Overview
Dependencies
Maintainers
0
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@digitalcredentials/credential-status-manager-db

A Typescript library for managing the status of Verifiable Credentials in a Database using Bitstring Status List.

0.0.1
latest
Source
npm
Version published
Weekly downloads
2
Maintainers
0
Weekly downloads
 
Created
Source

credential-status-manager-db

Build status NPM Version

A Typescript library for managing the status of Verifiable Credentials in a database using Bitstring Status List

Table of Contents

Background

Credentials are dynamic artifacts with a lifecycle that goes well beyond issuance. This lifecycle is liable to span revocation, suspension, and expiry, among other common states. Many proposals have been put forth to capture these statuses in Verifiable Credentials. One of the most mature specifications for this is Bitstring Status List. This library provides an implementation of this specification that leverages database services like MongoDB and MySQL for storage and authentication.

Install

  • Node.js 20+ is recommended.

NPM

To install via NPM:

npm install @digitalcredentials/credential-status-manager-db

Development

To install locally (for development):

git clone https://github.com/digitalcredentials/credential-status-manager-db.git
cd credential-status-manager-db
npm install

Usage

Create credential status manager

The createStatusManager function is the only exported pure function of this library. It is an asynchronous function that accepts configuration options and returns a credential status manager that aligns with these options. Here are all the possible configuration options:

KeyDescriptionTypeRequired
databaseServicename of the database service used to manage credential status datamongodbyes
statusCredentialSiteOriginbase URL of status credentials managed by a given deploymentstringyes
databaseUrlURL of the database instance used to manage credential status datastringyes (if databaseHost, databasePort, databaseUsername, and databasePassword are not set)
databaseHosthost of the database instance used to manage credential status datastringyes (if databaseUrl is not set)
databasePortport of the database instance used to manage credential status datanumberyes (if databaseUrl is not set)
databaseUsernameusername of user with read/write privileges on the database instance used to manage credential status datastringyes (if databaseUrl is not set)
databasePasswordpassword associated with databaseUsernamestringyes (if databaseUrl is not set)
databaseNamename of the database instance used to manage credential status datastringno (default: credentialStatus)
statusCredentialTableNamename of the database table used to manage status credentials (schema)stringno (default: StatusCredential)
userCredentialTableNamename of the database table used to manage user credentials (schema)stringno (default: UserCredential)
configTableNamename of the database table used to manage application configuration (schema)stringno (default: Config)
eventTableNamename of the database table used to manage credential status events (schema)stringno (default: Event)
credentialEventTableNamename of the database table used to manage the latest status event for a given credential (schema)stringno (default: CredentialEvent)
autoDeployDatabasewhether or not to automatically create the database (databaseName) and the initial tables (statusCredentialTableName and configTableName)stringno (default: true)
didMethodname of the DID method used for signingkey | webyes
didSeedseed used to deterministically generate DIDstringyes
didWebUrlURL for did:webstringyes (if didMethod = web)
signStatusCredentialwhether or not to sign status credentialsbooleanno (default: true)
signUserCredentialwhether or not to sign user credentialsbooleanno (default: false)

Here is a sample call to createStatusManager:

import { createStatusManager } from '@digitalcredentials/credential-status-manager-db';

const statusManager = await createStatusManager({
  databaseService: 'mongodb',
  statusCredentialSiteOrigin: 'https://credentials.example.edu/status',
  databaseUrl: 'mongodb+srv://testuser:testpass@domain.mongodb.net?retryWrites=false',
  databaseUsername: 'testuser',
  databasePassword: 'testpass',
  didMethod: 'key',
  didSeed: 'DsnrHBHFQP0ab59dQELh3uEwy7i5ArcOTwxkwRO2hM87CBRGWBEChPO7AjmwkAZ2' // Please create your own DID seed (see Dependencies section for detailed instructions)
});

Allocate status for credential

allocateStatus is an instance method that is called on a credential status manager initialized by createStatusManager. It is an asynchronous method that accepts a credential and an array of status purposes as input (options: revocation | suspension), records its status in a previously configured database instance, and returns the credential with status metadata attached.

Here is a sample call to allocateStatus:

const credential = {
  '@context': [
    'https://www.w3.org/ns/credentials/v2',
    'https://w3id.org/security/suites/ed25519-2020/v1'
  ],
  id: 'https://credentials.example.edu/3732',
  type: [
    'VerifiableCredential'
  ],
  issuer: 'did:key:z6MkhVTX9BF3NGYX6cc7jWpbNnR7cAjH8LUffabZP8Qu4ysC',
  validFrom: '2020-03-10T04:24:12.164Z',
  credentialSubject: {
    id: 'did:example:abcdef'
  }
};
const credentialWithStatus = await statusManager.allocateStatus({
  credential,
  statusPurposes: ['revocation', 'suspension']
});
console.log(credentialWithStatus);
/*
{
  '@context': [
    'https://www.w3.org/ns/credentials/v2'
  ],
  id: 'https://credentials.example.edu/3732',
  type: [ 'VerifiableCredential' ],
  issuer: 'did:key:z6MkhVTX9BF3NGYX6cc7jWpbNnR7cAjH8LUffabZP8Qu4ysC',
  validFrom: '2020-03-10T04:24:12.164Z',
  credentialSubject: { id: 'did:example:abcdef' },
  credentialStatus: [
    {
      id: 'https://credentials.example.edu/status/Uz42qSDSXTcoLH7kZ6ST#6',
      type: 'BitstringStatusListEntry',
      statusPurpose: 'revocation',
      statusListIndex: '6',
      statusListCredential: 'https://credentials.example.edu/status/Uz42qSDSXTcoLH7kZ6ST'
    },
    {
      id: 'https://credentials.example.edu/status/9kGimd8POqM88l32F9aT#3',
      type: 'BitstringStatusListEntry',
      statusPurpose: 'suspension',
      statusListIndex: '3',
      statusListCredential: 'https://credentials.example.edu/status/9kGimd8POqM88l32F9aT'
    }
  ]
}
*/

Note: You can also call allocateRevocationStatus(credential) to achieve the same effect as allocateStatus({ credential, statusPurposes: ['revocation'] }), allocateSuspensionStatus(credential) to achieve the same effect as allocateStatus({ credential, statusPurposes: ['suspension'] }), and allocateSupportedStatuses(credential) to achieve the same effect as allocateStatus({ credential, statusPurposes: ['revocation', 'suspension'] }).

Additionally, if the caller invokes allocateStatus multiple times with the same credential ID against the same instance of a credential status manager, the library will not allocate a new entry. It will just return a credential with the same status info as it did in the previous invocation.

Update status of credential

updateStatus is an instance method that is called on a credential status manager initialized by createStatusManager. It is an asynchronous method that accepts as input a credential ID, a status purpose (options: revocation | suspension), and whether to invalidate the status; records its new status in a previously configured database instance; and returns the status credential.

Here is a sample call to updateStatus:

const statusCredential = await statusManager.updateStatus({
  credentialId: credentialWithStatus.id,
  statusPurpose: 'revocation',
  invalidate: true
});
console.log(statusCredential);
/*
{
  '@context': [
    'https://www.w3.org/ns/credentials/v2'
  ],
  id: 'https://credentials.example.edu/status/Uz42qSDSXTcoLH7kZ6ST',
  type: [ 'VerifiableCredential', 'BitstringStatusListCredential' ],
  credentialSubject: {
    id: 'https://credentials.example.edu/status/Uz42qSDSXTcoLH7kZ6ST#list',
    type: 'BitstringStatusList',
    encodedList: 'H4sIAAAAAAAAA-3BMQ0AAAACIGf_0LbwAhoAAAAAAAAAAAAAAIC_AfqBUGnUMAAA',
    statusPurpose: 'revocation'
  },
  issuer: 'did:key:z6MkhVTX9BF3NGYX6cc7jWpbNnR7cAjH8LUffabZP8Qu4ysC',
  validFrom: '2024-03-10T00:00:00.000Z'
}
*/

Note: You can also call revokeCredential(credentialId) to achieve the same effect as updateStatus({ credentialId, statusPurpose: 'revocation', invalidate: true }) and suspendCredential(credentialId) to achieve the same effect as updateStatus({ credentialId, statusPurpose: 'suspension', invalidate: true }). Also note that unsuspendCredential(credentialId) will lift a suspension from a credential, while there is no equivalent reversal logic for revocation, since it is not allowed.

Check status of credential

getStatus is an instance method that is called on a credential status manager initialized by createStatusManager. It is an asynchronous method that accepts a credential ID as input and returns status information for the credential.

Here is a sample call to getStatus:

const credentialStatus = await statusManager.getStatus(credentialWithStatus.id);
console.log(credentialStatus);
/*
{
  revocation: {
    statusCredentialId: 'Uz42qSDSXTcoLH7kZ6ST',
    statusListIndex: 6,
    valid: true
  },
  suspension: {
    statusCredentialId: '9kGimd8POqM88l32F9aT',
    statusListIndex: 3,
    valid: false
  }
}
*/

Schemas

There is a lot of data that is managed by this service. In this section, we will outline the schemas for each database table maintained by a given deployment.

StatusCredential

KeyDescriptionType
idID of the status credential database recordstring
credentialBitstring Status List Verifiable Credentialobject (BitstringStatusListCredential)

UserCredential

KeyDescriptionType
idID of the user credential database recordstring
issuerID of the issuer of the credentialstring
subjectID of the subject of the credentialstring
statusInfomapping from status purpose to status infoobject
statusInfo[PURPOSE].statusCredentialIdID of the status credential associated with the credential for a given purposestring
statusInfo[PURPOSE].statusListIndexposition allocated on the status credential for the credential for a given purposenumber
statusInfo[PURPOSE].validvalidity of the credential according to the status credential tracking its status for a given purposeboolean

Config

KeyDescriptionType
idID of the config database recordstring
statusCredentialSiteOriginbase URL of status credentials managed by a given deploymentstring
statusCredentialInfomapping from status purpose to status credential infoobject
statusCredentialInfo[PURPOSE].latestStatusCredentialIdID of the latest status credential to be created for a given purpose in a given deploymentstring
statusCredentialInfo[PURPOSE].latestCredentialsIssuedCounternumber of credentials issued against the latest status credential for a given purpose in a given deploymentnumber
statusCredentialInfo[PURPOSE].statusCredentialsCountertotal number of status credentials for a given purpose in a given deploymentnumber
credentialsIssuedCountertotal number of credentials issued in a given deploymentnumber

Event

KeyDescriptionType
idID of the event database recordstring
timestampISO timestamp of the moment that the event was recordedstring
credentialIdID of the credential associated with the eventstring
statusPurposename of the purpose of the credential status whose modification is being tracked by the eventrevocation | suspension (see statusPurpose here)
validvalidity of the credential that is being applied by the eventboolean

CredentialEvent

KeyDescriptionType
credentialIdID of a previously issued credential database recordstring
eventIdID of the latest status event database record for credential with ID credentialIdstring

Dependencies

Generate DID seeds

In order to generate a DID seed, you will need to use software that is capable of creating it in a format that corresponds to a valid DID document. Here is sample code that does this:

import { generateSecretKeySeed } from '@digitalcredentials/bnid';

// Set `didSeed` key to this value
const secretKeySeed = await generateSecretKeySeed();

If didMethod = web, you must also generate a DID document and host it at didWebUrl/.well-known/did.json. Here is sample code that does this:

import { decodeSecretKeySeed } from '@digitalcredentials/bnid';
import { Ed25519VerificationKey2020 } from '@digitalcredentials/ed25519-verification-key-2020';
import { X25519KeyAgreementKey2020 } from '@digitalcredentials/x25519-key-agreement-key-2020';
import * as DidWeb from '@interop/did-web-resolver';
import { CryptoLD } from '@digitalcredentials/crypto-ld';

const cryptoLd = new CryptoLD();
cryptoLd.use(Ed25519VerificationKey2020);
cryptoLd.use(X25519KeyAgreementKey2020);
const didWebDriver = DidWeb.driver({ cryptoLd });

const decodedSeed = decodeSecretKeySeed({secretKeySeed});

// Host this document at `didWebUrl`/.well-known/did.json
const didWebUrl = 'https://example.edu';
const didDocument = didWebDriver.generate({ url: didWebUrl, seed: decodedSeed });

Contribute

PRs accepted.

If editing the Readme, please conform to the standard-readme specification.

License

MIT License © 2024 Digital Credentials Consortium.

Keywords

dcc

FAQs

Package last updated on 04 Sep 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts