@polymarbot/shared 0.2.3 → 0.3.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/crypto.cjs +92 -0
- package/crypto.d.cts +5 -0
- package/crypto.d.ts +5 -0
- package/crypto.js +56 -0
- package/index.cjs +1 -0
- package/index.d.cts +1 -0
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +11 -1
package/crypto.cjs
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
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 ENCRYPTION_KEY = (() => {
|
|
42
|
+
const key = process.env.ENCRYPTION_KEY;
|
|
43
|
+
if (!key) {
|
|
44
|
+
throw new Error("ENCRYPTION_KEY environment variable is required");
|
|
45
|
+
}
|
|
46
|
+
const keyBuffer = Buffer.from(key, "hex");
|
|
47
|
+
if (keyBuffer.length !== 32) {
|
|
48
|
+
throw new Error("ENCRYPTION_KEY must be a 32-byte hex string (64 characters)");
|
|
49
|
+
}
|
|
50
|
+
return keyBuffer;
|
|
51
|
+
})();
|
|
52
|
+
function encrypt(plaintext) {
|
|
53
|
+
const iv = import_node_crypto.default.randomBytes(IV_LENGTH);
|
|
54
|
+
const cipher = import_node_crypto.default.createCipheriv(ALGORITHM, ENCRYPTION_KEY, iv, {
|
|
55
|
+
authTagLength: AUTH_TAG_LENGTH
|
|
56
|
+
});
|
|
57
|
+
const encrypted = Buffer.concat([
|
|
58
|
+
cipher.update(plaintext, "utf8"),
|
|
59
|
+
cipher.final()
|
|
60
|
+
]);
|
|
61
|
+
const authTag = cipher.getAuthTag();
|
|
62
|
+
return `${iv.toString("hex")}:${authTag.toString("hex")}:${encrypted.toString("hex")}`;
|
|
63
|
+
}
|
|
64
|
+
function decrypt(encryptedData) {
|
|
65
|
+
const parts = encryptedData.split(":");
|
|
66
|
+
if (parts.length !== 3) {
|
|
67
|
+
throw new Error("Invalid encrypted data format");
|
|
68
|
+
}
|
|
69
|
+
const [ivHex, authTagHex, ciphertext] = parts;
|
|
70
|
+
const iv = Buffer.from(ivHex, "hex");
|
|
71
|
+
const authTag = Buffer.from(authTagHex, "hex");
|
|
72
|
+
if (iv.length !== IV_LENGTH) {
|
|
73
|
+
throw new Error("Invalid IV length");
|
|
74
|
+
}
|
|
75
|
+
if (authTag.length !== AUTH_TAG_LENGTH) {
|
|
76
|
+
throw new Error("Invalid auth tag length");
|
|
77
|
+
}
|
|
78
|
+
const decipher = import_node_crypto.default.createDecipheriv(ALGORITHM, ENCRYPTION_KEY, iv, {
|
|
79
|
+
authTagLength: AUTH_TAG_LENGTH
|
|
80
|
+
});
|
|
81
|
+
decipher.setAuthTag(authTag);
|
|
82
|
+
const decrypted = Buffer.concat([
|
|
83
|
+
decipher.update(Buffer.from(ciphertext, "hex")),
|
|
84
|
+
decipher.final()
|
|
85
|
+
]);
|
|
86
|
+
return decrypted.toString("utf8");
|
|
87
|
+
}
|
|
88
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
89
|
+
0 && (module.exports = {
|
|
90
|
+
decrypt,
|
|
91
|
+
encrypt
|
|
92
|
+
});
|
package/crypto.d.cts
ADDED
package/crypto.d.ts
ADDED
package/crypto.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
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 ENCRYPTION_KEY = (() => {
|
|
7
|
+
const key = process.env.ENCRYPTION_KEY;
|
|
8
|
+
if (!key) {
|
|
9
|
+
throw new Error("ENCRYPTION_KEY environment variable is required");
|
|
10
|
+
}
|
|
11
|
+
const keyBuffer = Buffer.from(key, "hex");
|
|
12
|
+
if (keyBuffer.length !== 32) {
|
|
13
|
+
throw new Error("ENCRYPTION_KEY must be a 32-byte hex string (64 characters)");
|
|
14
|
+
}
|
|
15
|
+
return keyBuffer;
|
|
16
|
+
})();
|
|
17
|
+
function encrypt(plaintext) {
|
|
18
|
+
const iv = crypto.randomBytes(IV_LENGTH);
|
|
19
|
+
const cipher = crypto.createCipheriv(ALGORITHM, ENCRYPTION_KEY, iv, {
|
|
20
|
+
authTagLength: AUTH_TAG_LENGTH
|
|
21
|
+
});
|
|
22
|
+
const encrypted = Buffer.concat([
|
|
23
|
+
cipher.update(plaintext, "utf8"),
|
|
24
|
+
cipher.final()
|
|
25
|
+
]);
|
|
26
|
+
const authTag = cipher.getAuthTag();
|
|
27
|
+
return `${iv.toString("hex")}:${authTag.toString("hex")}:${encrypted.toString("hex")}`;
|
|
28
|
+
}
|
|
29
|
+
function decrypt(encryptedData) {
|
|
30
|
+
const parts = encryptedData.split(":");
|
|
31
|
+
if (parts.length !== 3) {
|
|
32
|
+
throw new Error("Invalid encrypted data format");
|
|
33
|
+
}
|
|
34
|
+
const [ivHex, authTagHex, ciphertext] = parts;
|
|
35
|
+
const iv = Buffer.from(ivHex, "hex");
|
|
36
|
+
const authTag = Buffer.from(authTagHex, "hex");
|
|
37
|
+
if (iv.length !== IV_LENGTH) {
|
|
38
|
+
throw new Error("Invalid IV length");
|
|
39
|
+
}
|
|
40
|
+
if (authTag.length !== AUTH_TAG_LENGTH) {
|
|
41
|
+
throw new Error("Invalid auth tag length");
|
|
42
|
+
}
|
|
43
|
+
const decipher = crypto.createDecipheriv(ALGORITHM, ENCRYPTION_KEY, iv, {
|
|
44
|
+
authTagLength: AUTH_TAG_LENGTH
|
|
45
|
+
});
|
|
46
|
+
decipher.setAuthTag(authTag);
|
|
47
|
+
const decrypted = Buffer.concat([
|
|
48
|
+
decipher.update(Buffer.from(ciphertext, "hex")),
|
|
49
|
+
decipher.final()
|
|
50
|
+
]);
|
|
51
|
+
return decrypted.toString("utf8");
|
|
52
|
+
}
|
|
53
|
+
export {
|
|
54
|
+
decrypt,
|
|
55
|
+
encrypt
|
|
56
|
+
};
|
package/index.cjs
CHANGED
package/index.d.cts
CHANGED
package/index.d.ts
CHANGED
package/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@polymarbot/shared",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
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",
|