Skip to content

Commit

Permalink
feat(did): Add DID URL utilities
Browse files Browse the repository at this point in the history
- isDidUrl(): checks if given URL is a DID URL.
- decodeFromDidUrl(): decode information of the DID URL.

See also: https://w3c-ccg.github.io/did-spec/#fragment
  • Loading branch information
ender503 committed Jun 19, 2019
1 parent d564db5 commit 5675f46
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
26 changes: 26 additions & 0 deletions packages/did/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ Used to describe which Tangle network interacts.

* [~decodeFromDid(tangleid)](#module_did..decodeFromDid)

* [~isDidUrl(url)](#module_did..isDidUrl)

* [~decodeFromDidUrl(url)](#module_did..decodeFromDidUrl)

* [~createMetaPublicKeys(controller, publicKeyPems)](#module_did..createMetaPublicKeys)


Expand Down Expand Up @@ -62,6 +66,28 @@ Encode TangleID DID with specific network and address.
Decode infromation of network and address from TangleID.

**Returns**: <code>Object</code> - } The infromation of network and address.
<a name="module_did..isDidUrl"></a>

### *did*~isDidUrl(url)

| Param | Type | Description |
| --- | --- | --- |
| url | <code>string</code> | the URL to be checked. |

Checks if given URL is a DID URL.

**Returns**: <code>boolean</code> - The boolean that represent the url whether DID URL.
<a name="module_did..decodeFromDidUrl"></a>

### *did*~decodeFromDidUrl(url)

| Param | Type | Description |
| --- | --- | --- |
| url | <code>string</code> | the DID URL that will be decoded. |

Decode information of the DID URL.

**Returns**: <code>Object</code> - The information of the DID URL. [https://w3c-ccg.github.io/did-spec/#generic-did-syntax](https://w3c-ccg.github.io/did-spec/#generic-did-syntax)
<a name="module_did..createMetaPublicKeys"></a>

### *did*~createMetaPublicKeys(controller, publicKeyPems)
Expand Down
36 changes: 36 additions & 0 deletions packages/did/src/did.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { encodeToMnid, decodeFromMnid } from '@tangleid/mnid';
// @ts-ignore
import { parse } from 'did-resolver';
import { Did, MnidModel, PublicKeyPem, PublicKeyMeta } from '../../types';

const DID_URL_REGEX = /^did:(?<method>[a-z0-9]+):(?<idstring>[A-Za-z0-9\.\-_]+)(?:#(?<fragment>.*))?$/;

/**
* Encode TangleID DID with specific network and address.
* @function encodeToDid
Expand Down Expand Up @@ -36,6 +39,39 @@ export const decodeFromDid = (tangleid: Did): MnidModel => {
return mamParsed;
};

/**
* Checks if given URL is a DID URL.
* @function isDidUrl
* @param {string} url - the URL to be checked.
* @return {boolean} The boolean that represent the url whether DID URL.
*/
export const isDidUrl = (url: string): boolean => {
return DID_URL_REGEX.test(url);
};

/**
* Decode information of the DID URL.
* @function decodeFromDidUrl
* @param {string} url - the DID URL that will be decoded.
* @return {{did: string, method: string, idstring: string, fragment: string | null}}
* The information of the DID URL. {@link https://w3c-ccg.github.io/did-spec/#generic-did-syntax}
*/
export const decodeFromDidUrl = (url: string) => {
const matches = DID_URL_REGEX.exec(url);
if (matches === null) {
throw new Error('Invalid DID URL');
}

const [, method, idstring, fragment] = matches;

return {
did: `did:${method}:${idstring}`,
method,
idstring,
fragment: fragment || null,
};
};

/**
* Create metadata of public keys from PEM-formatted public Keys.
* @function createMetaPublicKeys
Expand Down

0 comments on commit 5675f46

Please sign in to comment.