@gardenfi/utils 2.0.4 → 2.0.6-beta.1

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 (243) hide show
  1. package/dist/index.cjs +1 -1
  2. package/dist/index.js +35 -24
  3. package/dist/index10.cjs +1 -1
  4. package/dist/index10.js +42 -32
  5. package/dist/index100.cjs +1 -0
  6. package/dist/index100.js +4 -0
  7. package/dist/index101.cjs +1 -0
  8. package/dist/index101.js +7 -0
  9. package/dist/index102.cjs +1 -0
  10. package/dist/index102.js +13 -0
  11. package/dist/index103.cjs +1 -0
  12. package/dist/index103.js +23 -0
  13. package/dist/index104.cjs +1 -0
  14. package/dist/index104.js +9 -0
  15. package/dist/index105.cjs +1 -0
  16. package/dist/index105.js +7 -0
  17. package/dist/index106.cjs +1 -0
  18. package/dist/index106.js +40 -0
  19. package/dist/index107.cjs +1 -0
  20. package/dist/index107.js +130 -0
  21. package/dist/index108.cjs +1 -0
  22. package/dist/index108.js +4 -0
  23. package/dist/index109.cjs +2 -0
  24. package/dist/index109.js +5 -0
  25. package/dist/index11.cjs +1 -1
  26. package/dist/index11.js +21 -14
  27. package/dist/index110.cjs +1 -0
  28. package/dist/index110.js +70 -0
  29. package/dist/index111.cjs +1 -0
  30. package/dist/index111.js +72 -0
  31. package/dist/index112.cjs +1 -0
  32. package/dist/index112.js +4 -0
  33. package/dist/index113.cjs +1 -0
  34. package/dist/index113.js +37 -0
  35. package/dist/index114.cjs +1 -0
  36. package/dist/index114.js +43 -0
  37. package/dist/index115.cjs +1 -0
  38. package/dist/index115.js +646 -0
  39. package/dist/index116.cjs +1 -0
  40. package/dist/index116.js +234 -0
  41. package/dist/index117.cjs +1 -0
  42. package/dist/index117.js +175 -0
  43. package/dist/index118.cjs +1 -0
  44. package/dist/index118.js +21 -0
  45. package/dist/index12.cjs +1 -1
  46. package/dist/index12.js +126 -173
  47. package/dist/index13.cjs +1 -1
  48. package/dist/index13.js +14 -93
  49. package/dist/index14.cjs +1 -16
  50. package/dist/index14.js +2 -114
  51. package/dist/index15.cjs +1 -1
  52. package/dist/index15.js +33 -31
  53. package/dist/index16.cjs +1 -1
  54. package/dist/index16.js +19 -31
  55. package/dist/index17.cjs +1 -1
  56. package/dist/index17.js +188 -17
  57. package/dist/index18.cjs +1 -1
  58. package/dist/index18.js +93 -7
  59. package/dist/index19.cjs +1 -1
  60. package/dist/index19.js +35 -20
  61. package/dist/index20.cjs +1 -1
  62. package/dist/index20.js +32 -22
  63. package/dist/index21.cjs +16 -1
  64. package/dist/index21.js +113 -8
  65. package/dist/index22.cjs +1 -1
  66. package/dist/index22.js +30 -31
  67. package/dist/index23.cjs +1 -1
  68. package/dist/index23.js +17 -26
  69. package/dist/index24.cjs +1 -1
  70. package/dist/index24.js +6 -22
  71. package/dist/index25.cjs +1 -1
  72. package/dist/index25.js +44 -50
  73. package/dist/index26.cjs +1 -1
  74. package/dist/index26.js +21 -12
  75. package/dist/index27.cjs +1 -1
  76. package/dist/index27.js +8 -24
  77. package/dist/index28.cjs +1 -1
  78. package/dist/index28.js +31 -75
  79. package/dist/index29.cjs +1 -1
  80. package/dist/index29.js +25 -22
  81. package/dist/index3.cjs +1 -1
  82. package/dist/index3.js +30 -7
  83. package/dist/index30.cjs +2 -1
  84. package/dist/index30.js +65 -7
  85. package/dist/index31.cjs +1 -1
  86. package/dist/index31.js +51 -32
  87. package/dist/index32.cjs +1 -1
  88. package/dist/index32.js +12 -15
  89. package/dist/index33.cjs +1 -1
  90. package/dist/index33.js +25 -2
  91. package/dist/index34.cjs +1 -1
  92. package/dist/index34.js +75 -8
  93. package/dist/index35.cjs +1 -1
  94. package/dist/index35.js +23 -20
  95. package/dist/index36.cjs +1 -1
  96. package/dist/index36.js +7 -12
  97. package/dist/index37.cjs +1 -1
  98. package/dist/index37.js +32 -34
  99. package/dist/index38.cjs +1 -2
  100. package/dist/index38.js +15 -59
  101. package/dist/index39.cjs +1 -1
  102. package/dist/index39.js +2 -2
  103. package/dist/index4.cjs +1 -1
  104. package/dist/index4.js +2 -2
  105. package/dist/index40.cjs +1 -1
  106. package/dist/index40.js +8 -4
  107. package/dist/index41.cjs +1 -1
  108. package/dist/index41.js +20 -11
  109. package/dist/index42.cjs +1 -1
  110. package/dist/index42.js +41 -3
  111. package/dist/index43.cjs +1 -1
  112. package/dist/index43.js +25 -21
  113. package/dist/index44.cjs +1 -1
  114. package/dist/index44.js +6 -12
  115. package/dist/index45.cjs +1 -1
  116. package/dist/index45.js +13 -55
  117. package/dist/index46.cjs +1 -1
  118. package/dist/index46.js +15 -6
  119. package/dist/index47.cjs +1 -1
  120. package/dist/index47.js +5 -21
  121. package/dist/index48.cjs +1 -1
  122. package/dist/index48.js +13 -11
  123. package/dist/index49.cjs +1 -1
  124. package/dist/index49.js +10 -3
  125. package/dist/index50.cjs +1 -1
  126. package/dist/index50.js +10 -9
  127. package/dist/index51.cjs +1 -1
  128. package/dist/index51.js +11 -19
  129. package/dist/index52.cjs +1 -1
  130. package/dist/index52.js +13 -6
  131. package/dist/index53.cjs +1 -1
  132. package/dist/index53.js +55 -8
  133. package/dist/index54.cjs +1 -1
  134. package/dist/index54.js +16 -104
  135. package/dist/index55.cjs +1 -1
  136. package/dist/index55.js +5 -28
  137. package/dist/index56.cjs +1 -1
  138. package/dist/index56.js +8 -18
  139. package/dist/index57.cjs +1 -1
  140. package/dist/index57.js +165 -32
  141. package/dist/index58.cjs +1 -0
  142. package/dist/index58.js +106 -0
  143. package/dist/index59.cjs +1 -0
  144. package/dist/index59.js +24 -0
  145. package/dist/index6.cjs +1 -1
  146. package/dist/index6.js +45 -22
  147. package/dist/index60.cjs +1 -0
  148. package/dist/index60.js +15 -0
  149. package/dist/index61.cjs +1 -0
  150. package/dist/index61.js +37 -0
  151. package/dist/index62.cjs +1 -0
  152. package/dist/index62.js +9 -0
  153. package/dist/index63.cjs +2 -0
  154. package/dist/index63.js +62 -0
  155. package/dist/index64.cjs +1 -0
  156. package/dist/index64.js +4 -0
  157. package/dist/index65.cjs +1 -0
  158. package/dist/index65.js +9 -0
  159. package/dist/index66.cjs +1 -0
  160. package/dist/index66.js +7 -0
  161. package/dist/index67.cjs +1 -0
  162. package/dist/index67.js +14 -0
  163. package/dist/index68.cjs +5 -0
  164. package/dist/index68.js +59 -0
  165. package/dist/index69.cjs +2 -0
  166. package/dist/index69.js +37 -0
  167. package/dist/index7.cjs +1 -1
  168. package/dist/index7.js +19 -104
  169. package/dist/index70.cjs +1 -0
  170. package/dist/index70.js +8 -0
  171. package/dist/index71.cjs +1 -0
  172. package/dist/index71.js +6 -0
  173. package/dist/index72.cjs +1 -0
  174. package/dist/index72.js +24 -0
  175. package/dist/index73.cjs +1 -0
  176. package/dist/index73.js +11 -0
  177. package/dist/index74.cjs +1 -0
  178. package/dist/index74.js +13 -0
  179. package/dist/index75.cjs +1 -0
  180. package/dist/index75.js +14 -0
  181. package/dist/index76.cjs +1 -0
  182. package/dist/index76.js +9 -0
  183. package/dist/index77.cjs +1 -0
  184. package/dist/index77.js +16 -0
  185. package/dist/index78.cjs +1 -0
  186. package/dist/index78.js +35 -0
  187. package/dist/index79.cjs +1 -0
  188. package/dist/index79.js +17 -0
  189. package/dist/index8.cjs +1 -1
  190. package/dist/index8.js +2 -15
  191. package/dist/index80.cjs +1 -0
  192. package/dist/index80.js +67 -0
  193. package/dist/index81.cjs +1 -0
  194. package/dist/index81.js +30 -0
  195. package/dist/index82.cjs +1 -0
  196. package/dist/index82.js +5 -0
  197. package/dist/index83.cjs +1 -0
  198. package/dist/index83.js +194 -0
  199. package/dist/index84.cjs +1 -0
  200. package/dist/index84.js +21 -0
  201. package/dist/index85.cjs +1 -0
  202. package/dist/index85.js +6 -0
  203. package/dist/index86.cjs +1 -0
  204. package/dist/index86.js +11 -0
  205. package/dist/index87.cjs +1 -0
  206. package/dist/index87.js +115 -0
  207. package/dist/index88.cjs +1 -0
  208. package/dist/index88.js +19 -0
  209. package/dist/index89.cjs +1 -0
  210. package/dist/index89.js +226 -0
  211. package/dist/index9.cjs +1 -1
  212. package/dist/index9.js +100 -2
  213. package/dist/index90.cjs +1 -0
  214. package/dist/index90.js +45 -0
  215. package/dist/index91.cjs +1 -0
  216. package/dist/index91.js +107 -0
  217. package/dist/index92.cjs +1 -0
  218. package/dist/index92.js +20 -0
  219. package/dist/index93.cjs +1 -0
  220. package/dist/index93.js +81 -0
  221. package/dist/index94.cjs +1 -0
  222. package/dist/index94.js +21 -0
  223. package/dist/index95.cjs +1 -0
  224. package/dist/index95.js +11 -0
  225. package/dist/index96.cjs +1 -0
  226. package/dist/index96.js +15 -0
  227. package/dist/index97.cjs +1 -0
  228. package/dist/index97.js +14 -0
  229. package/dist/index98.cjs +1 -0
  230. package/dist/index98.js +22 -0
  231. package/dist/index99.cjs +1 -0
  232. package/dist/index99.js +41 -0
  233. package/dist/src/index.d.ts +1 -0
  234. package/dist/src/lib/auth/apikey/apikey.d.ts +12 -0
  235. package/dist/src/lib/auth/auth.types.d.ts +21 -8
  236. package/dist/src/lib/auth/index.d.ts +5 -1
  237. package/dist/src/lib/auth/passkey/passkey.d.ts +10 -0
  238. package/dist/src/lib/auth/passkey/passkey.types.d.ts +17 -0
  239. package/dist/src/lib/auth/passkey/passkeyLogin.d.ts +37 -0
  240. package/dist/src/lib/auth/{siwe.d.ts → siwe/siwe.d.ts} +5 -15
  241. package/dist/src/lib/digestKey/digestKey.d.ts +12 -0
  242. package/dist/src/lib/utils.d.ts +3 -0
  243. package/package.json +3 -1
package/dist/index12.js CHANGED
@@ -1,190 +1,143 @@
1
- var p = (e, t, a) => {
2
- if (!t.has(e))
3
- throw TypeError("Cannot " + a);
4
- }, h = (e, t, a) => (p(e, t, "read from private field"), a ? a.call(e) : t.get(e)), u = (e, t, a) => {
5
- if (t.has(e))
6
- throw TypeError("Cannot add the same private member more than once");
7
- t instanceof WeakSet ? t.add(e) : t.set(e, a);
8
- }, w = (e, t, a, r) => (p(e, t, "write to private field"), t.set(e, a), a);
9
- const d = (e) => new Promise((t) => setTimeout(t, e));
10
- class g {
11
- /**
12
- * @param {number} maxRetries - The maximum number of retries, if less < 0 then it is set to 0
13
- * @param {number} delay - The delay between retries
14
- */
15
- constructor(t, a) {
16
- this.maxRetries = Math.max(t, 0), this.delay = a;
1
+ import { Ok as l, Err as e, Fetcher as c } from "./index17.js";
2
+ import { startRegistration as d, startAuthentication as u } from "@simplewebauthn/browser";
3
+ class f {
4
+ constructor(t, r) {
5
+ if (this.storeKey = "passkey-auth", !window)
6
+ throw new Error("Passkey is only supported in the browser environment.");
7
+ this.url = t.endpoint("passkey"), this.store = r;
8
+ }
9
+ getPasskeyFromStore() {
10
+ if (this.token && this.userName)
11
+ return l({ token: this.token, gardenUserName: this.userName });
12
+ const t = this.store.getItem(this.storeKey);
13
+ if (t) {
14
+ const r = JSON.parse(t);
15
+ return l(r);
16
+ }
17
+ return e("Passkey is not set");
17
18
  }
18
19
  /**
19
- * Retries a function until it succeeds or the max number of retries is reached
20
- *
21
- * @param {() => Promise<T>} fn - The function to retry
22
- * @return {Promise<T>} a Promise that resolves to the result of the function
20
+ * Validates the username according to predefined rules.
21
+ * @param username The username to validate.
22
+ * @returns Result<string, string> - Ok if valid, Err with message if invalid.
23
23
  */
24
- async retry(t) {
25
- let a = 0, r;
26
- for (; a < this.maxRetries + 1; )
27
- try {
28
- return await t();
29
- } catch (i) {
30
- a++, r = i, await d(this.delay * a);
31
- }
32
- throw r;
33
- }
34
- }
35
- const v = (e) => {
36
- try {
37
- return JSON.parse(e);
38
- } catch {
39
- return e;
40
- }
41
- };
42
- class m {
43
- static async _postWithFallback(t, a) {
44
- let r = "";
45
- for (const i of t)
46
- try {
47
- const s = await fetch(i, {
48
- method: "POST",
49
- ...a
50
- });
51
- if (s.status >= 500) {
52
- r = await (s.text() || s.json());
53
- continue;
54
- }
55
- return await this.parse(s);
56
- } catch (s) {
57
- r = y(s);
58
- continue;
59
- }
60
- throw new Error(r || "All APIs failed");
24
+ validateUsername(t) {
25
+ return !t || t.length === 0 ? e("Username cannot be empty") : t.length < 3 || t.length > 32 ? e("Username must be between 3 and 32 characters") : t.includes(" ") ? e("Username cannot contain spaces") : l(t.trim().toLowerCase());
61
26
  }
62
27
  /**
63
- * Asynchronously sends a POST request to multiple URLs with fallback logic.
64
- *
65
- * @param {string[]} input - array of URLs to send the POST request to
66
- * @param {RequestInit} [init] - optional request initialization options
67
- * @return {Promise<T>} a Promise that resolves to the parsed response data
28
+ * Handles user registration using WebAuthn passkeys.
29
+ * @param username The username to register.
30
+ * @returns AsyncResult<PasskeyToken, string> - Ok if successful, Err if failed.
68
31
  */
69
- static async postWithFallback(t, a) {
70
- return await n(a).retry(
71
- () => this._postWithFallback(t, a)
72
- );
73
- }
74
- static async _getWithFallback(t, a) {
75
- let r = "";
76
- for (const i of t)
77
- try {
78
- const s = await fetch(i, a);
79
- if (s.status >= 500) {
80
- r = await (s.text() || s.json());
81
- continue;
32
+ async register(t) {
33
+ var i;
34
+ const r = this.validateUsername(t);
35
+ if (r.error) return e(r.error);
36
+ try {
37
+ const n = await c.post(this.url.endpoint("register/begin"), {
38
+ body: JSON.stringify({ username: r }),
39
+ headers: { credentials: "include", "Content-Type": "application/json" }
40
+ });
41
+ if (n.error)
42
+ return e("Failed to start registration", n.error);
43
+ const s = (i = n.result) == null ? void 0 : i.challenge;
44
+ if (!s) return e("No challenge found");
45
+ if (!s.publicKey.authenticatorSelection)
46
+ return e("No authenticator selection found");
47
+ s.publicKey.authenticatorSelection.requireResidentKey = !0, s.publicKey.authenticatorSelection.residentKey = "required";
48
+ const a = await d({
49
+ optionsJSON: s.publicKey
50
+ }), o = await c.post(
51
+ this.url.endpoint("register/finish"),
52
+ {
53
+ body: JSON.stringify({
54
+ username: r,
55
+ credential: a
56
+ }),
57
+ headers: {
58
+ credentials: "include",
59
+ "Content-Type": "application/json"
60
+ }
82
61
  }
83
- return await this.parse(s);
84
- } catch (s) {
85
- r = y(s);
86
- continue;
87
- }
88
- throw new Error(r || "All APIs failed");
89
- }
90
- /**
91
- * Asynchronously sends a POST request to multiple URLs with fallback mechanism.
92
- *
93
- * @param {string[]} input - An array of URLs to retrieve data from.
94
- * @param {RequestInit} [init] - Optional request options.
95
- * @return {Promise<T>} A promise that resolves to the retrieved data.
96
- */
97
- static async getWithFallback(t, a) {
98
- return await n(a).retry(
99
- () => this._getWithFallback(t, a)
100
- );
101
- }
102
- static async _get(t, a) {
103
- return await this.parse(await fetch(t, a));
104
- }
105
- /**
106
- * Asynchronously retrieves data of type T from the specified URL or RequestInfo, with optional initialization options.
107
- *
108
- * @param {RequestInfo | URL} input - The URL or RequestInfo to fetch data from
109
- * @param {RequestInit} init - Optional initialization options for the fetch request
110
- * @return {Promise<T>} The retrieved data of type T
111
- */
112
- static async get(t, a) {
113
- return await n(a).retry(() => this._get(t, a));
114
- }
115
- static async _post(t, a) {
116
- return await this.parse(
117
- await fetch(t, {
118
- method: "POST",
119
- ...a
120
- })
121
- );
62
+ );
63
+ return o.error ? e("Failed to verify registration: " + o.error) : o.result ? (this.token = o.result.token, this.userName = r.val, l(o.result)) : e("No verification result found");
64
+ } catch (n) {
65
+ return e("Registration error", n);
66
+ }
122
67
  }
123
68
  /**
124
- * Asynchronously sends a POST request to the specified URL or RequestInfo, with optional initialization options.
69
+ * Handles user login using WebAuthn passkeys.
70
+ * @param username The username to log in.
71
+ * @returns AsyncResult<PasskeyToken, string> - Ok if successful, Err if failed.
125
72
  */
126
- static async post(t, a) {
127
- return await n(a).retry(() => this._post(t, a));
73
+ async login(t) {
74
+ var i;
75
+ const r = this.validateUsername(t);
76
+ if (r.error) return e(r.error);
77
+ try {
78
+ const n = await c.post(this.url.endpoint("login/begin"), {
79
+ body: JSON.stringify({ username: r }),
80
+ headers: { credentials: "include", "Content-Type": "application/json" }
81
+ });
82
+ if (n.error)
83
+ return e("Failed to start login", n.error);
84
+ const s = (i = n.result) == null ? void 0 : i.challenge;
85
+ if (!s) return e("No challenge found");
86
+ const a = await u({
87
+ optionsJSON: s.publicKey
88
+ }), o = await c.post(
89
+ this.url.endpoint("login/finish"),
90
+ {
91
+ body: JSON.stringify({
92
+ username: r,
93
+ credential: a
94
+ }),
95
+ headers: {
96
+ credentials: "include",
97
+ "Content-Type": "application/json"
98
+ }
99
+ }
100
+ );
101
+ return o.error ? e("Failed to verify login", o.error) : o.result ? (this.token = o.result.token, this.userName = r.val, l(o.result)) : e("No verification result found");
102
+ } catch (n) {
103
+ return e("Login error", n);
104
+ }
128
105
  }
129
106
  /**
130
- * Parses the response and returns the result as the specified type.
131
- *
132
- * @param {Response} res - the response object to be parsed
133
- * @return {Promise<T>} the parsed result of type T
107
+ * Handles conditional login (without username) for existing users.
108
+ * @returns AsyncResult<PasskeyToken, string> - Ok if successful, Err if failed.
134
109
  */
135
- static async parse(t) {
136
- const a = await t.text();
137
- if (t.status >= 200 && t.status < 300)
138
- return v(a);
139
- throw new Error(a);
140
- }
141
- }
142
- function y(e) {
143
- return (e == null ? void 0 : e.message) || (e == null ? void 0 : e.toString()) || "unknown error";
144
- }
145
- const n = (e) => new g((e == null ? void 0 : e.retryCount) ?? 2, (e == null ? void 0 : e.retryDelay) ?? 1e3);
146
- var o, c, l;
147
- class f {
148
- constructor(t, a, r = void 0) {
149
- u(this, o, void 0), u(this, c, void 0), u(this, l, void 0), w(this, o, t), w(this, l, r), w(this, c, a);
150
- }
151
- get ok() {
152
- return h(this, o);
153
- }
154
- get error() {
155
- return h(this, l);
156
- }
157
- get val() {
158
- return h(this, c);
110
+ async conditionalLogin() {
111
+ var t, r;
112
+ try {
113
+ const i = await c.get(this.url.endpoint("conditional-login/begin"), {
114
+ headers: { credentials: "include" }
115
+ });
116
+ if (i.error)
117
+ return e("Failed to start login", i.error);
118
+ const n = (t = i.result) == null ? void 0 : t.challenge;
119
+ if (!n) return e("No challenge found");
120
+ const s = await u({
121
+ optionsJSON: n.publicKey
122
+ }), a = await c.post(
123
+ this.url.endpoint("conditional-login/finish"),
124
+ {
125
+ body: JSON.stringify({
126
+ login_id: (r = i.result) == null ? void 0 : r.login_id,
127
+ credential: s
128
+ }),
129
+ headers: {
130
+ credentials: "include",
131
+ "Content-Type": "application/json"
132
+ }
133
+ }
134
+ );
135
+ return a.error ? e("Failed to verify login", a.error) : a.result ? (this.token = a.result.token, this.userName = a.result.gardenUserName, l(a.result)) : e("No verification result found");
136
+ } catch (i) {
137
+ return e("Conditional login error", i);
138
+ }
159
139
  }
160
140
  }
161
- o = /* @__PURE__ */ new WeakMap(), c = /* @__PURE__ */ new WeakMap(), l = /* @__PURE__ */ new WeakMap();
162
- const k = (e) => new f(!0, e), W = (e, ...t) => {
163
- if (typeof e == "string" && t && t.length > 0) {
164
- let a = [e, ...t].map((r) => {
165
- if (r) {
166
- if (r instanceof Error)
167
- return r.message;
168
- if (typeof r == "string")
169
- return r;
170
- if (r != null && r.toString)
171
- return r.toString();
172
- }
173
- });
174
- return new f(
175
- !1,
176
- null,
177
- a.filter((r) => r !== void 0).join(" ")
178
- );
179
- }
180
- return new f(!1, null, e);
181
- };
182
141
  export {
183
- W as Err,
184
- m as Fetcher,
185
- k as Ok,
186
- f as Result,
187
- g as Retry,
188
- v as safeParseJson,
189
- d as sleep
142
+ f as PasskeyLogin
190
143
  };
package/dist/index13.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const M=require("./index18.cjs"),k=require("./index24.cjs"),g=require("./index30.cjs"),O=require("./index31.cjs"),S=require("./index34.cjs"),y=require("./index35.cjs"),$=require("./index33.cjs"),P=require("./index17.cjs"),W=require("./index23.cjs"),q=require("./index26.cjs"),x=require("./index28.cjs");async function z(n,{confirmations:c=1,hash:p,onReplaced:h,pollingInterval:m=n.pollingInterval,retryCount:w=6,retryDelay:R=({count:d})=>~~(1<<d)*200,timeout:N=18e4}){const d=$.stringify(["waitForTransactionReceipt",n.uid,p]);let a,o,e,i=!1;const{promise:B,resolve:F,reject:v}=S.withResolvers(),A=N?setTimeout(()=>v(new k.WaitForTransactionReceiptTimeoutError({hash:p})),N):void 0,j=O.observe(d,{onReplaced:h,resolve:F,reject:v},r=>{const E=g.getAction(n,x.watchBlockNumber,"watchBlockNumber")({emitMissed:!0,emitOnBegin:!0,poll:!0,pollingInterval:m,async onBlockNumber(_){const u=b=>{clearTimeout(A),E(),b(),j()};let l=_;if(!i)try{if(e){if(c>1&&(!e.blockNumber||l-e.blockNumber+1n<c))return;u(()=>r.resolve(e));return}if(a||(i=!0,await y.withRetry(async()=>{a=await g.getAction(n,W.getTransaction,"getTransaction")({hash:p}),a.blockNumber&&(l=a.blockNumber)},{delay:R,retryCount:w}),i=!1),e=await g.getAction(n,q.getTransactionReceipt,"getTransactionReceipt")({hash:p}),c>1&&(!e.blockNumber||l-e.blockNumber+1n<c))return;u(()=>r.resolve(e))}catch(b){if(b instanceof k.TransactionNotFoundError||b instanceof k.TransactionReceiptNotFoundError){if(!a){i=!1;return}try{o=a,i=!0;const f=await y.withRetry(()=>g.getAction(n,P.getBlock,"getBlock")({blockNumber:l,includeTransactions:!0}),{delay:R,retryCount:w,shouldRetry:({error:s})=>s instanceof M.BlockNotFoundError});i=!1;const t=f.transactions.find(({from:s,nonce:I})=>s===o.from&&I===o.nonce);if(!t||(e=await g.getAction(n,q.getTransactionReceipt,"getTransactionReceipt")({hash:t.hash}),c>1&&(!e.blockNumber||l-e.blockNumber+1n<c)))return;let T="replaced";t.to===o.to&&t.value===o.value&&t.input===o.input?T="repriced":t.from===t.to&&t.value===0n&&(T="cancelled"),u(()=>{var s;(s=r.onReplaced)==null||s.call(r,{reason:T,replacedTransaction:o,transaction:t,transactionReceipt:e}),r.resolve(e)})}catch(f){u(()=>r.reject(f))}}else u(()=>r.reject(b))}}})});return B}exports.waitForTransactionReceipt=z;
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/index13.js CHANGED
@@ -1,96 +1,17 @@
1
- import { BlockNotFoundError as A } from "./index18.js";
2
- import { WaitForTransactionReceiptTimeoutError as M, TransactionNotFoundError as O, TransactionReceiptNotFoundError as W } from "./index24.js";
3
- import { getAction as f } from "./index30.js";
4
- import { observe as q } from "./index31.js";
5
- import { withResolvers as z } from "./index34.js";
6
- import { withRetry as v } from "./index35.js";
7
- import { stringify as G } from "./index33.js";
8
- import { getBlock as H } from "./index17.js";
9
- import { getTransaction as J } from "./index23.js";
10
- import { getTransactionReceipt as R } from "./index26.js";
11
- import { watchBlockNumber as K } from "./index28.js";
12
- async function D(o, {
13
- confirmations: a = 1,
14
- hash: m,
15
- onReplaced: y,
16
- pollingInterval: B = o.pollingInterval,
17
- retryCount: k = 6,
18
- retryDelay: N = ({ count: b }) => ~~(1 << b) * 200,
19
- // exponential backoff
20
- timeout: w = 18e4
21
- }) {
22
- const b = G(["waitForTransactionReceipt", o.uid, m]);
23
- let c, n, r, i = !1;
24
- const { promise: h, resolve: F, reject: g } = z(), E = w ? setTimeout(() => g(new M({ hash: m })), w) : void 0, _ = q(b, { onReplaced: y, resolve: F, reject: g }, (t) => {
25
- const j = f(o, K, "watchBlockNumber")({
26
- emitMissed: !0,
27
- emitOnBegin: !0,
28
- poll: !0,
29
- pollingInterval: B,
30
- async onBlockNumber(I) {
31
- const l = (p) => {
32
- clearTimeout(E), j(), p(), _();
33
- };
34
- let u = I;
35
- if (!i)
36
- try {
37
- if (r) {
38
- if (a > 1 && (!r.blockNumber || u - r.blockNumber + 1n < a))
39
- return;
40
- l(() => t.resolve(r));
41
- return;
42
- }
43
- if (c || (i = !0, await v(async () => {
44
- c = await f(o, J, "getTransaction")({ hash: m }), c.blockNumber && (u = c.blockNumber);
45
- }, {
46
- delay: N,
47
- retryCount: k
48
- }), i = !1), r = await f(o, R, "getTransactionReceipt")({ hash: m }), a > 1 && (!r.blockNumber || u - r.blockNumber + 1n < a))
49
- return;
50
- l(() => t.resolve(r));
51
- } catch (p) {
52
- if (p instanceof O || p instanceof W) {
53
- if (!c) {
54
- i = !1;
55
- return;
56
- }
57
- try {
58
- n = c, i = !0;
59
- const d = await v(() => f(o, H, "getBlock")({
60
- blockNumber: u,
61
- includeTransactions: !0
62
- }), {
63
- delay: N,
64
- retryCount: k,
65
- shouldRetry: ({ error: s }) => s instanceof A
66
- });
67
- i = !1;
68
- const e = d.transactions.find(({ from: s, nonce: x }) => s === n.from && x === n.nonce);
69
- if (!e || (r = await f(o, R, "getTransactionReceipt")({
70
- hash: e.hash
71
- }), a > 1 && (!r.blockNumber || u - r.blockNumber + 1n < a)))
72
- return;
73
- let T = "replaced";
74
- e.to === n.to && e.value === n.value && e.input === n.input ? T = "repriced" : e.from === e.to && e.value === 0n && (T = "cancelled"), l(() => {
75
- var s;
76
- (s = t.onReplaced) == null || s.call(t, {
77
- reason: T,
78
- replacedTransaction: n,
79
- transaction: e,
80
- transactionReceipt: r
81
- }), t.resolve(r);
82
- });
83
- } catch (d) {
84
- l(() => t.reject(d));
85
- }
86
- } else
87
- l(() => t.reject(p));
88
- }
89
- }
90
- });
91
- });
92
- return h;
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
+ }
93
14
  }
94
15
  export {
95
- D as waitForTransactionReceipt
16
+ m as MemoryStorage
96
17
  };
package/dist/index14.cjs CHANGED
@@ -1,16 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./index50.cjs"),M=require("./index43.cjs"),h=require("./index51.cjs");function R(r){const{chainId:o,domain:s,expirationTime:l,issuedAt:m=new Date,nonce:a,notBefore:f,requestId:w,resources:g,scheme:i,uri:c,version:d}=r;{if(o!==Math.floor(o))throw new e.SiweInvalidMessageFieldError({field:"chainId",metaMessages:["- Chain ID must be a EIP-155 chain ID.","- See https://eips.ethereum.org/EIPS/eip-155","",`Provided value: ${o}`]});if(!(E.test(s)||F.test(s)||b.test(s)))throw new e.SiweInvalidMessageFieldError({field:"domain",metaMessages:["- Domain must be an RFC 3986 authority.","- See https://www.rfc-editor.org/rfc/rfc3986","",`Provided value: ${s}`]});if(!A.test(a))throw new e.SiweInvalidMessageFieldError({field:"nonce",metaMessages:["- Nonce must be at least 8 characters.","- Nonce must be alphanumeric.","",`Provided value: ${a}`]});if(!h.isUri(c))throw new e.SiweInvalidMessageFieldError({field:"uri",metaMessages:["- URI must be a RFC 3986 URI referring to the resource that is the subject of the signing.","- See https://www.rfc-editor.org/rfc/rfc3986","",`Provided value: ${c}`]});if(d!=="1")throw new e.SiweInvalidMessageFieldError({field:"version",metaMessages:["- Version must be '1'.","",`Provided value: ${d}`]});if(i&&!P.test(i))throw new e.SiweInvalidMessageFieldError({field:"scheme",metaMessages:["- Scheme must be an RFC 3986 URI scheme.","- See https://www.rfc-editor.org/rfc/rfc3986#section-3.1","",`Provided value: ${i}`]});const t=r.statement;if(t!=null&&t.includes(`
2
- `))throw new e.SiweInvalidMessageFieldError({field:"statement",metaMessages:["- Statement must not include '\\n'.","",`Provided value: ${t}`]})}const $=M.getAddress(r.address),v=i?`${i}://${s}`:s,I=r.statement?`${r.statement}
3
- `:"",S=`${v} wants you to sign in with your Ethereum account:
4
- ${$}
5
-
6
- ${I}`;let n=`URI: ${c}
7
- Version: ${d}
8
- Chain ID: ${o}
9
- Nonce: ${a}
10
- Issued At: ${m.toISOString()}`;if(l&&(n+=`
11
- Expiration Time: ${l.toISOString()}`),f&&(n+=`
12
- Not Before: ${f.toISOString()}`),w&&(n+=`
13
- Request ID: ${w}`),g){let t=`
14
- Resources:`;for(const u of g){if(!h.isUri(u))throw new e.SiweInvalidMessageFieldError({field:"resources",metaMessages:["- Every resource must be a RFC 3986 URI.","- See https://www.rfc-editor.org/rfc/rfc3986","",`Provided value: ${u}`]});t+=`
15
- - ${u}`}n+=t}return`${S}
16
- ${n}`}const E=/^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}(:[0-9]{1,5})?$/,F=/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(:[0-9]{1,5})?$/,b=/^localhost(:[0-9]{1,5})?$/,A=/^[a-zA-Z0-9]{8,}$/,P=/^([a-zA-Z][a-zA-Z0-9+-.]*)$/;exports.createSiweMessage=R;
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/index14.js CHANGED
@@ -1,116 +1,4 @@
1
- import { SiweInvalidMessageFieldError as e } from "./index50.js";
2
- import { getAddress as S } from "./index43.js";
3
- import { isUri as $ } from "./index51.js";
4
- function C(n) {
5
- const { chainId: i, domain: s, expirationTime: u, issuedAt: g = /* @__PURE__ */ new Date(), nonce: a, notBefore: m, requestId: h, resources: w, scheme: o, uri: c, version: f } = n;
6
- {
7
- if (i !== Math.floor(i))
8
- throw new e({
9
- field: "chainId",
10
- metaMessages: [
11
- "- Chain ID must be a EIP-155 chain ID.",
12
- "- See https://eips.ethereum.org/EIPS/eip-155",
13
- "",
14
- `Provided value: ${i}`
15
- ]
16
- });
17
- if (!(p.test(s) || M.test(s) || x.test(s)))
18
- throw new e({
19
- field: "domain",
20
- metaMessages: [
21
- "- Domain must be an RFC 3986 authority.",
22
- "- See https://www.rfc-editor.org/rfc/rfc3986",
23
- "",
24
- `Provided value: ${s}`
25
- ]
26
- });
27
- if (!A.test(a))
28
- throw new e({
29
- field: "nonce",
30
- metaMessages: [
31
- "- Nonce must be at least 8 characters.",
32
- "- Nonce must be alphanumeric.",
33
- "",
34
- `Provided value: ${a}`
35
- ]
36
- });
37
- if (!$(c))
38
- throw new e({
39
- field: "uri",
40
- metaMessages: [
41
- "- URI must be a RFC 3986 URI referring to the resource that is the subject of the signing.",
42
- "- See https://www.rfc-editor.org/rfc/rfc3986",
43
- "",
44
- `Provided value: ${c}`
45
- ]
46
- });
47
- if (f !== "1")
48
- throw new e({
49
- field: "version",
50
- metaMessages: [
51
- "- Version must be '1'.",
52
- "",
53
- `Provided value: ${f}`
54
- ]
55
- });
56
- if (o && !P.test(o))
57
- throw new e({
58
- field: "scheme",
59
- metaMessages: [
60
- "- Scheme must be an RFC 3986 URI scheme.",
61
- "- See https://www.rfc-editor.org/rfc/rfc3986#section-3.1",
62
- "",
63
- `Provided value: ${o}`
64
- ]
65
- });
66
- const t = n.statement;
67
- if (t != null && t.includes(`
68
- `))
69
- throw new e({
70
- field: "statement",
71
- metaMessages: [
72
- "- Statement must not include '\\n'.",
73
- "",
74
- `Provided value: ${t}`
75
- ]
76
- });
77
- }
78
- const l = S(n.address), v = o ? `${o}://${s}` : s, I = n.statement ? `${n.statement}
79
- ` : "", R = `${v} wants you to sign in with your Ethereum account:
80
- ${l}
81
-
82
- ${I}`;
83
- let r = `URI: ${c}
84
- Version: ${f}
85
- Chain ID: ${i}
86
- Nonce: ${a}
87
- Issued At: ${g.toISOString()}`;
88
- if (u && (r += `
89
- Expiration Time: ${u.toISOString()}`), m && (r += `
90
- Not Before: ${m.toISOString()}`), h && (r += `
91
- Request ID: ${h}`), w) {
92
- let t = `
93
- Resources:`;
94
- for (const d of w) {
95
- if (!$(d))
96
- throw new e({
97
- field: "resources",
98
- metaMessages: [
99
- "- Every resource must be a RFC 3986 URI.",
100
- "- See https://www.rfc-editor.org/rfc/rfc3986",
101
- "",
102
- `Provided value: ${d}`
103
- ]
104
- });
105
- t += `
106
- - ${d}`;
107
- }
108
- r += t;
109
- }
110
- return `${R}
111
- ${r}`;
112
- }
113
- const p = /^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}(:[0-9]{1,5})?$/, M = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(:[0-9]{1,5})?$/, x = /^localhost(:[0-9]{1,5})?$/, A = /^[a-zA-Z0-9]{8,}$/, P = /^([a-zA-Z][a-zA-Z0-9+-.]*)$/;
1
+ var t = /* @__PURE__ */ ((r) => (r.AUTH_TOKEN = "auth_token", r))(t || {});
114
2
  export {
115
- C as createSiweMessage
3
+ t as StoreKeys
116
4
  };
package/dist/index15.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./index53.cjs"),t=e.defineChain({id:1,name:"Ethereum",nativeCurrency:{name:"Ether",symbol:"ETH",decimals:18},rpcUrls:{default:{http:["https://eth.merkle.io"]}},blockExplorers:{default:{name:"Etherscan",url:"https://etherscan.io",apiUrl:"https://api.etherscan.io/api"}},contracts:{ensRegistry:{address:"0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e"},ensUniversalResolver:{address:"0xce01f8eee7E479C928F8919abD53E553a36CeF67",blockCreated:19258213},multicall3:{address:"0xca11bde05977b3631167028862be2a173976ca11",blockCreated:14353601}}});exports.mainnet=t;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=require("./index17.cjs"),n=require("viem"),s=require("./index20.cjs"),l=require("./index22.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/index15.js CHANGED
@@ -1,34 +1,36 @@
1
- import { defineChain as e } from "./index53.js";
2
- const t = /* @__PURE__ */ e({
3
- id: 1,
4
- name: "Ethereum",
5
- nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
6
- rpcUrls: {
7
- default: {
8
- http: ["https://eth.merkle.io"]
9
- }
10
- },
11
- blockExplorers: {
12
- default: {
13
- name: "Etherscan",
14
- url: "https://etherscan.io",
15
- apiUrl: "https://api.etherscan.io/api"
16
- }
17
- },
18
- contracts: {
19
- ensRegistry: {
20
- address: "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e"
21
- },
22
- ensUniversalResolver: {
23
- address: "0xce01f8eee7E479C928F8919abD53E553a36CeF67",
24
- blockCreated: 19258213
25
- },
26
- multicall3: {
27
- address: "0xca11bde05977b3631167028862be2a173976ca11",
28
- blockCreated: 14353601
29
- }
1
+ import { Err as o, Ok as n } from "./index17.js";
2
+ import { createPublicClient as s, http as m } from "viem";
3
+ import { mainnet as u } from "./index20.js";
4
+ import { sepolia as b } from "./index22.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);
30
23
  }
31
- });
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
+ };
32
32
  export {
33
- t as mainnet
33
+ l as _fetchEVMBlockNumber,
34
+ C as fetchBitcoinBlockNumber,
35
+ N as fetchEVMBlockNumber
34
36
  };