Skip to content

Commit 92f2e14

Browse files
committed
refactor(org.id-resolver): parseDid and parseUri are moved to org.id-utils package
1 parent 7a979c4 commit 92f2e14

File tree

2 files changed

+10
-102
lines changed

2 files changed

+10
-102
lines changed

packages/resolver/src/index.ts

Lines changed: 5 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type { JWK } from '@windingtree/org.id-auth/dist/keys';
99
import type { OrgIdData, KnownProvider } from '@windingtree/org.id-core';
1010
import { parseBlockchainAccountId, verifyVC } from '@windingtree/org.id-auth/dist/vc';
1111
import { OrgIdContract } from '@windingtree/org.id-core';
12-
import { regexp, http, object } from '@windingtree/org.id-utils';
12+
import { regexp, http, object, parsers } from '@windingtree/org.id-utils';
1313
import { DateTime } from 'luxon';
1414
import { version } from '../package.json';
1515

@@ -95,38 +95,6 @@ export interface OrgIdResolverAPI {
9595
resolve(orgId: string): Promise<DidResolutionResponse>;
9696
}
9797

98-
export interface DidGroupedCheckResult extends RegExpExecArray {
99-
groups: {
100-
did: string;
101-
method: string;
102-
network?: string;
103-
id: string;
104-
query?: string;
105-
fragment?: string;
106-
}
107-
}
108-
109-
export interface IpfsUriGroupedResult extends RegExpExecArray {
110-
groups: {
111-
protocol: string;
112-
cid: string;
113-
}
114-
}
115-
116-
export interface ParsedDid {
117-
did: string;
118-
method: string;
119-
network: string;
120-
orgId: string;
121-
query?: string;
122-
fragment?: string;
123-
}
124-
125-
export interface ParsedUri {
126-
uri: string;
127-
type: string;
128-
}
129-
13098
export interface ResolverCache {
13199
[did: string]: DidResolutionResponse
132100
}
@@ -165,68 +133,6 @@ export const setupFetchers = (fetchers: FetcherConfig[]): Fetchers =>
165133
{}
166134
);
167135

168-
// Parse raw DID and extract its parts
169-
// @todo Move `parseDid` to SDK utils
170-
export const parseDid = (did: string): ParsedDid => {
171-
const groupedCheck = regexp.didGrouped.exec(did) as DidGroupedCheckResult;
172-
173-
if (!groupedCheck || !groupedCheck.groups || !groupedCheck.groups.did) {
174-
throw new Error(`Invalid DID format: ${did}`);
175-
}
176-
177-
const {
178-
method,
179-
network,
180-
id,
181-
query,
182-
fragment
183-
} = groupedCheck.groups;
184-
185-
return {
186-
did,
187-
method,
188-
network: network || '1', // Mainnet is default value
189-
orgId: id,
190-
query,
191-
fragment
192-
};
193-
};
194-
195-
// Parse raw ORG.JSON uri and extract an uri type
196-
// @todo Move `parseUri` to SDK utils
197-
export const parseUri = (uri: string): ParsedUri => {
198-
let parsedUri: string;
199-
let type: string;
200-
201-
if (regexp.uriHttp.exec(uri)) {
202-
parsedUri = uri;
203-
type = 'http';
204-
} else if (regexp.ipfs.exec(uri) || regexp.ipfsUri.exec(uri)) {
205-
type = 'ipfs';
206-
207-
if (!regexp.ipfsUri.exec(uri)) {
208-
parsedUri = uri;
209-
} else {
210-
const ipfsGroupedResult = regexp.ipfsUriGrouped.exec(uri) as IpfsUriGroupedResult;
211-
212-
if (!ipfsGroupedResult) {
213-
// should never happen because it checked twice
214-
throw new Error(`Unable to extract CID from IPFS URI: ${uri}`);
215-
}
216-
217-
parsedUri = ipfsGroupedResult.groups.cid;
218-
}
219-
220-
} else {
221-
throw new Error(`Invalid URI: ${uri}`);
222-
}
223-
224-
return {
225-
uri: parsedUri,
226-
type
227-
}
228-
};
229-
230136
export const parseCapabilityDelegates = (
231137
capabilityDelegate: CapabilityDelegationReference
232138
): string[] => capabilityDelegate.map(
@@ -369,7 +275,7 @@ export const OrgIdResolver = (options: ResolverOptions): OrgIdResolverAPI => {
369275
level: number
370276
): Promise<VerificationMethodPublicKey> => {
371277

372-
const { did } = parseDid(verificationMethodId);
278+
const { did } = parsers.parseDid(verificationMethodId);
373279
const parentDid = object.getDeepValue(
374280
parentOrgJsonVc,
375281
'credentialSubject.id'
@@ -451,7 +357,7 @@ export const OrgIdResolver = (options: ResolverOptions): OrgIdResolverAPI => {
451357
const resolutionStart = Date.now();
452358

453359
try {
454-
const { did: parsedDid, network, orgId } = parseDid(did);
360+
const { did: parsedDid, network, orgId } = parsers.parseDid(did);
455361
did = parsedDid;
456362

457363
if (did === rootResolutionDid) {
@@ -479,7 +385,7 @@ export const OrgIdResolver = (options: ResolverOptions): OrgIdResolverAPI => {
479385
const {
480386
uri,
481387
type: uriType
482-
} = parseUri(orgJsonUri);
388+
} = parsers.parseUri(orgJsonUri);
483389

484390
const selectedFetcher = fetchers[uriType];
485391

@@ -563,7 +469,7 @@ export const OrgIdResolver = (options: ResolverOptions): OrgIdResolverAPI => {
563469
const {
564470
orgId: verificationOrgId,
565471
network: verificationNetwork
566-
} = parseDid(vcVerificationMethod);
472+
} = parsers.parseDid(vcVerificationMethod);
567473

568474
if (verificationOrgId !== orgId) {
569475
throw new Error(

packages/resolver/test/resolver.spec.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ import {
1717
generateOrgIdWithSigner
1818
} from '@windingtree/org.id-utils/dist/common';
1919
import {
20-
OrgIdResolver,
21-
buildEvmChainConfig,
22-
buildHttpFetcherConfig,
2320
parseDid,
2421
parseUri
22+
} from '@windingtree/org.id-utils/dist/parsers';
23+
import {
24+
OrgIdResolver,
25+
buildEvmChainConfig,
26+
buildHttpFetcherConfig
2527
} from '../src';
2628
import chai, { expect } from 'chai';
2729
import chp from 'chai-as-promised';

0 commit comments

Comments
 (0)