# VC.js

## Install

The `vc.js` library can be installed with the following command

```
npm i @transmute/vc.js --save
```

## Verifiable Credentials

The spec for the Verifiable Credential Data model can be found here: <https://www.w3.org/TR/vc-data-model/>. `vc.js` supports issuing and verifying credentials. Code examples can be found in the following sections.&#x20;

### Issue a Credential

The act of issuing a credential is creating a `proof` attribute that gets added to the credential as an attribute. The proof contains a signature from the provided key which can then later be verified.

The following code snippet provides an example of how to issue a credential.&#x20;

```typescript
import {
  JsonWebKey,
  JsonWebSignature,
  JsonWebKey2020,
} from "@transmute/json-web-signature";
import { documentLoader } from "path/somewhere";
import { verifiable } from "@transmute/vc.js";

const key = {
  id: "did:key:z6MkokrsVo8DbGDsnMAjnoHhJotMbDZiHfvxM4j65d8prXUr#z6MkokrsVo8DbGDsnMAjnoHhJotMbDZiHfvxM4j65d8prXUr",
  type: "JsonWebKey2020",
  controller: "did:key:z6MkokrsVo8DbGDsnMAjnoHhJotMbDZiHfvxM4j65d8prXUr",
  publicKeyJwk: {
    kty: "OKP",
    crv: "Ed25519",
    x: "ijtvFnowiumYMcYVbaz6p64Oz6bXwe2V_9IlCgDR_38",
  },
  privateKeyJwk: {
    kty: "OKP",
    crv: "Ed25519",
    x: "ijtvFnowiumYMcYVbaz6p64Oz6bXwe2V_9IlCgDR_38",
    d: "ZrHpIW1JBb-sK2-wzKV0mQjbxpnxjUCu151QZ9_F_Vs",
  },
};

const credential = {
  "@context": [
    "https://www.w3.org/2018/credentials/v1",
    "https://w3id.org/security/suites/jws-2020/v1",
  ],
  id: "http://example.edu/credentials/3732",
  type: ["VerifiableCredential"],
  issuer: {
    id: "did:key:z6MkokrsVo8DbGDsnMAjnoHhJotMbDZiHfvxM4j65d8prXUr",
  },
  issuanceDate: "2010-01-01T19:23:24Z",
  credentialSubject: {
    id: "did:example:ebfeb1f712ebc6f1c276e12ec21",
  },
};

const result = await verifiable.credential.create({
  credential,
  format: ["vc", "vc-jwt"],
  documentLoader: documentLoader,
  suite: new JsonWebSignature({
    key: await JsonWebKey.from(key as JsonWebKey2020),
  }),
});
```

### Verify a Credential

A credential can be verified by checking the hash of the credential with the public key component of the private key used to sign the credential. The following snippet provides the code for how to do this.&#x20;

```typescript
import {
  JsonWebKey,
  JsonWebSignature,
  JsonWebKey2020,
} from "@transmute/json-web-signature";
import { documentLoader } from "path/somewhere";
import { verifiable } from "@transmute/vc.js";
import { credential } from "path/somewhere";

const result = await verifiable.credential.verify({
  credential,
  format: ["vc", "vc-jwt"],
  documentLoader: documentLoader,
  suite: [new JsonWebSignature()],
});
```

## Verifiable Presentation

A verifiable presentation is a way to exchange verifiable credentials from a holder to a verifier over an API.&#x20;

Spec: <https://www.w3.org/TR/vc-data-model/#presentations>

More information with respect to verifiable presentations can be found with their exchange specs.

1. Traceable API: <https://github.com/w3c-ccg/traceability-interop>
2. VC-API: <https://github.com/w3c-ccg/vc-api/>
3. CHAPI: <https://github.com/w3c-ccg/credential-handler-api/>

### Issue a Verifiable Presentation

A verifiable presentation can be signed by the holder. The following is a code snippet for how to sign a verifiable presentations.

```typescript
import {
  JsonWebKey,
  JsonWebSignature,
  JsonWebKey2020,
} from "@transmute/json-web-signature";
import { documentLoader } from "path/somewhere";
import { key } from "path/somewhere";
import { verifiable } from "@transmute/vc.js";

const presentation = {
  "@context": [
    "https://www.w3.org/2018/credentials/v1",
    "https://w3id.org/security/suites/jws-2020/v1",
  ],
  type: ["VerifiablePresentation"],
  holder: {
    id: key.controller,
  },
};

const result = await verifiable.presentation.create({
  presentation,
  format: ["vp", "vp-jwt"],
  documentLoader: documentLoader,
  challenge: "123", // this is supplied by the verifier / presentation recipient
  suite: new JsonWebSignature({
    key: await JsonWebKey.from(fixtures.key as JsonWebKey2020),
  }),
});
```

### Verify a presentation

The following code snippet provides how to&#x20;

```typescript
import {
  JsonWebKey,
  JsonWebSignature,
  JsonWebKey2020,
} from "@transmute/json-web-signature";
import { documentLoader } from "path/somewhere";
import { verifiable } from "@transmute/vc.js";
import { presentation } from "path/somewhere";

const result = await verifiable.presentation.verify({
  presentation,
  format: ["vp", "vp-jwt"],
  documentLoader: documentLoader,
  challenge: "123", // this is supplied by the verifier / presentation recipient
  suite: new JsonWebSignature(),
});
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://guide.transmute.industries/vc.js/vc.js.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
