# @did-core/data-model

> ``` npm i @did-core/data-model --save ```

Latest version **0.1.1-unstable.15** (published 2021-06-30) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @did-core/data-model
pnpm add @did-core/data-model
yarn add @did-core/data-model
bun add @did-core/data-model
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.1-unstable.15 |
| Published | 2021-06-30 |
| First published | 2020-07-19 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=10 |
| Dependencies | 1 |
| Unpacked size | 218 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 9 |
| Author | Orie Steele |
| Maintainers | or13, transmute-ci |

## Links

- npm: https://www.npmjs.com/package/@did-core/data-model
- Repository: https://github.com/transmute-industries/did-core
- Homepage: https://github.com/transmute-industries/did-core/tree/main/packages/data-model
- Issues: https://github.com/transmute-industries/did-core/issues
- npm.io page: https://npm.io/package/@did-core/data-model

## Dependencies (1)

- [factory.ts](https://npm.io/package/factory.ts.md) ^0.5.1

## Recent versions

- 0.1.1-unstable.15 (latest) — 2021-06-30
- 0.1.1-unstable.14 — 2021-06-30
- 0.1.1-unstable.13 — 2021-05-31
- 0.1.1-unstable.12 — 2021-05-31
- 0.1.1-unstable.11 — 2021-05-19
- 0.1.1-unstable.10 — 2021-05-08
- 0.1.1-unstable.9 — 2021-02-23
- 0.1.1-unstable.8 — 2021-02-21
- 0.1.1-unstable.7 — 2021-02-21
- 0.1.1-unstable.6 — 2021-02-21
- 0.1.1-unstable.5 — 2021-02-21
- 0.1.1-unstable.4 — 2021-02-21
- 0.1.1-unstable.3 — 2021-02-20
- 0.1.1-unstable.2 — 2021-02-20
- 0.1.1-unstable.1 — 2021-02-13
- … 3 more at https://npm.io/package/@did-core/data-model/versions

## README

# @did-core/data-model

```
npm i @did-core/data-model --save
```

## Usage

```ts
import { factory, DidDocument } from '@did-core/data-model';
import { representation } from '@did-core/did-ld-json';

const didDocument: DidDocument = factory.build({
  entries: {
    id: 'did:example:123',
  },
});
```

## For use with Verifiable Credentials

```ts
// Add support for production and consumption for the JSON-LD Representation
didDocument.addRepresentation({ 'application/did+ld+json': representation });

// JSON-LD requires `@context` and that all terms be defined by it.
try {
  await didDocument.produce('application/did+ld+json');
} catch (e) {
  expect(e.message).toBe('@context is required and not present.');
}

// Add `@context` to the Abstract Data Model
didDocument.assign({
  '@context': 'https://www.w3.org/ns/did/v1',
});

// Produce JSON-LD
const serialization = await didDocument.produce('application/did+ld+json');
expect(JSON.parse(serialization.toString())).toEqual({
  '@context': 'https://www.w3.org/ns/did/v1',
  id: 'did:example:123',
});

// What about unregistered properties?
const didDocument = factory.build({
  entries: {
    '@context': ['https://www.w3.org/ns/did/v1'],
    id: 'did:example:123',
    '🔥': '💩',
  },
});
didDocument.addRepresentation({ 'application/did+ld+json': representation });

// JSON-LD Production fails when `@context` does not define all properties
try {
  await didDocument.produce('application/did+ld+json');
} catch (e) {
  expect(e.message).toBe('@context does not define: 🔥');
}

// Add context so your DID Document works with the open world model of verifiable credentials...
didDocument.assign({
  '@context': [
    'https://www.w3.org/ns/did/v1',
    {
      '🔥': 'https://en.wikipedia.org/wiki/Open-world_assumption',
    },
  ],
});

// Produce JSON-LD that works with Verifiable Credentials
const serialization = await didDocument.produce('application/did+ld+json');
expect(JSON.parse(serialization.toString())).toEqual({
  '@context': [
    'https://www.w3.org/ns/did/v1',
    {
      '🔥': 'https://en.wikipedia.org/wiki/Open-world_assumption',
    },
  ],
  id: 'did:example:123',
  '🔥': '💩',
});
```

## What about other representations?

### application/did+dag+cbor

```ts
import { factory } from '@did-core/data-model';
import { representation } from '@did-core/did-dag-cbor';

const serialization = await factory
  .build({ entries: { id: 'did:example:123' } })
  .addRepresentation({ 'application/did+dag+cbor': representation })
  .produce('application/did+dag+cbor');

expect(serialization.toString('hex')).toBe(
  'a16269646f6469643a6578616d706c653a313233'
);
```

### application/did+json

```ts
import { factory } from '@did-core/data-model';
import { representation } from '@did-core/did-json';

const didDocument = await factory
  .build()
  .addRepresentation({ 'application/did+json': representation })
  .consume(
    'application/did+json',
    // be careful what you consume!
    Buffer.from(
      `{"id": "did:example:123","__proto__":{"isAdmin": "Let json be json!"}}`
    )
  );
const serialization = await didDocument
  // be careful what you assign!
  .assign({ 'this is safe': 'right guys...?' })
  .produce('application/did+json');

// JSON only requires `id` be present...
expect(JSON.parse(serialization.toString())).toEqual({
  id: 'did:example:123',
  'this is safe': 'right guys...?',
});

// prototype pollution will succeeed if you are not careful...
// expect((didDocument.entries as any).isAdmin).toBe('Let json be json!');
```

---
_Source: https://npm.io/package/@did-core/data-model · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
