Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
API_URL=http://localhost:5000
CLIENT_URL=http://localhost:3000
PORT=5000
APP_ACCESS_TOKEN_KEY=<UNIQUE_ACCESS_SALT>
APP_REFRESH_TOKEN_KEY=<UNIQUE_REFRESH_SALT>
APP_WALLET_PASSPHRASE=<WALLET_PASSPHRASE>
WEB3STORAGE_KEY=<WEBSTORAGE_API_KEY>
APP_ACCESS_TOKEN_KEY=UNIQUE_ACCESS_SALT
APP_REFRESH_TOKEN_KEY=UNIQUE_REFRESH_SALT
APP_WALLET_PASSPHRASE=WALLET_PASSPHRASE
WEB3STORAGE_KEY=WEBSTORAGE_API_KEY
DEBUG_LPMS_SERVER=true
APP_CHAIN_ID=31337
APP_VERIFYING_CONTRACT=<APP_VERIFYING_CONTRACT>
APP_VERIFYING_CONTRACT=APP_VERIFYING_CONTRACT
APP_PROMETHEUS_PORT=9100
PROMETHEUS_ENABLED=true
PROMETHEUS_ENABLED=false
8 changes: 4 additions & 4 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ jobs:
fetch-depth: 0

- name: Set up node
uses: actions/setup-node@v2
uses: actions/setup-node@v3
with:
node-version: 16

- name: Install dependencies
run: yarn install --frozen-lockfile

# lint commit messages
- uses: wagoid/commitlint-github-action@v4
- name: Lint commit messages
uses: wagoid/commitlint-github-action@v4
env:
NODE_PATH: ${{ github.workspace }}/node_modules

- name: Run linters
- name: Lint code
uses: wearerequired/lint-action@v2
with:
eslint: true
Expand Down
8 changes: 5 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16

Expand All @@ -27,4 +27,6 @@ jobs:
run: yarn install --frozen-lockfile

- name: Run unit tests
run: yarn test
env:
WEB3STORAGE_KEY: ${{ secrets.WEB3STORAGE_KEY }}
run: cp .env.example .env && yarn test
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"semi": false,
"semi": true,
"singleQuote": true,
"printWidth": 80,
"trailingComma": "none"
Expand Down
23 changes: 12 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"lint": "npx eslint . --ext .ts",
"lint:fix": "npx eslint . --ext .ts --fix",
"test": "npx mocha -r ts-node/register test/**/*.spec.ts",
"build": "npx tsc",
"clean": "rm -rf dist",
"build": "npx clean && npx tsc -p tsconfig-build.json",
"protoc:libs": "cp -pR ./node_modules/@windingtree/stays-models/dist/proto/*.proto ./src/proto/",
"protoc:local": "protoc --ts_out ./src/proto --proto_path ./src/proto ./src/proto/*.proto",
"prepare": "husky install",
Expand All @@ -49,14 +50,14 @@
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"cross-env": "^7.0.3",
"dotenv": "^12.0.0",
"ethers": "^5.6.5",
"dotenv": "^16.0.1",
"ethers": "^5.6.8",
"express": "^4.18.1",
"express-rate-limit": "^6.4.0",
"express-validator": "^6.14.0",
"express-validator": "^6.14.1",
"form-data": "^4.0.0",
"helmet": "^5.1.0",
"js-waku": "^0.23.0",
"js-waku": "^0.24.0",
"jsonwebtoken": "^8.5.1",
"level": "^8.0.0",
"level-ts": "^2.1.0",
Expand All @@ -82,11 +83,11 @@
"@types/mocha": "^9.1.1",
"@types/morgan": "^1.9.3",
"@types/multer": "^1.4.7",
"@types/node": "^17.0.32",
"@types/node": "^17.0.38",
"@types/response-time": "^2.3.5",
"@types/swagger-ui-express": "^4.1.3",
"@typescript-eslint/eslint-plugin": "^5.23.0",
"@typescript-eslint/parser": "^5.23.0",
"@typescript-eslint/eslint-plugin": "^5.27.0",
"@typescript-eslint/parser": "^5.27.0",
"@windingtree/videre-contracts": "^0.2.0-alpha.1",
"chai": "^4.3.6",
"conventional-changelog-cli": "^2.2.2",
Expand All @@ -105,9 +106,9 @@
"prettier": "^2.6.2",
"supertest": "^6.2.3",
"swagger-jsdoc": "^6.2.1",
"swagger-ui-express": "^4.3.0",
"ts-node": "^10.7.0",
"swagger-ui-express": "^4.4.0",
"ts-node": "^10.8.0",
"typechain": "^8.0.0",
"typescript": "^4.6.4"
"typescript": "^4.7.2"
}
}
19 changes: 8 additions & 11 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import type { TypedDataDomain } from '@ethersproject/abstract-signer';
import dotenv from "dotenv";
import dotenv from 'dotenv';

dotenv.config();

const checkEnvVariables = (vars: string[]): void =>
vars.forEach(
variable => {
if (
!process.env[variable] ||
process.env[variable] === ''
) {
throw new Error(`${variable} must be provided in the ENV`);
}
vars.forEach((variable) => {
if (!process.env[variable] || process.env[variable] === '') {
throw new Error(`${variable} must be provided in the ENV`);
}
);
});

checkEnvVariables([
'PORT',
Expand All @@ -31,7 +26,9 @@ export const accessTokenKey = String(process.env.APP_ACCESS_TOKEN_KEY);
export const refreshTokenKey = String(process.env.APP_REFRESH_TOKEN_KEY);
export const walletPassphrase = String(process.env.APP_WALLET_PASSPHRASE);
export const debugEnabled = Boolean(process.env.DEBUG_LPMS_SERVER === 'true');
export const prometheusEnabled = Boolean(process.env.PROMETHEUS_ENABLED === 'true');
export const prometheusEnabled = Boolean(
process.env.PROMETHEUS_ENABLED === 'true'
);
export const prometheusPort = Number(process.env.APP_PROMETHEUS_PORT);
export const refreshTokenMaxAge = 30 * 24 * 60 * 60 * 1000; //30d
export const accessTokenMaxAge = 30 * 60 * 1000; //30m
Expand Down
76 changes: 29 additions & 47 deletions src/controllers/StorageController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ export class StorageController {
signer
);

return brotliCompressSync(
ServiceProviderData.toBinary(signedMessage)
);
}
return brotliCompressSync(ServiceProviderData.toBinary(signedMessage));
};

uploadFile = async (req: Request, res: Response, next: NextFunction) => {
try {
Expand All @@ -45,7 +43,7 @@ export class StorageController {
} catch (e) {
next(e);
}
}
};

uploadMetadata = async (req: Request, res: Response, next: NextFunction) => {
try {
Expand All @@ -59,23 +57,22 @@ export class StorageController {

try {
fileBuffer = brotliDecompressSync(fileBuffer);
} catch(_) {
} catch (_) {
// data is not compressed
}

const serviceProviderData = ServiceProviderData.fromBinary(fileBuffer);
const serviceProviderId = utils.hexlify(serviceProviderData.serviceProvider);
const serviceProviderId = utils.hexlify(
serviceProviderData.serviceProvider
);

// Extract ans save/update facility from metadata
await facilitiesService.setFacilityDbKeys(
serviceProviderId,
await facilitiesService.setFacilityDbKeys(serviceProviderId, [
[
[
'metadata',
Facility.fromBinary(serviceProviderData.payload) as Facility
]
'metadata',
Facility.fromBinary(serviceProviderData.payload) as Facility
]
);
]);

// Extract spaces from metadata
const spaces: Record<string, [string, FacilitySpaceLevelValues][]> = {};
Expand All @@ -87,51 +84,36 @@ export class StorageController {

if (type === ItemType.SPACE) {
spaces[itemId] = [
[
'metadata_generic',
generic as Item
],
[
'metadata',
(payload ? Space.fromBinary(payload) : {}) as Space
]
['metadata_generic', generic as Item],
['metadata', (payload ? Space.fromBinary(payload) : {}) as Space]
];
} else {
otherItems[itemId] = [
[
'metadata_generic',
generic as Item
]
];
otherItems[itemId] = [['metadata_generic', generic as Item]];
}
}

// Add/update spaces to DB
await Promise.all(
Object
.entries(spaces)
.map(
([itemId, entries]) => facilitiesService.setItemDbKeys(
serviceProviderId,
'spaces',
itemId,
entries
)
Object.entries(spaces).map(([itemId, entries]) =>
facilitiesService.setItemDbKeys(
serviceProviderId,
'spaces',
itemId,
entries
)
)
);

// Add/update other items to DB
await Promise.all(
Object
.entries(otherItems)
.map(
([itemId, entries]) => facilitiesService.setItemDbKeys(
serviceProviderId,
'otherItems',
itemId,
entries
)
Object.entries(otherItems).map(([itemId, entries]) =>
facilitiesService.setItemDbKeys(
serviceProviderId,
'otherItems',
itemId,
entries
)
)
);

const signer = await walletService.getWalletByIndex(
Expand All @@ -154,7 +136,7 @@ export class StorageController {
} catch (e) {
next(e);
}
}
};
}

export default new StorageController();
14 changes: 10 additions & 4 deletions src/controllers/UserController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import ApiError from '../exceptions/ApiError';
import { refreshTokenMaxAge } from '../config';

export class UserController {

public async login(req: Request, res: Response, next: NextFunction) {
try {
const errors = validationResult(req);
Expand Down Expand Up @@ -101,7 +100,11 @@ export class UserController {
}
}

public async updateUserPassword(req: AuthRequest, res: Response, next: NextFunction) {
public async updateUserPassword(
req: AuthRequest,
res: Response,
next: NextFunction
) {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return next(ApiError.BadRequest('Validation error', errors.array()));
Expand All @@ -123,7 +126,11 @@ export class UserController {
}
}

public async updateUserRoles(req: AuthRequest, res: Response, next: NextFunction) {
public async updateUserRoles(
req: AuthRequest,
res: Response,
next: NextFunction
) {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return next(ApiError.BadRequest('Validation error', errors.array()));
Expand All @@ -139,7 +146,6 @@ export class UserController {
} catch (e) {
next(e);
}

}
}

Expand Down
6 changes: 5 additions & 1 deletion src/controllers/WalletController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import walletService from '../services/WalletService';
import { NextFunction, Request, Response } from 'express';

export class WalletController {
public async getWallets(req: Request, res: Response, next: NextFunction): Promise<Response | void> {
public async getWallets(
req: Request,
res: Response,
next: NextFunction
): Promise<Response | void> {
try {
const accountsList = await walletService.getWalletAccounts();

Expand Down
13 changes: 6 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import bootstrapService from './services/BootstrapService';
import DBService from './services/DBService';
import { MetricsService } from './services/MetricsService';

process.on('unhandledRejection', async error => {
process.on('unhandledRejection', async (error) => {
console.log(error);
await DBService.getInstance().close();
process.exit(1);
Expand All @@ -22,9 +22,8 @@ const main = async (): Promise<ServerService> => {
return server.start();
};

export default main()
.catch(async error => {
console.log(error);
await DBService.getInstance().close();
process.exit(1);
});
export default main().catch(async (error) => {
console.log(error);
await DBService.getInstance().close();
process.exit(1);
});
2 changes: 1 addition & 1 deletion src/middlewares/AuthMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ export default async (req, res, next) => {
} catch (e) {
return next(ApiError.UnauthorizedError());
}
}
};
Loading