Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to latest OBv3 contexts, from beta through 3.0.2 #5

Merged
merged 3 commits into from
Jul 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# security-document-loader Changelog

## 3.0.0 -
### Changed
- **BREAKING**: Now includes OBv3 contexts from beta to 3.0.2

## 2.0.0 - 2023-05-17
### Changed
- **Breaking**: Update OpenBadges v3 context to `v1.0.0`.
Expand Down
56 changes: 53 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Example Isomorphic TS/JS Lib Template _(@digitalcredentials/security-document-loader)_
# JSON-LD Document Loader _(@digitalcredentials/security-document-loader)_

[![Build status](https://img.shields.io/github/workflow/status/digitalcredentials/security-document-loader/Node.js%20CI)](https://github.com/digitalcredentials/security-document-loader/actions?query=workflow%3A%22Node.js+CI%22)
[![Build status](https://img.shields.io/github/actions/workflow/status/digitalcredentials/security-document-loader/main.yml?branch=main)](https://github.com/digitalcredentials/security-document-loader/actions?query=workflow%3A%22Node.js+CI%22)
[![NPM Version](https://img.shields.io/npm/v/@digitalcredentials/security-document-loader.svg)](https://npm.im/@digitalcredentials/security-document-loader)

> A Typescript/Javascript isomorphic library template, for use in the browser, Node.js, and React Native.
> A secure and convenient JSON-LD document loader.

## Table of Contents

Expand Down Expand Up @@ -70,13 +70,63 @@ loader.addStatic('https://example.com/my-context/v1', contextObject)
const documentLoader = loader.build()
```

### Fetching From the Web
To enable fetching arbitrary contexts from the web (not recommended, if you can
avoid it):

```js
const documentLoader = securityLoader({ fetchRemoteContexts: true }).build()
```

### Supported URL Protocol Handlers

Out of the box, this library supports loading the following documents:

* Explicitly added URLs from static local caches (that is, ones that you
explicitly add via `loader.addStatic`)
* DID Documents using the `did:key` and `did:web` methods.

Additionally, if your use case allows it, you can enable `fetchRemoteContexts`,
which will add support for URLs using the `http` and `https` protocols (see
previous section).

#### Adding Custom Protocol Handlers
Sometimes you will need to add documents with other URL protocols. If you have
internal code to resolve those protocols (for example, you can fetch some
`urn:` documents from a local database), you can write a custom protocol
handler:

```js
import { securityLoader } from '@digitalcredentials/security-document-loader'

function getDocument (url) {
// Some internal function that fetches or creates documents
}

const customResolver = {
/**
* @param {object} options - Options hashmap.
* @param {string} options.url - Document URL (here `urn:...` key id)
* @returns {Promise<object>} - Resolves with key object document.
*/
async get(params: Record<string, string>): Promise<unknown> {
let document
try {
document = await getDocument(params.url)
} catch(e) {
throw new Error('NotFoundError')
}

return document
}
};

// For example, use your `getDocument` function to resolve all `urn:` URIs:
securityLoader.setProtocolHandler({protocol: 'urn', handler: customResolver})

const documentLoader = securityLoader().build()
```

## Contribute

PRs accepted.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"@digitalcredentials/did-io": "^1.0.2",
"@digitalcredentials/did-method-key": "^2.0.3",
"@digitalcredentials/ed25519-verification-key-2020": "^3.2.2",
"@digitalcredentials/open-badges-context": "^1.0.0",
"@digitalcredentials/open-badges-context": "^2.0.1",
"@digitalcredentials/x25519-key-agreement-key-2020": "^3.0.0",
"@interop/did-web-resolver": "^3.0.0",
"credentials-context": "^2.0.0",
Expand Down
8 changes: 4 additions & 4 deletions src/documentLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ export function securityLoader({ fetchRemoteContexts = false }: SecurityLoaderPa

loader.addStatic(vcStatusListCtx.CONTEXT_URL_V1, vcStatusListCtx.CONTEXT_V1);

// Open Badges v3 Context (with multiple URL aliases)
loader.addStatic(obCtx.CONTEXT_URL_V3, obCtx.CONTEXT_V3)
loader.addStatic(obCtx.CONTEXT_URL_V3_JFF_V1, obCtx.CONTEXT_V3)
loader.addStatic(obCtx.CONTEXT_URL_V3_IMS, obCtx.CONTEXT_V3)
// Open Badges v3 Contexts, includes OBv3 Beta, 3.0, 3.0.1, 3.0.2, etc.
for (const [url, contextBody] of obCtx.contexts) {
loader.addStatic(url, contextBody)
}

loader.setDidResolver(resolver);

Expand Down
2 changes: 1 addition & 1 deletion test/documentLoader.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from 'chai'
import { httpClientHandler, securityLoader } from "../src";
import { httpClientHandler, securityLoader } from '../src';

describe('documentLoader', () => {
it('should load a document', async () => {
Expand Down