@gardenfi/utils 2.0.2 → 2.0.3-beta.11
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 +1 -1
- package/dist/index6.js +52 -52
- package/dist/src/lib/auth/siwe.d.ts +1 -2
- package/package.json +2 -2
package/dist/index6.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
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
|
|
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
|
|
4
|
-
import { jwtDecode as
|
|
5
|
-
import { Authorization as
|
|
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(
|
|
10
|
-
this.API = "https://api.garden.finance", this.url = new g("/",
|
|
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(
|
|
12
|
+
verifyToken(e, r) {
|
|
13
13
|
try {
|
|
14
|
-
const
|
|
15
|
-
if (!
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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
|
|
26
|
-
if (!((
|
|
27
|
-
return
|
|
28
|
-
const
|
|
29
|
-
if (
|
|
30
|
-
const
|
|
31
|
-
|
|
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 (
|
|
34
|
+
if (o.ok && o.val) return n(e);
|
|
35
35
|
}
|
|
36
|
-
const
|
|
37
|
-
if (
|
|
38
|
-
return
|
|
39
|
-
const
|
|
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
|
-
...
|
|
43
|
+
...r.val
|
|
44
44
|
}),
|
|
45
45
|
headers: {
|
|
46
46
|
"Content-Type": "application/json"
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
49
|
);
|
|
50
|
-
if (
|
|
51
|
-
const
|
|
52
|
-
if (!
|
|
53
|
-
if (!this.verifyToken(
|
|
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(
|
|
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
|
|
61
|
-
const
|
|
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 (!
|
|
65
|
-
return
|
|
66
|
-
const
|
|
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:
|
|
70
|
+
nonce: i,
|
|
71
71
|
uri: "https://" + this.domain,
|
|
72
72
|
version: "1",
|
|
73
|
-
chainId:
|
|
74
|
-
notBefore:
|
|
75
|
-
}),
|
|
73
|
+
chainId: h,
|
|
74
|
+
notBefore: r
|
|
75
|
+
}), o = await this.walletClient.signMessage({
|
|
76
76
|
account: this.walletClient.account,
|
|
77
|
-
message:
|
|
77
|
+
message: s
|
|
78
78
|
});
|
|
79
79
|
return n({
|
|
80
|
-
message:
|
|
81
|
-
signature:
|
|
82
|
-
nonce:
|
|
80
|
+
message: s,
|
|
81
|
+
signature: o,
|
|
82
|
+
nonce: i
|
|
83
83
|
});
|
|
84
84
|
}
|
|
85
85
|
}
|
|
86
|
-
const p = (
|
|
86
|
+
const p = (c) => {
|
|
87
87
|
try {
|
|
88
|
-
return
|
|
88
|
+
return m(c);
|
|
89
89
|
} catch {
|
|
90
90
|
return;
|
|
91
91
|
}
|
|
92
92
|
};
|
|
93
93
|
class E {
|
|
94
|
-
constructor(
|
|
95
|
-
if (!
|
|
94
|
+
constructor(e) {
|
|
95
|
+
if (!e.siwe && !e.apiKey)
|
|
96
96
|
throw new Error("Either siwe or apiKey must be provided");
|
|
97
|
-
this.siwe =
|
|
97
|
+
this.siwe = e.siwe, this.apiKey = e.apiKey;
|
|
98
98
|
}
|
|
99
99
|
async getAuthHeaders() {
|
|
100
100
|
if (this.siwe) {
|
|
101
|
-
const
|
|
102
|
-
return
|
|
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 }) :
|
|
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
|
|
20
|
-
address?: string;
|
|
19
|
+
user_id: string;
|
|
21
20
|
exp: number;
|
|
22
21
|
} | undefined;
|
|
23
22
|
export declare class Auth implements IAuth {
|