@interop/did-web-resolver 2.2.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,6 +8,7 @@ var ed25519Context = require('ed25519-signature-2020-context');
8
8
  var x25519Context = require('x25519-key-agreement-2020-context');
9
9
  var didContext = require('did-context');
10
10
  var bnid = require('@digitalcredentials/bnid');
11
+ var whatwgUrl = require('whatwg-url');
11
12
 
12
13
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
13
14
 
@@ -54,7 +55,7 @@ function didFromUrl ({ url } = {}) {
54
55
 
55
56
  let parsedUrl;
56
57
  try {
57
- parsedUrl = new URL(url);
58
+ parsedUrl = new whatwgUrl.URL(url);
58
59
  } catch (error) {
59
60
  throw new TypeError(`Invalid url: "${url}".`)
60
61
  }
@@ -82,22 +83,25 @@ function urlFromDid ({ did } = {}) {
82
83
  // const [didResource, query] = didUrl.split('?')
83
84
 
84
85
  // eslint-disable-next-line no-unused-vars
85
- const [_did, _web, urlNoProtocol] = didUrl.split(':');
86
+ const [_did, _web, urlNoProtocol, ...pathFragments] = didUrl.split(':');
87
+
88
+ if (urlNoProtocol.includes('/')) {
89
+ throw new TypeError(`Cannot construct url from did: "${did}". domain-name cannot contain a path.`)
90
+ }
86
91
 
87
92
  let parsedUrl;
88
93
  try {
89
94
  // URI-decode the url (in case it contained a port number,
90
95
  // for example, `did:web:localhost%3A8080`
91
- parsedUrl = new URL('https://' + decodeURIComponent(urlNoProtocol));
96
+ parsedUrl = new whatwgUrl.URL('https://' + decodeURIComponent(urlNoProtocol));
92
97
  } catch (error) {
93
98
  throw new TypeError(`Cannot construct url from did: "${did}".`)
94
99
  }
95
100
 
96
- if (!parsedUrl.pathname || parsedUrl.pathname === '/') {
101
+ if (pathFragments.length === 0) {
97
102
  parsedUrl.pathname = '/.well-known/did.json';
98
103
  } else {
99
- const pathFragments = parsedUrl.pathname.split('/');
100
- parsedUrl.pathname = pathFragments.map(decodeURIComponent).join('/');
104
+ parsedUrl.pathname = pathFragments.map(decodeURIComponent).join('/') + '/did.json';
101
105
  }
102
106
 
103
107
  if (hashFragment) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@interop/did-web-resolver",
3
3
  "description": "A did:web method Decentralized Identifier (DID) resolver for the did-io library.",
4
- "version": "2.2.0",
4
+ "version": "3.0.0",
5
5
  "author": {
6
6
  "name": "Dmitri Zagidulin",
7
7
  "url": "https://github.com/dmitrizagidulin/"
@@ -48,6 +48,7 @@
48
48
  "@digitalcredentials/http-client": "^1.2.2",
49
49
  "did-context": "^3.1.1",
50
50
  "ed25519-signature-2020-context": "^1.1.0",
51
+ "whatwg-url": "^11.0.0",
51
52
  "x25519-key-agreement-2020-context": "^1.0.0"
52
53
  },
53
54
  "devDependencies": {
@@ -4,6 +4,7 @@ import ed25519Context from 'ed25519-signature-2020-context'
4
4
  import x25519Context from 'x25519-key-agreement-2020-context'
5
5
  import didContext from 'did-context'
6
6
  import { decodeSecretKeySeed } from '@digitalcredentials/bnid'
7
+ import { URL } from 'whatwg-url'
7
8
 
8
9
  const { VERIFICATION_RELATIONSHIPS } = didIo
9
10
 
@@ -53,7 +54,11 @@ export function urlFromDid ({ did } = {}) {
53
54
  // const [didResource, query] = didUrl.split('?')
54
55
 
55
56
  // eslint-disable-next-line no-unused-vars
56
- const [_did, _web, urlNoProtocol] = didUrl.split(':')
57
+ const [_did, _web, urlNoProtocol, ...pathFragments] = didUrl.split(':')
58
+
59
+ if (urlNoProtocol.includes('/')) {
60
+ throw new TypeError(`Cannot construct url from did: "${did}". domain-name cannot contain a path.`)
61
+ }
57
62
 
58
63
  let parsedUrl
59
64
  try {
@@ -64,11 +69,10 @@ export function urlFromDid ({ did } = {}) {
64
69
  throw new TypeError(`Cannot construct url from did: "${did}".`)
65
70
  }
66
71
 
67
- if (!parsedUrl.pathname || parsedUrl.pathname === '/') {
72
+ if (pathFragments.length === 0) {
68
73
  parsedUrl.pathname = '/.well-known/did.json'
69
74
  } else {
70
- const pathFragments = parsedUrl.pathname.split('/')
71
- parsedUrl.pathname = pathFragments.map(decodeURIComponent).join('/')
75
+ parsedUrl.pathname = pathFragments.map(decodeURIComponent).join('/') + '/did.json'
72
76
  }
73
77
 
74
78
  if (hashFragment) {