The SDK must instantiate a for your application before it can be used.
Each DWN's protocol must also be associated with an initial . Therefore, an DID must be added to the DWN before the protocol is configured.
This logic is typically handled in the sdk/bootstrap.jsfile of your project.
Instantiate the DWN
The first step in the initialization process is to instantiate the DWN.
import { IDDwn } from '@dwn-protocol/id-sdk';
If you're using the CDN, you can skip this step. Whenever IDDwn is referenced in the following examples, use window.IDDwn instead.
Create the initial DID
Once the DWN has been instantiated, an initial DID must be created. This involves checking for the existence of the initial DID, and creating it if there isn't one.
// init and connect to the id++ secure relayer network responding with your DID
const { iddwn, did: identity } = await IDDwn.connect();
// *OPTIONAL* dynamically create DIDs on demand
// default here will create a new did and automatically include the live relayer
// dwnEndpoints
const identity = await sdk.did.create({
sync: 'off' | '60s' // default sync is on
});
// adding the following serviceOptions override the live relayer endpoints
// if you are running a local relayer
const identity = await sdk.did.create({
serviceOptions: {
dwnEndpoints: ["http://localhost:8085"],
},
sync: 'off' | '60s' // default sync is on
});
// Note: using these methods will automatically store the dids in the dwn kms and be
// retrieved on next time the app connects.
Configure the message protocol you want to use
With the initial DID added to the DWN, the protocol is ready to be installed.
// configure your protocol definition
const { protocol } = await sdk.dwn.protocols.configure({
message: { definition: protocolDef } });
await protocol.send(identity);
// query the dwn network for configured protocols
const { protocols } = await sdk.dwn.protocols.query({
message: {
filter: {
protocol: "http://message-protocol.xyz",
},
},
});
// iterate through all the protocols that have been configured
for (const proto of protocols) {
console.log('[PROTOCOL]', proto.definition.protocol);
}