-
-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(lib): simplified lib by externalizing crypto lib
- Loading branch information
1 parent
9522dab
commit 6acd186
Showing
33 changed files
with
518 additions
and
609 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
### Enclosed lib | ||
|
||
This package contains the core functionalities of [Enclosed](https://enclosed.cc/), an open-source project that aims to provide a simple and secure way to share e2e encrypted notes. | ||
|
||
## Installation | ||
|
||
```bash | ||
# with npm | ||
npm install @enclosed/lib | ||
|
||
# with yarn | ||
yarn add @enclosed/lib | ||
|
||
# with pnpm | ||
pnpm add @enclosed/lib | ||
``` | ||
|
||
## Usage | ||
|
||
```javascript | ||
import { createNote } from '@enclosed/lib'; | ||
|
||
const { noteUrl } = await createNote({ | ||
content: 'Hello, World!', | ||
password: 'password', | ||
ttlInSeconds: 3600, | ||
deleteAfterReading: true, | ||
}); | ||
|
||
console.log(noteUrl); | ||
``` | ||
|
||
## License | ||
|
||
This project is licensed under the Apache 2.0 License. See the [LICENSE](./LICENSE) file for more information. | ||
|
||
## Credits and Acknowledgements | ||
|
||
This project is crafted with ❤️ by [Corentin Thomasset](https://corentin.tech). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { defineBuildConfig } from 'unbuild'; | ||
|
||
export default defineBuildConfig({ | ||
entries: [ | ||
'src/index.node', | ||
'src/index.web', | ||
], | ||
clean: true, | ||
declaration: true, | ||
sourcemap: true, | ||
rollup: { | ||
emitCJS: true, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import antfu from '@antfu/eslint-config'; | ||
|
||
export default antfu({ | ||
stylistic: { | ||
semi: true, | ||
}, | ||
|
||
rules: { | ||
// To allow export on top of files | ||
'ts/no-use-before-define': ['error', { allowNamedExports: true, functions: false }], | ||
'curly': ['error', 'all'], | ||
'vitest/consistent-test-it': ['error', { fn: 'test' }], | ||
'ts/consistent-type-definitions': ['error', 'type'], | ||
'style/brace-style': ['error', '1tbs', { allowSingleLine: false }], | ||
'unused-imports/no-unused-vars': ['error', { | ||
argsIgnorePattern: '^_', | ||
varsIgnorePattern: '^_', | ||
caughtErrorsIgnorePattern: '^_', | ||
}], | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
{ | ||
"name": "@enclosed/crypto", | ||
"type": "module", | ||
"version": "1.6.2", | ||
"packageManager": "[email protected]", | ||
"description": "Enclosed cross-env crypto primitives", | ||
"author": "Corentin Thomasset <[email protected]> (https://corentin.tech)", | ||
"license": "Apache-2.0", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/CorentinTh/enclosed" | ||
}, | ||
"exports": { | ||
"./package.json": "./package.json", | ||
".": { | ||
"browser": "./dist/index.web.mjs", | ||
"bun": "./dist/index.web.mjs", | ||
"deno": "./dist/index.web.mjs", | ||
"edge-light": "./dist/index.web.mjs", | ||
"edge-routine": "./dist/index.web.mjs", | ||
"netlify": "./dist/index.web.mjs", | ||
"react-native": "./dist/index.web.mjs", | ||
"wintercg": "./dist/index.web.mjs", | ||
"worker": "./dist/index.web.mjs", | ||
"workerd": "./dist/index.web.mjs", | ||
"node": { | ||
"import": { | ||
"types": "./dist/index.node.d.mts", | ||
"default": "./dist/index.node.mjs" | ||
}, | ||
"require": { | ||
"types": "./dist/index.node.d.cts", | ||
"default": "./dist/index.node.cjs" | ||
} | ||
}, | ||
"types": "./dist/index.web.d.mts", | ||
"import": { | ||
"types": "./dist/index.web.d.mts", | ||
"default": "./dist/index.web.mjs" | ||
}, | ||
"require": { | ||
"types": "./dist/index.node.d.cts", | ||
"default": "./dist/index.node.cjs" | ||
}, | ||
"default": "./dist/index.web.mjs" | ||
}, | ||
"./node": { | ||
"import": { | ||
"types": "./dist/index.node.d.mts", | ||
"default": "./dist/index.node.mjs" | ||
}, | ||
"require": { | ||
"types": "./dist/index.node.d.cts", | ||
"default": "./dist/index.node.cjs" | ||
} | ||
} | ||
}, | ||
"main": "./dist/index.node.cjs", | ||
"module": "./dist/index.web.mjs", | ||
"types": "./dist/index.web.d.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"engines": { | ||
"node": ">=22.0.0" | ||
}, | ||
"react-native": "./dist/index.web.mjs", | ||
"scripts": { | ||
"prepare": "pnpm run build", | ||
"build": "unbuild", | ||
"lint": "eslint .", | ||
"lint:fix": "eslint --fix .", | ||
"test": "pnpm run test:unit", | ||
"test:unit": "vitest run", | ||
"test:unit:watch": "vitest watch", | ||
"typecheck": "tsc --noEmit", | ||
"prepublishOnly": "pnpm run build" | ||
}, | ||
"dependencies": { | ||
"lodash-es": "^4.17.21" | ||
}, | ||
"devDependencies": { | ||
"@antfu/eslint-config": "^3.0.0", | ||
"@types/lodash-es": "^4.17.12", | ||
"@types/node": "^22.5.4", | ||
"@vitest/coverage-v8": "^2.0.5", | ||
"eslint": "^9.10.0", | ||
"tsx": "^4.17.0", | ||
"typescript": "^5.5.4", | ||
"unbuild": "^2.0.0", | ||
"vitest": "^2.0.5" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import type { EncryptionMethodsDefinition } from './encryption-algorithms/encryption-algorithms.types'; | ||
import { createEncryptionAlgorithmsRegistry } from './encryption-algorithms/encryption-algorithms.registry'; | ||
|
||
export { createEnclosedCryptoApi }; | ||
|
||
function createEnclosedCryptoApi({ | ||
encryptionMethodDefinitions, | ||
...api | ||
}: { | ||
generateBaseKey: () => { baseKey: Uint8Array }; | ||
deriveMasterKey: ({ baseKey, password }: { baseKey: Uint8Array; password?: string }) => Promise<{ masterKey: Uint8Array }>; | ||
base64UrlToBuffer: ({ base64Url }: { base64Url: string }) => Uint8Array; | ||
bufferToBase64Url: ({ buffer }: { buffer: Uint8Array }) => string; | ||
encryptionMethodDefinitions: { | ||
'aes-256-gcm': EncryptionMethodsDefinition; | ||
}; | ||
}) { | ||
const { encryptionAlgorithms, getDecryptionMethod, getEncryptionMethod } = createEncryptionAlgorithmsRegistry({ encryptionMethodDefinitions }); | ||
|
||
return { | ||
...api, | ||
encryptionAlgorithms, | ||
encryptionMethodDefinitions, | ||
getDecryptionMethod, | ||
getEncryptionMethod, | ||
}; | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/crypto/src/encryption-algorithms/encryption-algorithms.models.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export { defineEncryptionMethods }; | ||
|
||
function defineEncryptionMethods<Name extends string>(args: { | ||
name: Name; | ||
encryptBuffer: (args: { buffer: Uint8Array; encryptionKey: Uint8Array }) => Promise<{ encryptedString: string }>; | ||
decryptString: (args: { encryptedString: string; encryptionKey: Uint8Array }) => Promise<{ decryptedBuffer: Uint8Array }>; | ||
}) { | ||
return args; | ||
} |
38 changes: 38 additions & 0 deletions
38
packages/crypto/src/encryption-algorithms/encryption-algorithms.registry.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import type { EncryptionMethodsDefinition } from './encryption-algorithms.types'; | ||
import { keys } from 'lodash-es'; | ||
|
||
export { createEncryptionAlgorithmsRegistry }; | ||
|
||
function createEncryptionAlgorithmsRegistry<EncryptionAlgorithmNames extends string>({ encryptionMethodDefinitions }: { encryptionMethodDefinitions: Record<EncryptionAlgorithmNames, EncryptionMethodsDefinition> }) { | ||
const encryptionAlgorithms = keys(encryptionMethodDefinitions); | ||
|
||
return { | ||
encryptionMethodDefinitions, | ||
encryptionAlgorithms, | ||
|
||
getEncryptionMethod: ({ encryptionAlgorithm }: { encryptionAlgorithm: string }) => { | ||
const encryptionMethods = encryptionMethodDefinitions[encryptionAlgorithm]; | ||
|
||
if (!encryptionMethods) { | ||
throw new Error(`Encryption algorithm "${encryptionAlgorithm}" not found`); | ||
} | ||
|
||
const { encryptBuffer } = encryptionMethods; | ||
|
||
return { encryptBuffer }; | ||
}, | ||
|
||
getDecryptionMethod: ({ encryptionAlgorithm }: { encryptionAlgorithm: string }) => { | ||
const encryptionMethods = encryptionMethodDefinitions[encryptionAlgorithm]; | ||
|
||
if (!encryptionMethods) { | ||
throw new Error(`Encryption algorithm "${encryptionAlgorithm}" not found`); | ||
} | ||
|
||
const { decryptString } = encryptionMethods; | ||
|
||
return { decryptString }; | ||
}, | ||
|
||
}; | ||
}; |
4 changes: 4 additions & 0 deletions
4
packages/crypto/src/encryption-algorithms/encryption-algorithms.types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export type EncryptionMethodsDefinition = { | ||
encryptBuffer: (args: { buffer: Uint8Array; encryptionKey: Uint8Array }) => Promise<{ encryptedString: string }>; | ||
decryptString: (args: { encryptedString: string; encryptionKey: Uint8Array }) => Promise<{ decryptedBuffer: Uint8Array }>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { createEnclosedCryptoApi } from './api-definition'; | ||
import * as webCryptoApi from './node/crypto.node.usecases'; | ||
import { aes256GcmEncryptionAlgorithmDefinition } from './node/encryption-algorithms/crypto.node.aes-256-gcm'; | ||
|
||
export const { | ||
deriveMasterKey, | ||
generateBaseKey, | ||
encryptionAlgorithms, | ||
encryptionMethodDefinitions, | ||
getDecryptionMethod, | ||
getEncryptionMethod, | ||
base64UrlToBuffer, | ||
bufferToBase64Url, | ||
} = createEnclosedCryptoApi({ | ||
...webCryptoApi, | ||
encryptionMethodDefinitions: { | ||
'aes-256-gcm': aes256GcmEncryptionAlgorithmDefinition, | ||
}, | ||
}); |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { createEnclosedCryptoApi } from './api-definition'; | ||
import * as webCryptoApi from './web/crypto.web.usecases'; | ||
import { aes256GcmEncryptionAlgorithmDefinition } from './web/encryption-algorithms/crypto.web.aes-256-gcm'; | ||
|
||
export const { | ||
deriveMasterKey, | ||
generateBaseKey, | ||
encryptionAlgorithms, | ||
encryptionMethodDefinitions, | ||
getDecryptionMethod, | ||
getEncryptionMethod, | ||
base64UrlToBuffer, | ||
bufferToBase64Url, | ||
} = createEnclosedCryptoApi({ | ||
...webCryptoApi, | ||
encryptionMethodDefinitions: { | ||
'aes-256-gcm': aes256GcmEncryptionAlgorithmDefinition, | ||
}, | ||
}); |
2 changes: 1 addition & 1 deletion
2
...rc/crypto/node/crypto.node.models.test.ts → ...pto/src/node/crypto.node.usecases.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 14 additions & 3 deletions
17
...b/src/crypto/node/crypto.node.usecases.ts → ...s/crypto/src/node/crypto.node.usecases.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
...ion-algorithms/crypto.node.aes-256-gcm.ts → ...ion-algorithms/crypto.node.aes-256-gcm.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
2 changes: 1 addition & 1 deletion
2
.../src/crypto/web/crypto.web.models.test.ts → ...rypto/src/web/crypto.web.usecases.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 28 additions & 2 deletions
30
...lib/src/crypto/web/crypto.web.usecases.ts → ...ges/crypto/src/web/crypto.web.usecases.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
...tion-algorithms/crypto.web.aes-256-gcm.ts → ...tion-algorithms/crypto.web.aes-256-gcm.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.