import {JsonLdDocumentLoader} from 'jsonld-document-loader';const loader = new JsonLdDocumentLoader();
addStatic(url, document)
The addStatic() method allows developers to load fixed static contexts and
documents, to ensure known versions and contents, without going out to the
network. The context is cloned when added.
For example, to add support for the DID Core context, the VC context, and crypto
suite specific contexts:
import cred from 'credentials-context';import didContext from 'did-context';import ed25519Ctx from 'ed25519-signature-2020-context';const {contexts: credentialsContexts, constants: {CREDENTIALS_CONTEXT_V1_URL}} = cred;const jdl = new JsonLdDocumentLoader();jdl.addStatic(ed25519Ctx.CONTEXT_URL, ed25519Ctx.CONTEXT);jdl.addStatic( didContext.constants.DID_CONTEXT_URL, didContext.contexts.get(didContext.constants.DID_CONTEXT_URL));jdl.addStatic( CREDENTIALS_CONTEXT_V1_URL, credentialsContexts.get(CREDENTIALS_CONTEXT_V1_URL));const
addDocuments({documents})
Uses addStatic() to add many documents from an iterable object that returns
values of the form [url, document]. Can be used directly with a Map
associating URLs to documents.
import {contexts as credContexts} from '@digitalbazaar/credentials-context';const jdl = new JsonLdDocumentLoader();jdl.addDocuments({documents: credContexts});
setDidResolver()
To add support for resolving DIDs and DID-related key ids:
import * as didKey from '@digitalbazaar/did-method-key';import {CachedResolver} from '@digitalbazaar/did-io';const cachedResolver = new CachedResolver();const jdl = new JsonLdDocumentLoader();cachedResolver.use(didKey.driver());jdl.setDidResolver(cachedResolver);// Now you can resolve did:key type DIDs and key objectsconst verificationKeyId = 'did:key:z6MkuBLrjSGt1PPADAvuv6rmvj4FfSAfffJotC6K8ZEorYmv#z6MkuBLrjSGt1PPADAvuv6rmvj4FfSAfffJotC6K8ZEorYmv';await jdl.documentLoader(verificationKeyId);// ->{ documentUrl: 'did:key:z6MkuBLrjSGt1PPADAvuv6rmvj4FfSAfffJotC6K8ZEorYmv#z6MkuBLrjSGt1PPADAvuv6rmvj4FfSAfffJotC6K8ZEorYmv',
setProtocolHandler()
You can add support for loading https-based JSON-LD contexts (a common
case) by using your own loader or the one that comes with the
jsonld package:
import * as jsonld from 'jsonld';jdl.setProtocolHandler({protocol: 'https', handler: jsonld.documentLoader});
You can also add support for a custom protocol handler: