Skip to content

Commit

Permalink
Add versionTime query parameter for DID controller
Browse files Browse the repository at this point in the history
  • Loading branch information
jasny committed Jul 18, 2023
1 parent 6e3b31c commit 16beb77
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
31 changes: 23 additions & 8 deletions src/did/did.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Controller, Res, Get, Param, Req } from '@nestjs/common';
import { ApiParam, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { Controller, Res, Get, Param, Req, Query } from '@nestjs/common';
import { ApiParam, ApiOperation, ApiResponse, ApiTags, ApiQuery } from '@nestjs/swagger';
import { Request, Response } from 'express';
import { LoggerService } from '../common/logger/logger.service';
import { DIDService } from './did.service';
Expand All @@ -12,6 +12,11 @@ export class DidController {
@Get(':did')
@ApiOperation({ summary: 'DID resolver' })
@ApiParam({ name: 'did', description: 'DID or network address' })
@ApiQuery({
name: 'versionTime',
description: 'Get the DID document that was valid at the specified time',
required: false,
})
@ApiResponse({
status: 200,
description: 'DID document or DID resolution',
Expand Down Expand Up @@ -96,26 +101,36 @@ export class DidController {
},
},
})
async resolve(@Param('did') did: string, @Req() req: Request, @Res() res: Response): Promise<Response> {
async resolve(
@Req() req: Request,
@Res() res: Response,
@Param('did') did: string,
@Query('versionTime') versionTime?: string,
): Promise<Response> {
const accept = req.get('Accept') || '';
const isDidResolution = accept.includes('application/ld+json;profile="https://w3id.org/did-resolution"');

return isDidResolution ? this.resolveResolution(did, res) : this.resolveDocument(did, res);
const versionTimeDate = versionTime ? new Date(versionTime) : undefined;
if (versionTimeDate) versionTimeDate.setMilliseconds(999);

return isDidResolution
? this.resolveResolution(did, versionTimeDate, res)
: this.resolveDocument(did, versionTimeDate, res);
}

private async resolveDocument(did: string, res: Response): Promise<Response> {
private async resolveDocument(did: string, versionTime: Date | undefined, res: Response): Promise<Response> {
try {
const didDocument = await this.service.resolveDocument(did);
const didDocument = await this.service.resolveDocument(did, versionTime);
return didDocument ? res.status(200).json(didDocument) : res.status(404).json({ error: 'notFound' });
} catch (e) {
this.logger.error(`did-controller: failed to get DID document '${e}'`, { stack: e.stack });
return res.status(500).json({ error: `failed to get DID document '${e}'` });
}
}

private async resolveResolution(did: string, res: Response): Promise<Response> {
private async resolveResolution(did: string, versionTime: Date | undefined, res: Response): Promise<Response> {
try {
const resolution = await this.service.resolve(did);
const resolution = await this.service.resolve(did, versionTime);

return res
.status(resolution.didResolutionMetadata.error ? 404 : 200)
Expand Down
7 changes: 5 additions & 2 deletions src/did/did.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { KeyType } from './verification-method/model/verification-method.types';
import { base58 } from '@scure/base';
import * as ed2curve from 'ed2curve';
import { isValidAddress, networkId } from '../utils/crypto';
import { isoDate } from '../utils/isoDate';
import { isoDate } from '../utils/date';

type DIDDocumentVerificationMethods = Pick<
DIDDocument,
Expand Down Expand Up @@ -220,7 +220,10 @@ export class DIDService {

for (const serviceWithTimestamp of services) {
const { timestamp, ...service } = serviceWithTimestamp;
if (timestamp <= versionTimestamp) map.set(service.id, service);
if (timestamp <= versionTimestamp) {
const id = service.id.replace(new RegExp(`^did:lto:${address}#`), '#');
map.set(id, service);
}
}

return Array.from(map.values()).filter((service) => !!service.type);
Expand Down
File renamed without changes.

0 comments on commit 16beb77

Please sign in to comment.