Skip to content

Commit

Permalink
refactor: Fixed sonarjs lint warnings (#254)
Browse files Browse the repository at this point in the history
* Fixed sonarjs/prefer-immediate-return warnings

* Fixed eqeqeq warnings

* Fixed sonarjs/no-collapsible-if warnings

* Standardized expected exceptions in unit tests

* Removed warnings report

* Added standard exceptions.js

* Added file links to keymaster-app

* Refactored wallet exceptions

* Refactored createDID exceptions

* Consolidated gatekeeper exceptions

* Consolidated keymaster exceptions

* Added @babel dependency to remove warning

* Consolidated poll exceptions

* Consolidated more keymaster exceptions

* Removed wallet exception

* Fixed gk build

* Fixed CLI error messages

* Consolidated CLI status messages

* Refactored isEmpty

* Rephrased test descriptions

* Refactored jest config

* Removed last lint warning

* Removed 'only-warn' from lint reporting
  • Loading branch information
macterra authored Jul 25, 2024
1 parent 69c5eb9 commit cf2c41f
Show file tree
Hide file tree
Showing 28 changed files with 826 additions and 682 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
data/
keymaster-app/
src/kc-app/build/
src/keymaster-app/build/
5 changes: 2 additions & 3 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"node": true,
"jest": true
},
"plugins": ["sonarjs", "only-warn"],
"plugins": ["sonarjs"],
"extends": ["plugin:sonarjs/recommended-legacy","react-app"],
"parserOptions": {
"ecmaVersion": "latest",
Expand All @@ -16,6 +16,5 @@
"error",
4
]

}
}
}
2 changes: 2 additions & 0 deletions Dockerfile.gatekeeper
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ RUN cd keymaster-app && npm ci

COPY src/keymaster-app/public ./keymaster-app/public/
COPY src/keymaster-app/src ./keymaster-app/src/
COPY src/keymaster-lib.js ./keymaster-app/src/
COPY src/exceptions.js ./keymaster-app/src/
RUN cd keymaster-app && npm run build

# Copy app
Expand Down
4 changes: 3 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default {
const config = {
transform: {},
testEnvironment: 'node',
moduleFileExtensions: ['js', 'mjs'],
Expand All @@ -8,3 +8,5 @@ export default {
"/keymaster-app/"
],
};

export default config;
26 changes: 23 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"uuid": "^9.0.1"
},
"devDependencies": {
"@babel/plugin-proposal-private-property-in-object": "^7.21.11",
"eslint": "^8.57.0",
"eslint-config-react-app": "^7.0.1",
"eslint-plugin-only-warn": "^1.1.0",
Expand Down
2 changes: 2 additions & 0 deletions rebuildGatekeeper
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env bash
set -e

cp src/exceptions.js src/keymaster-app/src
cp src/keymaster-lib.js src/keymaster-app/src
npm --prefix src/keymaster-app run build
node src/gatekeeper-api.js
14 changes: 6 additions & 8 deletions src/cipher-lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ export function generateMnemonic() {

export function generateHDKey(mnemonic) {
const seed = bip39.mnemonicToSeedSync(mnemonic);
const hdkey = HDKey.fromMasterSeed(seed);
return hdkey;
return HDKey.fromMasterSeed(seed);
}

export function generateHDKeyJSON(json) {
Expand Down Expand Up @@ -76,8 +75,7 @@ export function convertJwkToCompressedBytes(jwk) {

export function hashMessage(msg) {
const hash = sha256(msg);
const msgHash = Buffer.from(hash).toString('hex');
return msgHash;
return Buffer.from(hash).toString('hex');
}

export function hashJSON(json) {
Expand All @@ -87,15 +85,15 @@ export function hashJSON(json) {
export function signHash(msgHash, privateJwk) {
const privKey = base64url.baseDecode(privateJwk.d);
const signature = secp.sign(msgHash, privKey);
const sigHex = signature.toCompactHex();
return sigHex;

return signature.toCompactHex();
}

export function verifySig(msgHash, sigHex, publicJwk) {
const compressedPublicKeyBytes = convertJwkToCompressedBytes(publicJwk);
const signature = secp.Signature.fromCompact(sigHex);
const isValid = secp.verify(signature, msgHash, compressedPublicKeyBytes);
return isValid;

return secp.verify(signature, msgHash, compressedPublicKeyBytes);
}

export function encryptMessage(pubKey, privKey, message) {
Expand Down
8 changes: 4 additions & 4 deletions src/db-json.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from 'fs';
import * as exceptions from './exceptions.js';

const dataFolder = 'data';
let dbName;
Expand Down Expand Up @@ -39,7 +40,7 @@ export async function addEvent(did, event) {
const db = loadDb();

if (!did) {
throw "Invalid DID";
throw exceptions.INVALID_DID;
}

const suffix = did.split(':').pop();
Expand Down Expand Up @@ -74,7 +75,7 @@ export async function getEvents(did) {

export async function setEvents(did, events) {
if (!did) {
throw "Invalid DID";
throw exceptions.INVALID_DID;
}

const db = loadDb();
Expand Down Expand Up @@ -156,6 +157,5 @@ export async function clearQueue(registry, batch) {

export async function getAllKeys() {
const db = loadDb();
const ids = Object.keys(db.dids);
return ids;
return Object.keys(db.dids);
}
10 changes: 5 additions & 5 deletions src/db-mongodb.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { MongoClient } from 'mongodb';
import config from './config.js';
import * as exceptions from './exceptions.js';

let client;
let db;
Expand All @@ -21,7 +22,7 @@ export async function resetDb() {

export async function addEvent(did, event) {
if (!did) {
throw "Invalid DID";
throw exceptions.INVALID_DID;
}

const id = did.split(':').pop();
Expand All @@ -37,7 +38,7 @@ export async function addEvent(did, event) {

export async function getEvents(did) {
if (!did) {
throw "Invalid DID";
throw exceptions.INVALID_DID;
}

try {
Expand All @@ -54,7 +55,7 @@ export async function getEvents(did) {

export async function deleteEvents(did) {
if (!did) {
throw "Invalid DID";
throw exceptions.INVALID_DID;
}

const id = did.split(':').pop();
Expand All @@ -63,8 +64,7 @@ export async function deleteEvents(did) {

export async function getAllKeys() {
const rows = await db.collection('dids').find().toArray();
const ids = rows.map(row => row.id);
return ids;
return rows.map(row => row.id);
}

export async function queueOperation(registry, op) {
Expand Down
16 changes: 7 additions & 9 deletions src/db-sqlite.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as sqlite from 'sqlite';
import sqlite3 from 'sqlite3';
import * as exceptions from './exceptions.js';

const dataFolder = 'data';

Expand Down Expand Up @@ -34,7 +35,7 @@ export async function resetDb() {

export async function addEvent(did, event) {
if (!did) {
throw "Invalid DID";
throw exceptions.INVALID_DID;
}

const events = await getEvents(did);
Expand All @@ -49,16 +50,15 @@ export async function addEvent(did, event) {

export async function getEvents(did) {
if (!did) {
throw "Invalid DID";
throw exceptions.INVALID_DID;
}

try {
const id = did.split(':').pop();
console.time("SELECT");
const row = await db.get('SELECT * FROM dids WHERE id = ?', id);
console.timeEnd("SELECT");
const events = JSON.parse(row.events);
return events;
return JSON.parse(row.events);
}
catch {
return [];
Expand All @@ -67,7 +67,7 @@ export async function getEvents(did) {

export async function deleteEvents(did) {
if (!did) {
throw "Invalid DID";
throw exceptions.INVALID_DID;
}

const id = did.split(':').pop();
Expand All @@ -90,8 +90,7 @@ export async function getQueue(registry) {
return [];
}

const ops = JSON.parse(row.ops);
return ops;
return JSON.parse(row.ops);
}
catch {
return [];
Expand All @@ -114,6 +113,5 @@ export async function clearQueue(registry, batch) {

export async function getAllKeys() {
const rows = await db.all('SELECT id FROM dids');
const ids = rows.map(row => row.id);
return ids;
return rows.map(row => row.id);
}
5 changes: 3 additions & 2 deletions src/db-wallet-json.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'fs';
import * as cipher from './cipher-lib.js';
import * as exceptions from './exceptions.js';

const dataFolder = 'data';
const walletName = `${dataFolder}/wallet.json`;
Expand All @@ -25,7 +26,7 @@ export function loadWallet() {

export function newWallet(mnemonic, overwrite) {
if (fs.existsSync(walletName) && !overwrite) {
throw "Wallet already exists";
throw exceptions.UPDATE_FAILED;
}

try {
Expand All @@ -49,6 +50,6 @@ export function newWallet(mnemonic, overwrite) {
return wallet;
}
catch (error) {
throw "Invalid mnemonic";
throw exceptions.INVALID_PARAMETER;
}
}
11 changes: 11 additions & 0 deletions src/exceptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const INVALID_DID = new Error('Invalid DID');
export const UNKNOWN_DID = new Error('Unknown DID');
export const UNKNOWN_ID = new Error('Unknown ID');
export const INVALID_UPDATE = new Error('Invalid update');
export const INVALID_OPERATION = new Error('Invalid operation');
export const INVALID_VERSION = new Error('Invalid version');
export const INVALID_TYPE = new Error('Invalid type');
export const INVALID_REGISTRY = new Error('Invalid registry');
export const INVALID_PARAMETER = new Error('Invalid parameter');
export const UPDATE_FAILED = new Error('Update failed');
export const EXPECTED_EXCEPTION = new Error('Expected to throw an exception');
Loading

0 comments on commit cf2c41f

Please sign in to comment.