@gbozee/ultimate 0.0.2-21 → 0.0.2-23

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/dist/index.d.ts CHANGED
@@ -265,6 +265,14 @@ export type ExchangeType = {
265
265
  export declare class AppDatabase {
266
266
  private pb;
267
267
  constructor(pb: PocketBase);
268
+ getCredentials(password: string): any;
269
+ saveCredentials(password: string, credentials: any): Promise<void>;
270
+ addNewCredential(password: string, payload: {
271
+ name: string;
272
+ exchange: string;
273
+ api_key: string;
274
+ api_secret: string;
275
+ }): Promise<void>;
268
276
  getAllSymbolsFromPositions(options?: {
269
277
  no_position?: boolean;
270
278
  kind?: "long" | "short";
@@ -462,6 +470,7 @@ declare class ExchangeAccount$1 {
462
470
  details for each symbol for an account in the database with an option
463
471
  to refresh.
464
472
  */
473
+ getDBInstance(): AppDatabase;
465
474
  getLiveExchangeInstance(payload: {
466
475
  symbol: string;
467
476
  refresh?: boolean;
@@ -784,10 +793,10 @@ declare class ExchangeAccount$1 {
784
793
  declare class App {
785
794
  private app_db;
786
795
  private getCredentials;
787
- constructor(app_db: AppDatabase, getCredentials: (account: string, exchange: string) => (account: string, exchange: string) => Promise<{
796
+ constructor(app_db: AppDatabase, getCredentials: (account: string, exchange: string) => {
788
797
  api_key: string;
789
798
  api_secret: string;
790
- }>);
799
+ });
791
800
  getExchangeAccount(account: ExchangeType): Promise<ExchangeAccount$1>;
792
801
  syncAccount(payload: {
793
802
  account: ExchangeType;
@@ -889,12 +898,13 @@ export declare function initApp(payload: {
889
898
  email: string;
890
899
  password: string;
891
900
  };
892
- getCredentials: (account: string, exchange: string) => (account: string, exchange: string) => Promise<{
901
+ password?: string;
902
+ getCredentials: (account: string, exchange: string) => {
893
903
  api_key: string;
894
904
  api_secret: string;
895
- }>;
905
+ };
896
906
  }): Promise<App>;
897
- export declare function initialize(): Promise<App>;
907
+ export declare function initialize(password?: string): Promise<App>;
898
908
 
899
909
  export {
900
910
  ExchangeAccount$1 as ExchangeAccount,
package/dist/index.js CHANGED
@@ -25320,7 +25320,7 @@ var require_websocket = __commonJS((exports, module) => {
25320
25320
  var http = __require("http");
25321
25321
  var net = __require("net");
25322
25322
  var tls = __require("tls");
25323
- var { randomBytes, createHash } = __require("crypto");
25323
+ var { randomBytes: randomBytes2, createHash } = __require("crypto");
25324
25324
  var { Duplex, Readable } = __require("stream");
25325
25325
  var { URL: URL2 } = __require("url");
25326
25326
  var PerMessageDeflate = require_permessage_deflate();
@@ -25729,7 +25729,7 @@ var require_websocket = __commonJS((exports, module) => {
25729
25729
  }
25730
25730
  }
25731
25731
  const defaultPort = isSecure ? 443 : 80;
25732
- const key = randomBytes(16).toString("base64");
25732
+ const key = randomBytes2(16).toString("base64");
25733
25733
  const request = isSecure ? https.request : http.request;
25734
25734
  const protocolSet = new Set;
25735
25735
  let perMessageDeflate;
@@ -32104,6 +32104,47 @@ class Client {
32104
32104
  // src/database.ts
32105
32105
  var import_socks_proxy_agent = __toESM(require_dist2(), 1);
32106
32106
  var import_https_proxy_agent = __toESM(require_dist3(), 1);
32107
+ import {
32108
+ createCipheriv,
32109
+ createDecipheriv,
32110
+ randomBytes,
32111
+ scryptSync
32112
+ } from "crypto";
32113
+ function encryptObject(obj, password) {
32114
+ const jsonString = JSON.stringify(obj);
32115
+ const salt = randomBytes(16);
32116
+ const key = scryptSync(password, salt, 32);
32117
+ const iv = randomBytes(12);
32118
+ const cipher = createCipheriv("aes-256-gcm", key, iv);
32119
+ const encrypted = Buffer.concat([
32120
+ cipher.update(jsonString, "utf8"),
32121
+ cipher.final()
32122
+ ]);
32123
+ const authTag = cipher.getAuthTag();
32124
+ const resultBuffer = Buffer.concat([salt, iv, authTag, encrypted]);
32125
+ return resultBuffer.toString("base64");
32126
+ }
32127
+ function decryptObject(encryptedString, password) {
32128
+ try {
32129
+ const dataBuffer = Buffer.from(encryptedString, "base64");
32130
+ const salt = dataBuffer.subarray(0, 16);
32131
+ const iv = dataBuffer.subarray(16, 28);
32132
+ const authTag = dataBuffer.subarray(28, 44);
32133
+ const encrypted = dataBuffer.subarray(44);
32134
+ const key = scryptSync(password, salt, 32);
32135
+ const decipher = createDecipheriv("aes-256-gcm", key, iv);
32136
+ decipher.setAuthTag(authTag);
32137
+ const decrypted = Buffer.concat([
32138
+ decipher.update(encrypted),
32139
+ decipher.final()
32140
+ ]);
32141
+ const jsonString = decrypted.toString("utf8");
32142
+ return JSON.parse(jsonString);
32143
+ } catch (error) {
32144
+ console.error("Decryption failed:", error.message);
32145
+ return null;
32146
+ }
32147
+ }
32107
32148
  async function initPocketBaseClient(proxy_credentials) {
32108
32149
  const pb = new Client(proxy_credentials.host);
32109
32150
  await pb.collection("_superusers").authWithPassword(proxy_credentials.email, proxy_credentials.password);
@@ -32115,6 +32156,37 @@ class AppDatabase {
32115
32156
  constructor(pb) {
32116
32157
  this.pb = pb;
32117
32158
  }
32159
+ getCredentials(password) {
32160
+ const credentials = this.pb.authStore.record.credentials;
32161
+ if (credentials) {
32162
+ return decryptObject(credentials, password);
32163
+ }
32164
+ return null;
32165
+ }
32166
+ async saveCredentials(password, credentials) {
32167
+ const encrypted = encryptObject(credentials, password);
32168
+ await this.pb.collection("_superusers").update(this.pb.authStore.record.id, {
32169
+ credentials: encrypted
32170
+ });
32171
+ }
32172
+ async addNewCredential(password, payload) {
32173
+ let credentials = this.getCredentials(password);
32174
+ if (credentials) {
32175
+ let found = false;
32176
+ for (const credential of credentials) {
32177
+ if (credential.name === payload.name && credential.exchange === payload.exchange) {
32178
+ credential.api_key = payload.api_key;
32179
+ credential.api_secret = payload.api_secret;
32180
+ found = true;
32181
+ break;
32182
+ }
32183
+ }
32184
+ if (!found) {
32185
+ credentials.push(payload);
32186
+ }
32187
+ await this.saveCredentials(password, credentials);
32188
+ }
32189
+ }
32118
32190
  async getAllSymbolsFromPositions(options) {
32119
32191
  const { no_position = false, kind = "long", custom_filter } = options || {};
32120
32192
  let filter = custom_filter || (no_position ? `quantity = 0` : undefined);
@@ -35873,6 +35945,9 @@ class ExchangeAccount {
35873
35945
  this.app_db = options.app_db;
35874
35946
  this.main_exchange = options.main_exchange;
35875
35947
  }
35948
+ getDBInstance() {
35949
+ return this.app_db;
35950
+ }
35876
35951
  async getLiveExchangeInstance(payload) {
35877
35952
  const symbol_config = await this.recomputeSymbolConfig({
35878
35953
  symbol: payload.symbol,
@@ -36133,12 +36208,7 @@ class ExchangeAccount {
36133
36208
  };
36134
36209
  }
36135
36210
  async determineAmountToBuy(payload) {
36136
- const {
36137
- orders,
36138
- kind,
36139
- decimal_places = "%.3f",
36140
- symbol
36141
- } = payload;
36211
+ const { orders, kind, decimal_places = "%.3f", symbol } = payload;
36142
36212
  const totalQuantity = orders.reduce((sum, order) => sum + (order.quantity || 0), 0);
36143
36213
  let runningTotal = to_f(totalQuantity, decimal_places);
36144
36214
  let sortedOrders = [...orders].sort((a, b) => (a.entry || 0) - (b.entry || 0));
@@ -37357,7 +37427,7 @@ function getExchangeKlass(exchange) {
37357
37427
  const func = exchange === "binance" ? BinanceExchange : BybitExchange;
37358
37428
  const clientFunc = exchange === "binance" ? initClient : initClient2;
37359
37429
  return async (payload) => {
37360
- const credentials = await payload.getCredentials(payload.account, exchange);
37430
+ const credentials = payload.getCredentials(payload.account, exchange);
37361
37431
  const client = await clientFunc(credentials, {
37362
37432
  type: "future",
37363
37433
  proxyAgent: payload.proxyAgent
@@ -37398,11 +37468,10 @@ class App {
37398
37468
  this.getCredentials = getCredentials;
37399
37469
  }
37400
37470
  async getExchangeAccount(account) {
37401
- const credentials = this.getCredentials(account.owner, account.exchange);
37402
37471
  return await getExchangeAccount({
37403
37472
  account,
37404
37473
  app_db: this.app_db,
37405
- getCredentials: credentials
37474
+ getCredentials: this.getCredentials
37406
37475
  });
37407
37476
  }
37408
37477
  async syncAccount(payload) {
@@ -37756,10 +37825,30 @@ class App {
37756
37825
  async function initApp(payload) {
37757
37826
  const pb = await initPocketBaseClient(payload.db);
37758
37827
  const app_db = new AppDatabase(pb);
37759
- const app = new App(app_db, payload.getCredentials);
37828
+ let _getCredentials = payload.getCredentials;
37829
+ if (payload.password) {
37830
+ try {
37831
+ const credentials = app_db.getCredentials(payload.password);
37832
+ if (credentials) {
37833
+ _getCredentials = (account, exchange) => {
37834
+ const credential = credentials.find((c) => c.name === account && c.exchange === exchange);
37835
+ if (!credential) {
37836
+ throw new Error(`Missing API Key or Secret for account '${account}' in .env file. Please check your environment variables.`);
37837
+ }
37838
+ return {
37839
+ api_key: credential?.api_key,
37840
+ api_secret: credential?.api_secret
37841
+ };
37842
+ };
37843
+ }
37844
+ } catch (error) {
37845
+ console.log("error", error);
37846
+ }
37847
+ }
37848
+ const app = new App(app_db, _getCredentials);
37760
37849
  return app;
37761
37850
  }
37762
- async function getCredentials(account, exchange) {
37851
+ function getCredentials(account, exchange) {
37763
37852
  console.log(`Fetching credentials for account: ${account}, exchange: ${exchange}`);
37764
37853
  let apiKey;
37765
37854
  let apiSecret;
@@ -37794,14 +37883,15 @@ async function getCredentials(account, exchange) {
37794
37883
  api_secret: apiSecret
37795
37884
  };
37796
37885
  }
37797
- async function initialize() {
37886
+ async function initialize(password) {
37798
37887
  const app = await initApp({
37799
37888
  db: {
37800
37889
  host: process.env.POCKETBASE_HOST,
37801
37890
  email: process.env.POCKETBASE_EMAIL,
37802
37891
  password: process.env.POCKETBASE_PASSWORD
37803
37892
  },
37804
- getCredentials: (account, exchange) => getCredentials
37893
+ password,
37894
+ getCredentials
37805
37895
  });
37806
37896
  return app;
37807
37897
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@gbozee/ultimate",
3
3
  "type": "module",
4
- "version": "0.0.2-21",
4
+ "version": "0.0.2-23",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",