@gardenfi/utils 2.0.0 → 2.0.2

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 (103) hide show
  1. package/dist/index.cjs +1 -1
  2. package/dist/index.js +12 -11
  3. package/dist/index11.cjs +1 -1
  4. package/dist/index11.js +186 -92
  5. package/dist/index12.cjs +1 -16
  6. package/dist/index12.js +93 -113
  7. package/dist/index13.cjs +16 -1
  8. package/dist/index13.js +112 -30
  9. package/dist/index14.cjs +1 -1
  10. package/dist/index14.js +18 -17
  11. package/dist/index15.cjs +1 -1
  12. package/dist/index15.js +31 -18
  13. package/dist/index16.cjs +1 -1
  14. package/dist/index16.js +17 -7
  15. package/dist/index17.cjs +1 -1
  16. package/dist/index17.js +6 -19
  17. package/dist/index18.cjs +1 -1
  18. package/dist/index18.js +20 -22
  19. package/dist/index19.cjs +1 -1
  20. package/dist/index19.js +21 -8
  21. package/dist/index20.cjs +1 -1
  22. package/dist/index20.js +8 -31
  23. package/dist/index21.cjs +1 -1
  24. package/dist/index21.js +31 -26
  25. package/dist/index22.cjs +1 -1
  26. package/dist/index22.js +25 -22
  27. package/dist/index23.cjs +1 -1
  28. package/dist/index23.js +22 -50
  29. package/dist/index24.cjs +1 -1
  30. package/dist/index24.js +51 -12
  31. package/dist/index25.cjs +1 -1
  32. package/dist/index25.js +12 -24
  33. package/dist/index26.cjs +1 -1
  34. package/dist/index26.js +24 -75
  35. package/dist/index27.cjs +1 -1
  36. package/dist/index27.js +74 -22
  37. package/dist/index28.cjs +1 -1
  38. package/dist/index28.js +23 -7
  39. package/dist/index29.cjs +1 -1
  40. package/dist/index29.js +7 -29
  41. package/dist/index30.cjs +1 -1
  42. package/dist/index30.js +31 -14
  43. package/dist/index31.cjs +1 -1
  44. package/dist/index31.js +16 -2
  45. package/dist/index32.cjs +1 -1
  46. package/dist/index32.js +2 -9
  47. package/dist/index33.cjs +1 -1
  48. package/dist/index33.js +8 -20
  49. package/dist/index34.cjs +1 -1
  50. package/dist/index34.js +20 -12
  51. package/dist/index35.cjs +1 -1
  52. package/dist/index35.js +10 -32
  53. package/dist/index36.cjs +1 -2
  54. package/dist/index36.js +33 -58
  55. package/dist/index37.cjs +2 -1
  56. package/dist/index37.js +60 -2
  57. package/dist/index38.cjs +1 -1
  58. package/dist/index38.js +2 -5
  59. package/dist/index39.cjs +1 -1
  60. package/dist/index39.js +4 -11
  61. package/dist/index4.cjs +1 -1
  62. package/dist/index4.js +15 -15
  63. package/dist/index40.cjs +1 -1
  64. package/dist/index40.js +11 -3
  65. package/dist/index41.cjs +1 -1
  66. package/dist/index41.js +3 -21
  67. package/dist/index42.cjs +1 -1
  68. package/dist/index42.js +21 -12
  69. package/dist/index43.cjs +1 -1
  70. package/dist/index43.js +11 -54
  71. package/dist/index44.cjs +1 -1
  72. package/dist/index44.js +55 -6
  73. package/dist/index45.cjs +1 -1
  74. package/dist/index45.js +6 -21
  75. package/dist/index46.cjs +1 -1
  76. package/dist/index46.js +21 -11
  77. package/dist/index47.cjs +1 -1
  78. package/dist/index47.js +11 -3
  79. package/dist/index48.cjs +1 -1
  80. package/dist/index48.js +3 -11
  81. package/dist/index49.cjs +1 -1
  82. package/dist/index49.js +11 -19
  83. package/dist/index50.cjs +1 -1
  84. package/dist/index50.js +19 -8
  85. package/dist/index51.cjs +1 -1
  86. package/dist/index51.js +1 -1
  87. package/dist/index52.cjs +1 -1
  88. package/dist/index52.js +8 -104
  89. package/dist/index53.cjs +1 -1
  90. package/dist/index53.js +102 -27
  91. package/dist/index54.cjs +1 -1
  92. package/dist/index54.js +27 -17
  93. package/dist/index55.cjs +1 -1
  94. package/dist/index55.js +16 -30
  95. package/dist/index56.cjs +1 -0
  96. package/dist/index56.js +41 -0
  97. package/dist/index6.cjs +1 -1
  98. package/dist/index6.js +65 -49
  99. package/dist/index9.cjs +1 -1
  100. package/dist/index9.js +6 -6
  101. package/dist/src/lib/auth/auth.types.d.ts +9 -10
  102. package/dist/src/lib/auth/siwe.d.ts +13 -3
  103. package/package.json +2 -4
package/dist/index55.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("./index53.cjs");/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */const c=t=>new Uint32Array(t.buffer,t.byteOffset,Math.floor(t.byteLength/4)),i=new Uint8Array(new Uint32Array([287454020]).buffer)[0]===68,o=t=>t<<24&4278190080|t<<8&16711680|t>>>8&65280|t>>>24&255;function y(t){for(let e=0;e<t.length;e++)t[e]=o(t[e])}function f(t){if(typeof t!="string")throw new Error(`utf8ToBytes expected string, got ${typeof t}`);return new Uint8Array(new TextEncoder().encode(t))}function r(t){return typeof t=="string"&&(t=f(t)),s.bytes(t),t}class p{clone(){return this._cloneInto()}}function b(t){const e=u=>t().update(r(u)).digest(),n=t();return e.outputLen=n.outputLen,e.blockLen=n.blockLen,e.create=()=>t(),e}exports.Hash=p;exports.byteSwap=o;exports.byteSwap32=y;exports.isLE=i;exports.toBytes=r;exports.u32=c;exports.utf8ToBytes=f;exports.wrapConstructor=b;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const n=BigInt(2**32-1),i=BigInt(32);function u(t,e=!1){return e?{h:Number(t&n),l:Number(t>>i&n)}:{h:Number(t>>i&n)|0,l:Number(t&n)|0}}function g(t,e=!1){let r=new Uint32Array(t.length),l=new Uint32Array(t.length);for(let o=0;o<t.length;o++){const{h:c,l:f}=u(t[o],e);[r[o],l[o]]=[c,f]}return[r,l]}const B=(t,e,r)=>t<<r|e>>>32-r,h=(t,e,r)=>e<<r|t>>>32-r,m=(t,e,r)=>e<<r-32|t>>>64-r,S=(t,e,r)=>t<<r-32|e>>>64-r;exports.fromBig=u;exports.rotlBH=m;exports.rotlBL=S;exports.rotlSH=B;exports.rotlSL=h;exports.split=g;
package/dist/index55.js CHANGED
@@ -1,35 +1,21 @@
1
- import { bytes as f } from "./index53.js";
2
- /*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */
3
- const s = (t) => new Uint32Array(t.buffer, t.byteOffset, Math.floor(t.byteLength / 4)), y = new Uint8Array(new Uint32Array([287454020]).buffer)[0] === 68, r = (t) => t << 24 & 4278190080 | t << 8 & 16711680 | t >>> 8 & 65280 | t >>> 24 & 255;
4
- function p(t) {
5
- for (let e = 0; e < t.length; e++)
6
- t[e] = r(t[e]);
1
+ const o = /* @__PURE__ */ BigInt(4294967295), i = /* @__PURE__ */ BigInt(32);
2
+ function f(t, r = !1) {
3
+ return r ? { h: Number(t & o), l: Number(t >> i & o) } : { h: Number(t >> i & o) | 0, l: Number(t & o) | 0 };
7
4
  }
8
- function c(t) {
9
- if (typeof t != "string")
10
- throw new Error(`utf8ToBytes expected string, got ${typeof t}`);
11
- return new Uint8Array(new TextEncoder().encode(t));
12
- }
13
- function u(t) {
14
- return typeof t == "string" && (t = c(t)), f(t), t;
15
- }
16
- class b {
17
- // Safe version that clones internal state
18
- clone() {
19
- return this._cloneInto();
5
+ function h(t, r = !1) {
6
+ let n = new Uint32Array(t.length), l = new Uint32Array(t.length);
7
+ for (let e = 0; e < t.length; e++) {
8
+ const { h: c, l: u } = f(t[e], r);
9
+ [n[e], l[e]] = [c, u];
20
10
  }
11
+ return [n, l];
21
12
  }
22
- function x(t) {
23
- const e = (o) => t().update(u(o)).digest(), n = t();
24
- return e.outputLen = n.outputLen, e.blockLen = n.blockLen, e.create = () => t(), e;
25
- }
13
+ const g = (t, r, n) => t << n | r >>> 32 - n, m = (t, r, n) => r << n | t >>> 32 - n, A = (t, r, n) => r << n - 32 | t >>> 64 - n, B = (t, r, n) => t << n - 32 | r >>> 64 - n;
26
14
  export {
27
- b as Hash,
28
- r as byteSwap,
29
- p as byteSwap32,
30
- y as isLE,
31
- u as toBytes,
32
- s as u32,
33
- c as utf8ToBytes,
34
- x as wrapConstructor
15
+ f as fromBig,
16
+ A as rotlBH,
17
+ B as rotlBL,
18
+ g as rotlSH,
19
+ m as rotlSL,
20
+ h as split
35
21
  };
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const i=require("./index54.cjs");/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */function c(t){return new Uint32Array(t.buffer,t.byteOffset,Math.floor(t.byteLength/4))}const s=new Uint8Array(new Uint32Array([287454020]).buffer)[0]===68;function o(t){return t<<24&4278190080|t<<8&16711680|t>>>8&65280|t>>>24&255}function y(t){for(let e=0;e<t.length;e++)t[e]=o(t[e])}function r(t){if(typeof t!="string")throw new Error("utf8ToBytes expected string, got "+typeof t);return new Uint8Array(new TextEncoder().encode(t))}function f(t){return typeof t=="string"&&(t=r(t)),i.abytes(t),t}class p{clone(){return this._cloneInto()}}function b(t){const e=u=>t().update(f(u)).digest(),n=t();return e.outputLen=n.outputLen,e.blockLen=n.blockLen,e.create=()=>t(),e}exports.Hash=p;exports.byteSwap=o;exports.byteSwap32=y;exports.isLE=s;exports.toBytes=f;exports.u32=c;exports.utf8ToBytes=r;exports.wrapConstructor=b;
@@ -0,0 +1,41 @@
1
+ import { abytes as o } from "./index54.js";
2
+ /*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */
3
+ function y(t) {
4
+ return new Uint32Array(t.buffer, t.byteOffset, Math.floor(t.byteLength / 4));
5
+ }
6
+ const p = new Uint8Array(new Uint32Array([287454020]).buffer)[0] === 68;
7
+ function r(t) {
8
+ return t << 24 & 4278190080 | t << 8 & 16711680 | t >>> 8 & 65280 | t >>> 24 & 255;
9
+ }
10
+ function s(t) {
11
+ for (let e = 0; e < t.length; e++)
12
+ t[e] = r(t[e]);
13
+ }
14
+ function u(t) {
15
+ if (typeof t != "string")
16
+ throw new Error("utf8ToBytes expected string, got " + typeof t);
17
+ return new Uint8Array(new TextEncoder().encode(t));
18
+ }
19
+ function i(t) {
20
+ return typeof t == "string" && (t = u(t)), o(t), t;
21
+ }
22
+ class b {
23
+ // Safe version that clones internal state
24
+ clone() {
25
+ return this._cloneInto();
26
+ }
27
+ }
28
+ function x(t) {
29
+ const e = (f) => t().update(i(f)).digest(), n = t();
30
+ return e.outputLen = n.outputLen, e.blockLen = n.blockLen, e.create = () => t(), e;
31
+ }
32
+ export {
33
+ b as Hash,
34
+ r as byteSwap,
35
+ s as byteSwap32,
36
+ p as isLE,
37
+ i as toBytes,
38
+ y as u32,
39
+ u as utf8ToBytes,
40
+ x as wrapConstructor
41
+ };
package/dist/index6.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("@catalogfi/utils"),u=require("./index7.cjs"),h=require("./index8.cjs"),w=require("jwt-decode"),f=require("./index5.cjs"),g=require("./index12.cjs");class m{constructor(n,i,e){this.API="https://api.garden.finance",this.url=new f.Url("/",n??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 u.MemoryStorage}verifyToken(n,i){try{const e=d(n);if(!e)return t.Ok(!1);const r=Math.floor(Date.now()/1e3)+120;return t.Ok(e.exp>r&&e.address.toLowerCase()===i.toLowerCase())}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 n=this.store.getItem(h.StoreKeys.AUTH_TOKEN);if(n){const a=this.verifyToken(n,this.walletClient.account.address);if(a.ok&&a.val)return t.Ok(n)}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 r=e.result;if(!r)return t.Err("Failed to get token");if(!this.verifyToken(r,this.walletClient.account.address).val)throw new Error("Token verification failed");return this.store.setItem(h.StoreKeys.AUTH_TOKEN,r),t.Ok(r)}async signStatement(){var l;if(!((l=this.walletClient.account)!=null&&l.address))return t.Err("Wallet client does not have a valid account");const n=new Date,i=new Date(n.getTime()+300*1e3),r=(await t.Fetcher.get(this.url.endpoint("nonce"))).result;if(!r)return t.Err("Failed to get nonce");const c=await this.walletClient.getChainId(),s=g.createSiweMessage({domain:this.domain,address:this.walletClient.account.address,statement:this.signingStatement,nonce:r,uri:"https://"+this.domain,version:"1",chainId:c,notBefore:i}),a=await this.walletClient.signMessage({account:this.walletClient.account,message:s});return t.Ok({message:s,signature:a,nonce:r})}}const d=o=>{try{return w.jwtDecode(o)}catch{return}};exports.Siwe=m;exports.parseJwt=d;
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;
package/dist/index6.js CHANGED
@@ -1,41 +1,42 @@
1
- import { Ok as r, Err as a, Fetcher as d } from "@catalogfi/utils";
2
- import { MemoryStorage as m } from "./index7.js";
3
- import { StoreKeys as f } from "./index8.js";
4
- import { jwtDecode as u } from "jwt-decode";
5
- import { Url as w } from "./index5.js";
6
- import { createSiweMessage as g } from "./index12.js";
1
+ import { Ok as n, Err as s, Fetcher as d } from "./index11.js";
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";
6
+ import { Url as g } from "./index5.js";
7
+ import { createSiweMessage as y } from "./index13.js";
7
8
  class I {
8
- constructor(n, i, t) {
9
- this.API = "https://api.garden.finance", this.url = new w("/", n ?? this.API), this.walletClient = i, 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 m();
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();
10
11
  }
11
- verifyToken(n, i) {
12
+ verifyToken(t, i) {
12
13
  try {
13
- const t = y(n);
14
- if (!t) return r(!1);
15
- const e = Math.floor(Date.now() / 1e3) + 120;
16
- return r(
17
- t.exp > e && t.address.toLowerCase() === i.toLowerCase()
18
- );
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);
19
20
  } catch {
20
- return r(!1);
21
+ return n(!1);
21
22
  }
22
23
  }
23
24
  async getToken() {
24
- var s;
25
- if (!((s = this.walletClient.account) != null && s.address))
26
- return a("Wallet client does not have an account");
27
- const n = this.store.getItem(f.AUTH_TOKEN);
28
- if (n) {
29
- const o = this.verifyToken(
30
- n,
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,
31
32
  this.walletClient.account.address
32
33
  );
33
- if (o.ok && o.val) return r(n);
34
+ if (c.ok && c.val) return n(t);
34
35
  }
35
36
  const i = await this.signStatement();
36
37
  if (i.error)
37
- return a(i.error);
38
- const t = await d.post(
38
+ return s(i.error);
39
+ const e = await d.post(
39
40
  this.url.endpoint("verify"),
40
41
  {
41
42
  body: JSON.stringify({
@@ -46,50 +47,65 @@ class I {
46
47
  }
47
48
  }
48
49
  );
49
- if (t.error) return a(t.error);
50
- const e = t.result;
51
- if (!e) return a("Failed to get token");
52
- if (!this.verifyToken(e, this.walletClient.account.address).val)
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)
53
54
  throw new Error("Token verification failed");
54
- return this.store.setItem(f.AUTH_TOKEN, e), r(e);
55
+ return this.store.setItem(u.AUTH_TOKEN, r), n(r);
55
56
  }
56
57
  async signStatement() {
57
- var h;
58
- if (!((h = this.walletClient.account) != null && h.address))
59
- return a("Wallet client does not have a valid account");
60
- const n = /* @__PURE__ */ new Date(), i = new Date(n.getTime() + 300 * 1e3), e = (await d.get(
58
+ var l;
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(
61
62
  this.url.endpoint("nonce")
62
63
  )).result;
63
- if (!e)
64
- return a("Failed to get nonce");
65
- const l = await this.walletClient.getChainId(), s = g({
64
+ if (!r)
65
+ return s("Failed to get nonce");
66
+ const o = await this.walletClient.getChainId(), a = y({
66
67
  domain: this.domain,
67
68
  address: this.walletClient.account.address,
68
69
  statement: this.signingStatement,
69
- nonce: e,
70
+ nonce: r,
70
71
  uri: "https://" + this.domain,
71
72
  version: "1",
72
- chainId: l,
73
+ chainId: o,
73
74
  notBefore: i
74
- }), o = await this.walletClient.signMessage({
75
+ }), c = await this.walletClient.signMessage({
75
76
  account: this.walletClient.account,
76
- message: s
77
+ message: a
77
78
  });
78
- return r({
79
- message: s,
80
- signature: o,
81
- nonce: e
79
+ return n({
80
+ message: a,
81
+ signature: c,
82
+ nonce: r
82
83
  });
83
84
  }
84
85
  }
85
- const y = (c) => {
86
+ const p = (h) => {
86
87
  try {
87
- return u(c);
88
+ return w(h);
88
89
  } catch {
89
90
  return;
90
91
  }
91
92
  };
93
+ class E {
94
+ constructor(t) {
95
+ if (!t.siwe && !t.apiKey)
96
+ throw new Error("Either siwe or apiKey must be provided");
97
+ this.siwe = t.siwe, this.apiKey = t.apiKey;
98
+ }
99
+ async getAuthHeaders() {
100
+ if (this.siwe) {
101
+ const t = await this.siwe.getToken();
102
+ return t.error ? s(t.error) : n({ Authorization: m(t.val) });
103
+ }
104
+ return this.apiKey ? n({ "api-key": this.apiKey }) : s("No authentication method available");
105
+ }
106
+ }
92
107
  export {
108
+ E as Auth,
93
109
  I as Siwe,
94
- y as parseJwt
110
+ p as parseJwt
95
111
  };
package/dist/index9.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=require("@catalogfi/utils"),n=require("viem"),s=require("./index13.cjs"),l=require("./index14.cjs"),o={42161:s.mainnet,421614:l.sepolia},b=async e=>{const{chain:t}=e;if(!t)return r.Err("No chain found");const u=(a=>a in o)(t.id)?o[t.id]:t;return await i(u)},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"});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;
package/dist/index9.js CHANGED
@@ -1,7 +1,7 @@
1
- import { Err as o, Ok as n } from "@catalogfi/utils";
2
- import { createPublicClient as m, http as s } from "viem";
3
- import { mainnet as u } from "./index13.js";
4
- import { sepolia as b } from "./index14.js";
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
5
  const e = {
6
6
  42161: u,
7
7
  421614: b
@@ -11,9 +11,9 @@ const e = {
11
11
  const i = ((a) => a in e)(t.id) ? e[t.id] : t;
12
12
  return await l(i);
13
13
  }, l = async (r) => {
14
- const t = m({
14
+ const t = s({
15
15
  chain: r,
16
- transport: s()
16
+ transport: m()
17
17
  });
18
18
  try {
19
19
  const c = await t.getBlockNumber();
@@ -6,16 +6,15 @@ export type SiweOpts = {
6
6
  store?: IStore;
7
7
  signingStatement?: string;
8
8
  };
9
- export interface IAuth {
10
- /**
11
- * Get a token from store if available or fetch from the wallet
12
- * @returns verified token
13
- */
9
+ export type AuthHeader = {
10
+ [key: string]: string;
11
+ };
12
+ export interface ISiwe {
14
13
  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
14
  verifyToken(token: string, account: string): Result<boolean, string>;
21
15
  }
16
+ export interface IAuth {
17
+ siwe?: ISiwe;
18
+ apiKey?: string;
19
+ getAuthHeaders(): AsyncResult<AuthHeader, string>;
20
+ }
@@ -1,9 +1,9 @@
1
- import { IAuth, SiweOpts } from './auth.types';
1
+ import { AuthHeader, IAuth, ISiwe, SiweOpts } from './auth.types';
2
2
  import { AsyncResult, Result } from '@catalogfi/utils';
3
3
  import { Url } from '../url';
4
4
  import { WalletClient } from 'viem';
5
5
 
6
- export declare class Siwe implements IAuth {
6
+ export declare class Siwe implements ISiwe {
7
7
  private readonly API;
8
8
  private readonly url;
9
9
  private store;
@@ -16,6 +16,16 @@ export declare class Siwe implements IAuth {
16
16
  private signStatement;
17
17
  }
18
18
  export declare const parseJwt: (token: string) => {
19
- address: string;
19
+ user_id?: string;
20
+ address?: string;
20
21
  exp: number;
21
22
  } | undefined;
23
+ export declare class Auth implements IAuth {
24
+ siwe?: ISiwe;
25
+ apiKey?: string;
26
+ constructor(opts: {
27
+ siwe?: ISiwe;
28
+ apiKey?: string;
29
+ });
30
+ getAuthHeaders(): AsyncResult<AuthHeader, string>;
31
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gardenfi/utils",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "build": "vite build",
@@ -35,9 +35,7 @@
35
35
  },
36
36
  "sideEffects": false,
37
37
  "dependencies": {
38
- "@catalogfi/utils": "^0.1.11",
39
- "axios": "^1.7.2",
40
38
  "jwt-decode": "^4.0.0",
41
- "viem": "^2.21.15"
39
+ "viem": "^2.21.23"
42
40
  }
43
41
  }