Skip to content

Initial #1

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

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# bedrock-module-template-http ChangeLog
# bedrock-did-resolver-http ChangeLog

## 1.0.0 - TBD

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# bedrock-module-template-http
# bedrock-did-resolver-http
11 changes: 6 additions & 5 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
import bedrock from 'bedrock';
const {config} = bedrock;

const namespace = 'module-template-http';
const namespace = 'bedrock-did-resolver-http';
const cfg = config[namespace] = {};

const basePath = '/foo';
cfg.routes = {
basePath
};
// support did:key and veres one keys by default
cfg.supportedMethods = ['key', 'v1'];

const basePath = '/1.0/resolve/identifiers/:did';
cfg.routes = {basePath};
55 changes: 51 additions & 4 deletions lib/http.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,61 @@
/*!
* Copyright (c) 2021 Digital Bazaar, Inc. All rights reserved.
* Copyright (c) 2022 Digital Bazaar, Inc. All rights reserved.
*/
import {asyncHandler} from 'bedrock-express';
import bedrock from 'bedrock';
import {didIo} from 'bedrock-did-io';
import logger from './logger';
const {config} = bedrock;

bedrock.events.on('bedrock-express.configure.routes', app => {
const {routes} = config['module-template-http'];
app.post(
const {routes, supportedMethods} = config['bedrock-did-resolver-http'];
app.get(
routes.basePath,
asyncHandler(async (/*req, res*/) => {
asyncHandler(async (req, res) => {
// this can be resolution or dereferencing meta data
const metaData = {};
const resolutionResult = {
'@context': 'https://w3id.org/did-resolution/v1',
didDocument: {},
didDocumentMetadata: null,
didResolutionMetadata: metaData
};
const {did} = req.params;
const parsedDid = new URL(did);
// if there is a fragment `#` or a service `?service`
// then we are dereferencing a did url
const didUrl = (parsedDid.hash || parsedDid.search);
if(didUrl) {
// add a did dereferencing meta
resolutionResult.didDereferencingMetadata = metaData;
// delete the resolution metadata
delete resolutionResult.didResolutionMetadata;
}
// the second value should always be the method
const [prefix, method, id] = did.split(':');
// a did must have a did prefix, method, and id
if(!((prefix === 'did') && method && id)) {
metaData.error = didUrl ? 'invalidDidUrl' : 'invalidDid';
return res.status(400).json(resolutionResult);
}
if(!supportedMethods.includes(method)) {
//FIXME this might not be the right error code
metaData.error = 'representationNotSupported';
return res.status(406).json(resolutionResult);
}
try {
resolutionResult.didDocument = await didIo.get({did});
} catch(e) {
//FIXME the did resolver error could contain invalidDid, notFound,
//or representationNotSupported we need to check for those errors
//and newer errors and add that information in the future
logger.error('DID Resolution error', {error: e});
// the spec doesn't seem to handle what occurs if the
// did resolver fails for reasons unrelated to the did such
// as database timeouts.
metaData.error = 'InternalError';
return res.status(500).json(resolutionResult);
}
res.json(resolutionResult);
}));
});
11 changes: 11 additions & 0 deletions lib/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

/*!
* Copyright (c) 2022 Digital Bazaar, Inc. All rights reserved.
*/
'use strict';

import bedrock from 'bedrock';

const logger = bedrock.loggers.get('app').child('bedrock-did-resolver-http');

export default logger;
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "bedrock-module-template-http",
"name": "bedrock-did-resolver-http",
"version": "0.0.1-0",
"description": "Bedrock HTTP API",
"main": "./lib",
Expand All @@ -8,7 +8,7 @@
},
"repository": {
"type": "git",
"url": "https://github.com/digitalbazaar/bedrock-module-template-http"
"url": "https://github.com/digitalbazaar/bedrock-did-resolver-http"
},
"keywords": [
"bedrock"
Expand All @@ -19,15 +19,16 @@
"url": "https://digitalbazaar.com"
},
"bugs": {
"url": "https://github.com/digitalbazaar/bedrock-module-template-http/issues"
"url": "https://github.com/digitalbazaar/bedrock-did-resolver-http/issues"
},
"homepage": "https://github.com/digitalbazaar/bedrock-module-template-http",
"homepage": "https://github.com/digitalbazaar/bedrock-did-resolver-http",
"dependencies": {
"bedrock-did-io": "^5.0.0",
"esm": "^3.2.25"
},
"peerDependencies": {
"bedrock": "^4.1.1",
"bedrock-express": "^3.2.0"
"bedrock": "^4.4.3",
"bedrock-express": "^6.3.0"
},
"directories": {
"lib": "./lib"
Expand Down
8 changes: 4 additions & 4 deletions test/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "bedrock-module-template-http-test",
"name": "bedrock-did-resolver-http-test",
"version": "0.0.1-0",
"private": true,
"scripts": {
Expand All @@ -12,7 +12,7 @@
"bedrock": "^4.1.1",
"bedrock-express": "^3.2.0",
"bedrock-https-agent": "^2.0.0",
"bedrock-module-template-http": "file:..",
"bedrock-did-resolver-http": "file:..",
"bedrock-mongodb": "^8.2.0",
"bedrock-server": "^2.7.0",
"bedrock-test": "^5.3.2",
Expand All @@ -22,10 +22,10 @@
"nyc": {
"excludeNodeModules": false,
"include": [
"node_modules/bedrock-module-template-http/**"
"node_modules/bedrock-did-resolver-http/**"
],
"exclude": [
"node_modules/bedrock-module-template-http/node_modules/**"
"node_modules/bedrock-did-resolver-http/node_modules/**"
]
}
}
2 changes: 1 addition & 1 deletion test/test.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const {config} = require('bedrock');
const path = require('path');

// MongoDB
config.mongodb.name = 'bedrock_module_template_http_test';
config.mongodb.name = 'bedrock_did_resolver_http_test';
config.mongodb.dropCollections.onInit = true;
config.mongodb.dropCollections.collections = [];

Expand Down
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
const bedrock = require('bedrock');
require('bedrock-https-agent');
require('bedrock-mongodb');
require('bedrock-module-template-http');
require('bedrock-did-resolver-http');

require('bedrock-test');
bedrock.start();