@gardenfi/utils 2.0.2 → 2.0.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.
package/dist/index6.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("./index11.cjs"),d=require("./index7.cjs"),h=require("./index8.cjs"),w=require("jwt-decode"),f=require("./index3.cjs"),g=require("./index5.cjs"),y=require("./index13.cjs");class m{constructor(r,i,e){this.API="https://api.garden.finance",this.url=new g.Url("/",r??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 d.MemoryStorage}verifyToken(r,i){try{const e=u(r);if(!e)return t.Ok(!1);const n=Math.floor(Date.now()/1e3)+120;if(!e.address&&!e.user_id)return t.Ok(!1);const a=e.address?e.address.toLowerCase()===i.toLowerCase():!1,s=e.user_id?e.user_id.toLowerCase()===i.toLowerCase():!1;return t.Ok(e.exp>n&&a||s)}catch{return t.Ok(!1)}}async getToken(){var s;if(!((s=this.walletClient.account)!=null&&s.address))return t.Err("Wallet client does not have an account");const r=this.store.getItem(h.StoreKeys.AUTH_TOKEN);if(r){const o=this.verifyToken(r,this.walletClient.account.address);if(o.ok&&o.val)return t.Ok(r)}const i=await this.signStatement();if(i.error)return t.Err(i.error);const e=await t.Fetcher.post(this.url.endpoint("verify"),{body:JSON.stringify({...i.val}),headers:{"Content-Type":"application/json"}});if(e.error)return t.Err(e.error);const n=e.result;if(!n)return t.Err("Failed to get token");if(!this.verifyToken(n,this.walletClient.account.address).val)throw new Error("Token verification failed");return this.store.setItem(h.StoreKeys.AUTH_TOKEN,n),t.Ok(n)}async signStatement(){var l;if(!((l=this.walletClient.account)!=null&&l.address))return t.Err("Wallet client does not have a valid account");const r=new Date,i=new Date(r.getTime()+300*1e3),n=(await t.Fetcher.get(this.url.endpoint("nonce"))).result;if(!n)return t.Err("Failed to get nonce");const a=await this.walletClient.getChainId(),s=y.createSiweMessage({domain:this.domain,address:this.walletClient.account.address,statement:this.signingStatement,nonce:n,uri:"https://"+this.domain,version:"1",chainId:a,notBefore:i}),o=await this.walletClient.signMessage({account:this.walletClient.account,message:s});return t.Ok({message:s,signature:o,nonce:n})}}const u=c=>{try{return w.jwtDecode(c)}catch{return}};class k{constructor(r){if(!r.siwe&&!r.apiKey)throw new Error("Either siwe or apiKey must be provided");this.siwe=r.siwe,this.apiKey=r.apiKey}async getAuthHeaders(){if(this.siwe){const r=await this.siwe.getToken();return r.error?t.Err(r.error):t.Ok({Authorization:f.Authorization(r.val)})}return this.apiKey?t.Ok({"api-key":this.apiKey}):t.Err("No authentication method available")}}exports.Auth=k;exports.Siwe=m;exports.parseJwt=u;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=require("./index11.cjs"),d=require("./index7.cjs"),l=require("./index8.cjs"),w=require("jwt-decode"),f=require("./index3.cjs"),g=require("./index5.cjs"),y=require("./index13.cjs");class m{constructor(t,n,e){this.API="https://api.garden.finance",this.url=new g.Url("/",t??this.API),this.walletClient=n,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 d.MemoryStorage}verifyToken(t,n){try{const e=u(t);if(!e)return r.Ok(!1);const i=Math.floor(Date.now()/1e3)+120;return r.Ok(e.exp>i&&e.user_id.toLowerCase()===n.toLowerCase())}catch{return r.Ok(!1)}}async getToken(){var a;if(!((a=this.walletClient.account)!=null&&a.address))return r.Err("Wallet client does not have an account");const t=this.store.getItem(l.StoreKeys.AUTH_TOKEN);if(t){const s=this.verifyToken(t,this.walletClient.account.address);if(s.ok&&s.val)return r.Ok(t)}const n=await this.signStatement();if(n.error)return r.Err(n.error);const e=await r.Fetcher.post(this.url.endpoint("verify"),{body:JSON.stringify({...n.val}),headers:{"Content-Type":"application/json"}});if(e.error)return r.Err(e.error);const i=e.result;if(!i)return r.Err("Failed to get token");if(!this.verifyToken(i,this.walletClient.account.address).val)throw new Error("Token verification failed");return this.store.setItem(l.StoreKeys.AUTH_TOKEN,i),r.Ok(i)}async signStatement(){var h;if(!((h=this.walletClient.account)!=null&&h.address))return r.Err("Wallet client does not have a valid account");const t=new Date,n=new Date(t.getTime()+300*1e3),i=(await r.Fetcher.get(this.url.endpoint("nonce"))).result;if(!i)return r.Err("Failed to get nonce");const c=await this.walletClient.getChainId(),a=y.createSiweMessage({domain:this.domain,address:this.walletClient.account.address,statement:this.signingStatement,nonce:i,uri:"https://"+this.domain,version:"1",chainId:c,notBefore:n}),s=await this.walletClient.signMessage({account:this.walletClient.account,message:a});return r.Ok({message:a,signature:s,nonce:i})}}const u=o=>{try{return w.jwtDecode(o)}catch{return}};class k{constructor(t){if(!t.siwe&&!t.apiKey)throw new Error("Either siwe or apiKey must be provided");this.siwe=t.siwe,this.apiKey=t.apiKey}async getAuthHeaders(){if(this.siwe){const t=await this.siwe.getToken();return t.error?r.Err(t.error):r.Ok({Authorization:f.Authorization(t.val)})}return this.apiKey?r.Ok({"api-key":this.apiKey}):r.Err("No authentication method available")}}exports.Auth=k;exports.Siwe=m;exports.parseJwt=u;
package/dist/index6.js CHANGED
@@ -1,107 +1,107 @@
1
- import { Ok as n, Err as s, Fetcher as d } from "./index11.js";
1
+ import { Ok as n, Err as a, Fetcher as u } from "./index11.js";
2
2
  import { MemoryStorage as f } from "./index7.js";
3
- import { StoreKeys as u } from "./index8.js";
4
- import { jwtDecode as w } from "jwt-decode";
5
- import { Authorization as m } from "./index3.js";
3
+ import { StoreKeys as d } from "./index8.js";
4
+ import { jwtDecode as m } from "jwt-decode";
5
+ import { Authorization as w } from "./index3.js";
6
6
  import { Url as g } from "./index5.js";
7
7
  import { createSiweMessage as y } from "./index13.js";
8
8
  class I {
9
- constructor(t, i, e) {
10
- this.API = "https://api.garden.finance", this.url = new g("/", 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 f();
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();
11
11
  }
12
- verifyToken(t, i) {
12
+ verifyToken(e, r) {
13
13
  try {
14
- const e = p(t);
15
- if (!e) return n(!1);
16
- const r = Math.floor(Date.now() / 1e3) + 120;
17
- if (!e.address && !e.user_id) return n(!1);
18
- const o = e.address ? e.address.toLowerCase() === i.toLowerCase() : !1, a = e.user_id ? e.user_id.toLowerCase() === i.toLowerCase() : !1;
19
- return n(e.exp > r && o || a);
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
20
  } catch {
21
21
  return n(!1);
22
22
  }
23
23
  }
24
24
  async getToken() {
25
- var a;
26
- if (!((a = this.walletClient.account) != null && a.address))
27
- return s("Wallet client does not have an account");
28
- const t = this.store.getItem(u.AUTH_TOKEN);
29
- if (t) {
30
- const c = this.verifyToken(
31
- t,
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
32
  this.walletClient.account.address
33
33
  );
34
- if (c.ok && c.val) return n(t);
34
+ if (o.ok && o.val) return n(e);
35
35
  }
36
- const i = await this.signStatement();
37
- if (i.error)
38
- return s(i.error);
39
- const e = await d.post(
36
+ const r = await this.signStatement();
37
+ if (r.error)
38
+ return a(r.error);
39
+ const t = await u.post(
40
40
  this.url.endpoint("verify"),
41
41
  {
42
42
  body: JSON.stringify({
43
- ...i.val
43
+ ...r.val
44
44
  }),
45
45
  headers: {
46
46
  "Content-Type": "application/json"
47
47
  }
48
48
  }
49
49
  );
50
- if (e.error) return s(e.error);
51
- const r = e.result;
52
- if (!r) return s("Failed to get token");
53
- if (!this.verifyToken(r, this.walletClient.account.address).val)
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
54
  throw new Error("Token verification failed");
55
- return this.store.setItem(u.AUTH_TOKEN, r), n(r);
55
+ return this.store.setItem(d.AUTH_TOKEN, i), n(i);
56
56
  }
57
57
  async signStatement() {
58
58
  var l;
59
59
  if (!((l = this.walletClient.account) != null && l.address))
60
- return s("Wallet client does not have a valid account");
61
- const t = /* @__PURE__ */ new Date(), i = new Date(t.getTime() + 300 * 1e3), r = (await d.get(
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
62
  this.url.endpoint("nonce")
63
63
  )).result;
64
- if (!r)
65
- return s("Failed to get nonce");
66
- const o = await this.walletClient.getChainId(), a = y({
64
+ if (!i)
65
+ return a("Failed to get nonce");
66
+ const h = await this.walletClient.getChainId(), s = y({
67
67
  domain: this.domain,
68
68
  address: this.walletClient.account.address,
69
69
  statement: this.signingStatement,
70
- nonce: r,
70
+ nonce: i,
71
71
  uri: "https://" + this.domain,
72
72
  version: "1",
73
- chainId: o,
74
- notBefore: i
75
- }), c = await this.walletClient.signMessage({
73
+ chainId: h,
74
+ notBefore: r
75
+ }), o = await this.walletClient.signMessage({
76
76
  account: this.walletClient.account,
77
- message: a
77
+ message: s
78
78
  });
79
79
  return n({
80
- message: a,
81
- signature: c,
82
- nonce: r
80
+ message: s,
81
+ signature: o,
82
+ nonce: i
83
83
  });
84
84
  }
85
85
  }
86
- const p = (h) => {
86
+ const p = (c) => {
87
87
  try {
88
- return w(h);
88
+ return m(c);
89
89
  } catch {
90
90
  return;
91
91
  }
92
92
  };
93
93
  class E {
94
- constructor(t) {
95
- if (!t.siwe && !t.apiKey)
94
+ constructor(e) {
95
+ if (!e.siwe && !e.apiKey)
96
96
  throw new Error("Either siwe or apiKey must be provided");
97
- this.siwe = t.siwe, this.apiKey = t.apiKey;
97
+ this.siwe = e.siwe, this.apiKey = e.apiKey;
98
98
  }
99
99
  async getAuthHeaders() {
100
100
  if (this.siwe) {
101
- const t = await this.siwe.getToken();
102
- return t.error ? s(t.error) : n({ Authorization: m(t.val) });
101
+ const e = await this.siwe.getToken();
102
+ return e.error ? a(e.error) : n({ Authorization: w(e.val) });
103
103
  }
104
- return this.apiKey ? n({ "api-key": this.apiKey }) : s("No authentication method available");
104
+ return this.apiKey ? n({ "api-key": this.apiKey }) : a("No authentication method available");
105
105
  }
106
106
  }
107
107
  export {
@@ -16,8 +16,7 @@ export declare class Siwe implements ISiwe {
16
16
  private signStatement;
17
17
  }
18
18
  export declare const parseJwt: (token: string) => {
19
- user_id?: string;
20
- address?: string;
19
+ user_id: string;
21
20
  exp: number;
22
21
  } | undefined;
23
22
  export declare class Auth implements IAuth {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gardenfi/utils",
3
- "version": "2.0.2",
3
+ "version": "2.0.3",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "build": "vite build",