npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@did-core/data-model

v0.1.1-unstable.15

Published

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

Downloads

8,818

Readme

@did-core/data-model

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

Usage

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

// 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

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

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