@polymarbot/shared 0.2.3 → 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.
package/crypto.cjs ADDED
@@ -0,0 +1,97 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/utils/crypto.ts
31
+ var crypto_exports = {};
32
+ __export(crypto_exports, {
33
+ decrypt: () => decrypt,
34
+ encrypt: () => encrypt
35
+ });
36
+ module.exports = __toCommonJS(crypto_exports);
37
+ var import_node_crypto = __toESM(require("crypto"), 1);
38
+ var ALGORITHM = "aes-256-gcm";
39
+ var IV_LENGTH = 12;
40
+ var AUTH_TAG_LENGTH = 16;
41
+ var cachedKey = null;
42
+ function getEncryptionKey() {
43
+ if (cachedKey) {
44
+ return cachedKey;
45
+ }
46
+ const key = process.env.ENCRYPTION_KEY;
47
+ if (!key) {
48
+ throw new Error("ENCRYPTION_KEY environment variable is required");
49
+ }
50
+ const keyBuffer = Buffer.from(key, "hex");
51
+ if (keyBuffer.length !== 32) {
52
+ throw new Error("ENCRYPTION_KEY must be a 32-byte hex string (64 characters)");
53
+ }
54
+ cachedKey = keyBuffer;
55
+ return cachedKey;
56
+ }
57
+ function encrypt(plaintext) {
58
+ const iv = import_node_crypto.default.randomBytes(IV_LENGTH);
59
+ const cipher = import_node_crypto.default.createCipheriv(ALGORITHM, getEncryptionKey(), iv, {
60
+ authTagLength: AUTH_TAG_LENGTH
61
+ });
62
+ const encrypted = Buffer.concat([
63
+ cipher.update(plaintext, "utf8"),
64
+ cipher.final()
65
+ ]);
66
+ const authTag = cipher.getAuthTag();
67
+ return `${iv.toString("hex")}:${authTag.toString("hex")}:${encrypted.toString("hex")}`;
68
+ }
69
+ function decrypt(encryptedData) {
70
+ const parts = encryptedData.split(":");
71
+ if (parts.length !== 3) {
72
+ throw new Error("Invalid encrypted data format");
73
+ }
74
+ const [ivHex, authTagHex, ciphertext] = parts;
75
+ const iv = Buffer.from(ivHex, "hex");
76
+ const authTag = Buffer.from(authTagHex, "hex");
77
+ if (iv.length !== IV_LENGTH) {
78
+ throw new Error("Invalid IV length");
79
+ }
80
+ if (authTag.length !== AUTH_TAG_LENGTH) {
81
+ throw new Error("Invalid auth tag length");
82
+ }
83
+ const decipher = import_node_crypto.default.createDecipheriv(ALGORITHM, getEncryptionKey(), iv, {
84
+ authTagLength: AUTH_TAG_LENGTH
85
+ });
86
+ decipher.setAuthTag(authTag);
87
+ const decrypted = Buffer.concat([
88
+ decipher.update(Buffer.from(ciphertext, "hex")),
89
+ decipher.final()
90
+ ]);
91
+ return decrypted.toString("utf8");
92
+ }
93
+ // Annotate the CommonJS export names for ESM import in node:
94
+ 0 && (module.exports = {
95
+ decrypt,
96
+ encrypt
97
+ });
package/crypto.d.cts ADDED
@@ -0,0 +1,5 @@
1
+ declare function encrypt(plaintext: string): string;
2
+
3
+ declare function decrypt(encryptedData: string): string;
4
+
5
+ export { decrypt, encrypt };
package/crypto.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ declare function encrypt(plaintext: string): string;
2
+
3
+ declare function decrypt(encryptedData: string): string;
4
+
5
+ export { decrypt, encrypt };
package/crypto.js ADDED
@@ -0,0 +1,61 @@
1
+ // src/utils/crypto.ts
2
+ import crypto from "crypto";
3
+ var ALGORITHM = "aes-256-gcm";
4
+ var IV_LENGTH = 12;
5
+ var AUTH_TAG_LENGTH = 16;
6
+ var cachedKey = null;
7
+ function getEncryptionKey() {
8
+ if (cachedKey) {
9
+ return cachedKey;
10
+ }
11
+ const key = process.env.ENCRYPTION_KEY;
12
+ if (!key) {
13
+ throw new Error("ENCRYPTION_KEY environment variable is required");
14
+ }
15
+ const keyBuffer = Buffer.from(key, "hex");
16
+ if (keyBuffer.length !== 32) {
17
+ throw new Error("ENCRYPTION_KEY must be a 32-byte hex string (64 characters)");
18
+ }
19
+ cachedKey = keyBuffer;
20
+ return cachedKey;
21
+ }
22
+ function encrypt(plaintext) {
23
+ const iv = crypto.randomBytes(IV_LENGTH);
24
+ const cipher = crypto.createCipheriv(ALGORITHM, getEncryptionKey(), iv, {
25
+ authTagLength: AUTH_TAG_LENGTH
26
+ });
27
+ const encrypted = Buffer.concat([
28
+ cipher.update(plaintext, "utf8"),
29
+ cipher.final()
30
+ ]);
31
+ const authTag = cipher.getAuthTag();
32
+ return `${iv.toString("hex")}:${authTag.toString("hex")}:${encrypted.toString("hex")}`;
33
+ }
34
+ function decrypt(encryptedData) {
35
+ const parts = encryptedData.split(":");
36
+ if (parts.length !== 3) {
37
+ throw new Error("Invalid encrypted data format");
38
+ }
39
+ const [ivHex, authTagHex, ciphertext] = parts;
40
+ const iv = Buffer.from(ivHex, "hex");
41
+ const authTag = Buffer.from(authTagHex, "hex");
42
+ if (iv.length !== IV_LENGTH) {
43
+ throw new Error("Invalid IV length");
44
+ }
45
+ if (authTag.length !== AUTH_TAG_LENGTH) {
46
+ throw new Error("Invalid auth tag length");
47
+ }
48
+ const decipher = crypto.createDecipheriv(ALGORITHM, getEncryptionKey(), iv, {
49
+ authTagLength: AUTH_TAG_LENGTH
50
+ });
51
+ decipher.setAuthTag(authTag);
52
+ const decrypted = Buffer.concat([
53
+ decipher.update(Buffer.from(ciphertext, "hex")),
54
+ decipher.final()
55
+ ]);
56
+ return decrypted.toString("utf8");
57
+ }
58
+ export {
59
+ decrypt,
60
+ encrypt
61
+ };
package/index.cjs CHANGED
@@ -1,5 +1,6 @@
1
1
  module.exports = {
2
2
  ...require('./basic.cjs'),
3
+ ...require('./crypto.cjs'),
3
4
  ...require('./markets/tools.cjs'),
4
5
  ...require('./markets/types.cjs'),
5
6
  ...require('./relayer-client.cjs'),
package/index.d.cts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './basic.js'
2
+ export * from './crypto.js'
2
3
  export * from './markets/tools.js'
3
4
  export * from './markets/types.js'
4
5
  export * from './relayer-client.js'
package/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './basic.js'
2
+ export * from './crypto.js'
2
3
  export * from './markets/tools.js'
3
4
  export * from './markets/types.js'
4
5
  export * from './relayer-client.js'
package/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './basic.js'
2
+ export * from './crypto.js'
2
3
  export * from './markets/tools.js'
3
4
  export * from './markets/types.js'
4
5
  export * from './relayer-client.js'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polymarbot/shared",
3
- "version": "0.2.3",
3
+ "version": "0.3.1",
4
4
  "type": "module",
5
5
  "main": "./index.cjs",
6
6
  "module": "./index.js",
@@ -26,6 +26,16 @@
26
26
  "default": "./basic.cjs"
27
27
  }
28
28
  },
29
+ "./crypto": {
30
+ "import": {
31
+ "types": "./crypto.d.ts",
32
+ "default": "./crypto.js"
33
+ },
34
+ "require": {
35
+ "types": "./crypto.d.cts",
36
+ "default": "./crypto.cjs"
37
+ }
38
+ },
29
39
  "./markets/tools": {
30
40
  "import": {
31
41
  "types": "./markets/tools.d.ts",