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.

Returns

Did instance

Example

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

= Working with DID documents =

setDidDocument

did.setDidDocument(did, document);

Store given DID document for given DID.

Parameters

  1. did - string: DID to store DID document for
  2. document - any: 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);

getDidDocument

did.getDidDocument([did]);

Get DID document for given DID.

Parameters

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

Returns

Promise returns any: a DID document that MAY resemble DidDocumentTemplate format

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);

setService

did.setService(service[, did]);

Sets service in DID document.

Parameters

  1. did - string: DID name to set service for
  2. service - DidServiceEntry[] | DidServiceEntry: service 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);

getService

did.getService([did]);

Get service from DID document.

Parameters

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

Returns

Promise returns DidServiceEntry[] | DidServiceEntry: service

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);

= utilities =

convertDidToIdentity

did.convertDidToIdentity(did);

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(identity);

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 account/identity pair. 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": [
//         "Secp256k1SignatureVerificationKey2018",
//         "ERC725ManagementKey"
//       ],
//       "publicKeyHex": "045adfd502c0bc55f4fcb90eea36368d7e19c5b3045aa6f51dfa3699046e9751251d21bc6bdd06c1ff0014fcbbf9f1d83c714434f2b33d713aaf46760f2d53f10d"
//     }
//   ],
//   "authentication": [
//     "did:evan:testcore:0x126E901F6F408f5E260d95c62E7c73D9B60fd734#key-1"
//   ]
// }