@gardenfi/utils 2.0.3 → 2.0.5

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 -21
  3. package/dist/index10.cjs +1 -1
  4. package/dist/index10.js +21 -14
  5. package/dist/index100.cjs +1 -0
  6. package/dist/index100.js +9 -0
  7. package/dist/index101.cjs +2 -0
  8. package/dist/index101.js +5 -0
  9. package/dist/index102.cjs +1 -0
  10. package/dist/index102.js +115 -0
  11. package/dist/index103.cjs +1 -0
  12. package/dist/index103.js +81 -0
  13. package/dist/index104.cjs +1 -0
  14. package/dist/index104.js +4 -0
  15. package/dist/index105.cjs +1 -0
  16. package/dist/index105.js +20 -0
  17. package/dist/index106.cjs +1 -0
  18. package/dist/index106.js +21 -0
  19. package/dist/index107.cjs +1 -0
  20. package/dist/index107.js +19 -0
  21. package/dist/index108.cjs +1 -0
  22. package/dist/index108.js +226 -0
  23. package/dist/index109.cjs +1 -0
  24. package/dist/index109.js +37 -0
  25. package/dist/index11.cjs +1 -1
  26. package/dist/index11.js +126 -173
  27. package/dist/index110.cjs +1 -0
  28. package/dist/index110.js +21 -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 +70 -0
  33. package/dist/index113.cjs +1 -0
  34. package/dist/index113.js +4 -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/index12.cjs +1 -1
  44. package/dist/index12.js +14 -93
  45. package/dist/index13.cjs +1 -16
  46. package/dist/index13.js +2 -114
  47. package/dist/index14.cjs +1 -1
  48. package/dist/index14.js +33 -31
  49. package/dist/index15.cjs +1 -1
  50. package/dist/index15.js +19 -31
  51. package/dist/index16.cjs +1 -1
  52. package/dist/index16.js +187 -17
  53. package/dist/index17.cjs +1 -1
  54. package/dist/index17.js +93 -7
  55. package/dist/index18.cjs +1 -1
  56. package/dist/index18.js +35 -20
  57. package/dist/index19.cjs +1 -1
  58. package/dist/index19.js +32 -22
  59. package/dist/index20.cjs +16 -1
  60. package/dist/index20.js +113 -8
  61. package/dist/index21.cjs +1 -1
  62. package/dist/index21.js +30 -31
  63. package/dist/index22.cjs +1 -1
  64. package/dist/index22.js +17 -26
  65. package/dist/index23.cjs +1 -1
  66. package/dist/index23.js +6 -22
  67. package/dist/index24.cjs +1 -1
  68. package/dist/index24.js +44 -50
  69. package/dist/index25.cjs +1 -1
  70. package/dist/index25.js +21 -12
  71. package/dist/index26.cjs +1 -1
  72. package/dist/index26.js +8 -24
  73. package/dist/index27.cjs +1 -1
  74. package/dist/index27.js +31 -75
  75. package/dist/index28.cjs +1 -1
  76. package/dist/index28.js +25 -22
  77. package/dist/index29.cjs +2 -1
  78. package/dist/index29.js +65 -7
  79. package/dist/index3.cjs +1 -1
  80. package/dist/index3.js +30 -7
  81. package/dist/index30.cjs +1 -1
  82. package/dist/index30.js +51 -32
  83. package/dist/index31.cjs +1 -1
  84. package/dist/index31.js +12 -15
  85. package/dist/index32.cjs +1 -1
  86. package/dist/index32.js +25 -2
  87. package/dist/index33.cjs +1 -1
  88. package/dist/index33.js +75 -8
  89. package/dist/index34.cjs +1 -1
  90. package/dist/index34.js +23 -20
  91. package/dist/index35.cjs +1 -1
  92. package/dist/index35.js +7 -12
  93. package/dist/index36.cjs +1 -1
  94. package/dist/index36.js +32 -34
  95. package/dist/index37.cjs +1 -2
  96. package/dist/index37.js +15 -59
  97. package/dist/index38.cjs +1 -1
  98. package/dist/index38.js +2 -2
  99. package/dist/index39.cjs +1 -1
  100. package/dist/index39.js +8 -4
  101. package/dist/index4.cjs +1 -1
  102. package/dist/index4.js +2 -2
  103. package/dist/index40.cjs +1 -1
  104. package/dist/index40.js +20 -11
  105. package/dist/index41.cjs +1 -1
  106. package/dist/index41.js +12 -3
  107. package/dist/index42.cjs +1 -1
  108. package/dist/index42.js +33 -20
  109. package/dist/index43.cjs +2 -1
  110. package/dist/index43.js +58 -11
  111. package/dist/index44.cjs +1 -1
  112. package/dist/index44.js +2 -56
  113. package/dist/index45.cjs +1 -1
  114. package/dist/index45.js +6 -6
  115. package/dist/index46.cjs +1 -1
  116. package/dist/index46.js +6 -21
  117. package/dist/index47.cjs +1 -1
  118. package/dist/index47.js +4 -11
  119. package/dist/index48.cjs +1 -1
  120. package/dist/index48.js +11 -3
  121. package/dist/index49.cjs +5 -1
  122. package/dist/index49.js +53 -8
  123. package/dist/index5.cjs +1 -1
  124. package/dist/index5.js +43 -19
  125. package/dist/index50.cjs +1 -1
  126. package/dist/index50.js +3 -19
  127. package/dist/index51.cjs +2 -1
  128. package/dist/index51.js +33 -5
  129. package/dist/index52.cjs +1 -1
  130. package/dist/index52.js +5 -8
  131. package/dist/index53.cjs +1 -1
  132. package/dist/index53.js +20 -103
  133. package/dist/index54.cjs +1 -1
  134. package/dist/index54.js +12 -28
  135. package/dist/index55.cjs +1 -1
  136. package/dist/index55.js +53 -16
  137. package/dist/index56.cjs +1 -1
  138. package/dist/index56.js +8 -38
  139. package/dist/index57.cjs +1 -0
  140. package/dist/index57.js +24 -0
  141. package/dist/index58.cjs +1 -0
  142. package/dist/index58.js +14 -0
  143. package/dist/index59.cjs +1 -0
  144. package/dist/index59.js +11 -0
  145. package/dist/index6.cjs +1 -1
  146. package/dist/index6.js +19 -104
  147. package/dist/index60.cjs +1 -0
  148. package/dist/index60.js +13 -0
  149. package/dist/index61.cjs +1 -0
  150. package/dist/index61.js +14 -0
  151. package/dist/index62.cjs +1 -0
  152. package/dist/index62.js +9 -0
  153. package/dist/index63.cjs +1 -0
  154. package/dist/index63.js +16 -0
  155. package/dist/index64.cjs +1 -0
  156. package/dist/index64.js +35 -0
  157. package/dist/index65.cjs +1 -0
  158. package/dist/index65.js +17 -0
  159. package/dist/index66.cjs +1 -0
  160. package/dist/index66.js +67 -0
  161. package/dist/index67.cjs +1 -0
  162. package/dist/index67.js +30 -0
  163. package/dist/index68.cjs +1 -0
  164. package/dist/index68.js +5 -0
  165. package/dist/index69.cjs +1 -0
  166. package/dist/index69.js +8 -0
  167. package/dist/index7.cjs +1 -1
  168. package/dist/index7.js +2 -15
  169. package/dist/index70.cjs +1 -0
  170. package/dist/index70.js +11 -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 +16 -0
  175. package/dist/index73.cjs +1 -0
  176. package/dist/index73.js +44 -0
  177. package/dist/index74.cjs +1 -0
  178. package/dist/index74.js +106 -0
  179. package/dist/index75.cjs +1 -0
  180. package/dist/index75.js +194 -0
  181. package/dist/index76.cjs +1 -0
  182. package/dist/index76.js +21 -0
  183. package/dist/index77.cjs +1 -0
  184. package/dist/index77.js +14 -0
  185. package/dist/index78.cjs +1 -0
  186. package/dist/index78.js +22 -0
  187. package/dist/index79.cjs +1 -0
  188. package/dist/index79.js +28 -0
  189. package/dist/index8.cjs +1 -1
  190. package/dist/index8.js +100 -2
  191. package/dist/index80.cjs +1 -0
  192. package/dist/index80.js +9 -0
  193. package/dist/index81.cjs +1 -0
  194. package/dist/index81.js +16 -0
  195. package/dist/index82.cjs +1 -0
  196. package/dist/index82.js +18 -0
  197. package/dist/index83.cjs +1 -0
  198. package/dist/index83.js +8 -0
  199. package/dist/index84.cjs +1 -0
  200. package/dist/index84.js +16 -0
  201. package/dist/index85.cjs +1 -0
  202. package/dist/index85.js +13 -0
  203. package/dist/index86.cjs +1 -0
  204. package/dist/index86.js +19 -0
  205. package/dist/index87.cjs +1 -0
  206. package/dist/index87.js +174 -0
  207. package/dist/index88.cjs +1 -0
  208. package/dist/index88.js +11 -0
  209. package/dist/index89.cjs +1 -0
  210. package/dist/index89.js +15 -0
  211. package/dist/index9.cjs +1 -1
  212. package/dist/index9.js +42 -32
  213. package/dist/index90.cjs +1 -0
  214. package/dist/index90.js +107 -0
  215. package/dist/index91.cjs +1 -0
  216. package/dist/index91.js +41 -0
  217. package/dist/index92.cjs +1 -0
  218. package/dist/index92.js +4 -0
  219. package/dist/index93.cjs +1 -0
  220. package/dist/index93.js +7 -0
  221. package/dist/index94.cjs +1 -0
  222. package/dist/index94.js +13 -0
  223. package/dist/index95.cjs +1 -0
  224. package/dist/index95.js +45 -0
  225. package/dist/index96.cjs +1 -0
  226. package/dist/index96.js +23 -0
  227. package/dist/index97.cjs +1 -0
  228. package/dist/index97.js +7 -0
  229. package/dist/index98.cjs +1 -0
  230. package/dist/index98.js +40 -0
  231. package/dist/index99.cjs +1 -0
  232. package/dist/index99.js +130 -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} +4 -15
  241. package/dist/src/lib/result.d.ts +16 -0
  242. package/dist/src/lib/utils.d.ts +3 -0
  243. package/package.json +2 -1
@@ -0,0 +1,226 @@
1
+ import { bitMask as _, numberToBytesLE as S, numberToBytesBE as x, bytesToNumberLE as y, bytesToNumberBE as L, validateObject as M } from "./index116.js";
2
+ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
3
+ const d = BigInt(0), c = BigInt(1), w = /* @__PURE__ */ BigInt(2), C = /* @__PURE__ */ BigInt(3), b = /* @__PURE__ */ BigInt(4), N = /* @__PURE__ */ BigInt(5), m = /* @__PURE__ */ BigInt(8);
4
+ function g(t, i) {
5
+ const r = t % i;
6
+ return r >= d ? r : i + r;
7
+ }
8
+ function Z(t, i, r) {
9
+ if (i < d)
10
+ throw new Error("invalid exponent, negatives unsupported");
11
+ if (r <= d)
12
+ throw new Error("invalid modulus");
13
+ if (r === c)
14
+ return d;
15
+ let e = c;
16
+ for (; i > d; )
17
+ i & c && (e = e * t % r), t = t * t % r, i >>= c;
18
+ return e;
19
+ }
20
+ function V(t, i, r) {
21
+ let e = t;
22
+ for (; i-- > d; )
23
+ e *= e, e %= r;
24
+ return e;
25
+ }
26
+ function I(t, i) {
27
+ if (t === d)
28
+ throw new Error("invert: expected non-zero number");
29
+ if (i <= d)
30
+ throw new Error("invert: expected positive modulus, got " + i);
31
+ let r = g(t, i), e = i, s = d, u = c;
32
+ for (; r !== d; ) {
33
+ const o = e / r, n = e % r, l = s - u * o;
34
+ e = r, r = n, s = u, u = l;
35
+ }
36
+ if (e !== c)
37
+ throw new Error("invert: does not exist");
38
+ return g(s, i);
39
+ }
40
+ function j(t) {
41
+ const i = (t - c) / w;
42
+ let r, e, s;
43
+ for (r = t - c, e = 0; r % w === d; r /= w, e++)
44
+ ;
45
+ for (s = w; s < t && Z(s, i, t) !== t - c; s++)
46
+ if (s > 1e3)
47
+ throw new Error("Cannot find square root: likely non-prime P");
48
+ if (e === 1) {
49
+ const f = (t + c) / b;
50
+ return function(n, l) {
51
+ const h = n.pow(l, f);
52
+ if (!n.eql(n.sqr(h), l))
53
+ throw new Error("Cannot find square root");
54
+ return h;
55
+ };
56
+ }
57
+ const u = (r + c) / w;
58
+ return function(o, n) {
59
+ if (o.pow(n, i) === o.neg(o.ONE))
60
+ throw new Error("Cannot find square root");
61
+ let l = e, h = o.pow(o.mul(o.ONE, s), r), v = o.pow(n, u), a = o.pow(n, r);
62
+ for (; !o.eql(a, o.ONE); ) {
63
+ if (o.eql(a, o.ZERO))
64
+ return o.ZERO;
65
+ let q = 1;
66
+ for (let B = o.sqr(a); q < l && !o.eql(B, o.ONE); q++)
67
+ B = o.sqr(B);
68
+ const E = o.pow(h, c << BigInt(l - q - 1));
69
+ h = o.sqr(E), v = o.mul(v, E), a = o.mul(a, h), l = q;
70
+ }
71
+ return v;
72
+ };
73
+ }
74
+ function z(t) {
75
+ if (t % b === C) {
76
+ const i = (t + c) / b;
77
+ return function(e, s) {
78
+ const u = e.pow(s, i);
79
+ if (!e.eql(e.sqr(u), s))
80
+ throw new Error("Cannot find square root");
81
+ return u;
82
+ };
83
+ }
84
+ if (t % m === N) {
85
+ const i = (t - N) / m;
86
+ return function(e, s) {
87
+ const u = e.mul(s, w), f = e.pow(u, i), o = e.mul(s, f), n = e.mul(e.mul(o, w), f), l = e.mul(o, e.sub(n, e.ONE));
88
+ if (!e.eql(e.sqr(l), s))
89
+ throw new Error("Cannot find square root");
90
+ return l;
91
+ };
92
+ }
93
+ return j(t);
94
+ }
95
+ const A = [
96
+ "create",
97
+ "isValid",
98
+ "is0",
99
+ "neg",
100
+ "inv",
101
+ "sqrt",
102
+ "sqr",
103
+ "eql",
104
+ "add",
105
+ "sub",
106
+ "mul",
107
+ "pow",
108
+ "div",
109
+ "addN",
110
+ "subN",
111
+ "mulN",
112
+ "sqrN"
113
+ ];
114
+ function Y(t) {
115
+ const i = {
116
+ ORDER: "bigint",
117
+ MASK: "bigint",
118
+ BYTES: "isSafeInteger",
119
+ BITS: "isSafeInteger"
120
+ }, r = A.reduce((e, s) => (e[s] = "function", e), i);
121
+ return M(t, r);
122
+ }
123
+ function k(t, i, r) {
124
+ if (r < d)
125
+ throw new Error("invalid exponent, negatives unsupported");
126
+ if (r === d)
127
+ return t.ONE;
128
+ if (r === c)
129
+ return i;
130
+ let e = t.ONE, s = i;
131
+ for (; r > d; )
132
+ r & c && (e = t.mul(e, s)), s = t.sqr(s), r >>= c;
133
+ return e;
134
+ }
135
+ function H(t, i) {
136
+ const r = new Array(i.length), e = i.reduce((u, f, o) => t.is0(f) ? u : (r[o] = u, t.mul(u, f)), t.ONE), s = t.inv(e);
137
+ return i.reduceRight((u, f, o) => t.is0(f) ? u : (r[o] = t.mul(u, r[o]), t.mul(u, f)), s), r;
138
+ }
139
+ function K(t, i) {
140
+ const r = i !== void 0 ? i : t.toString(2).length, e = Math.ceil(r / 8);
141
+ return { nBitLength: r, nByteLength: e };
142
+ }
143
+ function p(t, i, r = !1, e = {}) {
144
+ if (t <= d)
145
+ throw new Error("invalid field: expected ORDER > 0, got " + t);
146
+ const { nBitLength: s, nByteLength: u } = K(t, i);
147
+ if (u > 2048)
148
+ throw new Error("invalid field: expected ORDER of <= 2048 bytes");
149
+ let f;
150
+ const o = Object.freeze({
151
+ ORDER: t,
152
+ isLE: r,
153
+ BITS: s,
154
+ BYTES: u,
155
+ MASK: _(s),
156
+ ZERO: d,
157
+ ONE: c,
158
+ create: (n) => g(n, t),
159
+ isValid: (n) => {
160
+ if (typeof n != "bigint")
161
+ throw new Error("invalid field element: expected bigint, got " + typeof n);
162
+ return d <= n && n < t;
163
+ },
164
+ is0: (n) => n === d,
165
+ isOdd: (n) => (n & c) === c,
166
+ neg: (n) => g(-n, t),
167
+ eql: (n, l) => n === l,
168
+ sqr: (n) => g(n * n, t),
169
+ add: (n, l) => g(n + l, t),
170
+ sub: (n, l) => g(n - l, t),
171
+ mul: (n, l) => g(n * l, t),
172
+ pow: (n, l) => k(o, n, l),
173
+ div: (n, l) => g(n * I(l, t), t),
174
+ // Same as above, but doesn't normalize
175
+ sqrN: (n) => n * n,
176
+ addN: (n, l) => n + l,
177
+ subN: (n, l) => n - l,
178
+ mulN: (n, l) => n * l,
179
+ inv: (n) => I(n, t),
180
+ sqrt: e.sqrt || ((n) => (f || (f = z(t)), f(o, n))),
181
+ invertBatch: (n) => H(o, n),
182
+ // TODO: do we really need constant cmov?
183
+ // We don't have const-time bigints anyway, so probably will be not very useful
184
+ cmov: (n, l, h) => h ? l : n,
185
+ toBytes: (n) => r ? S(n, u) : x(n, u),
186
+ fromBytes: (n) => {
187
+ if (n.length !== u)
188
+ throw new Error("Field.fromBytes: expected " + u + " bytes, got " + n.length);
189
+ return r ? y(n) : L(n);
190
+ }
191
+ });
192
+ return Object.freeze(o);
193
+ }
194
+ function T(t) {
195
+ if (typeof t != "bigint")
196
+ throw new Error("field order must be bigint");
197
+ const i = t.toString(2).length;
198
+ return Math.ceil(i / 8);
199
+ }
200
+ function O(t) {
201
+ const i = T(t);
202
+ return i + Math.ceil(i / 2);
203
+ }
204
+ function G(t, i, r = !1) {
205
+ const e = t.length, s = T(i), u = O(i);
206
+ if (e < 16 || e < u || e > 1024)
207
+ throw new Error("expected " + u + "-1024 bytes of input, got " + e);
208
+ const f = r ? y(t) : L(t), o = g(f, i - c) + c;
209
+ return r ? S(o, s) : x(o, s);
210
+ }
211
+ export {
212
+ p as Field,
213
+ H as FpInvertBatch,
214
+ k as FpPow,
215
+ z as FpSqrt,
216
+ T as getFieldBytesLength,
217
+ O as getMinHashLength,
218
+ I as invert,
219
+ G as mapHashToField,
220
+ g as mod,
221
+ K as nLength,
222
+ Z as pow,
223
+ V as pow2,
224
+ j as tonelliShanks,
225
+ Y as validateField
226
+ };
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function r(e){if(!Number.isSafeInteger(e)||e<0)throw new Error("positive integer expected, got "+e)}function i(e){return e instanceof Uint8Array||ArrayBuffer.isView(e)&&e.constructor.name==="Uint8Array"}function o(e,...t){if(!i(e))throw new Error("Uint8Array expected");if(t.length>0&&!t.includes(e.length))throw new Error("Uint8Array expected of length "+t+", got length="+e.length)}function a(e){if(typeof e!="function"||typeof e.create!="function")throw new Error("Hash should be wrapped by utils.wrapConstructor");r(e.outputLen),r(e.blockLen)}function s(e,t=!0){if(e.destroyed)throw new Error("Hash instance has been destroyed");if(t&&e.finished)throw new Error("Hash#digest() has already been called")}function u(e,t){o(e);const n=t.outputLen;if(e.length<n)throw new Error("digestInto() expects output buffer of length at least "+n)}exports.abytes=o;exports.aexists=s;exports.ahash=a;exports.anumber=r;exports.aoutput=u;
@@ -0,0 +1,37 @@
1
+ function n(e) {
2
+ if (!Number.isSafeInteger(e) || e < 0)
3
+ throw new Error("positive integer expected, got " + e);
4
+ }
5
+ function o(e) {
6
+ return e instanceof Uint8Array || ArrayBuffer.isView(e) && e.constructor.name === "Uint8Array";
7
+ }
8
+ function i(e, ...t) {
9
+ if (!o(e))
10
+ throw new Error("Uint8Array expected");
11
+ if (t.length > 0 && !t.includes(e.length))
12
+ throw new Error("Uint8Array expected of length " + t + ", got length=" + e.length);
13
+ }
14
+ function f(e) {
15
+ if (typeof e != "function" || typeof e.create != "function")
16
+ throw new Error("Hash should be wrapped by utils.wrapConstructor");
17
+ n(e.outputLen), n(e.blockLen);
18
+ }
19
+ function s(e, t = !0) {
20
+ if (e.destroyed)
21
+ throw new Error("Hash instance has been destroyed");
22
+ if (t && e.finished)
23
+ throw new Error("Hash#digest() has already been called");
24
+ }
25
+ function u(e, t) {
26
+ i(e);
27
+ const r = t.outputLen;
28
+ if (e.length < r)
29
+ throw new Error("digestInto() expects output buffer of length at least " + r);
30
+ }
31
+ export {
32
+ i as abytes,
33
+ s as aexists,
34
+ f as ahash,
35
+ n as anumber,
36
+ u as aoutput
37
+ };
package/dist/index11.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var p=(a,t,e)=>{if(!t.has(a))throw TypeError("Cannot "+e)},u=(a,t,e)=>(p(a,t,"read from private field"),e?e.call(a):t.get(a)),w=(a,t,e)=>{if(t.has(a))throw TypeError("Cannot add the same private member more than once");t instanceof WeakSet?t.add(a):t.set(a,e)},f=(a,t,e,r)=>(p(a,t,"write to private field"),t.set(a,e),e);const d=a=>new Promise(t=>setTimeout(t,a));class g{constructor(t,e){this.maxRetries=Math.max(t,0),this.delay=e}async retry(t){let e=0,r;for(;e<this.maxRetries+1;)try{return await t()}catch(s){e++,r=s,await d(this.delay*e)}throw r}}const v=a=>{try{return JSON.parse(a)}catch{return a}};class m{static async _postWithFallback(t,e){let r="";for(const s of t)try{const i=await fetch(s,{method:"POST",...e});if(i.status>=500){r=await(i.text()||i.json());continue}return await this.parse(i)}catch(i){r=y(i);continue}throw new Error(r||"All APIs failed")}static async postWithFallback(t,e){return await n(e).retry(()=>this._postWithFallback(t,e))}static async _getWithFallback(t,e){let r="";for(const s of t)try{const i=await fetch(s,e);if(i.status>=500){r=await(i.text()||i.json());continue}return await this.parse(i)}catch(i){r=y(i);continue}throw new Error(r||"All APIs failed")}static async getWithFallback(t,e){return await n(e).retry(()=>this._getWithFallback(t,e))}static async _get(t,e){return await this.parse(await fetch(t,e))}static async get(t,e){return await n(e).retry(()=>this._get(t,e))}static async _post(t,e){return await this.parse(await fetch(t,{method:"POST",...e}))}static async post(t,e){return await n(e).retry(()=>this._post(t,e))}static async parse(t){const e=await t.text();if(t.status>=200&&t.status<300)return v(e);throw new Error(e)}}function y(a){return(a==null?void 0:a.message)||(a==null?void 0:a.toString())||"unknown error"}const n=a=>new g((a==null?void 0:a.retryCount)??2,(a==null?void 0:a.retryDelay)??1e3);var o,c,l;class h{constructor(t,e,r=void 0){w(this,o,void 0),w(this,c,void 0),w(this,l,void 0),f(this,o,t),f(this,l,r),f(this,c,e)}get ok(){return u(this,o)}get error(){return u(this,l)}get val(){return u(this,c)}}o=new WeakMap,c=new WeakMap,l=new WeakMap;const k=a=>new h(!0,a),W=(a,...t)=>{if(typeof a=="string"&&t&&t.length>0){let e=[a,...t].map(r=>{if(r){if(r instanceof Error)return r.message;if(typeof r=="string")return r;if(r!=null&&r.toString)return r.toString()}});return new h(!1,null,e.filter(r=>r!==void 0).join(" "))}return new h(!1,null,a)};exports.Err=W;exports.Fetcher=m;exports.Ok=k;exports.Result=h;exports.Retry=g;exports.safeParseJson=v;exports.sleep=d;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./index16.cjs"),l=require("@simplewebauthn/browser");class c{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 e.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 l.startRegistration({optionsJSON:s.publicKey}),o=await e.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 e.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 l.startAuthentication({optionsJSON:s.publicKey}),o=await e.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 e.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 l.startAuthentication({optionsJSON:n.publicKey}),a=await e.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=c;
package/dist/index11.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 "./index16.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
  };
@@ -0,0 +1 @@
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;
@@ -0,0 +1,21 @@
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 };
4
+ }
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];
10
+ }
11
+ return [n, l];
12
+ }
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;
14
+ export {
15
+ f as fromBig,
16
+ A as rotlBH,
17
+ B as rotlBL,
18
+ g as rotlSH,
19
+ m as rotlSL,
20
+ h as split
21
+ };
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("./index113.cjs"),f=require("./index109.cjs");/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */function p(t){return new Uint32Array(t.buffer,t.byteOffset,Math.floor(t.byteLength/4))}function a(t){return new DataView(t.buffer,t.byteOffset,t.byteLength)}function l(t,e){return t<<32-e|t>>>e}const b=new Uint8Array(new Uint32Array([287454020]).buffer)[0]===68;function i(t){return t<<24&4278190080|t<<8&16711680|t>>>8&65280|t>>>24&255}function w(t){for(let e=0;e<t.length;e++)t[e]=i(t[e])}function y(t){if(typeof t!="string")throw new Error("utf8ToBytes expected string, got "+typeof t);return new Uint8Array(new TextEncoder().encode(t))}function s(t){return typeof t=="string"&&(t=y(t)),f.abytes(t),t}function g(...t){let e=0;for(let n=0;n<t.length;n++){const u=t[n];f.abytes(u),e+=u.length}const r=new Uint8Array(e);for(let n=0,u=0;n<t.length;n++){const c=t[n];r.set(c,u),u+=c.length}return r}class m{clone(){return this._cloneInto()}}function B(t){const e=n=>t().update(s(n)).digest(),r=t();return e.outputLen=r.outputLen,e.blockLen=r.blockLen,e.create=()=>t(),e}function d(t=32){if(o.crypto&&typeof o.crypto.getRandomValues=="function")return o.crypto.getRandomValues(new Uint8Array(t));if(o.crypto&&typeof o.crypto.randomBytes=="function")return o.crypto.randomBytes(t);throw new Error("crypto.getRandomValues must be defined")}exports.Hash=m;exports.byteSwap=i;exports.byteSwap32=w;exports.concatBytes=g;exports.createView=a;exports.isLE=b;exports.randomBytes=d;exports.rotr=l;exports.toBytes=s;exports.u32=p;exports.utf8ToBytes=y;exports.wrapConstructor=B;