DID

Class Name Did
Extends Logger
Source did.ts
Tests did.spec.ts

The Did module allows to interact with DIDs on evan.network. As development of identity and DID handling on evan.network is an ongoing process, this document describes the current interoperability of DIDs on evan.network and can be seen as a work-in-progress state of the current implementation.


constructor

new Did(options);

Creates a new Did instance.

Parameters

  1. options - DidOptions: options for Did constructor.
  2. config - DidConfig (optional): description, defaults to 123
    • registryAddress - string (optional): contract address or ENS name for DidRegistry

Returns

Did instance

Example

const did = new Did({
  contractLoader,
  dfs,
  executor,
  nameResolver,
  signerIdentity,
  web3,
});

= Working with DID documents =

deactivateDidDocument

did.deactivateDidDocument(didToDeactivate);

Unlinks the current DID document from the given DID

Parameters

  1. did - string: DID to unlink the DID document from

Returns

Promise returns void: Resolves when done

Example

const twinIdentity = '0x1234512345123451234512345123451234512345';
const twinDid = await runtime.did.convertIdentityToDid(twinIdentity);
await runtime.did.deactivateDidDocument(twinDid);

didIsDeactivated

did.didIsDeactivated(didToCheck);

Gets the deactivation status of a DID.

Parameters

  1. did - string: DID to check

Returns

Promise returns boolean: True if the DID has been deactivated

Example

const twinIdentity = '0x1234512345123451234512345123451234512345';
const twinDid = await runtime.did.convertIdentityToDid(twinIdentity);
await runtime.did.deactivateDidDocument(twinDid);
console.log(await runtime.did.didIsDeactivated(twinDid));
// Output: true

getDidDocument

did.getDidDocument(myDid);

Get DID document for given DID. If the DID has a proof property, getDidDocument will attempt to validate the proof and throw an error if the proof is invalid.

Parameters

  1. did - string: DID to fetch DID document for.

Returns

Promise returns DidDocument: A DID document. For deactivated DIDs it returns a default DID document containing no authentication material.

Example

const identity = await runtime.verifications.getIdentityForAccount(accountsId, true);
const did = await runtime.did.convertIdentityToDid(identity);
const document = await runtime.did.getDidDocumentTemplate();
await runtime.did.setDidDocument(did, document);
const retrieved = await runtime.did.getDidDocument(did);

getService

did.getService(myDid);

Get the services from a DID document.

Parameters

  1. did - string: DID to fetch DID service for.

Returns

Promise returns DidServiceEntry[]: Array of services.

Example

const document = await runtime.did.getDidDocumentTemplate();
const identity = await runtime.verifications.getIdentityForAccount(account, true);
const did = await runtime.did.convertIdentityToDid(identity);
await runtime.did.setDidDocument(did, document);
const service = {
  id: `${did}#randomService`,
  type: `randomService-${random}`,
  serviceEndpoint: `https://openid.example.com/${random}`,
};
await runtime.did.setService(did, service);
const retrieved = await runtime.did.getService(did);

setDidDocument

did.setDidDocument(myDid, document);

Store given DID document for given DID. If the document misses the property created, it will automatically be appended. The updated property will be updated accordingly. A proof over the DID document will be generated automatically and appended to the document.

Parameters

  1. did - string: DID to store DID document for
  2. document - DidDocument: DID document to store, getDidDocumentTemplate can be used as a starting point for DID documents

Returns

Promise returns void: resolved when done

Example

const identity = await runtime.verifications.getIdentityForAccount(accountsId, true);
const did = await runtime.did.convertIdentityToDid(identity);
const document = await runtime.did.getDidDocumentTemplate();
await runtime.did.setDidDocument(did, document);

setService

did.setService(myDid, service);

Sets service in DID document. Overrides old services, so make sure to include current service if you only want to add a service.

Parameters

  1. did - string: DID name to set service for
  2. service - DidServiceEntry[] | DidServiceEntry: service or array of services to set

Returns

Promise returns void: resolved when done

Example

const document = await runtime.did.getDidDocumentTemplate();
const identity = await runtime.verifications.getIdentityForAccount(account, true);
const did = await runtime.did.convertIdentityToDid(identity);
await runtime.did.setDidDocument(did, document);
const service = {
  id: `${did}#randomService`,
  type: `randomService-${random}`,
  serviceEndpoint: `https://openid.example.com/${random}`,
};
await runtime.did.setService(did, service);

= utilities =

convertDidToIdentity

did.convertDidToIdentity(didToConvert);

Converts given DID to a evan.network identity.

Parameters

  1. did - string: a DID like “did:evan:testcore:0x000000000000000000000000000000000000001234”

Returns

Promise returns string: evan.network identity like “0x000000000000000000000000000000000000001234”

Example

const did = 'did:evan:testcore:0x000000000000000000000000000000000000001234';
const identity = await did.convertDidToIdentity(did);
console.log(identity);
// Output:
// 0x000000000000000000000000000000000000001234

convertIdentityToDid

did.convertIdentityToDid(identityToConvert);

Converts given evan.network identity hash to DID.

Parameters

  1. identity - string: evan.network identity like “0x000000000000000000000000000000000000001234”

Returns

Promise returns string: a DID like “did:evan:testcore:0x000000000000000000000000000000000000001234”

Example

const identity = '0x000000000000000000000000000000000000001234';
const did = await did.convertIdentityToDid(identity);
console.log(did);
// Output:
// did:evan:testcore:0x000000000000000000000000000000000000001234

getDidDocumentTemplate

did.getDidDocumentTemplate();

Gets a DID document for currently configured identity. Notice, that this document may a complete DID document for currently configured active identity, a part of it or not matching it at all. You can use the result of this function to build a new DID document but should extend it or an existing DID document, if your details derive from default format.

All three arguments are optional. When they are used, all of them have to be given and the result then describes a contracts DID document. If all of them are omitted the result describes an accounts DID document.

Parameters

  1. did - string (optional): contract DID
  2. controllerDid - string (optional): controller of contracts identity (DID)
  3. authenticationKey - string (optional): authentication key used for contract

Returns

Promise returns DidDocumentTemplate: template for DID document

Example

const document = await runtime.did.getDidDocumentTemplate();
console.log(JSON.stringify(document, null, 2));
// Output:
// {
//   "@context": "https://w3id.org/did/v1",
//   "id": "did:evan:testcore:0x126E901F6F408f5E260d95c62E7c73D9B60fd734",
//   "publicKey": [
//     {
//       "id": "did:evan:testcore:0x126E901F6F408f5E260d95c62E7c73D9B60fd734#key-1",
//       "type": "Secp256k1VerificationKey2018",
//       "controller": "did:evan:testcore:0x126E901F6F408f5E260d95c62E7c73D9B60fd734",
//       "ethereumAddress": "0x126E901F6F408f5E260d95c62E7c73D9B60fd734"
//     }
//   ],
//   "authentication": [
//     "did:evan:testcore:0x126E901F6F408f5E260d95c62E7c73D9B60fd734#key-1"
//   ]
// }