Introduction
The client SDK packages include an API-agnostic library for verifying and issuing credentials, as well as modules to simplify API interaction.
Currently, two blockchains are supported:
- Dock is a blockchain built on Substrate. It facilitates the use of Verifiable Credentials Data Model 1.0 compliant documents and the creation/managing of W3C spec compliant DIDs, among other things.
- Cheqd is a blockchain built with the Cosmos SDK. It provides the trust and payment infrastructure necessary for the creation of Self-Sovereign Identity (SSI), eID, and digital credential ecosystems.
Overall, there're five packages located in the GitHub repository:
@docknetwork/credential-sdk
- An API-agnostic Javascript library for working with Verifiable Credentials, DIDs, Claim Deduction, and more.@docknetwork/dock-blockchain-api
- A Javascript library built with PolkadotJS, for use with the Dock Substrate Node or our public main/test networks.@docknetwork/dock-blockchain-modules
- A JavaScript library designed for handling credential SDK elements (DIDS, Accumulators, etc) on the Dock blockchain.@docknetwork/cheqd-blockchain-api
- A Javascript library built atop of@cheqd/sdk
that allows to interact with theCheqd
blockchain.@docknetwork/cheqd-blockchain-modules
- A JavaScript library created for managing credential SDK components such as DIDs, accumulators etc on the Cheqd blockchain.
Dock
Installation
Installing the SDK is straightforward. We use NPM, and the source is also available on GitHub (links below). To install via NPM or Yarn, run:
npm install @docknetwork/credential-sdk @docknetwork/dock-blockchain-modules @docknetwork/dock-blockchain-api
or
yarn add @docknetwork/credential-sdk @docknetwork/dock-blockchain-modules @docknetwork/dock-blockchain-api
Once the package and dependencies are installed, you can import it as an ES6/CJS module. The complete source for the SDK can be found at GitHub along with tutorials.
Running a Node
The simplest way to run a node is to use the script provided by the GitHub repository. It requires Docker to be installed.
bash scripts/run_dock_node_in_docker
Importing
In this tutorial series, we will use Node.js with Babel for ES6 support. This code will also work in browsers once transpiled. To start, import the Dock SDK. You can import the DockAPI
class and instantiate your object:
// Import the Dock SDK
import { DockAPI } from "@docknetwork/dock-blockchain-api";
const dock = new DockAPI();
We will also import shared constants across each tutorial, such as the node address and account secret:
// Import shared variables
import { address, secretUri } from "./shared-constants";
Create the shared-constants.js
file with the following contents:
export const address = "ws://localhost:9944"; // WebSocket address of your Dock node
export const secretUri = "//Alice"; // Account secret in URI format, for local testing
Connecting to a Node
With the required packages and variables imported, we can connect to our node. If you don't have a local testnet running, go to Docker Substrate for setup instructions. You could also use the Dock testnet if you have an account with sufficient funds. Begin by creating the following method:
export async function connectToNode() {}
Initialize the SDK to connect to the node with the supplied address and create a keyring to manage accounts:
// Initialize the SDK and connect to the node
await dock.init({ address });
console.log("Connected to the node and ready to go!");
Creating an Account
To write to the chain, you need to set up an account. Read operations are possible without an account, but for our examples, you'll need one. Accounts can be generated using the dock.keyring
object with methods such as URI, mnemonic phrase, and raw seed. For more details, see the Polkadot keyring documentation.
Use the URI secret //Alice
for local testnet work. Add this code after dock.init
:
// Create an Alice account for our local node using the dock keyring.
const account = dock.keyring.addFromUri(secretUri);
dock.setAccount(account);
// Ready to transact
console.log("Account set and ready to go!");
Basic Usage
To make the API object connect to the node, call the init
method with the WebSocket RPC endpoint of the node:
await dock.init({ address });
Disconnect from the node with:
await dock.disconnect();
Set the account to send transactions and pay fees:
const account = dock.keyring.addFromUri(secretUri);
dock.setAccount(account);
Retrieve the account:
dock.getAccount();
Send a transaction using signAndSend
:
const res = await dock.signAndSend(transaction);
Instantiate Dock modules with DockCoreModules
:
import { DockCoreModules } from "@docknetwork/dock-blockchain-modules";
const dockModules = new DockCoreModules(dock);
For the DID module:
const didModule = dockModules.did;
For the accumulator module:
const accumulator = dockModules.accumulator;
Cheqd
Installation
As with Dock, the process is simple. Use NPM to install:
npm install @docknetwork/credential-sdk @docknetwork/cheqd-blockchain-modules @docknetwork/cheqd-blockchain-api
or Yarn:
yarn add @docknetwork/credential-sdk @docknetwork/cheqd-blockchain-modules @docknetwork/cheqd-blockchain-api
The complete source for the SDK is available at GitHub and tutorials at GitHub Tutorials.
Running a Node
Use the provided script, requiring Docker:
CHEQD_MNEMONIC="steak come surprise obvious remain black trouble measure design volume retreat float coach amused match album moment radio stuff crack orphan ranch dose endorse" bash scripts/run_cheqd_node_in_docker
Importing
Similarly, use Node.js with Babel and import the Cheqd SDK:
// Import the Cheqd SDK
import { CheqdAPI } from "@docknetwork/cheqd-blockchain-api";
const cheqd = new CheqdAPI();
Import shared constants:
import { url, mnemonic } from "./shared-constants";
Create shared-constants.js
:
export const url = "http://localhost:26657"; // RPC URL of your Cheqd node
export const mnemonic =
"steak come surprise obvious remain black trouble measure design volume retreat float coach amused match album moment radio stuff crack orphan ranch dose endorse"; // Mnemonic for testing
Connecting to a Node
Initialize and connect to the node using the SDK:
await cheqd.init({ url, mnemonic });
console.log("Connected to the node and ready to go!");
Disconnect from the node:
await cheqd.disconnect();
Send a transaction:
const res = await cheqd.signAndSend(transaction);
Instantiate Cheqd modules:
import { CheqdCoreModules } from "@docknetwork/cheqd-blockchain-modules";
const cheqdModules = new CheqdCoreModules(cheqd);
For interacting with the DID module:
const didModule = cheqdModules.did;
For the accumulator module:
const accumulator = cheqdModules.accumulator;