Skip to content

Commit

Permalink
Merge pull request #90 from Tonomy-Foundation/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
sadiabbasi authored Dec 1, 2023
2 parents b065c20 + 0f3ee77 commit 6aa405d
Show file tree
Hide file tree
Showing 17 changed files with 939 additions and 144 deletions.
3 changes: 2 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
dist
.yarn
.yarn/cache
.yarn/unplugged
.git
28 changes: 14 additions & 14 deletions .github/workflows/check-sdk-version.yaml
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
name: Test - Check SDK version

on:
pull_request

on: pull_request

jobs:
check-sdk-version:
runs-on: ubuntu-20.04
runs-on: ubuntu-20.04

steps:
- name: 🏗 Setup repo
uses: actions/checkout@v3
steps:
- name: 🏗 Setup repo
uses: actions/checkout@v4

- name: 📦 Check it is using the latest version of the SDK
uses: actions/setup-node@v3
with:
node-version: 18.12.1
- run: corepack enable
- run: yarn install --immutable
- run: yarn run updateSdkVersion $GITHUB_BASE_REF check
- name: 📦 Check it is using the latest version of the SDK
uses: actions/setup-node@v4
with:
node-version: 18.12.1
- run: corepack enable
- run: yarn -v
- run: yarn install --immutable
- run: yarn run updateSdkVersion $GITHUB_BASE_REF check
2 changes: 1 addition & 1 deletion .github/workflows/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Login to Docker Hub
uses: docker/login-action@v2
Expand Down
21 changes: 10 additions & 11 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ on:
jobs:
tests:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Install dependencies
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: 18.12.1
# corepack is needed to run yarn through npm
- run: corepack enable
- run: yarn -v
- run: yarn install --immutable

- name: Run lint and test
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: 18.12.1
- run: yarn run build
Expand All @@ -47,22 +47,21 @@ jobs:
yarn run start:prod &
sleep 5
curl http://localhost:5000
zap_scan:
runs-on: ubuntu-latest
name: OWASP ZAP API Scan

steps:

- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Build and run server
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: 18.12.1
# corepack is needed to run yarn through npm
- run: corepack enable
# corepack is needed to run yarn through npm
- run: yarn install --immutable
- run: yarn run build
- run: yarn run start:prod &
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ lerna-debug.log*

# yarn caching
.yarn
.yarn/cache
.yarn/unplugged

# OS
.DS_Store
Expand Down
768 changes: 768 additions & 0 deletions .yarn/releases/yarn-3.1.1.cjs

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
nodeLinker: node-modules

yarnPath: .yarn/releases/yarn-3.1.1.cjs
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ FROM node:18.12.1 AS tonomy_communication_base

WORKDIR /app
COPY . .

RUN corepack enable
RUN yarn install --immutable

RUN yarn run build

EXPOSE 5000
Expand Down
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,23 @@ yarn run test:cov

<https://localhost:5000/openapi> (for REST API with Open API)
<https://localhost:5000/asyncapi> (for websocket with Async API)

## Digital Ocean Settings

Run in a Digital Ocean App which runs the nodejs project: <https://docs.digitalocean.com/products/app-platform/>

Using Ubuntu 22.04 with Nodejs 18.x is enabled:

- <https://docs.digitalocean.com/products/app-platform/reference/buildpacks/nodejs/#ubuntu-2204-stack-supporting-nodejs-18>

Build command: `yarn -v && yarn install && yarn run build`

- NOTE: we need to run `yarn install` again so that we install with `[email protected]` which we force via`.yarnrc.yml` and `.yarn/releases/yarn-3.1.1.cjs`. See <https://github.com/Tonomy-Foundation/Tonomy-Communication/issues/85> for more details.

Run command: `yarn run start:prod`

Environment variables

- NODE_ENV
- CREATE_ACCOUNT_PRIVATE_KEY
- HCAPTCHA_SECRET
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@
"private": true,
"license": "Apache-2.0",
"scripts": {
"yarn:version": "yarn set version 3.1.1 && echo \"yarn version set to 3.1.1, see https://github.com/Tonomy-Foundation/Tonomy-Communication/issues/85\"",
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\"",
"lint:fix": "yarn run lint --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:watch": "yarn run test --watch",
"test:cov": "yarn run test --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json",
"test:e2e": "yarn run test --config ./test/jest-e2e.json",
"updateSdkVersion": "./update_sdk_version.sh"
},
"dependencies": {
Expand All @@ -28,7 +30,7 @@
"@nestjs/platform-socket.io": "^10.2.3",
"@nestjs/swagger": "^7.1.10",
"@nestjs/websockets": "^10.2.3",
"@tonomy/tonomy-id-sdk": "0.16.0",
"@tonomy/tonomy-id-sdk": "0.18.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
"hcaptcha": "^0.1.1",
Expand Down
2 changes: 1 addition & 1 deletion src/accounts/accounts.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { Response } from 'express';
@Controller('accounts')
export class AccountsController {
private readonly logger = new Logger(AccountsController.name);
constructor(private accountService: AccountsService) { }
constructor(private accountService: AccountsService) {}

@Post()
@ApiOperation({
Expand Down
2 changes: 1 addition & 1 deletion src/communication/communication.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export type WebsocketReturnType = {
@UseFilters(new BaseWsExceptionFilter())
export class CommunicationGateway implements OnGatewayDisconnect {
private readonly logger = new Logger(CommunicationGateway.name);
constructor(private readonly usersService: CommunicationService) { }
constructor(private readonly usersService: CommunicationService) {}

/**
* Logs in the user and added it to the loggedIn map
Expand Down
4 changes: 2 additions & 2 deletions src/communication/dto/body.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MessageDto } from './message.dto';

export class BodyDto {
value?: MessageDto;
error?: Error | any;
value?: MessageDto;
error?: Error | any;
}
32 changes: 16 additions & 16 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ if (env !== 'test') console.log(`NODE_ENV=${env}`);
type ConfigType = {
blockchainUrl: string;
loggerLevel:
| 'emergency'
| 'alert'
| 'critical'
| 'error'
| 'warning'
| 'notice'
| 'info'
| 'debug';
| 'emergency'
| 'alert'
| 'critical'
| 'error'
| 'warning'
| 'notice'
| 'info'
| 'debug';
};

type SettingsType = {
Expand All @@ -38,14 +38,14 @@ const settings: SettingsType = {

type FixLoggerLevelEnumType<T> = Omit<T, 'loggerLevel'> & {
loggerLevel:
| 'emergency'
| 'alert'
| 'critical'
| 'error'
| 'warning'
| 'notice'
| 'info'
| 'debug';
| 'emergency'
| 'alert'
| 'critical'
| 'error'
| 'warning'
| 'notice'
| 'info'
| 'debug';
};

switch (env) {
Expand Down
86 changes: 43 additions & 43 deletions test/communication.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,65 +4,65 @@ import { AppModule } from '../src/app.module';
import { connectSocket, emitMessage } from './ws-client.helper';
import { Socket } from 'socket.io-client';
import {
AuthenticationMessage,
ES256KSigner,
generateRandomKeyPair,
setSettings,
AuthenticationMessage,
ES256KSigner,
generateRandomKeyPair,
setSettings,
} from '@tonomy/tonomy-id-sdk';
// @ts-expect-error - cannot find module or its corresponding type declarations
import { createJWK, toDid } from '@tonomy/tonomy-id-sdk/util';

setSettings({
blockchainUrl: 'http://localhost:8888',
blockchainUrl: 'http://localhost:8888',
});

describe('CommunicationGateway (e2e)', () => {
let app: INestApplication;
let socket: Socket;
let app: INestApplication;
let socket: Socket;

beforeEach(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
beforeEach(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();

app = moduleFixture.createNestApplication();
app.listen(5000);
app = moduleFixture.createNestApplication();
app.listen(5000);

socket = await connectSocket();
});
socket = await connectSocket();
});

afterEach(async () => {
await socket.disconnect();
await socket.close();
await app.close();
});
afterEach(async () => {
await socket.disconnect();
await socket.close();
await app.close();
});

describe('login event', () => {
it('fails when provide an empty body', async () => {
await expect(() => emitMessage(socket, 'login', {})).rejects.toThrow(
"Cannot read properties of undefined (reading 'getCredentialSubject')",
);
});
describe('login event', () => {
it('fails when provide an empty body', async () => {
await expect(() => emitMessage(socket, 'login', {})).rejects.toThrow(
"Cannot read properties of undefined (reading 'getCredentialSubject')",
);
});

it('succeeds for did:jwk message', async () => {
const { privateKey, publicKey } = generateRandomKeyPair();
const signer = ES256KSigner(privateKey.data.array, true);
const jwk = await createJWK(publicKey);
const did = toDid(jwk);
it('succeeds for did:jwk message', async () => {
const { privateKey, publicKey } = generateRandomKeyPair();
const signer = ES256KSigner(privateKey.data.array, true);
const jwk = await createJWK(publicKey);
const did = toDid(jwk);

const issuer = {
did,
signer,
alg: 'ES256K-R',
};
const message = await AuthenticationMessage.signMessageWithoutRecipient(
{ data: 'test' },
issuer,
);
const issuer = {
did,
signer,
alg: 'ES256K-R',
};
const message = await AuthenticationMessage.signMessageWithoutRecipient(
{ data: 'test' },
issuer,
);

const response = await emitMessage(socket, 'login', { message });
const response = await emitMessage(socket, 'login', { message });

expect(response).toBeTruthy();
});
expect(response).toBeTruthy();
});
});
});
Loading

0 comments on commit 6aa405d

Please sign in to comment.