@dotenvx/primitives 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +28 -0
- package/README.md +123 -0
- package/dist/decrypt.cjs +5964 -0
- package/dist/index.cjs +5974 -0
- package/package.json +17 -10
- package/src/decrypt.js +0 -32
- package/src/errors.js +0 -77
- package/src/index.js +0 -5
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "0.
|
|
2
|
+
"version": "0.2.0",
|
|
3
3
|
"name": "@dotenvx/primitives",
|
|
4
4
|
"description": "a secure dotenv–from the creator of `dotenv`",
|
|
5
5
|
"author": "@motdotla",
|
|
6
6
|
"keywords": [
|
|
7
|
+
"dotenvx",
|
|
7
8
|
"dotenv",
|
|
8
9
|
"env"
|
|
9
10
|
],
|
|
@@ -13,22 +14,28 @@
|
|
|
13
14
|
"url": "git+https://github.com/dotenvx/dotenvx.git"
|
|
14
15
|
},
|
|
15
16
|
"license": "BSD-3-Clause",
|
|
17
|
+
"main": "dist/index.cjs",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": "./dist/index.cjs",
|
|
20
|
+
"./decrypt": "./dist/decrypt.cjs"
|
|
21
|
+
},
|
|
16
22
|
"files": [
|
|
17
|
-
"
|
|
23
|
+
"dist/**/*",
|
|
24
|
+
"LICENSE",
|
|
25
|
+
"README.md"
|
|
18
26
|
],
|
|
19
27
|
"scripts": {
|
|
20
|
-
"
|
|
21
|
-
"
|
|
28
|
+
"build": "esbuild src/index.js src/decrypt.js --bundle --platform=node --format=cjs --target=node16 --outdir=dist --out-extension:.js=.cjs --legal-comments=none",
|
|
29
|
+
"prepack": "npm run build",
|
|
30
|
+
"prepublishOnly": "npm pack --dry-run",
|
|
31
|
+
"standard": "standard src",
|
|
32
|
+
"standard:fix": "standard src --fix"
|
|
22
33
|
},
|
|
23
|
-
"
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"esbuild": "^0.28.1",
|
|
24
36
|
"eciesjs": "^0.5.0"
|
|
25
37
|
},
|
|
26
38
|
"publishConfig": {
|
|
27
39
|
"access": "public"
|
|
28
|
-
},
|
|
29
|
-
"main": "src/index.js",
|
|
30
|
-
"exports": {
|
|
31
|
-
".": "./src/index.js",
|
|
32
|
-
"./decrypt": "./src/decrypt.js"
|
|
33
40
|
}
|
|
34
41
|
}
|
package/src/decrypt.js
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
const { decrypt: eciesDecrypt } = require('eciesjs')
|
|
2
|
-
const Errors = require('./errors')
|
|
3
|
-
|
|
4
|
-
const PREFIX = 'encrypted:'
|
|
5
|
-
|
|
6
|
-
function decrypt (privateKeyHex, encryptedValue) {
|
|
7
|
-
if (!encryptedValue.startsWith(PREFIX)) {
|
|
8
|
-
return encryptedValue
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
if (!privateKeyHex || privateKeyHex.length < 1) {
|
|
12
|
-
throw new Errors().missingPrivateKey()
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
try {
|
|
16
|
-
const privateKey = Buffer.from(privateKeyHex, 'hex')
|
|
17
|
-
const ciphertext = Buffer.from(encryptedValue.substring(PREFIX.length), 'base64')
|
|
18
|
-
return Buffer.from(eciesDecrypt(privateKey, ciphertext)).toString('utf8')
|
|
19
|
-
} catch (e) {
|
|
20
|
-
if (e.message === 'Invalid private key') {
|
|
21
|
-
throw new Errors().invalidPrivateKey()
|
|
22
|
-
} else if (e.message === 'Unsupported state or unable to authenticate data') {
|
|
23
|
-
throw new Errors().wrongPrivateKey()
|
|
24
|
-
} else if (e.message === 'Point of length 65 was invalid. Expected 33 compressed bytes or 65 uncompressed bytes') {
|
|
25
|
-
throw new Errors().malformedEncryptedData()
|
|
26
|
-
} else {
|
|
27
|
-
throw new Errors({ message: e.message }).decryptionFailed()
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
module.exports = decrypt
|
package/src/errors.js
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
const ISSUE_BY_CODE = {
|
|
2
|
-
MISSING_PRIVATE_KEY: 'https://github.com/dotenvx/dotenvx/issues/464',
|
|
3
|
-
INVALID_PRIVATE_KEY: 'https://github.com/dotenvx/dotenvx/issues/465',
|
|
4
|
-
WRONG_PRIVATE_KEY: 'https://github.com/dotenvx/dotenvx/issues/466',
|
|
5
|
-
MALFORMED_ENCRYPTED_DATA: 'https://github.com/dotenvx/dotenvx/issues/467',
|
|
6
|
-
DECRYPTION_FAILED: 'https://github.com/dotenvx/dotenvx/issues/757'
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
class Errors {
|
|
10
|
-
constructor (options = {}) {
|
|
11
|
-
this.message = options.message
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
missingPrivateKey () {
|
|
15
|
-
const code = 'MISSING_PRIVATE_KEY'
|
|
16
|
-
const message = `[${code}] could not decrypt because private key is missing`
|
|
17
|
-
const help = `fix: [${ISSUE_BY_CODE[code]}]`
|
|
18
|
-
|
|
19
|
-
const e = new Error(message)
|
|
20
|
-
e.code = code
|
|
21
|
-
e.help = help
|
|
22
|
-
e.messageWithHelp = `${message}. ${help}`
|
|
23
|
-
return e
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
invalidPrivateKey () {
|
|
27
|
-
const code = 'INVALID_PRIVATE_KEY'
|
|
28
|
-
const message = `[${code}] could not decrypt using private key`
|
|
29
|
-
const help = `fix: [${ISSUE_BY_CODE[code]}]`
|
|
30
|
-
|
|
31
|
-
const e = new Error(message)
|
|
32
|
-
e.code = code
|
|
33
|
-
e.help = help
|
|
34
|
-
e.messageWithHelp = `${message}. ${help}`
|
|
35
|
-
return e
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
wrongPrivateKey () {
|
|
39
|
-
const code = 'WRONG_PRIVATE_KEY'
|
|
40
|
-
const message = `[${code}] could not decrypt using private key`
|
|
41
|
-
const help = `fix: [${ISSUE_BY_CODE[code]}]`
|
|
42
|
-
|
|
43
|
-
const e = new Error(message)
|
|
44
|
-
e.code = code
|
|
45
|
-
e.help = help
|
|
46
|
-
e.messageWithHelp = `${message}. ${help}`
|
|
47
|
-
return e
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
malformedEncryptedData () {
|
|
51
|
-
const code = 'MALFORMED_ENCRYPTED_DATA'
|
|
52
|
-
const message = `[${code}] could not decrypt because encrypted data appears malformed`
|
|
53
|
-
const help = `fix: [${ISSUE_BY_CODE[code]}]`
|
|
54
|
-
|
|
55
|
-
const e = new Error(message)
|
|
56
|
-
e.code = code
|
|
57
|
-
e.help = help
|
|
58
|
-
e.messageWithHelp = `${message}. ${help}`
|
|
59
|
-
return e
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
decryptionFailed () {
|
|
63
|
-
const code = 'DECRYPTION_FAILED'
|
|
64
|
-
const message = `[${code}] ${this.message}`
|
|
65
|
-
const help = `fix: [${ISSUE_BY_CODE[code]}]`
|
|
66
|
-
|
|
67
|
-
const e = new Error(message)
|
|
68
|
-
e.code = code
|
|
69
|
-
e.help = help
|
|
70
|
-
e.messageWithHelp = `${message}. ${help}`
|
|
71
|
-
return e
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
Errors.ISSUE_BY_CODE = ISSUE_BY_CODE
|
|
76
|
-
|
|
77
|
-
module.exports = Errors
|