Skip to content

Commit

Permalink
fix: add checking if service endpoint is a CID
Browse files Browse the repository at this point in the history
  • Loading branch information
Harasz committed May 18, 2022
1 parent 9501481 commit a0b5d1a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
7 changes: 7 additions & 0 deletions docs/api/interfaces/modules_claims.Claim.md
Expand Up @@ -22,6 +22,7 @@
- [subject](modules_claims.Claim.md#subject)
- [subjectAgreement](modules_claims.Claim.md#subjectagreement)
- [token](modules_claims.Claim.md#token)
- [vp](modules_claims.Claim.md#vp)

## Properties

Expand Down Expand Up @@ -118,3 +119,9 @@ ___
### token

**token**: `string`

___

### vp

`Optional` **vp**: `VerifiablePresentation`
2 changes: 2 additions & 0 deletions src/modules/claims/claims.types.ts
@@ -1,4 +1,5 @@
import { utils } from 'ethers';
import { VerifiablePresentation } from '@ew-did-registry/credentials-interface';
import { ClaimData } from '../did-registry';
import { IMessage } from '../messaging/messaging.types';

Expand Down Expand Up @@ -54,6 +55,7 @@ export interface Claim {
isRejected?: boolean;
namespace: string;
redirectUri?: string;
vp?: VerifiablePresentation;
}

export const readyToBeRegisteredOnchain = (
Expand Down
31 changes: 31 additions & 0 deletions src/modules/did-registry/did-registry.service.ts
@@ -1,4 +1,5 @@
import { Wallet, providers } from 'ethers';
import { CID } from 'multiformats/cid';
import { KeyType } from '@ew-did-registry/keys';
import { JWT } from '@ew-did-registry/jwt';
import { ProxyOperator } from '@ew-did-registry/proxyidentity';
Expand Down Expand Up @@ -589,6 +590,10 @@ export class DidRegistry {
}: DownloadClaimsOptions): Promise<(IServiceEndpoint & ClaimData)[]> {
return Promise.all(
services.map(async ({ serviceEndpoint, ...rest }) => {
if (!this.isCID(serviceEndpoint)) {
return { serviceEndpoint, ...rest };
}

const data = await this._ipfsStore.get(serviceEndpoint);
const { claimData, ...claimRest } = this._jwt?.decode(data) as {
claimData: ClaimData;
Expand Down Expand Up @@ -644,4 +649,30 @@ export class DidRegistry {
);
}
}

/**
* Check if given value is a valid IPFS CID.
*
* ```typescript
* didRegistry.isCID('Qm...');
* ```
*
* @param {Any} hash value to check
*
*/
private isCID(hash: unknown): boolean {
try {
if (typeof hash === 'string') {
return Boolean(CID.parse(hash));
}

if (hash instanceof Uint8Array) {
return Boolean(CID.decode(hash));
}

return Boolean(CID.asCID(hash));
} catch (e) {
return false;
}
}
}

0 comments on commit a0b5d1a

Please sign in to comment.