Issue a Credential

Grant credentials to users

sampleIssueCredential

The sampleIssueCredential function demonstrates how to issue a credential using the DICE ID SDK.

Importing Required Modules

The sample code begins with importing the necessary modules and dependencies. These modules are required for executing the SDK functionality. The following modules are imported:

const { CREDENTIAL_DEF, ISSUER_CONNECTION_ID } = require('./appconfig');
const { SCHEMA_ID, ISSUER_DID, SCHEMA_NAME } = require('./appconfig');
const { WALLET_TOKEN } = require('./appconfig');
const { ISSUER_XAPIKEY } = require('./appconfig');

Sample Data

The sample code defines an array called cred_def_values, which contains the attribute-value pairs for the credential being issued. Each element in the array represents a specific attribute and its corresponding value.

const cred_def_values = [ {"name":"Id",
                               "value":"myid"},
                              {"name":"TypeOfDoc",
                               "value":"Phone Bill"},
                              {"name":"ValueOfDoc",
                               "value":"Bill Amount 700"},
                              {"name":"Validity",
                               "value":"always"},
                              {"name":"Provider",
                               "value":"BSNL"}]

The sampleIssueCredential function is responsible for issuing a credential using the diceidsdk module. It performs the following steps:

  1. Calls the issuer.sendCredOffer function to send a credential offer:

diceidsdk.issuer.sendCredOffer(ISSUER_CONNECTION_ID, CREDENTIAL_DEF, cred_def_values, ISSUER_XAPIKEY)
  • ISSUER_CONNECTION_ID: The ID of the connection with the credential recipient.

  • CREDENTIAL_DEF: The credential definition ID.

  • cred_def_values: The attribute-value pairs for the credential.

  • ISSUER_XAPIKEY: The API key for the issuer.

  1. Handles the response or error:

.then(resp => console.log("cred output"))
.catch(err => console.log("error=", err))
  • If the operation is successful, it logs the credential output.

  • If an error occurs, it logs the error message.

Running the Code

Step 1: Add the sampleIssueCredential function to your code. Copy the function code provided and paste it after importing the diceidsdk module.

Step 2: Run the function by invoking it in your code. For example:

node sampleIssueCredential.js

Step 3: Make sure you have provided appropriate values for the required variables (ISSUER_CONNECTION_ID, CREDENTIAL_DEF, cred_def_values, ISSUER_XAPIKEY) and have imported the necessary configuration from the appconfig.js file.

Step 4: The function will then use the sendCredOffer API to send a credential offer with the provided parameters. The response from the API will be logged to the console as "Credential output".

Last updated