@gardenfi/utils 0.0.1-beta.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.
Files changed (99) hide show
  1. package/README.md +0 -0
  2. package/dist/index.cjs +1 -0
  3. package/dist/index.js +21 -0
  4. package/dist/index10.cjs +1 -0
  5. package/dist/index10.js +97 -0
  6. package/dist/index11.cjs +1 -0
  7. package/dist/index11.js +33 -0
  8. package/dist/index12.cjs +1 -0
  9. package/dist/index12.js +34 -0
  10. package/dist/index13.cjs +1 -0
  11. package/dist/index13.js +27 -0
  12. package/dist/index14.cjs +1 -0
  13. package/dist/index14.js +33 -0
  14. package/dist/index15.cjs +1 -0
  15. package/dist/index15.js +11 -0
  16. package/dist/index16.cjs +1 -0
  17. package/dist/index16.js +20 -0
  18. package/dist/index17.cjs +1 -0
  19. package/dist/index17.js +10 -0
  20. package/dist/index18.cjs +1 -0
  21. package/dist/index18.js +23 -0
  22. package/dist/index19.cjs +1 -0
  23. package/dist/index19.js +25 -0
  24. package/dist/index2.cjs +1 -0
  25. package/dist/index2.js +4 -0
  26. package/dist/index20.cjs +1 -0
  27. package/dist/index20.js +11 -0
  28. package/dist/index21.cjs +1 -0
  29. package/dist/index21.js +34 -0
  30. package/dist/index22.cjs +1 -0
  31. package/dist/index22.js +29 -0
  32. package/dist/index23.cjs +1 -0
  33. package/dist/index23.js +26 -0
  34. package/dist/index24.cjs +1 -0
  35. package/dist/index24.js +55 -0
  36. package/dist/index25.cjs +1 -0
  37. package/dist/index25.js +15 -0
  38. package/dist/index26.cjs +1 -0
  39. package/dist/index26.js +27 -0
  40. package/dist/index27.cjs +1 -0
  41. package/dist/index27.js +78 -0
  42. package/dist/index28.cjs +1 -0
  43. package/dist/index28.js +26 -0
  44. package/dist/index29.cjs +1 -0
  45. package/dist/index29.js +10 -0
  46. package/dist/index3.cjs +1 -0
  47. package/dist/index3.js +6 -0
  48. package/dist/index30.cjs +1 -0
  49. package/dist/index30.js +32 -0
  50. package/dist/index31.cjs +1 -0
  51. package/dist/index31.js +18 -0
  52. package/dist/index32.cjs +1 -0
  53. package/dist/index32.js +4 -0
  54. package/dist/index33.cjs +1 -0
  55. package/dist/index33.js +23 -0
  56. package/dist/index34.cjs +1 -0
  57. package/dist/index34.js +15 -0
  58. package/dist/index35.cjs +1 -0
  59. package/dist/index35.js +37 -0
  60. package/dist/index36.cjs +2 -0
  61. package/dist/index36.js +62 -0
  62. package/dist/index37.cjs +1 -0
  63. package/dist/index37.js +4 -0
  64. package/dist/index38.cjs +1 -0
  65. package/dist/index38.js +7 -0
  66. package/dist/index39.cjs +1 -0
  67. package/dist/index39.js +14 -0
  68. package/dist/index4.cjs +1 -0
  69. package/dist/index4.js +37 -0
  70. package/dist/index40.cjs +1 -0
  71. package/dist/index40.js +6 -0
  72. package/dist/index41.cjs +1 -0
  73. package/dist/index41.js +6 -0
  74. package/dist/index42.cjs +1 -0
  75. package/dist/index42.js +9 -0
  76. package/dist/index5.cjs +1 -0
  77. package/dist/index5.js +33 -0
  78. package/dist/index6.cjs +1 -0
  79. package/dist/index6.js +20 -0
  80. package/dist/index7.cjs +1 -0
  81. package/dist/index7.js +95 -0
  82. package/dist/index8.cjs +1 -0
  83. package/dist/index8.js +17 -0
  84. package/dist/index9.cjs +1 -0
  85. package/dist/index9.js +4 -0
  86. package/dist/src/index.d.ts +7 -0
  87. package/dist/src/lib/apiResponse.types.d.ts +9 -0
  88. package/dist/src/lib/approve.d.ts +7 -0
  89. package/dist/src/lib/auth/auth.types.d.ts +21 -0
  90. package/dist/src/lib/auth/index.d.ts +2 -0
  91. package/dist/src/lib/auth/siwe.d.ts +17 -0
  92. package/dist/src/lib/store/index.d.ts +2 -0
  93. package/dist/src/lib/store/memoryStorage.d.ts +14 -0
  94. package/dist/src/lib/store/store.interface.d.ts +33 -0
  95. package/dist/src/lib/switchOrAddNetwork.d.ts +5 -0
  96. package/dist/src/lib/url/Url.d.ts +5 -0
  97. package/dist/src/lib/url/index.d.ts +1 -0
  98. package/dist/src/lib/utils.d.ts +8 -0
  99. package/package.json +43 -0
package/dist/index7.js ADDED
@@ -0,0 +1,95 @@
1
+ import { SiweMessage as w } from "siwe";
2
+ import { Ok as r, Err as s, Fetcher as m } from "@catalogfi/utils";
3
+ import { MemoryStorage as p } from "./index8.js";
4
+ import { StoreKeys as u } from "./index9.js";
5
+ import { Url as y } from "./index6.js";
6
+ class M {
7
+ constructor(t, i, e) {
8
+ this.API = "https://api.garden.finance", this.url = new y("/", t ?? this.API), this.walletClient = i, this.domain = (e == null ? void 0 : e.domain) || "app.garden.finance", this.domain.startsWith("https://") && (this.domain = this.domain.split("https://")[1]), this.signingStatement = (e == null ? void 0 : e.signingStatement) ?? "Garden.fi", this.store = (e == null ? void 0 : e.store) ?? new p();
9
+ }
10
+ verifyToken(t, i) {
11
+ const e = T(t);
12
+ if (!e) return r(!1);
13
+ try {
14
+ const n = Math.floor(Date.now() / 1e3) + 120;
15
+ return r(
16
+ e.exp > n && e.address.toLowerCase() === i.toLowerCase()
17
+ );
18
+ } catch {
19
+ return r(!1);
20
+ }
21
+ }
22
+ async getToken() {
23
+ var c;
24
+ if (!((c = this.walletClient.account) != null && c.address))
25
+ return s("Wallet client does not have an account");
26
+ const t = this.store.getItem(u.AUTH_TOKEN);
27
+ if (t) {
28
+ const a = this.verifyToken(
29
+ t,
30
+ this.walletClient.account.address
31
+ );
32
+ if (a.ok && a.val) return r(t);
33
+ }
34
+ const i = await this.signStatement();
35
+ if (i.error)
36
+ return s(i.error);
37
+ const e = await m.post(
38
+ this.url.endpoint("verify"),
39
+ {
40
+ body: JSON.stringify({
41
+ ...i.val
42
+ }),
43
+ headers: {
44
+ "Content-Type": "application/json"
45
+ }
46
+ }
47
+ );
48
+ if (e.error) return s(e.error);
49
+ const n = e.result;
50
+ if (!n) return s("Failed to get token");
51
+ if (!this.verifyToken(n, this.walletClient.account.address).val)
52
+ throw new Error("Token verification failed");
53
+ return this.store.setItem(u.AUTH_TOKEN, n), r(n);
54
+ }
55
+ async signStatement() {
56
+ var h, d, f;
57
+ if (!((h = this.walletClient.chain) != null && h.id) || !((d = this.walletClient.account) != null && d.address))
58
+ return s("Wallet client does not have a chain");
59
+ const t = /* @__PURE__ */ new Date(), i = new Date(t.getTime() + 300 * 1e3), n = (await m.get(
60
+ this.url.endpoint("nonce")
61
+ )).result;
62
+ if (!n)
63
+ return s("Failed to get nonce");
64
+ const l = (f = this.walletClient.chain) == null ? void 0 : f.id, a = new w({
65
+ domain: this.domain,
66
+ address: this.walletClient.account.address,
67
+ statement: this.signingStatement,
68
+ nonce: n,
69
+ uri: "https://" + this.domain,
70
+ version: "1",
71
+ chainId: l,
72
+ expirationTime: i.toISOString()
73
+ }).prepareMessage(), g = await this.walletClient.signMessage({
74
+ account: this.walletClient.account,
75
+ message: a
76
+ });
77
+ return r({
78
+ message: a,
79
+ signature: g,
80
+ nonce: n
81
+ });
82
+ }
83
+ }
84
+ const T = (o) => {
85
+ try {
86
+ if (o.split(".").length < 3) return;
87
+ const t = o.split(".")[1];
88
+ return t ? JSON.parse(Buffer.from(t, "base64").toString("latin1")) : void 0;
89
+ } catch {
90
+ return;
91
+ }
92
+ };
93
+ export {
94
+ M as Siwe
95
+ };
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class r{constructor(){this.memory=new Map}getItem(e){return this.memory.has(e)?this.memory.get(e):null}setItem(e,t){this.memory.set(e,t)}removeItem(e){this.memory.has(e)&&this.memory.delete(e)}}exports.MemoryStorage=r;
package/dist/index8.js ADDED
@@ -0,0 +1,17 @@
1
+ class m {
2
+ constructor() {
3
+ this.memory = /* @__PURE__ */ new Map();
4
+ }
5
+ getItem(e) {
6
+ return this.memory.has(e) ? this.memory.get(e) : null;
7
+ }
8
+ setItem(e, t) {
9
+ this.memory.set(e, t);
10
+ }
11
+ removeItem(e) {
12
+ this.memory.has(e) && this.memory.delete(e);
13
+ }
14
+ }
15
+ export {
16
+ m as MemoryStorage
17
+ };
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var t=(e=>(e.AUTH_TOKEN="auth_token",e))(t||{});exports.StoreKeys=t;
package/dist/index9.js ADDED
@@ -0,0 +1,4 @@
1
+ var t = /* @__PURE__ */ ((r) => (r.AUTH_TOKEN = "auth_token", r))(t || {});
2
+ export {
3
+ t as StoreKeys
4
+ };
@@ -0,0 +1,7 @@
1
+ export * from './lib/url';
2
+ export * from './lib/auth';
3
+ export * from './lib/store';
4
+ export * from './lib/apiResponse.types';
5
+ export * from './lib/utils';
6
+ export * from './lib/approve';
7
+ export * from './lib/switchOrAddNetwork';
@@ -0,0 +1,9 @@
1
+ export declare enum ApiStatus {
2
+ Ok = "Ok",
3
+ Error = "Error"
4
+ }
5
+ export type APIResponse<T> = {
6
+ status: ApiStatus;
7
+ result?: T;
8
+ error?: string;
9
+ };
@@ -0,0 +1,7 @@
1
+ import { AsyncResult } from '@catalogfi/utils';
2
+ import { WalletClient } from 'viem';
3
+
4
+ /**
5
+ * @description approves the staking contract to spend the SEED tokens.
6
+ */
7
+ export declare const checkAllowanceAndApprove: (amount: number, tokenAddress: string, contractAddress: string, walletClient: WalletClient) => AsyncResult<string, string>;
@@ -0,0 +1,21 @@
1
+ import { AsyncResult, Result } from '@catalogfi/utils';
2
+ import { IStore } from '../store/store.interface';
3
+
4
+ export type SiweOpts = {
5
+ domain?: string;
6
+ store?: IStore;
7
+ signingStatement?: string;
8
+ };
9
+ export interface IAuth {
10
+ /**
11
+ * Get a token from store if available or fetch from the wallet
12
+ * @returns verified token
13
+ */
14
+ getToken(): AsyncResult<string, string>;
15
+ /**
16
+ * Verify the token expiry and account
17
+ * @param token The token to verify
18
+ * @param account The account to verify the token against
19
+ */
20
+ verifyToken(token: string, account: string): Result<boolean, string>;
21
+ }
@@ -0,0 +1,2 @@
1
+ export * from './auth.types';
2
+ export * from './siwe';
@@ -0,0 +1,17 @@
1
+ import { IAuth, SiweOpts } from './auth.types';
2
+ import { AsyncResult, Result } from '@catalogfi/utils';
3
+ import { Url } from '../url';
4
+ import { WalletClient } from 'viem';
5
+
6
+ export declare class Siwe implements IAuth {
7
+ private readonly API;
8
+ private readonly url;
9
+ private store;
10
+ private walletClient;
11
+ private readonly signingStatement;
12
+ private readonly domain;
13
+ constructor(url: Url, walletClient: WalletClient, opts?: SiweOpts);
14
+ verifyToken(token: string, account: string): Result<boolean, string>;
15
+ getToken(): AsyncResult<string, string>;
16
+ private signStatement;
17
+ }
@@ -0,0 +1,2 @@
1
+ export * from './memoryStorage';
2
+ export * from './store.interface';
@@ -0,0 +1,14 @@
1
+ import { IStore } from './store.interface';
2
+
3
+ /**
4
+ * In-memory storage implementation.
5
+ *
6
+ * @class
7
+ * @implements {IStore}
8
+ */
9
+ export declare class MemoryStorage implements IStore {
10
+ private memory;
11
+ getItem(key: string): string | null;
12
+ setItem(key: string, value: string): void;
13
+ removeItem(key: string): void;
14
+ }
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Interface for a Store.
3
+ *
4
+ * @interface IStore
5
+ *
6
+ */
7
+ export interface IStore {
8
+ /**
9
+ * Retrieves an item from the store using the provided key.
10
+ * @param {string} key - The key of the item to retrieve.
11
+ * @returns {string | number} The item associated with the key, or null if no item is found.
12
+ *
13
+ */
14
+ getItem(key: string): string | null;
15
+ /**
16
+ * @method setItem - Sets an item in the store with the provided key and value.
17
+ * @param {string} key - The key to associate with the item.
18
+ * @param {any} value - The value of the item to store.
19
+ * @returns {void}
20
+ *
21
+ */
22
+ setItem(key: string, value: any): void;
23
+ /**
24
+ * @method removeItem - Removes an item from the store using the provided key.
25
+ * @param {string} key - The key of the item to remove.
26
+ * @returns {void}
27
+ *
28
+ */
29
+ removeItem(key: string): void;
30
+ }
31
+ export declare enum StoreKeys {
32
+ AUTH_TOKEN = "auth_token"
33
+ }
@@ -0,0 +1,5 @@
1
+ import { WalletClient, Chain } from 'viem';
2
+ import { Chain as GardenChain, EvmChain } from '@gardenfi/orderbook';
3
+
4
+ export declare const ChainToID: Record<EvmChain, Chain>;
5
+ export declare const switchOrAddNetwork: (chain: GardenChain, walletClient: WalletClient) => Promise<import('@catalogfi/utils').Result<never, string> | import('@catalogfi/utils').Result<string, never>>;
@@ -0,0 +1,5 @@
1
+ export declare class Url extends URL {
2
+ constructor(endpoint: string, base?: string | Url);
3
+ endpoint(endpoint: string): Url;
4
+ socket(): string;
5
+ }
@@ -0,0 +1 @@
1
+ export * from './Url';
@@ -0,0 +1,8 @@
1
+ export declare const with0x: (str: string) => `0x${string}`;
2
+ /**
3
+ * Includes `Bearer ` in the Authorization header
4
+ * @param authToken authToken
5
+ * @returns Authorization header
6
+ */
7
+ export declare const Authorization: (authToken: string) => string;
8
+ export declare const sleep: (ms: number) => Promise<unknown>;
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@gardenfi/utils",
3
+ "version": "0.0.1-beta.0",
4
+ "type": "module",
5
+ "scripts": {
6
+ "build": "vite build",
7
+ "test": "vitest run",
8
+ "dev": "vite build --watch"
9
+ },
10
+ "files": [
11
+ "dist"
12
+ ],
13
+ "main": "./dist/index.cjs",
14
+ "module": "./dist/index.js",
15
+ "typings": "./dist/src/index.d.ts",
16
+ "exports": {
17
+ ".": {
18
+ "require": "./dist/index.cjs",
19
+ "import": "./dist/index.js",
20
+ "types": "./dist/src/index.d.ts"
21
+ },
22
+ "./package.json": "./package.json"
23
+ },
24
+ "publishConfig": {
25
+ "access": "public",
26
+ "registry": "https://registry.npmjs.org/"
27
+ },
28
+ "devDependencies": {
29
+ "@types/ws": "^8.5.7",
30
+ "dotenv": "^16.3.1",
31
+ "typescript": "^5.2.2",
32
+ "vite": "^5.1.6",
33
+ "vite-plugin-dts": "^3.7.3",
34
+ "vitest": "^1.6.0"
35
+ },
36
+ "sideEffects": false,
37
+ "dependencies": {
38
+ "@catalogfi/utils": "^0.1.11",
39
+ "axios": "^1.7.2",
40
+ "siwe": "^2.3.2",
41
+ "viem": "^2.21.15"
42
+ }
43
+ }