From 3323fbe88f134002a5edc68992383c231bd28ae7 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Thu, 4 Aug 2022 15:26:55 -0400 Subject: [PATCH 01/39] Add DidSyntaxError. --- lib/DidSyntaxError.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 lib/DidSyntaxError.js diff --git a/lib/DidSyntaxError.js b/lib/DidSyntaxError.js new file mode 100644 index 0000000..0b8db7c --- /dev/null +++ b/lib/DidSyntaxError.js @@ -0,0 +1,18 @@ +/*! + * Copyright (c) 2021-2022 Digital Bazaar, Inc. All rights reserved. + */ + +/** + * Error for throwing did syntax related errors. + * + * @param {string} message - An error message. + * @param {string} code - A did core error. + * + */ +class DidSyntaxError extends SyntaxError { + constructor(message, code) { + super(message); + this.name = 'DidSyntaxError'; + this.code = code; + } +} From a0d8322cf539c85831bfbf82f0385690952ce0d9 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Thu, 4 Aug 2022 15:47:22 -0400 Subject: [PATCH 02/39] Parse did returns scheme and prefix. --- lib/did-io.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/did-io.js b/lib/did-io.js index 6d02b1d..25fd1dd 100644 --- a/lib/did-io.js +++ b/lib/did-io.js @@ -193,8 +193,10 @@ export function _methodById({doc, methodId}) { } } + + /** - * Parses the DID into various component (currently, only cares about prefix). + * Parses the DID into scheme and method (prefix) component. * * @example * parseDid({did: 'did:v1:test:nym'}); @@ -202,14 +204,14 @@ export function _methodById({doc, methodId}) { * * @param {string} did - DID uri. * - * @returns {{prefix: string}} Returns the method prefix (without `did:`). + * @returns {{scheme: string, prefix: string}} Returns the scheme & method prefix. */ export function parseDid({did}) { if(!did) { throw new TypeError('DID cannot be empty.'); } - const prefix = did.split(':').slice(1, 2).join(':'); + const [scheme, prefix] = did.split(':'); - return {prefix}; + return {scheme, prefix}; } From c833a5043b68bd9bb35458641abaec1d1dd1f79a Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Thu, 4 Aug 2022 20:25:22 -0400 Subject: [PATCH 03/39] Port validators for did and didUrl over. --- lib/validators.js | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 lib/validators.js diff --git a/lib/validators.js b/lib/validators.js new file mode 100644 index 0000000..ee0b253 --- /dev/null +++ b/lib/validators.js @@ -0,0 +1,47 @@ +/*! + * Copyright (c) 2022 Digital Bazaar, Inc. All rights reserved. + */ + +/** + * This function comes from the did-test-suite. + * + * @see https://github.com/w3c/did-test-suite/ + * + * @param {object} options - Options to use. + * @param {string} options.did - A prospective did. + * + * @returns {boolean} - Returns true or false. +*/ +function _isValidDid({did}) { + const didRegex1 = new RegExp('^did:(?[a-z0-9]+):' + + '(?([a-zA-Z0-9\\.\\-_]|%[0-9a-fA-F]{2}|:)+$)'); + const didRegex2 = /:$/; + return didRegex1.test(did) && !didRegex2.test(did); +} + +/** + * This function comes from the did-test-suite. + * + * @see https://github.com/w3c/did-test-suite/ + * + * @param {object} options - Options to use. + * @param {string} options.didUrl - A prospective didUrl. + * + * @returns {boolean} - Returns true or false. +*/ +function _isValidDidUrl({didUrl}) { + const pchar = '[a-zA-Z0-9\\-\\._~]|%[0-9a-fA-F]{2}|[!$&\'()*+,;=:@]'; + const didUrlPattern = + '^' + + 'did:' + + '([a-z0-9]+)' + // method_name + '(:' + // method-specific-id + '([a-zA-Z0-9\\.\\-_]|%[0-9a-fA-F]{2})+' + + ')+' + + '((/(' + pchar + ')+)+)?' + // path-abempty + '(\\?(' + pchar + '|/|\\?)+)?' + // [ "?" query ] + '(#(' + pchar + '|/|\\?)+)?' + // [ "#" fragment ] + '$' + ; + return new RegExp(didUrlPattern).test(didUrl); +} From 3128d6accb60f10977c0090459fd9da9992c3209 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 5 Aug 2022 12:04:19 -0400 Subject: [PATCH 04/39] Add validator for invalidDid and invalidDidUrl to CachedResolver. --- lib/CachedResolver.js | 5 +++-- lib/did-io.js | 12 ++++++++++++ lib/validators.js | 23 +++++++++++++++++++++-- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/lib/CachedResolver.js b/lib/CachedResolver.js index bfd0673..f605146 100644 --- a/lib/CachedResolver.js +++ b/lib/CachedResolver.js @@ -1,7 +1,8 @@ /*! * Copyright (c) 2021 Digital Bazaar, Inc. All rights reserved. */ -import {parseDid} from './did-io.js'; +import {parseDid, isDidUrl} from './did-io.js'; +import {validateDid} from './validators.js'; import {LruCache} from '@digitalbazaar/lru-memoize'; export class CachedResolver { @@ -45,7 +46,7 @@ export class CachedResolver { if(!did) { throw new TypeError('A string "did" or "url" parameter is required.'); } - + validateDid({did}); const method = this._methodForDid(did); return this._cache.memoize({ diff --git a/lib/did-io.js b/lib/did-io.js index 25fd1dd..f3dd976 100644 --- a/lib/did-io.js +++ b/lib/did-io.js @@ -215,3 +215,15 @@ export function parseDid({did}) { return {scheme, prefix}; } + +/** + * Takes in a did and determines if it has url characters in it. + * + * @param {oject} options - Options to use. + * @param {string} options.did - A DID. + * + * @returns {boolean} Does the did potentially have a query or a fragment in it? + */ +export function isDidUrl({did}) { + return /[\/#?]/.test(did); +} diff --git a/lib/validators.js b/lib/validators.js index ee0b253..92f1ecd 100644 --- a/lib/validators.js +++ b/lib/validators.js @@ -1,6 +1,25 @@ /*! * Copyright (c) 2022 Digital Bazaar, Inc. All rights reserved. */ +import {isDidUrl} from './did-io.js'; + +export function validateDid({did}) { + if(isDidUrl({did})) { + if(!isValidDidUrl({didUrl: did})) { + throw new SyntaxError( + `Invalid did url ${did}`, + {cause: 'invalidDidUrl'} + ); + } + return; + } + if(!isValidDid({did})) { + throw new SyntaxError( + `Invalid did ${did}`, + {cause: 'invalidDid'} + ); + } +} /** * This function comes from the did-test-suite. @@ -12,7 +31,7 @@ * * @returns {boolean} - Returns true or false. */ -function _isValidDid({did}) { +export function isValidDid({did}) { const didRegex1 = new RegExp('^did:(?[a-z0-9]+):' + '(?([a-zA-Z0-9\\.\\-_]|%[0-9a-fA-F]{2}|:)+$)'); const didRegex2 = /:$/; @@ -29,7 +48,7 @@ function _isValidDid({did}) { * * @returns {boolean} - Returns true or false. */ -function _isValidDidUrl({didUrl}) { +export function isValidDidUrl({didUrl}) { const pchar = '[a-zA-Z0-9\\-\\._~]|%[0-9a-fA-F]{2}|[!$&\'()*+,;=:@]'; const didUrlPattern = '^' + From e037690c3abbe9f3bdb9f5aaa81cfc925d6254a9 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 5 Aug 2022 13:20:35 -0400 Subject: [PATCH 05/39] Use DidResolverError. --- lib/CachedResolver.js | 2 +- lib/DidResolverError.js | 20 ++++++++++++++++++++ lib/DidSyntaxError.js | 18 ------------------ lib/did-io.js | 6 ++---- lib/validators.js | 17 +++++++++-------- 5 files changed, 32 insertions(+), 31 deletions(-) create mode 100644 lib/DidResolverError.js delete mode 100644 lib/DidSyntaxError.js diff --git a/lib/CachedResolver.js b/lib/CachedResolver.js index f605146..890374e 100644 --- a/lib/CachedResolver.js +++ b/lib/CachedResolver.js @@ -1,7 +1,7 @@ /*! * Copyright (c) 2021 Digital Bazaar, Inc. All rights reserved. */ -import {parseDid, isDidUrl} from './did-io.js'; +import {parseDid} from './did-io.js'; import {validateDid} from './validators.js'; import {LruCache} from '@digitalbazaar/lru-memoize'; diff --git a/lib/DidResolverError.js b/lib/DidResolverError.js new file mode 100644 index 0000000..076d8ed --- /dev/null +++ b/lib/DidResolverError.js @@ -0,0 +1,20 @@ +/*! + * Copyright (c) 2021-2022 Digital Bazaar, Inc. All rights reserved. + */ + +/** + * Error for throwing did syntax related errors. + * + * @param {object} options - Options to use. + * @param {string} options.message - An error message. + * @param {string} options.code - A did core error. + * @param {object} options.params - Params to be passed to the base Error Class. + * + */ +export class DidResolverError extends SyntaxError { + constructor({message, code, params}) { + super(message, params); + this.name = 'DidSyntaxError'; + this.code = code; + } +} diff --git a/lib/DidSyntaxError.js b/lib/DidSyntaxError.js deleted file mode 100644 index 0b8db7c..0000000 --- a/lib/DidSyntaxError.js +++ /dev/null @@ -1,18 +0,0 @@ -/*! - * Copyright (c) 2021-2022 Digital Bazaar, Inc. All rights reserved. - */ - -/** - * Error for throwing did syntax related errors. - * - * @param {string} message - An error message. - * @param {string} code - A did core error. - * - */ -class DidSyntaxError extends SyntaxError { - constructor(message, code) { - super(message); - this.name = 'DidSyntaxError'; - this.code = code; - } -} diff --git a/lib/did-io.js b/lib/did-io.js index f3dd976..38e4667 100644 --- a/lib/did-io.js +++ b/lib/did-io.js @@ -193,8 +193,6 @@ export function _methodById({doc, methodId}) { } } - - /** * Parses the DID into scheme and method (prefix) component. * @@ -204,7 +202,7 @@ export function _methodById({doc, methodId}) { * * @param {string} did - DID uri. * - * @returns {{scheme: string, prefix: string}} Returns the scheme & method prefix. + * @returns {{scheme: string, prefix: string}} Returns the scheme & method. */ export function parseDid({did}) { if(!did) { @@ -219,7 +217,7 @@ export function parseDid({did}) { /** * Takes in a did and determines if it has url characters in it. * - * @param {oject} options - Options to use. + * @param {object} options - Options to use. * @param {string} options.did - A DID. * * @returns {boolean} Does the did potentially have a query or a fragment in it? diff --git a/lib/validators.js b/lib/validators.js index 92f1ecd..7acb233 100644 --- a/lib/validators.js +++ b/lib/validators.js @@ -2,22 +2,23 @@ * Copyright (c) 2022 Digital Bazaar, Inc. All rights reserved. */ import {isDidUrl} from './did-io.js'; +import {DidResolverError} from './DidResolverError.js'; export function validateDid({did}) { if(isDidUrl({did})) { if(!isValidDidUrl({didUrl: did})) { - throw new SyntaxError( - `Invalid did url ${did}`, - {cause: 'invalidDidUrl'} - ); + throw new DidResolverError({ + message: `Invalid did url ${did}`, + code: 'invalidDidUrl' + }); } return; } if(!isValidDid({did})) { - throw new SyntaxError( - `Invalid did ${did}`, - {cause: 'invalidDid'} - ); + throw new DidResolverError({ + message: `Invalid did ${did}`, + code: 'invalidDid' + }); } } From 93a4df4ca7e87e6891960288e47a792e267e8c4f Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 5 Aug 2022 13:41:39 -0400 Subject: [PATCH 06/39] Update parseDid prefix to method to match did core syntax. --- lib/CachedResolver.js | 10 +++++----- lib/did-io.js | 10 +++++----- lib/validators.js | 11 +++++++++++ test/did-io.spec.js | 10 +++++++--- 4 files changed, 28 insertions(+), 13 deletions(-) diff --git a/lib/CachedResolver.js b/lib/CachedResolver.js index 890374e..4c367a5 100644 --- a/lib/CachedResolver.js +++ b/lib/CachedResolver.js @@ -29,7 +29,7 @@ export class CachedResolver { /** * Gets the DID Document, by selecting a registered driver based on the DID - * prefix (DID method). + * method (DID method). * Either `did` or `url` param is required. * * @param {object} options - Options hashmap. @@ -81,11 +81,11 @@ export class CachedResolver { * @private */ _methodForDid(did) { - const {prefix} = parseDid({did}); - const method = this._methods.get(prefix); - if(!method) { + const {method} = parseDid({did}); + const methodDriver = this._methods.get(method); + if(!methodDriver) { throw new Error(`Driver for DID ${did} not found.`); } - return method; + return methodDriver; } } diff --git a/lib/did-io.js b/lib/did-io.js index 38e4667..6f37455 100644 --- a/lib/did-io.js +++ b/lib/did-io.js @@ -194,24 +194,24 @@ export function _methodById({doc, methodId}) { } /** - * Parses the DID into scheme and method (prefix) component. + * Parses the DID into scheme and method component. * * @example * parseDid({did: 'did:v1:test:nym'}); - * // -> {prefix: 'v1'} + * // -> {method: 'v1'} * * @param {string} did - DID uri. * - * @returns {{scheme: string, prefix: string}} Returns the scheme & method. + * @returns {{scheme: string, method: string}} Returns the scheme & method. */ export function parseDid({did}) { if(!did) { throw new TypeError('DID cannot be empty.'); } - const [scheme, prefix] = did.split(':'); + const [scheme, method] = did.split(':'); - return {scheme, prefix}; + return {scheme, method}; } /** diff --git a/lib/validators.js b/lib/validators.js index 7acb233..617d86d 100644 --- a/lib/validators.js +++ b/lib/validators.js @@ -4,6 +4,17 @@ import {isDidUrl} from './did-io.js'; import {DidResolverError} from './DidResolverError.js'; +/** + * Determines if a did contains url characters and + * then validates either the didUrl or did. + * + * @param {object} options - Options to use. + * @param {string} options.did - A did. + * + * @throws {DidResolverError} Throws if did is invalid. + * + * @returns {undefined} Just returns on success. + */ export function validateDid({did}) { if(isDidUrl({did})) { if(!isValidDidUrl({didUrl: did})) { diff --git a/test/did-io.spec.js b/test/did-io.spec.js index 1bf81c6..6972223 100644 --- a/test/did-io.spec.js +++ b/test/did-io.spec.js @@ -17,9 +17,13 @@ const MOCK_KEY = { }; describe('parseDid', () => { - it('should return main did method identifier', async () => { - const {prefix} = parseDid({did: 'did:v1:test:nym:abcd'}); - expect(prefix).to.equal('v1'); + it('should return did method', async () => { + const {method} = parseDid({did: 'did:v1:test:nym:abcd'}); + expect(method).to.equal('v1'); + }); + it('should return did scheme', async () => { + const {scheme} = parseDid({did: 'did:v1:test:nym:abcd'}); + expect(scheme).to.equal('did'); }); }); From f4bff2e076501250000a2014b71cefe80ff2892d Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 5 Aug 2022 13:44:09 -0400 Subject: [PATCH 07/39] Add breaking CHANGELOG entry for release. --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9729181..f02af67 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # did-io ChangeLog +## 3.0.0 - + +### Changed +- **BREAKING**: `parseDid` not longer returns prefix. Returns method instead. + +### Added +- **BREAKING**: `CachedResolver` now validates dids before resolving them. + ## 2.0.0 - 2022-06-02 ### Changed From b81108001fb4168fcc9aa59b0a64541f4f804abd Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 5 Aug 2022 13:53:11 -0400 Subject: [PATCH 08/39] Export DidResolverError. --- lib/index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/index.js b/lib/index.js index f99bb49..bbc3a72 100644 --- a/lib/index.js +++ b/lib/index.js @@ -10,4 +10,6 @@ export { export {CachedResolver} from './CachedResolver.js'; +export {DidResolverError} from './DidResolverError.js'; + export {VERIFICATION_RELATIONSHIPS} from './constants.js'; From d2de11b1205b73140855c6bb61c4787017e52daf Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Mon, 8 Aug 2022 16:02:15 -0400 Subject: [PATCH 09/39] Correct name of DidResolverError & inherit from Error instead of SyntaxError. --- lib/DidResolverError.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/DidResolverError.js b/lib/DidResolverError.js index 076d8ed..754474a 100644 --- a/lib/DidResolverError.js +++ b/lib/DidResolverError.js @@ -11,10 +11,10 @@ * @param {object} options.params - Params to be passed to the base Error Class. * */ -export class DidResolverError extends SyntaxError { +export class DidResolverError extends Error { constructor({message, code, params}) { super(message, params); - this.name = 'DidSyntaxError'; + this.name = 'DidResolverError'; this.code = code; } } From 98135e8daa7dd83486af94cbae44da394f70986e Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Mon, 8 Aug 2022 16:07:12 -0400 Subject: [PATCH 10/39] Export isDidUrl. --- lib/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index bbc3a72..616fb26 100644 --- a/lib/index.js +++ b/lib/index.js @@ -5,7 +5,8 @@ export { approvesMethodFor, findVerificationMethod, initKeys, - parseDid + parseDid, + isDidUrl } from './did-io.js'; export {CachedResolver} from './CachedResolver.js'; From e418733a564cd53339bd1348df7435425cfe98a5 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 9 Aug 2022 10:46:03 -0400 Subject: [PATCH 11/39] Move did TypeError to validators. --- lib/CachedResolver.js | 3 --- lib/validators.js | 3 +++ 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/CachedResolver.js b/lib/CachedResolver.js index 4c367a5..48098d3 100644 --- a/lib/CachedResolver.js +++ b/lib/CachedResolver.js @@ -43,9 +43,6 @@ export class CachedResolver { */ async get({did, url, ...args} = {}) { did = did || url; - if(!did) { - throw new TypeError('A string "did" or "url" parameter is required.'); - } validateDid({did}); const method = this._methodForDid(did); diff --git a/lib/validators.js b/lib/validators.js index 617d86d..c9092a5 100644 --- a/lib/validators.js +++ b/lib/validators.js @@ -16,6 +16,9 @@ import {DidResolverError} from './DidResolverError.js'; * @returns {undefined} Just returns on success. */ export function validateDid({did}) { + if(!did) { + throw new TypeError('A string "did" or "url" parameter is required.'); + } if(isDidUrl({did})) { if(!isValidDidUrl({didUrl: did})) { throw new DidResolverError({ From 6287e709da0d1a084465f4de6b3bf00ec015fac8 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 10 Aug 2022 10:16:01 -0400 Subject: [PATCH 12/39] Add the ability to manually set the method for a did driver. --- lib/CachedResolver.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/CachedResolver.js b/lib/CachedResolver.js index 48098d3..6dcf0ea 100644 --- a/lib/CachedResolver.js +++ b/lib/CachedResolver.js @@ -21,9 +21,14 @@ export class CachedResolver { this._cache = new LruCache({max, maxAge, updateAgeOnGet, ...cacheOptions}); this._methods = new Map(); } - - use(driver) { - const methodName = driver.method; + /** + * Registers a driver for a did:method. + * + * @param {object} driver - A did method driver. + * @param {string} [method] - An optional method. + */ + use(driver, method) { + const methodName = method || driver.method; this._methods.set(methodName, driver); } From 82812a77bd2414f86bbdb50a589a41678cd24c76 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 10 Aug 2022 10:47:08 -0400 Subject: [PATCH 13/39] Add CHANGELOG entry for method parameter of CachedResolver.use. --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f02af67..8a07760 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ ### Added - **BREAKING**: `CachedResolver` now validates dids before resolving them. +- `CachedResolver.use` now accepts a second optional method parameter. ## 2.0.0 - 2022-06-02 From 5e5dc3e137bd571dad27b02aa8c799d37493032f Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Thu, 11 Aug 2022 09:36:36 -0400 Subject: [PATCH 14/39] Use DidResolverError w/ code methodNotSupported if no driver for did:method. --- lib/CachedResolver.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/CachedResolver.js b/lib/CachedResolver.js index 6dcf0ea..1d48d5d 100644 --- a/lib/CachedResolver.js +++ b/lib/CachedResolver.js @@ -1,6 +1,7 @@ /*! * Copyright (c) 2021 Digital Bazaar, Inc. All rights reserved. */ +import {DidResolverError} from './DidResolverError.js'; import {parseDid} from './did-io.js'; import {validateDid} from './validators.js'; import {LruCache} from '@digitalbazaar/lru-memoize'; @@ -70,7 +71,10 @@ export class CachedResolver { async generate({method, ...args}) { const driver = this._methods.get(method); if(!driver) { - throw new Error(`Driver for DID method "${method}" not found.`); + throw new DidResolverError({ + message: `Driver for DID method "${method}" not found.`, + code: 'methodNotSupported' + }); } return driver.generate(args); @@ -86,7 +90,10 @@ export class CachedResolver { const {method} = parseDid({did}); const methodDriver = this._methods.get(method); if(!methodDriver) { - throw new Error(`Driver for DID ${did} not found.`); + throw new DidResolverError({ + message: `Driver for DID ${did} not found.`, + code: 'methodNotSupported' + }); } return methodDriver; } From 7a76eae43e28d82582d7d325e2bff5a53534f653 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 12 Aug 2022 11:33:55 -0400 Subject: [PATCH 15/39] Add tests for validateDid, isValidDid, & isValidDidUrl. --- test/{did-io.spec.js => 01-did-io.spec.js} | 0 test/02-did-io-validators.spec.js | 99 ++++++++++++++++++++++ test/helpers.js | 12 +++ test/mock.data.js | 58 +++++++++++++ 4 files changed, 169 insertions(+) rename test/{did-io.spec.js => 01-did-io.spec.js} (100%) create mode 100644 test/02-did-io-validators.spec.js create mode 100644 test/helpers.js create mode 100644 test/mock.data.js diff --git a/test/did-io.spec.js b/test/01-did-io.spec.js similarity index 100% rename from test/did-io.spec.js rename to test/01-did-io.spec.js diff --git a/test/02-did-io-validators.spec.js b/test/02-did-io-validators.spec.js new file mode 100644 index 0000000..85bbd70 --- /dev/null +++ b/test/02-did-io-validators.spec.js @@ -0,0 +1,99 @@ +/*! + * Copyright (c) 2021 Digital Bazaar, Inc. All rights reserved. + */ +import chai from 'chai'; +import {testDid} from './helpers.js'; +import {isValidDid, isValidDidUrl} from '../lib/validators.js'; +import {DidResolverError} from '../lib/DidResolverError.js'; +import { + typeErrors, + invalidDids, + invalidDidSyntax, + invalidDidUrls, + validDids, + validDidUrls, +} from './mock.data.js'; + +const should = chai.should(); + +describe('validateDid', () => { + describe('should not throw', () => { + const inputs = new Set([...validDids, ...validDidUrls]); + for(const input of inputs) { + it(`should validate ${input}`, async () => { + const error = testDid(input); + should.not.exist(error, `Expected no error for did ${input}`); + }); + } + }); + describe('should throw `TypeError`', () => { + for(const input of typeErrors) { + it(`should not validate ${input}`, async () => { + const error = testDid(input); + should.exist(error, `Expected error for did ${input}`); + error.should.be.instanceof( + TypeError, + `Expected a TypeError for ${input}` + ); + }); + } + }); + describe('should throw `invalidDid`', () => { + const inputs = [...invalidDidSyntax, 'did:key:z4345345:']; + for(const input of inputs) { + it(`should not validate ${input}`, async () => { + const error = testDid(input); + should.exist(error, `Expected error for did ${input}`); + error.should.be.instanceof( + DidResolverError, + `Expected a DidResolverError for ${input}` + ); + error.code.should.equal( + 'invalidDid', + `Expected ${input} to be an invalid did.` + ); + }); + } + }); + +}); + +describe('isValidDidUrl', () => { + for(const validDidUrl of validDidUrls) { + it(`should validate ${validDidUrl}`, async () => { + const result = isValidDidUrl({didUrl: validDidUrl}); + should.exist(result, `Expected result for ${validDidUrl} to exist.`); + result.should.be.a( + 'boolean', 'Expected isValidDidUrl to return a boolean'); + result.should.equal(true, `Expected ${validDidUrl} to validate`); + }); + } + for(const invalidDidUrl of invalidDidUrls) { + it(`should not validate ${invalidDidUrl}`, async () => { + const result = isValidDidUrl({didUrl: invalidDidUrl}); + should.exist(result, `Expected result for ${invalidDidUrl} to exist.`); + result.should.be.a( + 'boolean', 'Expected isValidDidUrl to return a boolean'); + result.should.equal(false, `Expected ${invalidDidUrl} to not validate`); + }); + } +}); + +describe('isValidDid', () => { + for(const validDid of validDids) { + it(`should validate ${validDid}`, async () => { + const result = isValidDid({did: validDid}); + should.exist(result, `Expected result for ${validDid} to exist.`); + result.should.be.a('boolean', 'Expected isValidDid to return a boolean'); + result.should.equal(true, `Expected ${validDid} to validate`); + }); + } + for(const invalidDid of invalidDids) { + it(`should not validate ${invalidDid}`, async () => { + const result = isValidDid({did: invalidDid}); + should.exist(result, `Expected result for ${invalidDid} to exist.`); + result.should.be.a('boolean', 'Expected isValidDid to return a boolean'); + result.should.equal(false, `Expected ${invalidDid} to not validate`); + }); + } +}); diff --git a/test/helpers.js b/test/helpers.js new file mode 100644 index 0000000..2dc2edf --- /dev/null +++ b/test/helpers.js @@ -0,0 +1,12 @@ +/*! + * Copyright (c) 2021 Digital Bazaar, Inc. All rights reserved. + */ +import {validateDid} from '../lib/validators.js'; + +export function testDid(did) { + try { + validateDid({did}); + } catch(e) { + return e; + } +} diff --git a/test/mock.data.js b/test/mock.data.js new file mode 100644 index 0000000..afc78e0 --- /dev/null +++ b/test/mock.data.js @@ -0,0 +1,58 @@ +// these come from the did test suite and are here to ensure +// the RegExp ported over for dids and did urls behave as expected +export const validDidUrls = [ + 'did:example:123', + 'did:example:123456789abcdefghi', + 'did:example:123#ZC2jXTO6t4R501bfCXv3RxarZyUbdP2w_psLwMuY6ec', + 'did:example:123#keys-1', + 'did:example:123456/path', + 'did:example:123456/path/multiple/path', + 'did:example:123456/1path/2multiple/3path', + 'did:example:123456/1-path/2-multiple/3-path', + 'did:example:123456/path%20with%20space', + 'did:example:123456?versionId=1', + 'did:example:123#public-key-0', + 'did:example:123#sig_064bebcc', + 'did:example:123?service=agent&relativeRef=/credentials#degree', + 'did:example:abc:def-hij#klm', + 'did:orb:bafkreiazah4qrybzyapmrmk2dhldz24vfmavethcrgcoq7qhic63zz55ru:EiAag4' + + 'cmgxAE2isL5HG3mxjS7WRq4l-xyyTgULCAcEHQQQ#nMef0L2qNWVe8yt97ap0vH7kQK2oFdm4z' + + 'kQkYL7ymOo' +]; + +export const typeErrors = [ + false, + 0, + undefined, + null, + NaN +]; + +export const invalidDidSyntax = [ + 'STRING', + 'did:', + 'did:example' +]; + +export const invalidDidUrls = [ + ...typeErrors, + ...invalidDidSyntax +]; + +export const validDids = [ + 'did:example:123', + 'did:example:123456789abcdefghi', + 'did:example:123456789-abcdefghi', + 'did:example:123456789_abcdefghi', + 'did:example:123456789%20abcdefghi', + 'did:example:123abc:123456789abcdefghi', + 'did:example:abc%00', + 'did:example::::::abc:::123' +]; + +export const invalidDids = [ + ...invalidDidUrls, + 'did:example:123#ZC2jXTO6t4R501bfCXv3RxarZyUbdP2w_psLwMuY6ec', + 'did:example:123#keys-1', + 'did:example:abc:::' +]; From 99a3618c2a3cd10b31c366be27687b2b5df7ea37 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 12 Aug 2022 13:06:46 -0400 Subject: [PATCH 16/39] Add tests for invalidDidUrlSyntax. --- test/02-did-io-validators.spec.js | 18 +++++++++++++++++- test/mock.data.js | 8 +++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/test/02-did-io-validators.spec.js b/test/02-did-io-validators.spec.js index 85bbd70..c684b21 100644 --- a/test/02-did-io-validators.spec.js +++ b/test/02-did-io-validators.spec.js @@ -9,6 +9,7 @@ import { typeErrors, invalidDids, invalidDidSyntax, + invalidDidUrlSyntax, invalidDidUrls, validDids, validDidUrls, @@ -55,7 +56,22 @@ describe('validateDid', () => { }); } }); - + describe('should throw `invalidDidUrl`', () => { + for(const invalidDidUrl of invalidDidUrlSyntax) { + it(`should not validate ${invalidDidUrl}`, async () => { + const error = testDid(invalidDidUrl); + should.exist(error, `Expected error for did url ${invalidDidUrl}`); + error.should.be.instanceof( + DidResolverError, + `Expected a DidResolverError for ${invalidDidUrl}` + ); + error.code.should.equal( + 'invalidDidUrl', + `Expected ${invalidDidUrl} to be an invalid did url` + ); + }); + } + }); }); describe('isValidDidUrl', () => { diff --git a/test/mock.data.js b/test/mock.data.js index afc78e0..27b6cec 100644 --- a/test/mock.data.js +++ b/test/mock.data.js @@ -34,9 +34,15 @@ export const invalidDidSyntax = [ 'did:example' ]; +export const invalidDidUrlSyntax = [ + 'did:example:id/validPath/ invalid path /', + 'did:example:id/validPath/^invalid^path^/' +]; + export const invalidDidUrls = [ ...typeErrors, - ...invalidDidSyntax + ...invalidDidSyntax, + ...invalidDidUrlSyntax ]; export const validDids = [ From 349129f364150c81ad19e2ab56fe9d6c042b49c7 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 12 Aug 2022 13:14:56 -0400 Subject: [PATCH 17/39] Add note on method & expand README. --- CHANGELOG.md | 1 + README.md | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a07760..301fa2b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ ### Added - **BREAKING**: `CachedResolver` now validates dids before resolving them. - `CachedResolver.use` now accepts a second optional method parameter. +- Tests were added for the new validators. ## 2.0.0 - 2022-06-02 diff --git a/README.md b/README.md index 1c18d0f..18894b8 100644 --- a/README.md +++ b/README.md @@ -80,7 +80,8 @@ const resolver = new CachedResolver({max: 100}); // defaults to 100 On its own, the resolver does not know how to fetch or resolve any DID methods. Support for each one has to be enabled explicitly. It uses a [Chai](https://www.chaijs.com/)-like plugin architecture, where each driver -is loaded via `.use(driver)`. +is loaded via `.use(driver)`. Optionally method can be passed in as the second +parameter. ```js import * as didKey from '@digitalbazaar/did-method-key'; From 6f9062b98a5cdbffe9e45bfa801e60e9455bc00c Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 26 Aug 2022 10:08:07 -0400 Subject: [PATCH 18/39] Capitalize DID & URL in jsdoc comment. Co-authored-by: David I. Lehn --- lib/did-io.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/did-io.js b/lib/did-io.js index 6f37455..7728678 100644 --- a/lib/did-io.js +++ b/lib/did-io.js @@ -215,7 +215,7 @@ export function parseDid({did}) { } /** - * Takes in a did and determines if it has url characters in it. + * Determines if a DID has URL characters in it. * * @param {object} options - Options to use. * @param {string} options.did - A DID. From 93aa10568dd6b55b80291f28d628a19e5afa9c65 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 26 Aug 2022 10:08:20 -0400 Subject: [PATCH 19/39] Capitalize DID. Co-authored-by: David I. Lehn --- lib/did-io.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/did-io.js b/lib/did-io.js index 7728678..e461a3a 100644 --- a/lib/did-io.js +++ b/lib/did-io.js @@ -220,7 +220,7 @@ export function parseDid({did}) { * @param {object} options - Options to use. * @param {string} options.did - A DID. * - * @returns {boolean} Does the did potentially have a query or a fragment in it? + * @returns {boolean} Does the DID potentially have a query or a fragment in it? */ export function isDidUrl({did}) { return /[\/#?]/.test(did); From dee6f4930de3c95244f66dba42427562bfddc4f5 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 26 Aug 2022 10:08:39 -0400 Subject: [PATCH 20/39] Correct typos in changelog. Co-authored-by: David I. Lehn --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 301fa2b..304586b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ ## 3.0.0 - ### Changed -- **BREAKING**: `parseDid` not longer returns prefix. Returns method instead. +- **BREAKING**: `parseDid` no longer returns `prefix`. Returns `method` instead. ### Added - **BREAKING**: `CachedResolver` now validates dids before resolving them. From c580addf308f887a96a7e986d845055f9248c255 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 26 Aug 2022 10:09:03 -0400 Subject: [PATCH 21/39] Capitalize DIDs. Co-authored-by: David I. Lehn --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 304586b..c8fe030 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ - **BREAKING**: `parseDid` no longer returns `prefix`. Returns `method` instead. ### Added -- **BREAKING**: `CachedResolver` now validates dids before resolving them. +- **BREAKING**: `CachedResolver` now validates DIDs before resolving them. - `CachedResolver.use` now accepts a second optional method parameter. - Tests were added for the new validators. From b9a0aa2fd2a5ee6e40d769603ee23f0f56fd48b6 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 26 Aug 2022 10:09:22 -0400 Subject: [PATCH 22/39] Capitalize DID. Co-authored-by: David I. Lehn --- lib/DidResolverError.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/DidResolverError.js b/lib/DidResolverError.js index 754474a..0bdd439 100644 --- a/lib/DidResolverError.js +++ b/lib/DidResolverError.js @@ -7,7 +7,7 @@ * * @param {object} options - Options to use. * @param {string} options.message - An error message. - * @param {string} options.code - A did core error. + * @param {string} options.code - A DID core error. * @param {object} options.params - Params to be passed to the base Error Class. * */ From 79bbbad548ca5a69c43424aa61a51b2801fcefde Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 26 Aug 2022 10:09:52 -0400 Subject: [PATCH 23/39] Pluralize components comment. Co-authored-by: David I. Lehn --- lib/did-io.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/did-io.js b/lib/did-io.js index e461a3a..f041370 100644 --- a/lib/did-io.js +++ b/lib/did-io.js @@ -194,7 +194,7 @@ export function _methodById({doc, methodId}) { } /** - * Parses the DID into scheme and method component. + * Parses the DID into scheme and method components. * * @example * parseDid({did: 'did:v1:test:nym'}); From 150cd6f93e851a1c16a3ccfbd3d65ca24d8df352 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 26 Aug 2022 10:10:07 -0400 Subject: [PATCH 24/39] Capitalize DID. Co-authored-by: David I. Lehn --- lib/validators.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/validators.js b/lib/validators.js index c9092a5..b73a491 100644 --- a/lib/validators.js +++ b/lib/validators.js @@ -42,7 +42,7 @@ export function validateDid({did}) { * @see https://github.com/w3c/did-test-suite/ * * @param {object} options - Options to use. - * @param {string} options.did - A prospective did. + * @param {string} options.did - A prospective DID. * * @returns {boolean} - Returns true or false. */ From e465e3bfa2f4d5b45cfc56730b881c628b0f4a19 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 26 Aug 2022 10:10:21 -0400 Subject: [PATCH 25/39] Capitalize DID URL. Co-authored-by: David I. Lehn --- lib/validators.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/validators.js b/lib/validators.js index b73a491..4678ac3 100644 --- a/lib/validators.js +++ b/lib/validators.js @@ -59,7 +59,7 @@ export function isValidDid({did}) { * @see https://github.com/w3c/did-test-suite/ * * @param {object} options - Options to use. - * @param {string} options.didUrl - A prospective didUrl. + * @param {string} options.didUrl - A prospective DID URL. * * @returns {boolean} - Returns true or false. */ From 13a9c6c006f60f9e7e42006d7a354dc33dc3f585 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 26 Aug 2022 10:10:35 -0400 Subject: [PATCH 26/39] Bump copyright to 2022. Co-authored-by: David I. Lehn --- test/02-did-io-validators.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/02-did-io-validators.spec.js b/test/02-did-io-validators.spec.js index c684b21..4744c15 100644 --- a/test/02-did-io-validators.spec.js +++ b/test/02-did-io-validators.spec.js @@ -1,5 +1,5 @@ /*! - * Copyright (c) 2021 Digital Bazaar, Inc. All rights reserved. + * Copyright (c) 2022 Digital Bazaar, Inc. All rights reserved. */ import chai from 'chai'; import {testDid} from './helpers.js'; From 2237b5436932b00c5de6c6003333f6472fcc6c7c Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 26 Aug 2022 10:10:50 -0400 Subject: [PATCH 27/39] Extend Copyright to 2022. Co-authored-by: David I. Lehn --- lib/CachedResolver.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/CachedResolver.js b/lib/CachedResolver.js index 1d48d5d..04dec8b 100644 --- a/lib/CachedResolver.js +++ b/lib/CachedResolver.js @@ -1,5 +1,5 @@ /*! - * Copyright (c) 2021 Digital Bazaar, Inc. All rights reserved. + * Copyright (c) 2021-2022 Digital Bazaar, Inc. All rights reserved. */ import {DidResolverError} from './DidResolverError.js'; import {parseDid} from './did-io.js'; From 8c93c990263d2770cf886d28de583d4e833b8669 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 26 Aug 2022 10:11:04 -0400 Subject: [PATCH 28/39] Bump Copyright to 2022. Co-authored-by: David I. Lehn --- test/helpers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/helpers.js b/test/helpers.js index 2dc2edf..988d8b2 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -1,5 +1,5 @@ /*! - * Copyright (c) 2021 Digital Bazaar, Inc. All rights reserved. + * Copyright (c) 2022 Digital Bazaar, Inc. All rights reserved. */ import {validateDid} from '../lib/validators.js'; From debba558a4e6a13cc7fd1e4a82c662ccc972b29b Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 26 Aug 2022 10:14:04 -0400 Subject: [PATCH 29/39] Capitalize DID & URL. Co-authored-by: David I. Lehn --- lib/validators.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/validators.js b/lib/validators.js index 4678ac3..b32fb13 100644 --- a/lib/validators.js +++ b/lib/validators.js @@ -5,8 +5,8 @@ import {isDidUrl} from './did-io.js'; import {DidResolverError} from './DidResolverError.js'; /** - * Determines if a did contains url characters and - * then validates either the didUrl or did. + * Determines if a DID contains URL characters and + * then validates either the DID URL or DID. * * @param {object} options - Options to use. * @param {string} options.did - A did. From adb593d73a6a87267e555da46bc8be63b27e4fe2 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 26 Aug 2022 10:14:28 -0400 Subject: [PATCH 30/39] Capitalize DID. Co-authored-by: David I. Lehn --- lib/validators.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/validators.js b/lib/validators.js index b32fb13..597d953 100644 --- a/lib/validators.js +++ b/lib/validators.js @@ -9,7 +9,7 @@ import {DidResolverError} from './DidResolverError.js'; * then validates either the DID URL or DID. * * @param {object} options - Options to use. - * @param {string} options.did - A did. + * @param {string} options.did - A DID. * * @throws {DidResolverError} Throws if did is invalid. * From d722b5594e380d29c89dec086a7111b0a66765db Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 26 Aug 2022 10:14:45 -0400 Subject: [PATCH 31/39] Capitalize DID. Co-authored-by: David I. Lehn --- lib/validators.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/validators.js b/lib/validators.js index 597d953..bfdfa80 100644 --- a/lib/validators.js +++ b/lib/validators.js @@ -11,7 +11,7 @@ import {DidResolverError} from './DidResolverError.js'; * @param {object} options - Options to use. * @param {string} options.did - A DID. * - * @throws {DidResolverError} Throws if did is invalid. + * @throws {DidResolverError} Throws if DID is invalid. * * @returns {undefined} Just returns on success. */ From 402a14dc4ff3e019c4c5694eda3831dfdd235b69 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 26 Aug 2022 10:15:14 -0400 Subject: [PATCH 32/39] Capitalize DID & URL. Co-authored-by: David I. Lehn --- lib/validators.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/validators.js b/lib/validators.js index bfdfa80..5f20678 100644 --- a/lib/validators.js +++ b/lib/validators.js @@ -22,7 +22,7 @@ export function validateDid({did}) { if(isDidUrl({did})) { if(!isValidDidUrl({didUrl: did})) { throw new DidResolverError({ - message: `Invalid did url ${did}`, + message: `Invalid DID URL "${did}"`, code: 'invalidDidUrl' }); } From 389fa2c9c19b4b188b66861e772ea786cb456804 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 26 Aug 2022 10:15:33 -0400 Subject: [PATCH 33/39] Capitalize DID. Co-authored-by: David I. Lehn --- lib/validators.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/validators.js b/lib/validators.js index 5f20678..f2770d5 100644 --- a/lib/validators.js +++ b/lib/validators.js @@ -30,7 +30,7 @@ export function validateDid({did}) { } if(!isValidDid({did})) { throw new DidResolverError({ - message: `Invalid did ${did}`, + message: `Invalid DID "${did}"`, code: 'invalidDid' }); } From 720fe4fd19b761473dc8aca6d15b004fa7d8d572 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 26 Aug 2022 10:16:04 -0400 Subject: [PATCH 34/39] Clarify return statement of validator for did. Co-authored-by: David I. Lehn --- lib/validators.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/validators.js b/lib/validators.js index f2770d5..e37c57c 100644 --- a/lib/validators.js +++ b/lib/validators.js @@ -44,7 +44,7 @@ export function validateDid({did}) { * @param {object} options - Options to use. * @param {string} options.did - A prospective DID. * - * @returns {boolean} - Returns true or false. + * @returns {boolean} - Returns true if DID is valid. */ export function isValidDid({did}) { const didRegex1 = new RegExp('^did:(?[a-z0-9]+):' + From f1f6f696881158b19ee0378b60177a05e4855c57 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 26 Aug 2022 10:22:02 -0400 Subject: [PATCH 35/39] Clarify JSDOC comments. --- lib/validators.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/validators.js b/lib/validators.js index e37c57c..5231014 100644 --- a/lib/validators.js +++ b/lib/validators.js @@ -13,11 +13,11 @@ import {DidResolverError} from './DidResolverError.js'; * * @throws {DidResolverError} Throws if DID is invalid. * - * @returns {undefined} Just returns on success. + * @returns {undefined} Returns on success. */ export function validateDid({did}) { if(!did) { - throw new TypeError('A string "did" or "url" parameter is required.'); + throw new TypeError('The parameter "did" is required.'); } if(isDidUrl({did})) { if(!isValidDidUrl({didUrl: did})) { @@ -37,6 +37,7 @@ export function validateDid({did}) { } /** + * Validates a DID, but not a DID URL. * This function comes from the did-test-suite. * * @see https://github.com/w3c/did-test-suite/ @@ -54,6 +55,7 @@ export function isValidDid({did}) { } /** + * Validates a DID URL including the fragment and queries. * This function comes from the did-test-suite. * * @see https://github.com/w3c/did-test-suite/ From 3486bd2af650706038b2efae2bf21e6fe01beb67 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 26 Aug 2022 10:30:06 -0400 Subject: [PATCH 36/39] Use and over & in comments. Co-authored-by: David I. Lehn --- lib/did-io.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/did-io.js b/lib/did-io.js index f041370..e32b8c1 100644 --- a/lib/did-io.js +++ b/lib/did-io.js @@ -202,7 +202,7 @@ export function _methodById({doc, methodId}) { * * @param {string} did - DID uri. * - * @returns {{scheme: string, method: string}} Returns the scheme & method. + * @returns {{scheme: string, method: string}} Returns the scheme and method. */ export function parseDid({did}) { if(!did) { From d97608615ee9df8be573fdaa797a436379a699a2 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 26 Aug 2022 10:30:53 -0400 Subject: [PATCH 37/39] Include scheme in examples for parseDid. --- lib/did-io.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/did-io.js b/lib/did-io.js index e32b8c1..6e2d068 100644 --- a/lib/did-io.js +++ b/lib/did-io.js @@ -198,7 +198,7 @@ export function _methodById({doc, methodId}) { * * @example * parseDid({did: 'did:v1:test:nym'}); - * // -> {method: 'v1'} + * // -> {scheme: 'did', method: 'v1'} * * @param {string} did - DID uri. * From e77d4b7b797648e9383854048ff1639a1b90ceec Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 7 Dec 2022 09:31:20 -0500 Subject: [PATCH 38/39] Use more positive language in README. Co-authored-by: David I. Lehn --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c8fe030..d23f216 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,8 @@ ## 3.0.0 - ### Changed -- **BREAKING**: `parseDid` no longer returns `prefix`. Returns `method` instead. +- **BREAKING**: `parseDid` changed to return `scheme` and `method` instead of + only `prefix`. ### Added - **BREAKING**: `CachedResolver` now validates DIDs before resolving them. From 50ae1682e9d5c903f1a55c3fe745bda93810cd73 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 7 Dec 2022 09:48:10 -0500 Subject: [PATCH 39/39] Sort index.js exports. Co-authored-by: David I. Lehn --- lib/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index 616fb26..ae81f71 100644 --- a/lib/index.js +++ b/lib/index.js @@ -5,8 +5,8 @@ export { approvesMethodFor, findVerificationMethod, initKeys, - parseDid, - isDidUrl + isDidUrl, + parseDid } from './did-io.js'; export {CachedResolver} from './CachedResolver.js';