Skip to content

Commit 04c8e92

Browse files
Allan2000-Gitallan-classcardrajdip-b
authored
feat: Create new keyshade common package (#970)
Co-authored-by: Allan Joston Fernandes <allan@classcardapp.com> Co-authored-by: Rajdip Bhattacharya <agentR47@gmail.com>
1 parent b88fbfa commit 04c8e92

File tree

18 files changed

+12936
-10328
lines changed

18 files changed

+12936
-10328
lines changed

apps/api/src/common/cryptography.spec.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
import {
2-
createKeyPair,
3-
decrypt,
4-
encrypt,
5-
generateApiKey,
6-
toSHA256
7-
} from './cryptography'
1+
import { decrypt, encrypt } from '@keyshade/common'
2+
import { createKeyPair, generateApiKey, toSHA256 } from './cryptography'
83

94
describe('Cryptography Tests', () => {
105
it('should be defined', () => {
@@ -39,7 +34,7 @@ describe('Cryptography Tests', () => {
3934
await decrypt(differentKeyPair.privateKey, encrypted)
4035
} catch (e) {
4136
expect(e).toBeDefined()
42-
expect(e.message).toEqual('Bad MAC')
37+
expect(e.message).toEqual('Decryption failed: Bad MAC')
4338
}
4439
})
4540

apps/api/src/common/cryptography.ts

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-var-requires */
22
import { createHash, randomBytes } from 'crypto'
33

4-
/**
5-
* Encrypts the given data using the given public key.
6-
*
7-
* @param publicKey The public key to use for encryption. Must be a hexadecimal string.
8-
* @param data The data to encrypt.
9-
*
10-
* @returns The encrypted data as a JSON string.
11-
*/
12-
export const encrypt = async (
13-
publicKey: string,
14-
data: string
15-
): Promise<string> => {
16-
const eccrypto = require('eccrypto')
17-
18-
const encrypted = await eccrypto.encrypt(
19-
Buffer.from(publicKey, 'hex'),
20-
Buffer.from(data)
21-
)
22-
23-
return JSON.stringify(encrypted)
24-
}
25-
26-
/**
27-
* Decrypts the given data using the given private key.
28-
*
29-
* @param privateKey The private key to use for decryption. Must be a hexadecimal string.
30-
* @param data The data to decrypt.
31-
*
32-
* @returns The decrypted data as a string.
33-
*/
34-
export const decrypt = async (
35-
privateKey: string,
36-
data: string
37-
): Promise<string> => {
38-
const eccrypto = require('eccrypto')
39-
const parsed = JSON.parse(data)
40-
41-
const eicesData = {
42-
iv: Buffer.from(parsed.iv),
43-
ephemPublicKey: Buffer.from(parsed.ephemPublicKey),
44-
ciphertext: Buffer.from(parsed.ciphertext),
45-
mac: Buffer.from(parsed.mac)
46-
}
47-
48-
const decrypted = await eccrypto.decrypt(
49-
Buffer.from(privateKey, 'hex'),
50-
eicesData
51-
)
52-
53-
return decrypted.toString()
54-
}
55-
564
/**
575
* Generates a new key pair.
586
*

apps/api/src/common/secret.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { SecretWithValues } from '@/secret/secret.types'
2-
import { decrypt } from './cryptography'
2+
import { decrypt } from '@keyshade/common'
33

44
export async function getSecretWithValues(
55
secretWithVersion: SecretWithValues['secret'] & {

apps/api/src/project/project.service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { v4 } from 'uuid'
2424
import { ProjectWithCounts, ProjectWithSecrets } from './project.types'
2525
import { ForkProject } from './dto/fork.project/fork.project'
2626
import { paginate } from '@/common/paginate'
27-
import { createKeyPair, decrypt, encrypt } from '@/common/cryptography'
27+
import { createKeyPair } from '@/common/cryptography'
2828
import { createEvent } from '@/common/event'
2929
import {
3030
constructErrorBody,
@@ -34,6 +34,7 @@ import {
3434
import { AuthenticatedUser } from '@/user/user.types'
3535
import { TierLimitService } from '@/common/tier-limit.service'
3636
import SlugGenerator from '@/common/slug-generator.service'
37+
import { decrypt, encrypt } from '@keyshade/common'
3738

3839
@Injectable()
3940
export class ProjectService {

apps/api/src/secret/secret.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import {
3434
constructErrorBody,
3535
limitMaxItemsPerPage
3636
} from '@/common/util'
37-
import { decrypt, encrypt } from '@/common/cryptography'
3837
import { createEvent } from '@/common/event'
3938
import { getEnvironmentIdToSlugMap } from '@/common/environment'
4039
import { getSecretWithValues, generateSecretValue } from '@/common/secret'
@@ -43,6 +42,7 @@ import { SecretWithProject, SecretWithValues } from './secret.types'
4342
import { AuthenticatedUser } from '@/user/user.types'
4443
import { TierLimitService } from '@/common/tier-limit.service'
4544
import SlugGenerator from '@/common/slug-generator.service'
45+
import { decrypt, encrypt } from '@keyshade/common'
4646

4747
@Injectable()
4848
export class SecretService {

apps/platform/src/components/dashboard/secret/secretCard/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { useAtom, useSetAtom } from 'jotai'
44
import { NoteIconSVG } from '@public/svg/secret'
55
import { TrashWhiteSVG, EyeOpenSVG, EyeSlashSVG } from '@public/svg/shared'
66
import { useState } from 'react'
7+
import {decrypt} from '@keyshade/common'
78
import {
89
AccordionContent,
910
AccordionItem,
@@ -39,7 +40,6 @@ import {
3940
} from '@/store'
4041
import AvatarComponent from '@/components/common/avatar'
4142
import { copyToClipboard } from '@/lib/clipboard'
42-
import { decrypt } from '@/lib/decrypt'
4343

4444
interface SecretCardProps {
4545
secretData: Secret

apps/platform/src/lib/decrypt.ts

Lines changed: 0 additions & 67 deletions
This file was deleted.

codecov.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ flag_management:
5656
paths:
5757
- packages/secret-scan/
5858
carryforward: true
59+
statuses:
60+
- type: project
61+
target: 80%
62+
- type: patch
63+
target: 98%
64+
- name: common
65+
paths:
66+
- packages/common/
67+
carryforward: true
5968
statuses:
6069
- type: project
6170
target: 80%

package.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,23 +107,25 @@
107107
"lint:api-client": "turbo run lint --filter=@keyshade/api-client",
108108
"lint:secret-scan": "turbo run lint --filter=@keyshade/secret-scan",
109109
"lint:schema": "turbo run lint --filter=@keyshade/schema",
110+
"lint:common": "turbo run lint --filter=@keyshade/common",
110111
"build": "turbo run build",
111-
"build:api": "pnpm db:generate-types && pnpm build:schema && turbo run build --filter=@keyshade/api",
112+
"build:api": "pnpm db:generate-types && pnpm build:schema && pnpm build:common && turbo run build --filter=@keyshade/api",
112113
"build:web": "pnpm build:api-client && turbo run build --filter=@keyshade/web",
113-
"build:platform": "pnpm build:api-client && turbo run build --filter=@keyshade/platform",
114+
"build:platform": "pnpm build:api-client && pnpm build:common && turbo run build --filter=@keyshade/platform",
114115
"build:cli": "pnpm build:secret-scan && pnpm build:api-client && turbo run build --filter=@keyshade/cli",
115116
"build:api-client": "pnpm build:schema && turbo run --filter=@keyshade/api-client build",
116117
"build:schema": "turbo run build --filter=@keyshade/schema",
117118
"build:secret-scan": "turbo run build --filter=@keyshade/secret-scan",
118-
"build:packages": "pnpm build:schema && turbo run --filter=@keyshade/api-client build && turbo run build --filter=@keyshade/secret-scan",
119+
"build:common": "turbo run build --filter=@keyshade/common",
120+
"build:packages": "pnpm build:schema && turbo run --filter=@keyshade/api-client build && turbo run build --filter=@keyshade/secret-scan && turbo run build --filter=@keyshade/common",
119121
"start": "turbo run start",
120122
"start:api": "turbo run start --filter=@keyshade/api",
121123
"start:web": "turbo run start --filter=@keyshade/web",
122124
"start:platform": "turbo run start --filter=@keyshade/platform",
123125
"start:cli": "turbo run start --filter=@keyshade/cli",
124126
"test": "turbo run test",
125127
"test:api": "pnpm unit:api && pnpm e2e:api",
126-
"unit:api": "pnpm run --filter=@keyshade/api unit",
128+
"unit:api": "pnpm build:common && pnpm run --filter=@keyshade/api unit",
127129
"e2e:api": "pnpm run --filter=@keyshade/api e2e",
128130
"test:web": "turbo run test --filter=@keyshade/web",
129131
"test:platform": "turbo run test --filter=@keyshade/platform",
@@ -171,6 +173,7 @@
171173
"@keyshade/api-client": "workspace:*",
172174
"@keyshade/schema": "workspace:*",
173175
"@keyshade/secret-scan": "workspace:*",
176+
"@keyshade/common": "workspace:*",
174177
"@semantic-release/changelog": "^6.0.3",
175178
"@semantic-release/commit-analyzer": "^12.0.0",
176179
"@semantic-release/git": "^10.0.1",

packages/common/.eslintrc.cjs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module.exports = {
2+
parser: '@typescript-eslint/parser',
3+
parserOptions: {
4+
project: 'tsconfig.json',
5+
tsconfigRootDir: __dirname,
6+
sourceType: 'module'
7+
},
8+
plugins: ['@typescript-eslint/eslint-plugin'],
9+
extends: [
10+
'plugin:@typescript-eslint/recommended',
11+
'plugin:prettier/recommended'
12+
],
13+
root: true,
14+
env: {
15+
node: true,
16+
jest: true
17+
},
18+
ignorePatterns: ['.eslintrc.js'],
19+
rules: {
20+
'@typescript-eslint/interface-name-prefix': 'off',
21+
'@typescript-eslint/explicit-function-return-type': 'off',
22+
'@typescript-eslint/explicit-module-boundary-types': 'off',
23+
'@typescript-eslint/no-explicit-any': 'off',
24+
'@typescript-eslint/no-unused-vars': ['warn']
25+
}
26+
}

packages/common/.npmignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.turbo/
2+
src/
3+
tests/
4+
eslintrc.js
5+
jest.config.ts
6+
tsconfig.json
7+
tsconfig.spec.json
8+
node_modules/

packages/common/package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "@keyshade/common",
3+
"version": "1.0.0",
4+
"main": "dist/index.js",
5+
"description": "This package includes common functions, constants, and other utilities used across multiple applications.",
6+
"private": true,
7+
"scripts": {
8+
"build": "tsc && tsc-alias",
9+
"watch": "tsc -w",
10+
"start": "node dist/index.js",
11+
"dev": "npx tsc && node dist/index.js",
12+
"lint": "eslint \"{src,tests}/**/*.ts\" --fix",
13+
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\""
14+
},
15+
"dependencies": {
16+
"eccrypto": "^1.1.6"
17+
},
18+
"devDependencies": {
19+
"@types/eccrypto": "^1.1.6"
20+
}
21+
}

0 commit comments

Comments
 (0)