@polymarbot/shared 0.3.0 → 0.3.1

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.
Files changed (3) hide show
  1. package/crypto.cjs +10 -5
  2. package/crypto.js +10 -5
  3. package/package.json +1 -1
package/crypto.cjs CHANGED
@@ -38,7 +38,11 @@ var import_node_crypto = __toESM(require("crypto"), 1);
38
38
  var ALGORITHM = "aes-256-gcm";
39
39
  var IV_LENGTH = 12;
40
40
  var AUTH_TAG_LENGTH = 16;
41
- var ENCRYPTION_KEY = (() => {
41
+ var cachedKey = null;
42
+ function getEncryptionKey() {
43
+ if (cachedKey) {
44
+ return cachedKey;
45
+ }
42
46
  const key = process.env.ENCRYPTION_KEY;
43
47
  if (!key) {
44
48
  throw new Error("ENCRYPTION_KEY environment variable is required");
@@ -47,11 +51,12 @@ var ENCRYPTION_KEY = (() => {
47
51
  if (keyBuffer.length !== 32) {
48
52
  throw new Error("ENCRYPTION_KEY must be a 32-byte hex string (64 characters)");
49
53
  }
50
- return keyBuffer;
51
- })();
54
+ cachedKey = keyBuffer;
55
+ return cachedKey;
56
+ }
52
57
  function encrypt(plaintext) {
53
58
  const iv = import_node_crypto.default.randomBytes(IV_LENGTH);
54
- const cipher = import_node_crypto.default.createCipheriv(ALGORITHM, ENCRYPTION_KEY, iv, {
59
+ const cipher = import_node_crypto.default.createCipheriv(ALGORITHM, getEncryptionKey(), iv, {
55
60
  authTagLength: AUTH_TAG_LENGTH
56
61
  });
57
62
  const encrypted = Buffer.concat([
@@ -75,7 +80,7 @@ function decrypt(encryptedData) {
75
80
  if (authTag.length !== AUTH_TAG_LENGTH) {
76
81
  throw new Error("Invalid auth tag length");
77
82
  }
78
- const decipher = import_node_crypto.default.createDecipheriv(ALGORITHM, ENCRYPTION_KEY, iv, {
83
+ const decipher = import_node_crypto.default.createDecipheriv(ALGORITHM, getEncryptionKey(), iv, {
79
84
  authTagLength: AUTH_TAG_LENGTH
80
85
  });
81
86
  decipher.setAuthTag(authTag);
package/crypto.js CHANGED
@@ -3,7 +3,11 @@ import crypto from "crypto";
3
3
  var ALGORITHM = "aes-256-gcm";
4
4
  var IV_LENGTH = 12;
5
5
  var AUTH_TAG_LENGTH = 16;
6
- var ENCRYPTION_KEY = (() => {
6
+ var cachedKey = null;
7
+ function getEncryptionKey() {
8
+ if (cachedKey) {
9
+ return cachedKey;
10
+ }
7
11
  const key = process.env.ENCRYPTION_KEY;
8
12
  if (!key) {
9
13
  throw new Error("ENCRYPTION_KEY environment variable is required");
@@ -12,11 +16,12 @@ var ENCRYPTION_KEY = (() => {
12
16
  if (keyBuffer.length !== 32) {
13
17
  throw new Error("ENCRYPTION_KEY must be a 32-byte hex string (64 characters)");
14
18
  }
15
- return keyBuffer;
16
- })();
19
+ cachedKey = keyBuffer;
20
+ return cachedKey;
21
+ }
17
22
  function encrypt(plaintext) {
18
23
  const iv = crypto.randomBytes(IV_LENGTH);
19
- const cipher = crypto.createCipheriv(ALGORITHM, ENCRYPTION_KEY, iv, {
24
+ const cipher = crypto.createCipheriv(ALGORITHM, getEncryptionKey(), iv, {
20
25
  authTagLength: AUTH_TAG_LENGTH
21
26
  });
22
27
  const encrypted = Buffer.concat([
@@ -40,7 +45,7 @@ function decrypt(encryptedData) {
40
45
  if (authTag.length !== AUTH_TAG_LENGTH) {
41
46
  throw new Error("Invalid auth tag length");
42
47
  }
43
- const decipher = crypto.createDecipheriv(ALGORITHM, ENCRYPTION_KEY, iv, {
48
+ const decipher = crypto.createDecipheriv(ALGORITHM, getEncryptionKey(), iv, {
44
49
  authTagLength: AUTH_TAG_LENGTH
45
50
  });
46
51
  decipher.setAuthTag(authTag);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polymarbot/shared",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "type": "module",
5
5
  "main": "./index.cjs",
6
6
  "module": "./index.js",