GithubHelp home page GithubHelp logo

veramolabs / credential-ld-example Goto Github PK

View Code? Open in Web Editor NEW
4.0 2.0 3.0 951 KB

Sample code for issuing and verifying a JSON-LD credential with Veramo

Home Page: https://veramo.io

License: MIT License

TypeScript 98.61% JavaScript 1.39%
veramo json-ld verifiable-credentials did ed25519signature2018

credential-ld-example's Introduction

Veramo credential-LD sample (with did:ethr)

Sample code for issuing and verifying a JSON-LD credential with Veramo. See veramo.io for an introduction to Veramo.

In this version of the example we added the steps to create and verify a JSON-LD credentials with a did:ethr.

Usage

# install dependencies
yarn install

# run src/index.ts
yarn start

# or run the tests 
yarn test

src/setup.ts shows how to create a minimalistic agent that can manage did:key identifiers in memory and use them to issue credentials.

src/credential-flow.ts shows how to use that agent to create a DID and issue and verify a credential.

What's going on

Out of the box, Veramo works with JWT credentials and presentations, but when you use the CredentialIssuerLD plugin from @veramo/credential-ld your agent gains the ability to work with JSON-LD credentials and presentations.

In this sample project, the agent creates a did:key identifier (which automatically creates the corresponding signing key), and then uses that as the issuer of a credential which it later verifies.

Creating a credential as JSON-LD should be as simple as calling agent.createVerifiableCredential() and using proofFormat: 'lds' in the options.

const verifiableCredential = await agent.createVerifiableCredential({
  credential: {
    '@context': [/*add your custom context URIs here*/],
    issuer: issuer.did,
    credentialSubject: {
      // ...
    }
  },
  proofFormat: 'lds' // this triggers the use of LD Signatures as proof
})

There are, however, extra things to consider when working with JSON-LD.

JSON-LD requires all the contexts to make sense, so you will have to make sure that all the properties you use in the credential payload are defined in one of the entries you add to @context. The properties defined by the VC data model are automatically covered.

The context URIs you add to your credential[@context] array have to be resolvable by the CredentialIssuerLD plugin. This can be done by adding extra mappings URI => context definition to the contextMaps constructor param for the plugin.

Keep in mind that the same mappings need to be available to the verifier when credentials are verified, otherwise the LD signature will not match. Typically, this is done by having issuers and verifiers agree in advance, but this is out of scope here.

In this example, the same agent acts as both issuer and verifier, so the mappings are identical.

Did:ethr

To make it work with a did:ethr, you need to add VeramoEcdsaSecp256k1RecoverySignature2020 to your agent setup. Alternatively, you could call didManagerAddKey to add a Ed25519 key to your did:ethr DID document, but that's probably more complicated.

Contributing

Go to veramo on github. Contributions are welcome ;)

Join our discord for discussions.

credential-ld-example's People

Contributors

ajmasta avatar mirceanis avatar renovate-bot avatar renovate[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

credential-ld-example's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/test.yml
  • actions/checkout v4
  • actions/setup-node v4
  • ubuntu 22.04
npm
package.json
  • @types/node ^20.0.0
  • @veramo/core ^3.1.6-next.149
  • @veramo/credential-ld ^3.1.6-next.149
  • @veramo/credential-w3c ^3.1.6-next.149
  • @veramo/did-manager ^3.1.6-next.149
  • @veramo/did-provider-ethr ^3.1.6-next.149
  • @veramo/did-provider-key ^3.1.6-next.149
  • @veramo/did-resolver ^3.1.6-next.149
  • @veramo/key-manager ^3.1.6-next.149
  • @veramo/kms-local ^3.1.6-next.149
  • @babel/core 7.24.4
  • @babel/preset-env 7.24.4
  • @babel/preset-typescript 7.24.1
  • @types/jest 29.5.12
  • babel-jest 29.7.0
  • jest 29.7.0
  • typescript 5.4.4

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.