@gardenfi/utils 2.2.1-beta.1 → 2.2.1-beta.3

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 (55) hide show
  1. package/dist/index.cjs +1 -1
  2. package/dist/index.js +24 -24
  3. package/dist/index10.cjs +1 -1
  4. package/dist/index10.js +124 -2
  5. package/dist/index11.cjs +1 -1
  6. package/dist/index11.js +34 -114
  7. package/dist/index12.cjs +1 -1
  8. package/dist/index12.js +19 -37
  9. package/dist/index13.cjs +1 -1
  10. package/dist/index13.js +135 -19
  11. package/dist/index14.cjs +1 -1
  12. package/dist/index14.js +10 -137
  13. package/dist/index15.cjs +1 -1
  14. package/dist/index15.js +2 -15
  15. package/dist/index16.cjs +1 -1
  16. package/dist/index16.js +27 -2
  17. package/dist/index17.cjs +1 -1
  18. package/dist/index17.js +18 -26
  19. package/dist/index18.cjs +1 -1
  20. package/dist/index18.js +102 -14
  21. package/dist/index24.cjs +1 -1
  22. package/dist/index24.js +12 -26
  23. package/dist/index25.cjs +1 -1
  24. package/dist/index25.js +58 -8
  25. package/dist/index26.cjs +16 -1
  26. package/dist/index26.js +113 -12
  27. package/dist/index27.cjs +1 -1
  28. package/dist/index27.js +32 -58
  29. package/dist/index28.cjs +1 -16
  30. package/dist/index28.js +24 -111
  31. package/dist/index29.cjs +1 -1
  32. package/dist/index29.js +7 -31
  33. package/dist/index7.cjs +1 -1
  34. package/dist/index7.js +8 -105
  35. package/dist/index8.cjs +1 -1
  36. package/dist/index8.js +22 -8
  37. package/dist/index9.cjs +1 -1
  38. package/dist/index9.js +2 -24
  39. package/dist/src/index.d.ts +2 -2
  40. package/dist/src/lib/approve.d.ts +1 -1
  41. package/dist/src/lib/auth/apikey/apikey.d.ts +1 -1
  42. package/dist/src/lib/auth/auth.types.d.ts +1 -1
  43. package/dist/src/lib/auth/passkey/passkey.d.ts +1 -1
  44. package/dist/src/lib/auth/passkey/passkeyLogin.d.ts +1 -1
  45. package/dist/src/lib/auth/siwe/siwe.d.ts +1 -1
  46. package/dist/src/lib/blockNumber/blockNumber.d.ts +1 -1
  47. package/dist/src/lib/digestKey/digestKey.d.ts +1 -1
  48. package/dist/src/lib/fetcher/index.d.ts +1 -0
  49. package/dist/src/lib/result/index.d.ts +1 -0
  50. package/dist/src/lib/tryCatch/index.d.ts +1 -0
  51. package/dist/src/lib/{tryCatch.d.ts → tryCatch/tryCatch.d.ts} +1 -1
  52. package/package.json +1 -1
  53. /package/dist/src/lib/{fetcher.d.ts → fetcher/fetcher.d.ts} +0 -0
  54. /package/dist/src/lib/{safeParseJson.d.ts → fetcher/safeParseJson.d.ts} +0 -0
  55. /package/dist/src/lib/{result.d.ts → result/result.d.ts} +0 -0
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("./index2.cjs"),e=require("./index3.cjs"),t=require("./index4.cjs"),r=require("./index5.cjs"),i=require("./index6.cjs"),s=require("./index7.cjs"),n=require("./index8.cjs"),c=require("./index9.cjs"),a=require("./index10.cjs"),u=require("./index11.cjs"),y=require("./index12.cjs"),p=require("./index13.cjs"),h=require("./index14.cjs"),k=require("./index15.cjs"),l=require("./index16.cjs"),q=require("./index17.cjs"),A=require("./index18.cjs");exports.ApiStatus=o.ApiStatus;exports.Authorization=e.Authorization;exports.Environment=e.Environment;exports.Network=e.Network;exports.add0x=e.add0x;exports.hexToU32Array=e.hexToU32Array;exports.parseJwt=e.parseJwt;exports.sleep=e.sleep;exports.trim0x=e.trim0x;exports.with0x=e.with0x;exports.checkAllowanceAndApprove=t.checkAllowanceAndApprove;exports.waitForTransactionReceipt=t.waitForTransactionReceipt;exports.Err=r.Err;exports.Ok=r.Ok;exports.Void=r.Void;exports.DigestKey=i.DigestKey;exports.Fetcher=s.Fetcher;exports.executeWithTryCatch=n.executeWithTryCatch;exports.Url=c.Url;exports.AuthHeaderEnum=a.AuthHeaderEnum;exports.Siwe=u.Siwe;exports.ApiKey=y.ApiKey;exports.Passkey=p.Passkey;exports.PasskeyLogin=h.PasskeyLogin;exports.MemoryStorage=k.MemoryStorage;exports.StoreKeys=l.StoreKeys;exports.fetchEVMBlockNumber=q.fetchEVMBlockNumber;exports.EventBroker=A.EventBroker;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("./index2.cjs"),e=require("./index3.cjs"),t=require("./index4.cjs"),r=require("./index5.cjs"),i=require("./index6.cjs"),s=require("./index7.cjs"),n=require("./index8.cjs"),c=require("./index9.cjs"),a=require("./index10.cjs"),u=require("./index11.cjs"),y=require("./index12.cjs"),p=require("./index13.cjs"),h=require("./index14.cjs"),k=require("./index15.cjs"),l=require("./index16.cjs"),q=require("./index17.cjs"),A=require("./index18.cjs");exports.ApiStatus=o.ApiStatus;exports.Authorization=e.Authorization;exports.Environment=e.Environment;exports.Network=e.Network;exports.add0x=e.add0x;exports.hexToU32Array=e.hexToU32Array;exports.parseJwt=e.parseJwt;exports.sleep=e.sleep;exports.trim0x=e.trim0x;exports.with0x=e.with0x;exports.checkAllowanceAndApprove=t.checkAllowanceAndApprove;exports.waitForTransactionReceipt=t.waitForTransactionReceipt;exports.Err=r.Err;exports.Ok=r.Ok;exports.Void=r.Void;exports.DigestKey=i.DigestKey;exports.executeWithTryCatch=s.executeWithTryCatch;exports.Url=n.Url;exports.AuthHeaderEnum=c.AuthHeaderEnum;exports.Siwe=a.Siwe;exports.ApiKey=u.ApiKey;exports.Passkey=y.Passkey;exports.PasskeyLogin=p.PasskeyLogin;exports.MemoryStorage=h.MemoryStorage;exports.StoreKeys=k.StoreKeys;exports.fetchEVMBlockNumber=l.fetchEVMBlockNumber;exports.EventBroker=q.EventBroker;exports.Fetcher=A.Fetcher;
package/dist/index.js CHANGED
@@ -3,41 +3,41 @@ import { Authorization as p, Environment as m, Network as x, add0x as f, hexToU3
3
3
  import { checkAllowanceAndApprove as y, waitForTransactionReceipt as A } from "./index4.js";
4
4
  import { Err as u, Ok as w, Void as d } from "./index5.js";
5
5
  import { DigestKey as E } from "./index6.js";
6
- import { Fetcher as g } from "./index7.js";
7
- import { executeWithTryCatch as K } from "./index8.js";
8
- import { Url as B } from "./index9.js";
9
- import { AuthHeaderEnum as M } from "./index10.js";
10
- import { Siwe as P } from "./index11.js";
11
- import { ApiKey as V } from "./index12.js";
12
- import { Passkey as z } from "./index13.js";
13
- import { PasskeyLogin as D } from "./index14.js";
14
- import { MemoryStorage as J } from "./index15.js";
15
- import { StoreKeys as O } from "./index16.js";
16
- import { fetchEVMBlockNumber as W } from "./index17.js";
17
- import { EventBroker as q } from "./index18.js";
6
+ import { executeWithTryCatch as g } from "./index7.js";
7
+ import { Url as K } from "./index8.js";
8
+ import { AuthHeaderEnum as B } from "./index9.js";
9
+ import { Siwe as M } from "./index10.js";
10
+ import { ApiKey as P } from "./index11.js";
11
+ import { Passkey as V } from "./index12.js";
12
+ import { PasskeyLogin as z } from "./index13.js";
13
+ import { MemoryStorage as D } from "./index14.js";
14
+ import { StoreKeys as J } from "./index15.js";
15
+ import { fetchEVMBlockNumber as O } from "./index16.js";
16
+ import { EventBroker as W } from "./index17.js";
17
+ import { Fetcher as q } from "./index18.js";
18
18
  export {
19
- V as ApiKey,
19
+ P as ApiKey,
20
20
  e as ApiStatus,
21
- M as AuthHeaderEnum,
21
+ B as AuthHeaderEnum,
22
22
  p as Authorization,
23
23
  E as DigestKey,
24
24
  m as Environment,
25
25
  u as Err,
26
- q as EventBroker,
27
- g as Fetcher,
28
- J as MemoryStorage,
26
+ W as EventBroker,
27
+ q as Fetcher,
28
+ D as MemoryStorage,
29
29
  x as Network,
30
30
  w as Ok,
31
- z as Passkey,
32
- D as PasskeyLogin,
33
- P as Siwe,
34
- O as StoreKeys,
35
- B as Url,
31
+ V as Passkey,
32
+ z as PasskeyLogin,
33
+ M as Siwe,
34
+ J as StoreKeys,
35
+ K as Url,
36
36
  d as Void,
37
37
  f as add0x,
38
38
  y as checkAllowanceAndApprove,
39
- K as executeWithTryCatch,
40
- W as fetchEVMBlockNumber,
39
+ g as executeWithTryCatch,
40
+ O as fetchEVMBlockNumber,
41
41
  i as hexToU32Array,
42
42
  a as parseJwt,
43
43
  n as sleep,
package/dist/index10.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var e=(t=>(t.Authorization="Authorization",t.ApiKey="api-key",t))(e||{});exports.AuthHeaderEnum=e;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const w=require("tough-cookie"),g=require("fetch-cookie"),k=require("./index14.cjs"),f=require("./index15.cjs"),h=require("./index3.cjs"),n=require("./index5.cjs"),m=require("./index24.cjs"),y=require("./index25.cjs"),C=require("./index23.cjs"),T=require("./index26.cjs"),v=require("./index27.cjs"),S=c=>c&&c.__esModule?c:{default:c},p=S(g);class u{constructor(t,i,e){if(this.url=t.endpoint("siwe"),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)??(typeof window<"u"?window.localStorage:new k.MemoryStorage),typeof window>"u"){const r=new w.CookieJar;this.fetchWithCookies=p.default(fetch,r)}else this.fetchWithCookies=window.fetch.bind(window)}static fromDigestKey(t,i,e){const r=m.createWalletClient({account:C.privateKeyToAccount(h.add0x(i.digestKey)),transport:y.http(),chain:v.mainnet});return new u(t,r,e)}verifyToken(t,i){try{const e=h.parseJwt(t);if(!e)return n.Ok(!1);const r=Math.floor(Date.now()/1e3)+120;return n.Ok(e.exp>r&&e.user_id.toLowerCase()===i.toLowerCase())}catch{return n.Ok(!1)}}async getToken(){var s;if(!((s=this.walletClient.account)!=null&&s.address))return n.Err("Wallet client does not have an account");const t=this.store.getItem(f.StoreKeys.AUTH_TOKEN);if(t){const o=this.verifyToken(t,this.walletClient.account.address);if(o.ok&&o.val)return n.Ok(t)}const i=await this.signStatement();if(i.error)return n.Err(i.error);let e;try{const a=await(await this.fetchWithCookies(this.url.endpoint("tokens"),{method:"POST",body:JSON.stringify({...i.val}),headers:{"Content-Type":"application/json"},credentials:"include"})).json();if(a.error||!a.result)return n.Err(a.error??"Failed to get token");e=a.result}catch(o){return n.Err("Failed to get token",o)}if(!this.verifyToken(e,this.walletClient.account.address).val)throw new Error("Token verification failed");return this.store.setItem(f.StoreKeys.AUTH_TOKEN,e),n.Ok(e)}async signStatement(){var a;if(!((a=this.walletClient.account)!=null&&a.address))return n.Err("Wallet client does not have a valid account");const t=new Date,i=new Date(t.getTime()+300*1e3);let e;try{const l=await(await this.fetchWithCookies(this.url.endpoint("challenges"),{method:"POST",headers:{"Content-Type":"application/json"},credentials:"include"})).json();if(l.error||!l.result)return n.Err("Failed to get nonce");e=l.result}catch(d){return n.Err("Failed to get nonce",d)}const r=await this.walletClient.getChainId(),s=T.createSiweMessage({domain:this.domain,address:this.walletClient.account.address,statement:this.signingStatement,nonce:e,uri:"https://"+this.domain,version:"1",chainId:r,notBefore:i}),o=await this.walletClient.signMessage({account:this.walletClient.account,message:s});return n.Ok({message:s,signature:o,nonce:e})}async getAuthHeaders(){const t=await this.getToken();return t.ok?n.Ok({Authorization:h.Authorization(t.val)}):n.Err(t.error)}}exports.Siwe=u;
package/dist/index10.js CHANGED
@@ -1,4 +1,126 @@
1
- var o = /* @__PURE__ */ ((i) => (i.Authorization = "Authorization", i.ApiKey = "api-key", i))(o || {});
1
+ import { CookieJar as m } from "tough-cookie";
2
+ import u from "fetch-cookie";
3
+ import { MemoryStorage as w } from "./index14.js";
4
+ import { StoreKeys as d } from "./index15.js";
5
+ import { add0x as g, parseJwt as k, Authorization as p } from "./index3.js";
6
+ import { Ok as s, Err as r } from "./index5.js";
7
+ import { createWalletClient as y } from "./index24.js";
8
+ import { http as C } from "./index25.js";
9
+ import { privateKeyToAccount as T } from "./index23.js";
10
+ import { createSiweMessage as v } from "./index26.js";
11
+ import { mainnet as S } from "./index27.js";
12
+ class f {
13
+ constructor(e, n, t) {
14
+ if (this.url = e.endpoint("siwe"), this.walletClient = n, 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) ?? (typeof window < "u" ? window.localStorage : new w()), typeof window > "u") {
15
+ const i = new m();
16
+ this.fetchWithCookies = u(fetch, i);
17
+ } else
18
+ this.fetchWithCookies = window.fetch.bind(window);
19
+ }
20
+ static fromDigestKey(e, n, t) {
21
+ const i = y({
22
+ account: T(g(n.digestKey)),
23
+ transport: C(),
24
+ chain: S
25
+ });
26
+ return new f(e, i, t);
27
+ }
28
+ verifyToken(e, n) {
29
+ try {
30
+ const t = k(e);
31
+ if (!t) return s(!1);
32
+ const i = Math.floor(Date.now() / 1e3) + 120;
33
+ return s(
34
+ t.exp > i && t.user_id.toLowerCase() === n.toLowerCase()
35
+ );
36
+ } catch {
37
+ return s(!1);
38
+ }
39
+ }
40
+ async getToken() {
41
+ var c;
42
+ if (!((c = this.walletClient.account) != null && c.address))
43
+ return r("Wallet client does not have an account");
44
+ const e = this.store.getItem(d.AUTH_TOKEN);
45
+ if (e) {
46
+ const o = this.verifyToken(
47
+ e,
48
+ this.walletClient.account.address
49
+ );
50
+ if (o.ok && o.val) return s(e);
51
+ }
52
+ const n = await this.signStatement();
53
+ if (n.error)
54
+ return r(n.error);
55
+ let t;
56
+ try {
57
+ const a = await (await this.fetchWithCookies(
58
+ this.url.endpoint("tokens"),
59
+ {
60
+ method: "POST",
61
+ body: JSON.stringify({
62
+ ...n.val
63
+ }),
64
+ headers: {
65
+ "Content-Type": "application/json"
66
+ },
67
+ credentials: "include"
68
+ }
69
+ )).json();
70
+ if (a.error || !a.result)
71
+ return r(a.error ?? "Failed to get token");
72
+ t = a.result;
73
+ } catch (o) {
74
+ return r("Failed to get token", o);
75
+ }
76
+ if (!this.verifyToken(t, this.walletClient.account.address).val)
77
+ throw new Error("Token verification failed");
78
+ return this.store.setItem(d.AUTH_TOKEN, t), s(t);
79
+ }
80
+ async signStatement() {
81
+ var a;
82
+ if (!((a = this.walletClient.account) != null && a.address))
83
+ return r("Wallet client does not have a valid account");
84
+ const e = /* @__PURE__ */ new Date(), n = new Date(e.getTime() + 300 * 1e3);
85
+ let t;
86
+ try {
87
+ const l = await (await this.fetchWithCookies(this.url.endpoint("challenges"), {
88
+ method: "POST",
89
+ headers: {
90
+ "Content-Type": "application/json"
91
+ },
92
+ credentials: "include"
93
+ })).json();
94
+ if (l.error || !l.result)
95
+ return r("Failed to get nonce");
96
+ t = l.result;
97
+ } catch (h) {
98
+ return r("Failed to get nonce", h);
99
+ }
100
+ const i = await this.walletClient.getChainId(), c = v({
101
+ domain: this.domain,
102
+ address: this.walletClient.account.address,
103
+ statement: this.signingStatement,
104
+ nonce: t,
105
+ uri: "https://" + this.domain,
106
+ version: "1",
107
+ chainId: i,
108
+ notBefore: n
109
+ }), o = await this.walletClient.signMessage({
110
+ account: this.walletClient.account,
111
+ message: c
112
+ });
113
+ return s({
114
+ message: c,
115
+ signature: o,
116
+ nonce: t
117
+ });
118
+ }
119
+ async getAuthHeaders() {
120
+ const e = await this.getToken();
121
+ return e.ok ? s({ Authorization: p(e.val) }) : r(e.error);
122
+ }
123
+ }
2
124
  export {
3
- o as AuthHeaderEnum
125
+ f as Siwe
4
126
  };
package/dist/index11.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const w=require("tough-cookie"),g=require("fetch-cookie"),k=require("./index15.cjs"),f=require("./index16.cjs"),h=require("./index3.cjs"),n=require("./index5.cjs"),m=require("./index26.cjs"),y=require("./index27.cjs"),C=require("./index23.cjs"),T=require("./index28.cjs"),v=require("./index29.cjs"),S=c=>c&&c.__esModule?c:{default:c},p=S(g);class u{constructor(t,i,e){if(this.url=t.endpoint("siwe"),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)??(typeof window<"u"?window.localStorage:new k.MemoryStorage),typeof window>"u"){const r=new w.CookieJar;this.fetchWithCookies=p.default(fetch,r)}else this.fetchWithCookies=window.fetch.bind(window)}static fromDigestKey(t,i,e){const r=m.createWalletClient({account:C.privateKeyToAccount(h.add0x(i.digestKey)),transport:y.http(),chain:v.mainnet});return new u(t,r,e)}verifyToken(t,i){try{const e=h.parseJwt(t);if(!e)return n.Ok(!1);const r=Math.floor(Date.now()/1e3)+120;return n.Ok(e.exp>r&&e.user_id.toLowerCase()===i.toLowerCase())}catch{return n.Ok(!1)}}async getToken(){var s;if(!((s=this.walletClient.account)!=null&&s.address))return n.Err("Wallet client does not have an account");const t=this.store.getItem(f.StoreKeys.AUTH_TOKEN);if(t){const o=this.verifyToken(t,this.walletClient.account.address);if(o.ok&&o.val)return n.Ok(t)}const i=await this.signStatement();if(i.error)return n.Err(i.error);let e;try{const a=await(await this.fetchWithCookies(this.url.endpoint("tokens"),{method:"POST",body:JSON.stringify({...i.val}),headers:{"Content-Type":"application/json"},credentials:"include"})).json();if(a.error||!a.result)return n.Err(a.error??"Failed to get token");e=a.result}catch(o){return n.Err("Failed to get token",o)}if(!this.verifyToken(e,this.walletClient.account.address).val)throw new Error("Token verification failed");return this.store.setItem(f.StoreKeys.AUTH_TOKEN,e),n.Ok(e)}async signStatement(){var a;if(!((a=this.walletClient.account)!=null&&a.address))return n.Err("Wallet client does not have a valid account");const t=new Date,i=new Date(t.getTime()+300*1e3);let e;try{const l=await(await this.fetchWithCookies(this.url.endpoint("challenges"),{method:"POST",headers:{"Content-Type":"application/json"},credentials:"include"})).json();if(l.error||!l.result)return n.Err("Failed to get nonce");e=l.result}catch(d){return n.Err("Failed to get nonce",d)}const r=await this.walletClient.getChainId(),s=T.createSiweMessage({domain:this.domain,address:this.walletClient.account.address,statement:this.signingStatement,nonce:e,uri:"https://"+this.domain,version:"1",chainId:r,notBefore:i}),o=await this.walletClient.signMessage({account:this.walletClient.account,message:s});return n.Ok({message:s,signature:o,nonce:e})}async getAuthHeaders(){const t=await this.getToken();return t.ok?n.Ok({Authorization:h.Authorization(t.val)}):n.Err(t.error)}}exports.Siwe=u;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("./index5.cjs"),d=require("./index9.cjs");class f{constructor(e){this.apiKey=e}async getToken(){const e=this.verifyToken();return e.error?t.Err(e.error):e.val?t.Ok(this.apiKey):t.Err("Token verification failed")}verifyToken(){const e=this.decodeBase64UrlSafe(this.apiKey),r=this.extractData(e);if(!r.ok)return t.Err(r.error);const{expiryTimestamp:i}=r.val;return i<new Date?t.Err("Token expired"):t.Ok(!0)}decodeBase64UrlSafe(e){let r=e.replace(/_/g,"/").replace(/-/g,"+");for(;r.length%4!==0;)r+="=";return Uint8Array.from(atob(r),i=>i.charCodeAt(0))}extractData(e){const r=new TextEncoder().encode("::"),i=e.findIndex((n,a)=>e.slice(a,a+r.length).every((u,l)=>u===r[l]));if(i===-1)return t.Err("Invalid format: delimiter not found");const o=e.slice(0,8),s=new DataView(o.buffer).getBigUint64(0,!1),c=e.slice(i+r.length);return t.Ok({expiryTimestamp:new Date(Number(s)*1e3),signature:Array.from(c).map(n=>n.toString(16).padStart(2,"0")).join("")})}async getAuthHeaders(){const e=await this.getToken();return e.ok?t.Ok({[d.AuthHeaderEnum.ApiKey]:e.val}):t.Err(e.error??"Failed to get auth token")}}exports.ApiKey=f;
package/dist/index11.js CHANGED
@@ -1,126 +1,46 @@
1
- import { CookieJar as m } from "tough-cookie";
2
- import u from "fetch-cookie";
3
- import { MemoryStorage as w } from "./index15.js";
4
- import { StoreKeys as d } from "./index16.js";
5
- import { add0x as g, parseJwt as k, Authorization as p } from "./index3.js";
6
- import { Ok as s, Err as r } from "./index5.js";
7
- import { createWalletClient as y } from "./index26.js";
8
- import { http as C } from "./index27.js";
9
- import { privateKeyToAccount as T } from "./index23.js";
10
- import { createSiweMessage as v } from "./index28.js";
11
- import { mainnet as S } from "./index29.js";
12
- class f {
13
- constructor(e, n, t) {
14
- if (this.url = e.endpoint("siwe"), this.walletClient = n, 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) ?? (typeof window < "u" ? window.localStorage : new w()), typeof window > "u") {
15
- const i = new m();
16
- this.fetchWithCookies = u(fetch, i);
17
- } else
18
- this.fetchWithCookies = window.fetch.bind(window);
1
+ import { Err as i, Ok as n } from "./index5.js";
2
+ import { AuthHeaderEnum as u } from "./index9.js";
3
+ class h {
4
+ constructor(e) {
5
+ this.apiKey = e;
19
6
  }
20
- static fromDigestKey(e, n, t) {
21
- const i = y({
22
- account: T(g(n.digestKey)),
23
- transport: C(),
24
- chain: S
25
- });
26
- return new f(e, i, t);
7
+ async getToken() {
8
+ const e = this.verifyToken();
9
+ return e.error ? i(e.error) : e.val ? n(this.apiKey) : i("Token verification failed");
27
10
  }
28
- verifyToken(e, n) {
29
- try {
30
- const t = k(e);
31
- if (!t) return s(!1);
32
- const i = Math.floor(Date.now() / 1e3) + 120;
33
- return s(
34
- t.exp > i && t.user_id.toLowerCase() === n.toLowerCase()
35
- );
36
- } catch {
37
- return s(!1);
38
- }
11
+ verifyToken() {
12
+ const e = this.decodeBase64UrlSafe(this.apiKey), r = this.extractData(e);
13
+ if (!r.ok)
14
+ return i(r.error);
15
+ const { expiryTimestamp: t } = r.val;
16
+ return t < /* @__PURE__ */ new Date() ? i("Token expired") : n(!0);
39
17
  }
40
- async getToken() {
41
- var c;
42
- if (!((c = this.walletClient.account) != null && c.address))
43
- return r("Wallet client does not have an account");
44
- const e = this.store.getItem(d.AUTH_TOKEN);
45
- if (e) {
46
- const o = this.verifyToken(
47
- e,
48
- this.walletClient.account.address
49
- );
50
- if (o.ok && o.val) return s(e);
51
- }
52
- const n = await this.signStatement();
53
- if (n.error)
54
- return r(n.error);
55
- let t;
56
- try {
57
- const a = await (await this.fetchWithCookies(
58
- this.url.endpoint("tokens"),
59
- {
60
- method: "POST",
61
- body: JSON.stringify({
62
- ...n.val
63
- }),
64
- headers: {
65
- "Content-Type": "application/json"
66
- },
67
- credentials: "include"
68
- }
69
- )).json();
70
- if (a.error || !a.result)
71
- return r(a.error ?? "Failed to get token");
72
- t = a.result;
73
- } catch (o) {
74
- return r("Failed to get token", o);
75
- }
76
- if (!this.verifyToken(t, this.walletClient.account.address).val)
77
- throw new Error("Token verification failed");
78
- return this.store.setItem(d.AUTH_TOKEN, t), s(t);
18
+ decodeBase64UrlSafe(e) {
19
+ let r = e.replace(/_/g, "/").replace(/-/g, "+");
20
+ for (; r.length % 4 !== 0; )
21
+ r += "=";
22
+ return Uint8Array.from(atob(r), (t) => t.charCodeAt(0));
79
23
  }
80
- async signStatement() {
81
- var a;
82
- if (!((a = this.walletClient.account) != null && a.address))
83
- return r("Wallet client does not have a valid account");
84
- const e = /* @__PURE__ */ new Date(), n = new Date(e.getTime() + 300 * 1e3);
85
- let t;
86
- try {
87
- const l = await (await this.fetchWithCookies(this.url.endpoint("challenges"), {
88
- method: "POST",
89
- headers: {
90
- "Content-Type": "application/json"
91
- },
92
- credentials: "include"
93
- })).json();
94
- if (l.error || !l.result)
95
- return r("Failed to get nonce");
96
- t = l.result;
97
- } catch (h) {
98
- return r("Failed to get nonce", h);
99
- }
100
- const i = await this.walletClient.getChainId(), c = v({
101
- domain: this.domain,
102
- address: this.walletClient.account.address,
103
- statement: this.signingStatement,
104
- nonce: t,
105
- uri: "https://" + this.domain,
106
- version: "1",
107
- chainId: i,
108
- notBefore: n
109
- }), o = await this.walletClient.signMessage({
110
- account: this.walletClient.account,
111
- message: c
112
- });
113
- return s({
114
- message: c,
115
- signature: o,
116
- nonce: t
24
+ extractData(e) {
25
+ const r = new TextEncoder().encode("::"), t = e.findIndex(
26
+ (a, o) => e.slice(o, o + r.length).every((f, l) => f === r[l])
27
+ );
28
+ if (t === -1)
29
+ return i("Invalid format: delimiter not found");
30
+ const s = e.slice(0, 8), c = new DataView(s.buffer).getBigUint64(
31
+ 0,
32
+ !1
33
+ ), d = e.slice(t + r.length);
34
+ return n({
35
+ expiryTimestamp: new Date(Number(c) * 1e3),
36
+ signature: Array.from(d).map((a) => a.toString(16).padStart(2, "0")).join("")
117
37
  });
118
38
  }
119
39
  async getAuthHeaders() {
120
40
  const e = await this.getToken();
121
- return e.ok ? s({ Authorization: p(e.val) }) : r(e.error);
41
+ return e.ok ? n({ [u.ApiKey]: e.val }) : i(e.error ?? "Failed to get auth token");
122
42
  }
123
43
  }
124
44
  export {
125
- f as Siwe
45
+ h as ApiKey
126
46
  };
package/dist/index12.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("./index5.cjs"),d=require("./index10.cjs");class f{constructor(e){this.apiKey=e}async getToken(){const e=this.verifyToken();return e.error?t.Err(e.error):e.val?t.Ok(this.apiKey):t.Err("Token verification failed")}verifyToken(){const e=this.decodeBase64UrlSafe(this.apiKey),r=this.extractData(e);if(!r.ok)return t.Err(r.error);const{expiryTimestamp:i}=r.val;return i<new Date?t.Err("Token expired"):t.Ok(!0)}decodeBase64UrlSafe(e){let r=e.replace(/_/g,"/").replace(/-/g,"+");for(;r.length%4!==0;)r+="=";return Uint8Array.from(atob(r),i=>i.charCodeAt(0))}extractData(e){const r=new TextEncoder().encode("::"),i=e.findIndex((n,a)=>e.slice(a,a+r.length).every((u,l)=>u===r[l]));if(i===-1)return t.Err("Invalid format: delimiter not found");const o=e.slice(0,8),s=new DataView(o.buffer).getBigUint64(0,!1),c=e.slice(i+r.length);return t.Ok({expiryTimestamp:new Date(Number(s)*1e3),signature:Array.from(c).map(n=>n.toString(16).padStart(2,"0")).join("")})}async getAuthHeaders(){const e=await this.getToken();return e.ok?t.Ok({[d.AuthHeaderEnum.ApiKey]:e.val}):t.Err(e.error??"Failed to get auth token")}}exports.ApiKey=f;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("./index9.cjs"),s=require("./index3.cjs"),t=require("./index5.cjs");class i{constructor(e){this.token=e}async getToken(){return this.token?this.verifyToken(this.token).ok?t.Ok(this.token):t.Err("Token is invalid"):t.Err("Token is not set")}verifyToken(e){try{const r=s.parseJwt(e);if(!r)return t.Ok(!1);const n=Math.floor(Date.now()/1e3)+120;return t.Ok(r.exp>n)}catch{return t.Ok(!1)}}async getAuthHeaders(){const e=await this.getToken();return e.ok?t.Ok({[o.AuthHeaderEnum.Authorization]:e.val}):t.Err(e.error??"Failed to get auth token")}}exports.Passkey=i;
package/dist/index12.js CHANGED
@@ -1,46 +1,28 @@
1
- import { Err as i, Ok as n } from "./index5.js";
2
- import { AuthHeaderEnum as u } from "./index10.js";
3
- class h {
4
- constructor(e) {
5
- this.apiKey = e;
1
+ import { AuthHeaderEnum as i } from "./index9.js";
2
+ import { parseJwt as s } from "./index3.js";
3
+ import { Err as r, Ok as e } from "./index5.js";
4
+ class c {
5
+ constructor(t) {
6
+ this.token = t;
6
7
  }
7
8
  async getToken() {
8
- const e = this.verifyToken();
9
- return e.error ? i(e.error) : e.val ? n(this.apiKey) : i("Token verification failed");
9
+ return this.token ? this.verifyToken(this.token).ok ? e(this.token) : r("Token is invalid") : r("Token is not set");
10
10
  }
11
- verifyToken() {
12
- const e = this.decodeBase64UrlSafe(this.apiKey), r = this.extractData(e);
13
- if (!r.ok)
14
- return i(r.error);
15
- const { expiryTimestamp: t } = r.val;
16
- return t < /* @__PURE__ */ new Date() ? i("Token expired") : n(!0);
17
- }
18
- decodeBase64UrlSafe(e) {
19
- let r = e.replace(/_/g, "/").replace(/-/g, "+");
20
- for (; r.length % 4 !== 0; )
21
- r += "=";
22
- return Uint8Array.from(atob(r), (t) => t.charCodeAt(0));
23
- }
24
- extractData(e) {
25
- const r = new TextEncoder().encode("::"), t = e.findIndex(
26
- (a, o) => e.slice(o, o + r.length).every((f, l) => f === r[l])
27
- );
28
- if (t === -1)
29
- return i("Invalid format: delimiter not found");
30
- const s = e.slice(0, 8), c = new DataView(s.buffer).getBigUint64(
31
- 0,
32
- !1
33
- ), d = e.slice(t + r.length);
34
- return n({
35
- expiryTimestamp: new Date(Number(c) * 1e3),
36
- signature: Array.from(d).map((a) => a.toString(16).padStart(2, "0")).join("")
37
- });
11
+ verifyToken(t) {
12
+ try {
13
+ const o = s(t);
14
+ if (!o) return e(!1);
15
+ const n = Math.floor(Date.now() / 1e3) + 120;
16
+ return e(o.exp > n);
17
+ } catch {
18
+ return e(!1);
19
+ }
38
20
  }
39
21
  async getAuthHeaders() {
40
- const e = await this.getToken();
41
- return e.ok ? n({ [u.ApiKey]: e.val }) : i(e.error ?? "Failed to get auth token");
22
+ const t = await this.getToken();
23
+ return t.ok ? e({ [i.Authorization]: t.val }) : r(t.error ?? "Failed to get auth token");
42
24
  }
43
25
  }
44
26
  export {
45
- h as ApiKey
27
+ c as Passkey
46
28
  };
package/dist/index13.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("./index10.cjs"),s=require("./index3.cjs"),t=require("./index5.cjs");class i{constructor(e){this.token=e}async getToken(){return this.token?this.verifyToken(this.token).ok?t.Ok(this.token):t.Err("Token is invalid"):t.Err("Token is not set")}verifyToken(e){try{const r=s.parseJwt(e);if(!r)return t.Ok(!1);const n=Math.floor(Date.now()/1e3)+120;return t.Ok(r.exp>n)}catch{return t.Ok(!1)}}async getAuthHeaders(){const e=await this.getToken();return e.ok?t.Ok({[o.AuthHeaderEnum.Authorization]:e.val}):t.Err(e.error??"Failed to get auth token")}}exports.Passkey=i;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("@simplewebauthn/browser"),e=require("./index5.cjs"),l=require("./index18.cjs");class u{constructor(r,t){if(this.storeKey="passkey-auth",!window)throw new Error("Passkey is only supported in the browser environment.");this.url=r.endpoint("passkey"),this.store=t}getPasskeyFromStore(){if(this.token&&this.userName)return e.Ok({token:this.token,gardenUserName:this.userName});const r=this.store.getItem(this.storeKey);if(r){const t=JSON.parse(r);return e.Ok(t)}return e.Err("Passkey is not set")}validateUsername(r){return!r||r.length===0?e.Err("Username cannot be empty"):r.length<3||r.length>32?e.Err("Username must be between 3 and 32 characters"):r.includes(" ")?e.Err("Username cannot contain spaces"):e.Ok(r.trim().toLowerCase())}async register(r){var i;const t=this.validateUsername(r);if(t.error)return e.Err(t.error);try{const n=await l.Fetcher.post(this.url.endpoint("register/begin"),{body:JSON.stringify({username:t}),headers:{credentials:"include","Content-Type":"application/json"}});if(n.error)return e.Err("Failed to start registration",n.error);const s=(i=n.result)==null?void 0:i.challenge;if(!s)return e.Err("No challenge found");if(!s.publicKey.authenticatorSelection)return e.Err("No authenticator selection found");s.publicKey.authenticatorSelection.requireResidentKey=!0,s.publicKey.authenticatorSelection.residentKey="required";const a=await c.startRegistration({optionsJSON:s.publicKey}),o=await l.Fetcher.post(this.url.endpoint("register/finish"),{body:JSON.stringify({username:t,credential:a}),headers:{credentials:"include","Content-Type":"application/json"}});return o.error?e.Err("Failed to verify registration: "+o.error):o.result?(this.token=o.result.token,this.userName=t.val,e.Ok(o.result)):e.Err("No verification result found")}catch(n){return e.Err("Registration error",n)}}async login(r){var i;const t=this.validateUsername(r);if(t.error)return e.Err(t.error);try{const n=await l.Fetcher.post(this.url.endpoint("login/begin"),{body:JSON.stringify({username:t}),headers:{credentials:"include","Content-Type":"application/json"}});if(n.error)return e.Err("Failed to start login",n.error);const s=(i=n.result)==null?void 0:i.challenge;if(!s)return e.Err("No challenge found");const a=await c.startAuthentication({optionsJSON:s.publicKey}),o=await l.Fetcher.post(this.url.endpoint("login/finish"),{body:JSON.stringify({username:t,credential:a}),headers:{credentials:"include","Content-Type":"application/json"}});return o.error?e.Err("Failed to verify login",o.error):o.result?(this.token=o.result.token,this.userName=t.val,e.Ok(o.result)):e.Err("No verification result found")}catch(n){return e.Err("Login error",n)}}async conditionalLogin(){var r,t;try{const i=await l.Fetcher.get(this.url.endpoint("conditional-login/begin"),{headers:{credentials:"include"}});if(i.error)return e.Err("Failed to start login",i.error);const n=(r=i.result)==null?void 0:r.challenge;if(!n)return e.Err("No challenge found");const s=await c.startAuthentication({optionsJSON:n.publicKey}),a=await l.Fetcher.post(this.url.endpoint("conditional-login/finish"),{body:JSON.stringify({login_id:(t=i.result)==null?void 0:t.login_id,credential:s}),headers:{credentials:"include","Content-Type":"application/json"}});return a.error?e.Err("Failed to verify login",a.error):a.result?(this.token=a.result.token,this.userName=a.result.gardenUserName,e.Ok(a.result)):e.Err("No verification result found")}catch(i){return e.Err("Conditional login error",i)}}}exports.PasskeyLogin=u;