Skip to content

Commit

Permalink
Corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
olegnn committed Oct 30, 2024
1 parent 8aaee1f commit 8af66d1
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 88 deletions.
3 changes: 2 additions & 1 deletion examples/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
DidKey,
DIDDocument,
} from '@docknetwork/credential-sdk/types';
import { parseDIDUrl } from '@docknetwork/credential-sdk/utils';
import { NoDIDError } from '@docknetwork/credential-sdk/modules/abstract/did';
import {
DIDKeyResolver,
Expand Down Expand Up @@ -50,7 +51,7 @@ class EtherResolver extends Resolver {
}

async resolve(did) {
const parsed = this.parseDid(did);
const parsed = parseDIDUrl(did);
try {
return this.ethres(did, parsed);
} catch (e) {
Expand Down
157 changes: 75 additions & 82 deletions packages/credential-sdk/src/modules/abstract/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
ensurePrototypeOf,
withExtendedPrototypeProperties,
withExtendedStaticProperties,
} from '../../utils';
import { ensurePrototypeOf, withExtendedStaticProperties } from '../../utils';
import AbstractAccumulatorModule from './accumulator/module';
import AbstractAnchorModule from './anchor/module';
import AbstractAttestModule from './attest/module';
Expand All @@ -15,89 +11,86 @@ import AbstractTrustRegistryModule from './trust-registry/module';
/**
* Class representing a set of core modules each of which is an instance of its respective abstract module.
*/
export const AbstractCoreModules = withExtendedPrototypeProperties(
['methods'],
withExtendedStaticProperties(
[
'AccumulatorModule',
'AnchorModule',
'AttestModule',
'BlobModule',
'DIDModule',
'OffchainSignaturesModule',
'BBSModule',
'BBSPlusModule',
'PSModule',
'StatusListCredentialModule',
'TrustRegistryModule',
],
class AbstractCoreModules {
static AccumulatorModule = AbstractAccumulatorModule;

static AnchorModule = AbstractAnchorModule;

static AttestModule = AbstractAttestModule;

static BlobModule = AbstractBlobModule;

static DIDModule = AbstractDIDModule;

static OffchainSignaturesModule = AbstractOffchainSignaturesModule;

static BBSModule = AbstractOffchainSignaturesModule;

static BBSPlusModule = AbstractOffchainSignaturesModule;

static PSModule = AbstractOffchainSignaturesModule;

static StatusListCredentialModule = AbstractStatusListCredentialModule;

static TrustRegistryModule = AbstractTrustRegistryModule;

static ModuleMap = {
AccumulatorModule: { key: 'accumulator', optional: false },
AnchorModule: { key: 'anchor', optional: true },
AttestModule: { key: 'attest', optional: false },
BlobModule: { key: 'blob', optional: false },
DIDModule: { key: 'did', optional: false },
OffchainSignaturesModule: {
key: 'offchainSignatures',
optional: false,
},
BBSModule: { key: 'bbs', optional: false },
BBSPlusModule: { key: 'bbsPlus', optional: false },
PSModule: { key: 'ps', optional: false },
StatusListCredentialModule: {
key: 'statusListCredential',
optional: false,
},
TrustRegistryModule: { key: 'trustRegistry', optional: false },
};

constructor(...args) {
for (const [prop, key] of Object.entries(this.constructor.ModuleMap)) {
this.attachModule(prop, key, args);
}
export const AbstractCoreModules = withExtendedStaticProperties(
[
'AccumulatorModule',
'AnchorModule',
'AttestModule',
'BlobModule',
'DIDModule',
'OffchainSignaturesModule',
'BBSModule',
'BBSPlusModule',
'PSModule',
'StatusListCredentialModule',
'TrustRegistryModule',
],
class AbstractCoreModules {
static AccumulatorModule = AbstractAccumulatorModule;

static AnchorModule = AbstractAnchorModule;

static AttestModule = AbstractAttestModule;

static BlobModule = AbstractBlobModule;

static DIDModule = AbstractDIDModule;

static OffchainSignaturesModule = AbstractOffchainSignaturesModule;

static BBSModule = AbstractOffchainSignaturesModule;

static BBSPlusModule = AbstractOffchainSignaturesModule;

static PSModule = AbstractOffchainSignaturesModule;

static StatusListCredentialModule = AbstractStatusListCredentialModule;

static TrustRegistryModule = AbstractTrustRegistryModule;

static ModuleMap = {
AccumulatorModule: { key: 'accumulator', optional: false },
AnchorModule: { key: 'anchor', optional: true },
AttestModule: { key: 'attest', optional: false },
BlobModule: { key: 'blob', optional: false },
DIDModule: { key: 'did', optional: false },
OffchainSignaturesModule: {
key: 'offchainSignatures',
optional: false,
},
BBSModule: { key: 'bbs', optional: false },
BBSPlusModule: { key: 'bbsPlus', optional: false },
PSModule: { key: 'ps', optional: false },
StatusListCredentialModule: {
key: 'statusListCredential',
optional: false,
},
TrustRegistryModule: { key: 'trustRegistry', optional: false },
};

constructor(...args) {
for (const [prop, key] of Object.entries(this.constructor.ModuleMap)) {
this.attachModule(prop, key, args);
}
}

attachModule(prop, { key, optional }, args) {
const { [prop]: Module } = this.constructor;

if (Module == null) {
if (optional) {
return this;
}
attachModule(prop, { key, optional }, args) {
const { [prop]: Module } = this.constructor;

throw new Error(`No such module: ${prop}`);
if (Module == null) {
if (optional) {
return this;
}

ensurePrototypeOf(AbstractCoreModules[prop], Module);
this[key] = new Module(...args);

return this;
throw new Error(`No such module: ${prop}`);
}
},
),

ensurePrototypeOf(AbstractCoreModules[prop], Module);
this[key] = new Module(...args);

return this;
}
},
);

export {
Expand Down
3 changes: 2 additions & 1 deletion packages/credential-sdk/src/resolver/did/did-key-resolver.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getResolver } from 'key-did-resolver';
import { Resolver } from '../generic';
import { parseDIDUrl } from '../../utils';

const resolveMethod = getResolver().key;

Expand All @@ -16,7 +17,7 @@ export default class DIDKeyResolver extends Resolver {
}

async resolve(did) {
const parsed = this.parseDid(did);
const parsed = parseDIDUrl(did);
const { didDocument } = await resolveMethod(did, parsed, null, {});

return {
Expand Down
49 changes: 49 additions & 0 deletions packages/credential-sdk/src/utils/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import elliptic from 'elliptic';

import { sha256 } from 'js-sha256';
import { PatternMatcher } from './generic';
import { METHOD_REG_EXP_PATTERN } from '../resolver/generic/const';

const EC = elliptic.ec;
const secp256k1Curve = new EC('secp256k1');
Expand Down Expand Up @@ -81,6 +82,54 @@ export function getUniqueElementsFromArray(a, filterCallback) {
});
}

const ID_CHAR = '[a-zA-Z0-9_.-]';
const METHOD_ID = `(${ID_CHAR}+(:${ID_CHAR}+)*)`;
const PARAM_CHAR = '[a-zA-Z0-9_.:%-]';
const PARAM = `;${PARAM_CHAR}+=${PARAM_CHAR}*`;
const PARAMS = `((${PARAM})*)`;
// eslint-disable-next-line no-useless-escape
const PATH = '(/[^#?]*)?';
const QUERY = '([?][^#]*)?';
// eslint-disable-next-line no-useless-escape
const FRAGMENT = '(#.*)?';
const DID_MATCHER = new RegExp(
`^did:${METHOD_REG_EXP_PATTERN}:${METHOD_ID}${PARAMS}${PATH}${QUERY}${FRAGMENT}$`,
);

/**
* Parses supplied DID URL.
* @param {string} didUrl
* @returns
*/
export function parseDIDUrl(didUrl) {
if (didUrl === '' || !didUrl) throw new Error('Missing DID');
const sections = didUrl.match(DID_MATCHER);
if (sections) {
const parts = {
did: `did:${sections[1]}:${sections[2]}`,
method: sections[1],
id: sections[2],
didUrl,
};
if (sections[4]) {
const params = sections[4].slice(1).split(';');
parts.params = {};
for (const p of params) {
const kv = p.split('=');

// eslint-disable-next-line prefer-destructuring
parts.params[kv[0]] = kv[1];
}
}
// eslint-disable-next-line prefer-destructuring
if (sections[6]) parts.path = sections[6];
if (sections[7]) parts.query = sections[7].slice(1);
if (sections[8]) parts.fragment = sections[8].slice(1);
return parts;
}
throw new Error(`Invalid DID: \`${didUrl}\``);
}

/**
* Ensures that provided value matches supplied pattern(s), throws an error otherwise.
*
Expand Down
7 changes: 3 additions & 4 deletions tutorials/src/tutorial_resolver.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Following is an example to build a custom Ethereum resolver. It uses the library
as configuration. The example below uses Infura to get access to an Ethereum node and read the DID off Ethereum.

```js
import { DIDResolver } from "@docknetwork/credential-sdk/resolver";
import { parseDIDUrl } from "@docknetwork/credential-sdk/utils";
import ethr from "ethr-did-resolver";

// Infura's Ethereum provider for the main net.
Expand All @@ -67,7 +67,7 @@ class EtherResolver extends Resolver {
}

async resolve(did) {
const parsed = this.parseDid(did);
const parsed = parseDIDUrl(did);
try {
return this.ethres(did, parsed);
} catch (e) {
Expand Down Expand Up @@ -113,8 +113,7 @@ For resolving DID of any other method, `UniversalResolver` object will be used.

```js
import {
DIDResolver,
DIDResolver,
Resolver,
WILDCARD,
} from "@docknetwork/credential-sdk/resolver";

Expand Down

0 comments on commit 8af66d1

Please sign in to comment.