Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Commit

Permalink
Rewrite to typescript (relayer: [middlewares, routes]) (#410)
Browse files Browse the repository at this point in the history
* Rewrite to typescript (relayer: [middlewares, routes])

* Change AuthorisationRequest type on external types

* Extract define Type
  • Loading branch information
xitronix authored and marekkirejczyk committed Apr 5, 2019
1 parent 66559f0 commit de865de
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 49 deletions.
@@ -1,13 +1,16 @@
require('dotenv').config();

module.exports = Object.freeze({
const config = Object.freeze({
legacyENS: true,
jsonRpcUrl: process.env.JSON_RPC_URL,
port: process.env.PORT,
privateKey: process.env.PRIVATE_KEY,
chainSpec: {
chainSpec: Object.freeze({
ensAddress: process.env.ENS_ADDRESS,
chainId: 0,
},
}),
ensRegistrars: [process.env.ENS_DOMAIN_1, process.env.ENS_DOMAIN_2, process.env.ENS_DOMAIN_3],
});

export type Config = typeof config;
export type ChainSpecConfig = Config['chainSpec'];
7 changes: 0 additions & 7 deletions universal-login-relayer/lib/middlewares/async_middleware.js

This file was deleted.

9 changes: 9 additions & 0 deletions universal-login-relayer/lib/middlewares/async_middleware.ts
@@ -0,0 +1,9 @@
import {Request, Response, NextFunction} from 'express';

const asyncMiddleware = (fn : Function) =>
(req : Request, res : Response, next : NextFunction) => {
Promise.resolve(fn(req, res, next))
.catch(next);
};

export default asyncMiddleware;
@@ -1,34 +1,33 @@
import express from 'express';
import {Router, Request, Response} from 'express';
import asyncMiddleware from '../middlewares/async_middleware';
import geoip from 'geoip-lite';
import moment from 'moment';

export const request = (authorisationService) => async (req, res) => {
const ipAddress = req.headers['x-forwarded-for'] || req.ip;
export const request = (authorisationService : any) => async (req : Request, res : Response) => {
const ipAddress : string = req.headers['x-forwarded-for'] as string || req.ip;
const {platform, os, browser} = req.useragent || {platform: '', os: '', browser: ''};
const deviceInfo = {
ipAddress,
name: req.useragent.platform,
name: platform,
city: geoip.lookup(ipAddress) ? geoip.lookup(ipAddress).city : 'unknown',
os: req.useragent.os,
browser: req.useragent.browser,
os,
browser,
time: moment().format('h:mm'),
};
const requestAuthorisation = {...req.body, deviceInfo};
await authorisationService.addRequest(requestAuthorisation);
res.status(201)
.type('json')
.send(JSON.stringify());
res.status(201).send();
};

export const getPending = (authorisationService) => async (req, res) => {
export const getPending = (authorisationService : any) => async (req : Request, res : Response) => {
const {walletContractAddress} = req.params;
const response = await authorisationService.getPendingAuthorisations(walletContractAddress);
res.status(200)
.type('json')
.send(JSON.stringify({response}));
};

export const denyRequest = (authorisationService) => async (req, res) => {
export const denyRequest = (authorisationService : any) => async (req : Request, res : Response) => {
const {walletContractAddress} = req.params;
const {key} = req.body;
const response = await authorisationService.removeRequest(walletContractAddress, key);
Expand All @@ -37,8 +36,8 @@ export const denyRequest = (authorisationService) => async (req, res) => {
.send(JSON.stringify({response}));
};

export default (authorisationService) => {
const router = new express.Router();
export default (authorisationService : any) => {
const router = Router();

router.post('/',
asyncMiddleware(request(authorisationService)));
Expand Down
17 changes: 0 additions & 17 deletions universal-login-relayer/lib/routes/config.js

This file was deleted.

20 changes: 20 additions & 0 deletions universal-login-relayer/lib/routes/config.ts
@@ -0,0 +1,20 @@
import {Router, Request, Response} from 'express';
import asyncMiddleware from '../middlewares/async_middleware';
import {ChainSpecConfig} from '../config/relayer';



export const network = (config : ChainSpecConfig) => async (req : Request, res : Response) => {
res.status(200)
.type('json')
.send(JSON.stringify({config}));
};

export default (config : ChainSpecConfig) => {
const router = Router();

router.get('/',
asyncMiddleware(network(config)));

return router;
};
@@ -1,7 +1,7 @@
import express from 'express';
import {Router, Request, Response, NextFunction} from 'express';
import asyncMiddleware from '../middlewares/async_middleware';

export const create = (walletContractService) => async (req, res, next) => {
export const create = (walletContractService : any) => async (req : Request, res : Response, next : NextFunction) => {
const {managementKey, ensName} = req.body;
try {
const transaction = await walletContractService.create(managementKey, ensName);
Expand All @@ -13,7 +13,7 @@ export const create = (walletContractService) => async (req, res, next) => {
}
};

export const execution = (walletContractService) => async (req, res, next) => {
export const execution = (walletContractService : any) => async (req : Request, res : Response, next : NextFunction) => {
try {
const transaction = await walletContractService.executeSigned(req.body);
res.status(201)
Expand All @@ -24,8 +24,8 @@ export const execution = (walletContractService) => async (req, res, next) => {
}
};

export default (walletContractService) => {
const router = new express.Router();
export default (walletContractService : any) => {
const router = Router();

router.post('/',
asyncMiddleware(create(walletContractService)));
Expand Down
11 changes: 7 additions & 4 deletions universal-login-relayer/package.json
Expand Up @@ -5,6 +5,8 @@
"main": "build/index.js",
"types": "./types.d.ts",
"dependencies": {
"@types/express": "^4.16.1",
"@types/geoip-lite": "^1.1.29",
"babel-register": "^6.26.0",
"body-parser": "^1.18.3",
"cors": "^2.8.4",
Expand All @@ -18,8 +20,8 @@
"knex": "^0.16.3",
"moment": "^2.22.2",
"pg": "^7.7.1",
"universal-login-contracts": "^0.1.4",
"universal-login-commons": "^1.0.0"
"universal-login-commons": "^1.0.0",
"universal-login-contracts": "^0.1.4"
},
"scripts": {
"test": "NODE_ENV=test mocha -r ts-node/register test -t 10000 \"test/**/*.{js,ts}\"",
Expand All @@ -41,17 +43,18 @@
"@types/chai-as-promised": "^7.1.0",
"@types/chai-http": "^3.0.5",
"@types/chai-string": "^1.4.1",
"@types/express-useragent": "^0.2.21",
"@types/mocha": "^5.2.5",
"chai": "^4.1.2",
"chai-as-promised": "^7.1.1",
"chai-http": "^4.0.0",
"chai-string": "^1.4.0",
"sinon": "^6.3.1",
"sinon-chai": "^3.2.0",
"ethereum-waffle": "2.0.5",
"ganache-core": "^2.2.1",
"mocha": "^5.2.0",
"node-fetch": "^2.2.0",
"sinon": "^6.3.1",
"sinon-chai": "^3.2.0",
"solium": "^1.1.8",
"ts-node": "^8.0.2",
"tslint": "^5.12.1",
Expand Down
62 changes: 62 additions & 0 deletions yarn.lock
Expand Up @@ -208,6 +208,14 @@
dependencies:
"@types/node" "*"

"@types/body-parser@*":
version "1.17.0"
resolved "https://registry.yarnpkg.com/@types/body-parser/-/body-parser-1.17.0.tgz#9f5c9d9bd04bb54be32d5eb9fc0d8c974e6cf58c"
integrity sha512-a2+YeUjPkztKJu5aIF2yArYFQQp8d51wZ7DavSHjFuY1mqVgidGyzEQ41JIVNy82fXj8yPgy2vJmfIywgESW6w==
dependencies:
"@types/connect" "*"
"@types/node" "*"

"@types/chai-as-promised@^7.1.0":
version "7.1.0"
resolved "https://registry.yarnpkg.com/@types/chai-as-promised/-/chai-as-promised-7.1.0.tgz#010b04cde78eacfb6e72bfddb3e58fe23c2e78b9"
Expand Down Expand Up @@ -248,6 +256,13 @@
dependencies:
commander "*"

"@types/connect@*":
version "3.4.32"
resolved "https://registry.yarnpkg.com/@types/connect/-/connect-3.4.32.tgz#aa0e9616b9435ccad02bc52b5b454ffc2c70ba28"
integrity sha512-4r8qa0quOvh7lGD0pre62CAb1oni1OO6ecJLGCezTmhQ8Fz50Arx9RUszryR8KlgK6avuSXvviL6yWyViQABOg==
dependencies:
"@types/node" "*"

"@types/cookiejar@*":
version "2.1.0"
resolved "https://registry.yarnpkg.com/@types/cookiejar/-/cookiejar-2.1.0.tgz#4b7daf2c51696cfc70b942c11690528229d1a1ce"
Expand Down Expand Up @@ -275,14 +290,48 @@
"@types/cheerio" "*"
"@types/react" "*"

"@types/express-serve-static-core@*":
version "4.16.2"
resolved "https://registry.yarnpkg.com/@types/express-serve-static-core/-/express-serve-static-core-4.16.2.tgz#5ee8a22e602005be6767df6b2cba9879df3f75aa"
integrity sha512-qgc8tjnDrc789rAQed8NoiFLV5VGcItA4yWNFphqGU0RcuuQngD00g3LHhWIK3HQ2XeDgVCmlNPDlqi3fWBHnQ==
dependencies:
"@types/node" "*"
"@types/range-parser" "*"

"@types/express-useragent@^0.2.21":
version "0.2.21"
resolved "https://registry.yarnpkg.com/@types/express-useragent/-/express-useragent-0.2.21.tgz#dbbc684e15aa5dfeef1118eabcbc0084716be59f"
integrity sha512-xVZ9GVsrYa5PFghnb9UGiWR0ZOrAWhxSwbTVhqI1tMGU0I1TnAvzCQi15P29GWhc8mR384IUEINlhrPy0mz1Pg==
dependencies:
"@types/express" "*"

"@types/express@*", "@types/express@^4.16.1":
version "4.16.1"
resolved "https://registry.yarnpkg.com/@types/express/-/express-4.16.1.tgz#d756bd1a85c34d87eaf44c888bad27ba8a4b7cf0"
integrity sha512-V0clmJow23WeyblmACoxbHBu2JKlE5TiIme6Lem14FnPW9gsttyHtk6wq7njcdIWH1njAaFgR8gW09lgY98gQg==
dependencies:
"@types/body-parser" "*"
"@types/express-serve-static-core" "*"
"@types/serve-static" "*"

"@types/fbemitter@^2.0.32":
version "2.0.32"
resolved "https://registry.yarnpkg.com/@types/fbemitter/-/fbemitter-2.0.32.tgz#8ed204da0f54e9c8eaec31b1eec91e25132d082c"

"@types/geoip-lite@^1.1.29":
version "1.1.29"
resolved "https://registry.yarnpkg.com/@types/geoip-lite/-/geoip-lite-1.1.29.tgz#c2c4cecc49d988d8816b8be006c08f211103351b"
integrity sha1-wsTOzEnZiNiBa4vgBsCPIREDNRs=

"@types/history@*":
version "4.7.2"
resolved "https://registry.yarnpkg.com/@types/history/-/history-4.7.2.tgz#0e670ea254d559241b6eeb3894f8754991e73220"

"@types/mime@*":
version "2.0.1"
resolved "https://registry.yarnpkg.com/@types/mime/-/mime-2.0.1.tgz#dc488842312a7f075149312905b5e3c0b054c79d"
integrity sha512-FwI9gX75FgVBJ7ywgnq/P7tw+/o1GUbtP0KzbtusLigAOgIgNISRK0ZPl4qertvXSIE8YbsVJueQ90cDt9YYyw==

"@types/mocha@^5.2.5":
version "5.2.5"
resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-5.2.5.tgz#8a4accfc403c124a0bafe8a9fc61a05ec1032073"
Expand All @@ -306,6 +355,11 @@
version "15.5.8"
resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.5.8.tgz#8ae4e0ea205fe95c3901a5a1df7f66495e3a56ce"

"@types/range-parser@*":
version "1.2.3"
resolved "https://registry.yarnpkg.com/@types/range-parser/-/range-parser-1.2.3.tgz#7ee330ba7caafb98090bece86a5ee44115904c2c"
integrity sha512-ewFXqrQHlFsgc09MK5jP5iR7vumV/BYayNC6PgJO2LPe8vrnNFyjQjSppfEngITi0qvfKtzFvgKymGheFM9UOA==

"@types/react-dom@^16.8.0":
version "16.8.0"
resolved "https://registry.yarnpkg.com/@types/react-dom/-/react-dom-16.8.0.tgz#c565f43f9d2ec911f9e0b8f3b74e25e67879aa3f"
Expand Down Expand Up @@ -338,6 +392,14 @@
version "5.5.0"
resolved "https://registry.yarnpkg.com/@types/semver/-/semver-5.5.0.tgz#146c2a29ee7d3bae4bf2fcb274636e264c813c45"

"@types/serve-static@*":
version "1.13.2"
resolved "https://registry.yarnpkg.com/@types/serve-static/-/serve-static-1.13.2.tgz#f5ac4d7a6420a99a6a45af4719f4dcd8cd907a48"
integrity sha512-/BZ4QRLpH/bNYgZgwhKEh+5AsboDBcUdlBYgzoLX0fpj3Y2gp6EApyOlM3bK53wQS/OE1SrdSYBAbux2D1528Q==
dependencies:
"@types/express-serve-static-core" "*"
"@types/mime" "*"

"@types/sinon-chai@^3.2.2":
version "3.2.2"
resolved "https://registry.yarnpkg.com/@types/sinon-chai/-/sinon-chai-3.2.2.tgz#5cfdbda70bae30f79a9423334af9e490e4cce793"
Expand Down

0 comments on commit de865de

Please sign in to comment.