Skip to content

Commit

Permalink
feat: api for resolve did
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerson Pool Miranda Robles committed Jul 14, 2022
1 parent c64e284 commit 669436e
Show file tree
Hide file tree
Showing 8 changed files with 416 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules
dist
package-lock.json
.env
abiIM.json
abiProxy.json
48 changes: 48 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import * as bodyParser from "body-parser";
import express from "express";
import dotenv from 'dotenv';
import logger from "./logger";
import swaggerUi = require('swagger-ui-express');
import fs = require('fs');

import Routes from "./routes";

dotenv.config();
const port = process.env.PORT;

class App {

public express: express.Application;

private swaggerFile: any = (process.cwd()+"/swagger.json");
private swaggerData: any = fs.readFileSync(this.swaggerFile, 'utf8');
private swaggerDocument = JSON.parse(this.swaggerData);

constructor() {
this.express = express();
this.middleware();
this.routes();
}

private middleware(): void {
this.express.use(bodyParser.json());
this.express.use(bodyParser.urlencoded({ extended: false }));
}

private routes(): void {

this.express.get("/", (req, res, next) => {
res.send("Welcome to KT-Provider-Resolver-did-ev");
});

this.express.use("/api", Routes);

this.express.use('/swagger', swaggerUi.serve, swaggerUi.setup(this.swaggerDocument));

this.express.listen(port, () => {
logger.info(`Server is running at https://localhost:${port}`);
});
}
}

export default new App().express;
19 changes: 19 additions & 0 deletions logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { createLogger, format, transports } from 'winston';
const { combine, timestamp, label, printf } = format;

const myFormat = printf(({ level, message, label, timestamp }: any) => {
return `${timestamp} [${label}] ${level}: ${message}`;
});

const logger = createLogger({
format: combine(
label({ label: '⚡️ provider resolver' }),
timestamp(),
myFormat
),
transports: [
new transports.Console()
]
});

export default logger;
32 changes: 32 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "KT-Provider-Resolver-did-ev",
"version": "1.0.0",
"description": "",
"main": "index.ts",
"scripts": {
"build": "npx tsc",
"start": "node dist/index.js",
"dev": "concurrently \"npx tsc --watch\" \"cp swagger.json dist/swagger.json\" \"cp abiIM.json dist/abiIM.json\" \"cp abiProxy.json dist/abiProxy.json\" \"nodemon -q dist/index.js\""
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@kaytrust/ev-did-resolver": "^1.0.0",
"did-resolver": "^3.2.2",
"dotenv": "^16.0.1",
"express": "^4.18.1",
"swagger-jsdoc": "^6.2.1",
"swagger-ui-express": "^4.5.0",
"winston": "^3.8.1"
},
"devDependencies": {
"@types/express": "^4.17.13",
"@types/node": "^18.0.4",
"@types/swagger-jsdoc": "^6.0.1",
"@types/swagger-ui-express": "^4.1.3",
"concurrently": "^7.2.2",
"nodemon": "^2.0.19",
"typescript": "^4.7.4"
}
}
52 changes: 52 additions & 0 deletions routes/identifiers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import express from "express";
import logger from "../logger";
import dotenv from 'dotenv';

import EvResolver from '@kaytrust/ev-did-resolver';
import { Resolver } from 'did-resolver';

import fs = require('fs');

dotenv.config();

class Identifiers {

public express: express.Application;
private options: any = {
host: process.env.NODE_HOST,
abiIM: JSON.parse(fs.readFileSync(process.cwd()+"/abiIM.json", 'utf8')),
abiProxy: JSON.parse(fs.readFileSync(process.cwd()+"/abiProxy.json", 'utf8')),
addressIM: process.env.ADDRESS_IM,
};

constructor() {
this.express = express();
this.routes();
}

private routes(): void {

this.express.get("/identifiers/:did", async (req, res, next) => {
const { params } = req;
if (!params?.did) {
res.status(400).send("Params not contains did");
return;
}
const headers = [{ name: 'Authorization', value: req.headers.authorization }];
const newOptions = { ...this.options, headers: headers, ...req.body };
const evResolver = EvResolver.getResolver(newOptions);
const resolver = new Resolver(evResolver);
try {
const didDocument = await resolver.resolve(params.did);
logger.info(`Resolve did: ${params.did} - ${JSON.stringify(didDocument)}`);
res.status(200).json(didDocument);
} catch (error: any) {
const responseMessage = `Can't resolve did: ${params.did}`;
logger.error(`${responseMessage} - ${error.message}`);
res.status(500).json(responseMessage);
}
});
}
}

export default new Identifiers().express;
21 changes: 21 additions & 0 deletions routes/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import express from "express";
import logger from "../logger";

import Identifiers from "./identifiers";

class Routes {

public express: express.Application;

constructor() {
this.express = express();
this.routes();
}

private routes(): void {
logger.info('Register identifiers routes');
this.express.use("/", Identifiers);
}
}

export default new Routes().express;
135 changes: 135 additions & 0 deletions swagger.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
{
"openapi": "3.0.0",
"info": {
"description": "This is a provider resolver for did ev",
"version": "1.0.0",
"title": "KT Provider resolver did ev",
"contact": {
"email": "[email protected]"
},
"license": {
"name": "Apache 2.0",
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
}
},
"servers": [
{
"url": "http://localhost:8000/api",
"description": "Development server"
}
],
"paths" : {
"/identifiers/{did}" : {
"get" : {
"summary" : "Get did document",
"description": "Get did document",
"produces": ["application/json"],
"parameters": [
{
"name": "did",
"in": "path",
"description": "did for resolver",
"required": true,
"type": "string"
},
{
"in": "body",
"name": "body",
"description": "Options for resolver",
"required": false,
"schema": {
"type": "object",
"properties": {
"baseBlocks" : { "type": "number" },
"lastBlocks" : { "type": "number" },
"bufferSize" : { "type": "number" },
"startBlockMargin" : { "type": "number" },
"findEvents" : { "type": "boolean" },
"keys" : { "type": "array", "items": { "type": "string" } }
}
}
}
],
"responses": {
"200": {
"description": "successful operation",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/didDocumentResponse"
}
}
}
},
"400": {
"description": "Invalid did param",
"content": {
"text/html": {
"schema": {
"$ref": "#/components/schemas/invalidRequest"
}
}
}
},
"500": {
"description": "Can't resolve did",
"content": {
"text/html": {
"schema": {
"$ref": "#/components/schemas/resolveFailed"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"didDocumentResponse": {
"type": "object",
"properties": {
"@context": {
"type": "string"
},
"id": {
"type": "string"
},
"authentication": {
"type": "array",
"items": {
"$ref": "#/components/schemas/authenticationDidDocument"
}
}
}
},
"authenticationDidDocument": {
"type": "object",
"properties": {
"id": { "type": "string" },
"type": { "type": "string" },
"blockchainAccountId": { "type": "string" }
}
},
"invalidRequest": {
"type": "string"
},
"resolveFailed": {
"type": "string"
}
},
"securitySchemes": {
"bearerAuth": {
"type": "apiKey",
"name": "Authorization",
"in": "header"
}
}
},
"security": [
{
"bearerAuth": []
}
]
}
Loading

0 comments on commit 669436e

Please sign in to comment.