@polymarbot/shared 0.2.2 → 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 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
@@ -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,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
@@ -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.2",
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",
@@ -58,23 +58,23 @@ function normalizeTradeStepBuy(step, options) {
58
58
  const isLowPriceMode = price <= 0.5;
59
59
  const isChaseMode = price > 0.5;
60
60
  if (isLowPriceMode) {
61
- if (step.maxUnderlyingDiff !== void 0) {
62
- if (step.maxUnderlyingDiff >= 1 && step.maxUnderlyingDiff <= 1e3) {
63
- normalized.maxUnderlyingDiff = step.maxUnderlyingDiff;
61
+ if (step.maxUnderlyingChange !== void 0) {
62
+ if (step.maxUnderlyingChange >= 1 && step.maxUnderlyingChange <= 1e3) {
63
+ normalized.maxUnderlyingChange = step.maxUnderlyingChange;
64
64
  } else {
65
65
  }
66
66
  }
67
- if (step.minUnderlyingDiff !== void 0) {
67
+ if (step.minUnderlyingChange !== void 0) {
68
68
  }
69
69
  }
70
70
  if (isChaseMode) {
71
- if (step.minUnderlyingDiff !== void 0) {
72
- if (step.minUnderlyingDiff >= 1 && step.minUnderlyingDiff <= 1e3) {
73
- normalized.minUnderlyingDiff = step.minUnderlyingDiff;
71
+ if (step.minUnderlyingChange !== void 0) {
72
+ if (step.minUnderlyingChange >= 1 && step.minUnderlyingChange <= 1e3) {
73
+ normalized.minUnderlyingChange = step.minUnderlyingChange;
74
74
  } else {
75
75
  }
76
76
  }
77
- if (step.maxUnderlyingDiff !== void 0) {
77
+ if (step.maxUnderlyingChange !== void 0) {
78
78
  }
79
79
  }
80
80
  return normalized;
@@ -108,8 +108,8 @@ function normalizeTradeStepSell(step, options) {
108
108
  const invalidFields = [];
109
109
  if ("once" in stepAsTradeStep && stepAsTradeStep.once !== void 0) invalidFields.push("once");
110
110
  if ("outcomeIndex" in stepAsTradeStep && stepAsTradeStep.outcomeIndex !== void 0) invalidFields.push("outcomeIndex");
111
- if ("maxUnderlyingDiff" in stepAsTradeStep && stepAsTradeStep.maxUnderlyingDiff !== void 0) invalidFields.push("maxUnderlyingDiff");
112
- if ("minUnderlyingDiff" in stepAsTradeStep && stepAsTradeStep.minUnderlyingDiff !== void 0) invalidFields.push("minUnderlyingDiff");
111
+ if ("maxUnderlyingChange" in stepAsTradeStep && stepAsTradeStep.maxUnderlyingChange !== void 0) invalidFields.push("maxUnderlyingChange");
112
+ if ("minUnderlyingChange" in stepAsTradeStep && stepAsTradeStep.minUnderlyingChange !== void 0) invalidFields.push("minUnderlyingChange");
113
113
  if (invalidFields.length > 0) {
114
114
  }
115
115
  return normalized;
@@ -35,23 +35,23 @@ function normalizeTradeStepBuy(step, options) {
35
35
  const isLowPriceMode = price <= 0.5;
36
36
  const isChaseMode = price > 0.5;
37
37
  if (isLowPriceMode) {
38
- if (step.maxUnderlyingDiff !== void 0) {
39
- if (step.maxUnderlyingDiff >= 1 && step.maxUnderlyingDiff <= 1e3) {
40
- normalized.maxUnderlyingDiff = step.maxUnderlyingDiff;
38
+ if (step.maxUnderlyingChange !== void 0) {
39
+ if (step.maxUnderlyingChange >= 1 && step.maxUnderlyingChange <= 1e3) {
40
+ normalized.maxUnderlyingChange = step.maxUnderlyingChange;
41
41
  } else {
42
42
  }
43
43
  }
44
- if (step.minUnderlyingDiff !== void 0) {
44
+ if (step.minUnderlyingChange !== void 0) {
45
45
  }
46
46
  }
47
47
  if (isChaseMode) {
48
- if (step.minUnderlyingDiff !== void 0) {
49
- if (step.minUnderlyingDiff >= 1 && step.minUnderlyingDiff <= 1e3) {
50
- normalized.minUnderlyingDiff = step.minUnderlyingDiff;
48
+ if (step.minUnderlyingChange !== void 0) {
49
+ if (step.minUnderlyingChange >= 1 && step.minUnderlyingChange <= 1e3) {
50
+ normalized.minUnderlyingChange = step.minUnderlyingChange;
51
51
  } else {
52
52
  }
53
53
  }
54
- if (step.maxUnderlyingDiff !== void 0) {
54
+ if (step.maxUnderlyingChange !== void 0) {
55
55
  }
56
56
  }
57
57
  return normalized;
@@ -85,8 +85,8 @@ function normalizeTradeStepSell(step, options) {
85
85
  const invalidFields = [];
86
86
  if ("once" in stepAsTradeStep && stepAsTradeStep.once !== void 0) invalidFields.push("once");
87
87
  if ("outcomeIndex" in stepAsTradeStep && stepAsTradeStep.outcomeIndex !== void 0) invalidFields.push("outcomeIndex");
88
- if ("maxUnderlyingDiff" in stepAsTradeStep && stepAsTradeStep.maxUnderlyingDiff !== void 0) invalidFields.push("maxUnderlyingDiff");
89
- if ("minUnderlyingDiff" in stepAsTradeStep && stepAsTradeStep.minUnderlyingDiff !== void 0) invalidFields.push("minUnderlyingDiff");
88
+ if ("maxUnderlyingChange" in stepAsTradeStep && stepAsTradeStep.maxUnderlyingChange !== void 0) invalidFields.push("maxUnderlyingChange");
89
+ if ("minUnderlyingChange" in stepAsTradeStep && stepAsTradeStep.minUnderlyingChange !== void 0) invalidFields.push("minUnderlyingChange");
90
90
  if (invalidFields.length > 0) {
91
91
  }
92
92
  return normalized;
@@ -8,7 +8,7 @@ interface PositionInfo {
8
8
  balance: string;
9
9
  }
10
10
 
11
- interface PriceDifferenceInfo {
11
+ interface PriceChangeInfo {
12
12
 
13
13
  start: number;
14
14
 
@@ -42,8 +42,8 @@ interface TradeStepBuy {
42
42
  end: number;
43
43
  once?: boolean;
44
44
  outcomeIndex?: 0 | 1;
45
- maxUnderlyingDiff?: number;
46
- minUnderlyingDiff?: number;
45
+ maxUnderlyingChange?: number;
46
+ minUnderlyingChange?: number;
47
47
  }
48
48
 
49
49
  interface TradeStepSell {
@@ -95,7 +95,7 @@ interface MarketTradeContext {
95
95
 
96
96
  tradeRecord: MarketTradeRecord;
97
97
 
98
- underlyingPrice: PriceDifferenceInfo | null;
98
+ underlyingPrice: PriceChangeInfo | null;
99
99
  }
100
100
 
101
101
  interface MarketTokenData extends MarketToken {
@@ -8,7 +8,7 @@ interface PositionInfo {
8
8
  balance: string;
9
9
  }
10
10
 
11
- interface PriceDifferenceInfo {
11
+ interface PriceChangeInfo {
12
12
 
13
13
  start: number;
14
14
 
@@ -42,8 +42,8 @@ interface TradeStepBuy {
42
42
  end: number;
43
43
  once?: boolean;
44
44
  outcomeIndex?: 0 | 1;
45
- maxUnderlyingDiff?: number;
46
- minUnderlyingDiff?: number;
45
+ maxUnderlyingChange?: number;
46
+ minUnderlyingChange?: number;
47
47
  }
48
48
 
49
49
  interface TradeStepSell {
@@ -95,7 +95,7 @@ interface MarketTradeContext {
95
95
 
96
96
  tradeRecord: MarketTradeRecord;
97
97
 
98
- underlyingPrice: PriceDifferenceInfo | null;
98
+ underlyingPrice: PriceChangeInfo | null;
99
99
  }
100
100
 
101
101
  interface MarketTokenData extends MarketToken {