# DIDs

`Did`s are a core component of the Cloud Agent, they are the identifiers that the cloud agent uses when engaging in DI operations (e.g. issuing VCs).

## Create a DID

The Cloud Agent service supports multiple DID types (DID methods) to be created. All DIDs are created with the `createDid` mutation, with different inputs depending on the DID method. A private human readable and searchable `alias` can be assigned to DIDs for record-keeping purposes.

### Cheqd DID

`did:cheqd` DIDs can be created by the cloud agent and written permanently to the cheqd ledger as a production-ready DID. The `network` parameter determines whether the DID is created on testnet or mainnet.

```graphql
mutation MyMutation {
  createDid(
    input: {
      alias: "MyCheqdDID"
      createCheqdDidOptions: {
        network: TESTNET
      }
    }
  ) {
    did
    alias
    methodData {
      ... on CheqdDidMethodData {
        network
      }
    }
    createdTimestamp
    primary
  }
}
```

### Key DID

Alternatively, `did:key` DIDs are a basic no-cost DID that the cloud agent can create, ideal for development. The `keyType` parameter determines which cryptographic key type is used for the DID.

```graphql
mutation MyMutation {
  createDid(
    input: {
      alias: "MyKeyDID"
      createKeyDidOptions: {
        keyType: ED25519
      }
    }
  ) {
    did
    alias
    methodData {
      ... on KeyDidMethodData {
        keyType
      }
    }
    createdTimestamp
    primary
  }
}
```

## Set a primary DID

A Cloud Agent DID can be set the 'primary' DID for the agent, unsetting any existing primary DID. Setting a primary DID is purely for referential and record-keeping purposes when interacting with the Cloud Agent.

```graphql
mutation MyMutation {
  setPrimaryDid(
    did: "did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK"
  ) {
    did
    alias
    primary
    modifiedTimestamp
  }
}
```

## Update a DID

A DID's metadata, including its alias and archived status can be updated.

```graphql
mutation MyMutation {
  updateDid(
    did: "did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK"
    input: {
      updateAlias: "UpdatedAlias"
      updatedArchived: false
    }
  ) {
    did
    alias
    archived
    modifiedTimestamp
  }
}
```

## View DIDs

DIDs created by the cloud agent can be listed, using pagination and filtering options. This query returns a `nextToken` which can be passed into the same query in order to fetch subsequent pages.

```graphql
query MyQuery {
  dids(
    page: {
      nextToken: null
    }
    filter: {
      methods: [KEY, CHEQD]
      archived: false
    }
  ) {
    items {
      did
      alias
      methodData {
        ... on KeyDidMethodData {
          keyType
        }
        ... on CheqdDidMethodData {
          network
        }
      }
      primary
      createdTimestamp
    }
    nextToken
  }
}
```

Alternatively, a single DID can be viewed, referenced by its unique identifier.

```graphql
query MyQuery {
  did(
    did: "did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK"
  ) {
    did
    alias
    methodData {
      ... on KeyDidMethodData {
        keyType
      }
    }
    primary
    archived
    createdTimestamp
  }
}
```

The current primary DID for the agent can be viewed with a standalone query.

```graphql
query MyQuery {
  primaryDid {
    did
    alias
    methodData {
      ... on KeyDidMethodData {
        keyType
      }
      ... on CheqdDidMethodData {
        network
      }
    }
    primary
    createdTimestamp
  }
}
```
