@gardenfi/utils 2.0.3 → 2.0.4

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 (113) hide show
  1. package/dist/index.cjs +1 -1
  2. package/dist/index.js +22 -17
  3. package/dist/index10.cjs +1 -1
  4. package/dist/index10.js +32 -17
  5. package/dist/index11.cjs +1 -1
  6. package/dist/index11.js +14 -183
  7. package/dist/index12.cjs +1 -1
  8. package/dist/index12.js +186 -92
  9. package/dist/index13.cjs +1 -16
  10. package/dist/index13.js +93 -113
  11. package/dist/index14.cjs +16 -1
  12. package/dist/index14.js +112 -30
  13. package/dist/index15.cjs +1 -1
  14. package/dist/index15.js +18 -17
  15. package/dist/index16.cjs +1 -1
  16. package/dist/index16.js +31 -18
  17. package/dist/index17.cjs +1 -1
  18. package/dist/index17.js +17 -7
  19. package/dist/index18.cjs +1 -1
  20. package/dist/index18.js +6 -19
  21. package/dist/index19.cjs +1 -1
  22. package/dist/index19.js +20 -21
  23. package/dist/index20.cjs +1 -1
  24. package/dist/index20.js +21 -8
  25. package/dist/index21.cjs +1 -1
  26. package/dist/index21.js +8 -31
  27. package/dist/index22.cjs +1 -1
  28. package/dist/index22.js +31 -26
  29. package/dist/index23.cjs +1 -1
  30. package/dist/index23.js +25 -22
  31. package/dist/index24.cjs +1 -1
  32. package/dist/index24.js +22 -50
  33. package/dist/index25.cjs +1 -1
  34. package/dist/index25.js +51 -12
  35. package/dist/index26.cjs +1 -1
  36. package/dist/index26.js +12 -24
  37. package/dist/index27.cjs +1 -1
  38. package/dist/index27.js +24 -75
  39. package/dist/index28.cjs +1 -1
  40. package/dist/index28.js +74 -22
  41. package/dist/index29.cjs +1 -1
  42. package/dist/index29.js +23 -7
  43. package/dist/index30.cjs +1 -1
  44. package/dist/index30.js +7 -32
  45. package/dist/index31.cjs +1 -1
  46. package/dist/index31.js +31 -14
  47. package/dist/index32.cjs +1 -1
  48. package/dist/index32.js +16 -2
  49. package/dist/index33.cjs +1 -1
  50. package/dist/index33.js +2 -9
  51. package/dist/index34.cjs +1 -1
  52. package/dist/index34.js +8 -20
  53. package/dist/index35.cjs +1 -1
  54. package/dist/index35.js +20 -12
  55. package/dist/index36.cjs +1 -1
  56. package/dist/index36.js +10 -32
  57. package/dist/index37.cjs +1 -2
  58. package/dist/index37.js +33 -58
  59. package/dist/index38.cjs +2 -1
  60. package/dist/index38.js +60 -2
  61. package/dist/index39.cjs +1 -1
  62. package/dist/index39.js +2 -5
  63. package/dist/index4.cjs +1 -1
  64. package/dist/index4.js +2 -2
  65. package/dist/index40.cjs +1 -1
  66. package/dist/index40.js +4 -11
  67. package/dist/index41.cjs +1 -1
  68. package/dist/index41.js +11 -3
  69. package/dist/index42.cjs +1 -1
  70. package/dist/index42.js +3 -21
  71. package/dist/index43.cjs +1 -1
  72. package/dist/index43.js +21 -12
  73. package/dist/index44.cjs +1 -1
  74. package/dist/index44.js +11 -54
  75. package/dist/index45.cjs +1 -1
  76. package/dist/index45.js +55 -6
  77. package/dist/index46.cjs +1 -1
  78. package/dist/index46.js +6 -21
  79. package/dist/index47.cjs +1 -1
  80. package/dist/index47.js +21 -11
  81. package/dist/index48.cjs +1 -1
  82. package/dist/index48.js +11 -3
  83. package/dist/index49.cjs +1 -1
  84. package/dist/index49.js +3 -11
  85. package/dist/index5.cjs +1 -1
  86. package/dist/index5.js +43 -19
  87. package/dist/index50.cjs +1 -1
  88. package/dist/index50.js +11 -19
  89. package/dist/index51.cjs +1 -1
  90. package/dist/index51.js +19 -6
  91. package/dist/index52.cjs +1 -1
  92. package/dist/index52.js +6 -8
  93. package/dist/index53.cjs +1 -1
  94. package/dist/index53.js +8 -104
  95. package/dist/index54.cjs +1 -1
  96. package/dist/index54.js +102 -26
  97. package/dist/index55.cjs +1 -1
  98. package/dist/index55.js +27 -17
  99. package/dist/index56.cjs +1 -1
  100. package/dist/index56.js +16 -36
  101. package/dist/index57.cjs +1 -0
  102. package/dist/index57.js +41 -0
  103. package/dist/index6.cjs +1 -1
  104. package/dist/index6.js +19 -104
  105. package/dist/index7.cjs +1 -1
  106. package/dist/index7.js +104 -10
  107. package/dist/index8.cjs +1 -1
  108. package/dist/index8.js +15 -2
  109. package/dist/index9.cjs +1 -1
  110. package/dist/index9.js +2 -34
  111. package/dist/src/index.d.ts +1 -0
  112. package/dist/src/lib/result.d.ts +16 -0
  113. package/package.json +1 -1
package/dist/index7.js CHANGED
@@ -1,17 +1,111 @@
1
- class m {
2
- constructor() {
3
- this.memory = /* @__PURE__ */ new Map();
1
+ import { Ok as n, Err as a, Fetcher as u } from "./index12.js";
2
+ import { MemoryStorage as f } from "./index8.js";
3
+ import { StoreKeys as d } from "./index9.js";
4
+ import { jwtDecode as m } from "jwt-decode";
5
+ import { Authorization as w } from "./index3.js";
6
+ import { Url as g } from "./index6.js";
7
+ import { createSiweMessage as y } from "./index14.js";
8
+ class I {
9
+ constructor(e, r, t) {
10
+ this.API = "https://api.garden.finance", this.url = new g("/", e ?? this.API), this.walletClient = r, this.domain = (t == null ? void 0 : t.domain) || "app.garden.finance", this.domain.startsWith("https://") && (this.domain = this.domain.split("https://")[1]), this.signingStatement = (t == null ? void 0 : t.signingStatement) ?? "Garden.fi", this.store = (t == null ? void 0 : t.store) ?? new f();
4
11
  }
5
- getItem(e) {
6
- return this.memory.has(e) ? this.memory.get(e) : null;
12
+ verifyToken(e, r) {
13
+ try {
14
+ const t = p(e);
15
+ if (!t) return n(!1);
16
+ const i = Math.floor(Date.now() / 1e3) + 120;
17
+ return n(
18
+ t.exp > i && t.user_id.toLowerCase() === r.toLowerCase()
19
+ );
20
+ } catch {
21
+ return n(!1);
22
+ }
7
23
  }
8
- setItem(e, t) {
9
- this.memory.set(e, t);
24
+ async getToken() {
25
+ var s;
26
+ if (!((s = this.walletClient.account) != null && s.address))
27
+ return a("Wallet client does not have an account");
28
+ const e = this.store.getItem(d.AUTH_TOKEN);
29
+ if (e) {
30
+ const o = this.verifyToken(
31
+ e,
32
+ this.walletClient.account.address
33
+ );
34
+ if (o.ok && o.val) return n(e);
35
+ }
36
+ const r = await this.signStatement();
37
+ if (r.error)
38
+ return a(r.error);
39
+ const t = await u.post(
40
+ this.url.endpoint("verify"),
41
+ {
42
+ body: JSON.stringify({
43
+ ...r.val
44
+ }),
45
+ headers: {
46
+ "Content-Type": "application/json"
47
+ }
48
+ }
49
+ );
50
+ if (t.error) return a(t.error);
51
+ const i = t.result;
52
+ if (!i) return a("Failed to get token");
53
+ if (!this.verifyToken(i, this.walletClient.account.address).val)
54
+ throw new Error("Token verification failed");
55
+ return this.store.setItem(d.AUTH_TOKEN, i), n(i);
10
56
  }
11
- removeItem(e) {
12
- this.memory.has(e) && this.memory.delete(e);
57
+ async signStatement() {
58
+ var l;
59
+ if (!((l = this.walletClient.account) != null && l.address))
60
+ return a("Wallet client does not have a valid account");
61
+ const e = /* @__PURE__ */ new Date(), r = new Date(e.getTime() + 300 * 1e3), i = (await u.get(
62
+ this.url.endpoint("nonce")
63
+ )).result;
64
+ if (!i)
65
+ return a("Failed to get nonce");
66
+ const h = await this.walletClient.getChainId(), s = y({
67
+ domain: this.domain,
68
+ address: this.walletClient.account.address,
69
+ statement: this.signingStatement,
70
+ nonce: i,
71
+ uri: "https://" + this.domain,
72
+ version: "1",
73
+ chainId: h,
74
+ notBefore: r
75
+ }), o = await this.walletClient.signMessage({
76
+ account: this.walletClient.account,
77
+ message: s
78
+ });
79
+ return n({
80
+ message: s,
81
+ signature: o,
82
+ nonce: i
83
+ });
84
+ }
85
+ }
86
+ const p = (c) => {
87
+ try {
88
+ return m(c);
89
+ } catch {
90
+ return;
91
+ }
92
+ };
93
+ class E {
94
+ constructor(e) {
95
+ if (!e.siwe && !e.apiKey)
96
+ throw new Error("Either siwe or apiKey must be provided");
97
+ this.siwe = e.siwe, this.apiKey = e.apiKey;
98
+ }
99
+ async getAuthHeaders() {
100
+ if (this.siwe) {
101
+ const e = await this.siwe.getToken();
102
+ return e.error ? a(e.error) : n({ Authorization: w(e.val) });
103
+ }
104
+ return this.apiKey ? n({ "api-key": this.apiKey }) : a("No authentication method available");
13
105
  }
14
106
  }
15
107
  export {
16
- m as MemoryStorage
108
+ E as Auth,
109
+ I as Siwe,
110
+ p as parseJwt
17
111
  };
package/dist/index8.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var t=(e=>(e.AUTH_TOKEN="auth_token",e))(t||{});exports.StoreKeys=t;
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 CHANGED
@@ -1,4 +1,17 @@
1
- var t = /* @__PURE__ */ ((r) => (r.AUTH_TOKEN = "auth_token", r))(t || {});
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
+ }
2
15
  export {
3
- t as StoreKeys
16
+ m as MemoryStorage
4
17
  };
package/dist/index9.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=require("./index11.cjs"),n=require("viem"),s=require("./index14.cjs"),l=require("./index15.cjs"),o={42161:s.mainnet,421614:l.sepolia},b=async e=>{const{chain:t}=e;if(!t)return r.Err("No chain found");const a=(u=>u in o)(t.id)?o[t.id]:t;return await i(a)},i=async e=>{const t=n.createPublicClient({chain:e,transport:n.http()});try{const c=await t.getBlockNumber();return r.Ok(Number(c))}catch(c){return r.Err("Failed to fetch evm block number",c)}},m=async e=>{try{const t=await e.getLatestTip();return r.Ok(t)}catch(t){return r.Err("Failed to fetch bitcoin block number",t)}};exports._fetchEVMBlockNumber=i;exports.fetchBitcoinBlockNumber=m;exports.fetchEVMBlockNumber=b;
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 CHANGED
@@ -1,36 +1,4 @@
1
- import { Err as o, Ok as n } from "./index11.js";
2
- import { createPublicClient as s, http as m } from "viem";
3
- import { mainnet as u } from "./index14.js";
4
- import { sepolia as b } from "./index15.js";
5
- const e = {
6
- 42161: u,
7
- 421614: b
8
- }, N = async (r) => {
9
- const { chain: t } = r;
10
- if (!t) return o("No chain found");
11
- const i = ((a) => a in e)(t.id) ? e[t.id] : t;
12
- return await l(i);
13
- }, l = async (r) => {
14
- const t = s({
15
- chain: r,
16
- transport: m()
17
- });
18
- try {
19
- const c = await t.getBlockNumber();
20
- return n(Number(c));
21
- } catch (c) {
22
- return o("Failed to fetch evm block number", c);
23
- }
24
- }, C = async (r) => {
25
- try {
26
- const t = await r.getLatestTip();
27
- return n(t);
28
- } catch (t) {
29
- return o("Failed to fetch bitcoin block number", t);
30
- }
31
- };
1
+ var t = /* @__PURE__ */ ((r) => (r.AUTH_TOKEN = "auth_token", r))(t || {});
32
2
  export {
33
- l as _fetchEVMBlockNumber,
34
- C as fetchBitcoinBlockNumber,
35
- N as fetchEVMBlockNumber
3
+ t as StoreKeys
36
4
  };
@@ -6,3 +6,4 @@ export * from './lib/utils';
6
6
  export * from './lib/approve';
7
7
  export * from './lib/blockNumber';
8
8
  export * from './lib/eventBroker';
9
+ export * from './lib/result';
@@ -0,0 +1,16 @@
1
+ export declare class Result<T, E> {
2
+ #private;
3
+ constructor(ok: boolean, val: T, error?: E | undefined);
4
+ get ok(): boolean;
5
+ get error(): E | undefined;
6
+ get val(): T;
7
+ }
8
+ export type AsyncResult<T, E> = Promise<Result<T, E>>;
9
+ export declare const Ok: <T>(val: T) => Result<T, never>;
10
+ export declare const Void: undefined;
11
+ /**
12
+ * Constructs an error result with the given error value.
13
+ *
14
+ * If the error value is a string, it can be followed by any number of optional messages which will be concatenated
15
+ */
16
+ export declare const Err: <E>(error: E, ...optionalMsg: (E extends string ? any : never)[]) => Result<never, E>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gardenfi/utils",
3
- "version": "2.0.3",
3
+ "version": "2.0.4",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "build": "vite build",