@mezo-org/passport 0.4.0-dev.1 → 0.4.0-dev.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.
Files changed (233) hide show
  1. package/dist/src/api/auth.d.ts +57 -52
  2. package/dist/src/api/auth.d.ts.map +1 -1
  3. package/dist/src/api/auth.js +22 -53
  4. package/dist/src/api/auth.js.map +1 -1
  5. package/dist/src/api/client.d.ts +24 -0
  6. package/dist/src/api/client.d.ts.map +1 -0
  7. package/dist/src/api/client.js +54 -0
  8. package/dist/src/api/client.js.map +1 -0
  9. package/dist/src/api/fetch-error.d.ts +5 -0
  10. package/dist/src/api/fetch-error.d.ts.map +1 -0
  11. package/dist/src/api/fetch-error.js +8 -0
  12. package/dist/src/api/fetch-error.js.map +1 -0
  13. package/dist/src/api/index.d.ts +3 -0
  14. package/dist/src/api/index.d.ts.map +1 -0
  15. package/dist/src/api/index.js +3 -0
  16. package/dist/src/api/index.js.map +1 -0
  17. package/dist/src/api/portal.d.ts +32 -0
  18. package/dist/src/api/portal.d.ts.map +1 -0
  19. package/dist/src/api/portal.js +23 -0
  20. package/dist/src/api/portal.js.map +1 -0
  21. package/dist/src/assets/DefaultAvatar.d.ts +5 -0
  22. package/dist/src/assets/DefaultAvatar.d.ts.map +1 -0
  23. package/dist/src/assets/DefaultAvatar.js +21 -0
  24. package/dist/src/assets/DefaultAvatar.js.map +1 -0
  25. package/dist/src/components/Dropdown/AccountAddress.d.ts +8 -0
  26. package/dist/src/components/Dropdown/AccountAddress.d.ts.map +1 -0
  27. package/dist/src/components/Dropdown/AccountAddress.js +66 -0
  28. package/dist/src/components/Dropdown/AccountAddress.js.map +1 -0
  29. package/dist/src/components/Dropdown/AccountAssets.d.ts +14 -0
  30. package/dist/src/components/Dropdown/AccountAssets.d.ts.map +1 -0
  31. package/dist/src/components/Dropdown/AccountAssets.js +44 -0
  32. package/dist/src/components/Dropdown/AccountAssets.js.map +1 -0
  33. package/dist/src/components/Dropdown/AccountBalance.d.ts +7 -0
  34. package/dist/src/components/Dropdown/AccountBalance.d.ts.map +1 -0
  35. package/dist/src/components/Dropdown/AccountBalance.js +18 -0
  36. package/dist/src/components/Dropdown/AccountBalance.js.map +1 -0
  37. package/dist/src/components/Dropdown/ConnectedTrigger.d.ts +7 -0
  38. package/dist/src/components/Dropdown/ConnectedTrigger.d.ts.map +1 -0
  39. package/dist/src/components/Dropdown/ConnectedTrigger.js +30 -0
  40. package/dist/src/components/Dropdown/ConnectedTrigger.js.map +1 -0
  41. package/dist/src/components/Dropdown/Content.d.ts +9 -0
  42. package/dist/src/components/Dropdown/Content.d.ts.map +1 -0
  43. package/dist/src/components/Dropdown/Content.js +69 -0
  44. package/dist/src/components/Dropdown/Content.js.map +1 -0
  45. package/dist/src/components/Dropdown/DisconnectedTrigger.d.ts +7 -0
  46. package/dist/src/components/Dropdown/DisconnectedTrigger.d.ts.map +1 -0
  47. package/dist/src/components/Dropdown/DisconnectedTrigger.js +13 -0
  48. package/dist/src/components/Dropdown/DisconnectedTrigger.js.map +1 -0
  49. package/dist/src/components/Dropdown/Dropdown.d.ts +20 -0
  50. package/dist/src/components/Dropdown/Dropdown.d.ts.map +1 -0
  51. package/dist/src/components/Dropdown/Dropdown.js +64 -0
  52. package/dist/src/components/Dropdown/Dropdown.js.map +1 -0
  53. package/dist/src/components/Dropdown/WelcomeBlock.d.ts +8 -0
  54. package/dist/src/components/Dropdown/WelcomeBlock.d.ts.map +1 -0
  55. package/dist/src/components/Dropdown/WelcomeBlock.js +44 -0
  56. package/dist/src/components/Dropdown/WelcomeBlock.js.map +1 -0
  57. package/dist/src/components/Dropdown/index.d.ts +3 -0
  58. package/dist/src/components/Dropdown/index.d.ts.map +1 -0
  59. package/dist/src/components/Dropdown/index.js +2 -0
  60. package/dist/src/components/Dropdown/index.js.map +1 -0
  61. package/dist/src/components/index.d.ts +2 -0
  62. package/dist/src/components/index.d.ts.map +1 -0
  63. package/dist/src/components/index.js +2 -0
  64. package/dist/src/components/index.js.map +1 -0
  65. package/dist/src/hooks/constants.d.ts +3 -3
  66. package/dist/src/hooks/constants.js +4 -4
  67. package/dist/src/hooks/constants.js.map +1 -1
  68. package/dist/src/hooks/index.d.ts +1 -4
  69. package/dist/src/hooks/index.d.ts.map +1 -1
  70. package/dist/src/hooks/index.js +1 -4
  71. package/dist/src/hooks/index.js.map +1 -1
  72. package/dist/src/hooks/useAssetsUSDConversion.d.ts +8 -0
  73. package/dist/src/hooks/useAssetsUSDConversion.d.ts.map +1 -0
  74. package/dist/src/hooks/useAssetsUSDConversion.js +21 -0
  75. package/dist/src/hooks/useAssetsUSDConversion.js.map +1 -0
  76. package/dist/src/hooks/useAuthApiClient.d.ts +1 -1
  77. package/dist/src/hooks/useAuthApiClient.d.ts.map +1 -1
  78. package/dist/src/hooks/useAuthenticateWithWallet.d.ts +73 -0
  79. package/dist/src/hooks/useAuthenticateWithWallet.d.ts.map +1 -0
  80. package/dist/src/hooks/useAuthenticateWithWallet.js +69 -0
  81. package/dist/src/hooks/useAuthenticateWithWallet.js.map +1 -0
  82. package/dist/src/hooks/useCreateAccount.d.ts +41 -137
  83. package/dist/src/hooks/useCreateAccount.d.ts.map +1 -1
  84. package/dist/src/hooks/useCreateAccount.js +8 -10
  85. package/dist/src/hooks/useCreateAccount.js.map +1 -1
  86. package/dist/src/hooks/useCreateSession.d.ts +12 -182
  87. package/dist/src/hooks/useCreateSession.d.ts.map +1 -1
  88. package/dist/src/hooks/useCreateSession.js +12 -18
  89. package/dist/src/hooks/useCreateSession.js.map +1 -1
  90. package/dist/src/hooks/useDropdownData.d.ts +45 -0
  91. package/dist/src/hooks/useDropdownData.d.ts.map +1 -0
  92. package/dist/src/hooks/useDropdownData.js +74 -0
  93. package/dist/src/hooks/useDropdownData.js.map +1 -0
  94. package/dist/src/hooks/useEnsureNoSessionAndFetchNonce.d.ts +5 -0
  95. package/dist/src/hooks/useEnsureNoSessionAndFetchNonce.d.ts.map +1 -0
  96. package/dist/src/hooks/useEnsureNoSessionAndFetchNonce.js +34 -0
  97. package/dist/src/hooks/useEnsureNoSessionAndFetchNonce.js.map +1 -0
  98. package/dist/src/hooks/useGetAccountByAddress.d.ts +3 -5
  99. package/dist/src/hooks/useGetAccountByAddress.d.ts.map +1 -1
  100. package/dist/src/hooks/useGetAccountByAddress.js +3 -4
  101. package/dist/src/hooks/useGetAccountByAddress.js.map +1 -1
  102. package/dist/src/hooks/useGetAccountByMezoId.d.ts +3 -5
  103. package/dist/src/hooks/useGetAccountByMezoId.d.ts.map +1 -1
  104. package/dist/src/hooks/useGetAccountByMezoId.js +3 -4
  105. package/dist/src/hooks/useGetAccountByMezoId.js.map +1 -1
  106. package/dist/src/hooks/useGetCurrentAccount.d.ts +14 -8
  107. package/dist/src/hooks/useGetCurrentAccount.d.ts.map +1 -1
  108. package/dist/src/hooks/useGetCurrentAccount.js +28 -6
  109. package/dist/src/hooks/useGetCurrentAccount.js.map +1 -1
  110. package/dist/src/hooks/useGetSession.d.ts +3 -20
  111. package/dist/src/hooks/useGetSession.d.ts.map +1 -1
  112. package/dist/src/hooks/useGetSession.js +2 -2
  113. package/dist/src/hooks/useGetSession.js.map +1 -1
  114. package/dist/src/hooks/useLinkAccount.d.ts +15 -121
  115. package/dist/src/hooks/useLinkAccount.d.ts.map +1 -1
  116. package/dist/src/hooks/useLinkAccount.js +8 -21
  117. package/dist/src/hooks/useLinkAccount.js.map +1 -1
  118. package/dist/src/hooks/usePortalApiClient.d.ts +2 -0
  119. package/dist/src/hooks/usePortalApiClient.d.ts.map +1 -0
  120. package/dist/src/hooks/usePortalApiClient.js +6 -0
  121. package/dist/src/hooks/usePortalApiClient.js.map +1 -0
  122. package/dist/src/hooks/useSignInWithDiscord.d.ts +12 -181
  123. package/dist/src/hooks/useSignInWithDiscord.d.ts.map +1 -1
  124. package/dist/src/hooks/useSignInWithDiscord.js +11 -22
  125. package/dist/src/hooks/useSignInWithDiscord.js.map +1 -1
  126. package/dist/src/hooks/useSignInWithWallet.d.ts +12 -181
  127. package/dist/src/hooks/useSignInWithWallet.d.ts.map +1 -1
  128. package/dist/src/hooks/useSignInWithWallet.js +6 -43
  129. package/dist/src/hooks/useSignInWithWallet.js.map +1 -1
  130. package/dist/src/hooks/useSignOut.d.ts +12 -28
  131. package/dist/src/hooks/useSignOut.d.ts.map +1 -1
  132. package/dist/src/hooks/useSignOut.js +11 -6
  133. package/dist/src/hooks/useSignOut.js.map +1 -1
  134. package/dist/src/hooks/useSignUpWithWallet.d.ts +73 -0
  135. package/dist/src/hooks/useSignUpWithWallet.d.ts.map +1 -0
  136. package/dist/src/hooks/useSignUpWithWallet.js +11 -0
  137. package/dist/src/hooks/useSignUpWithWallet.js.map +1 -0
  138. package/dist/src/hooks/useUpdateMezoId.d.ts +30 -82
  139. package/dist/src/hooks/useUpdateMezoId.d.ts.map +1 -1
  140. package/dist/src/hooks/useUpdateMezoId.js +8 -12
  141. package/dist/src/hooks/useUpdateMezoId.js.map +1 -1
  142. package/dist/src/hooks/useWalletAccount.d.ts +12 -0
  143. package/dist/src/hooks/useWalletAccount.d.ts.map +1 -0
  144. package/dist/src/hooks/useWalletAccount.js +28 -0
  145. package/dist/src/hooks/useWalletAccount.js.map +1 -0
  146. package/dist/src/index.d.ts +1 -0
  147. package/dist/src/index.d.ts.map +1 -1
  148. package/dist/src/index.js +1 -0
  149. package/dist/src/index.js.map +1 -1
  150. package/dist/src/provider.d.ts +10 -13
  151. package/dist/src/provider.d.ts.map +1 -1
  152. package/dist/src/provider.js +11 -20
  153. package/dist/src/provider.js.map +1 -1
  154. package/dist/src/utils/address.d.ts +15 -0
  155. package/dist/src/utils/address.d.ts.map +1 -0
  156. package/dist/src/utils/address.js +35 -0
  157. package/dist/src/utils/address.js.map +1 -0
  158. package/dist/src/utils/address.test.d.ts +2 -0
  159. package/dist/src/utils/address.test.d.ts.map +1 -0
  160. package/dist/src/utils/address.test.js +32 -0
  161. package/dist/src/utils/address.test.js.map +1 -0
  162. package/dist/src/utils/cryptoAssets.d.ts +28 -0
  163. package/dist/src/utils/cryptoAssets.d.ts.map +1 -0
  164. package/dist/src/utils/cryptoAssets.js +73 -0
  165. package/dist/src/utils/cryptoAssets.js.map +1 -0
  166. package/dist/src/utils/cryptoAssets.test.d.ts +2 -0
  167. package/dist/src/utils/cryptoAssets.test.d.ts.map +1 -0
  168. package/dist/src/utils/cryptoAssets.test.js +50 -0
  169. package/dist/src/utils/cryptoAssets.test.js.map +1 -0
  170. package/dist/src/utils/currency.d.ts +14 -0
  171. package/dist/src/utils/currency.d.ts.map +1 -0
  172. package/dist/src/utils/currency.js +27 -0
  173. package/dist/src/utils/currency.js.map +1 -0
  174. package/dist/src/utils/currency.test.d.ts +2 -0
  175. package/dist/src/utils/currency.test.d.ts.map +1 -0
  176. package/dist/src/utils/currency.test.js +34 -0
  177. package/dist/src/utils/currency.test.js.map +1 -0
  178. package/dist/src/utils/numbers.d.ts +26 -0
  179. package/dist/src/utils/numbers.d.ts.map +1 -0
  180. package/dist/src/utils/numbers.js +40 -0
  181. package/dist/src/utils/numbers.js.map +1 -0
  182. package/dist/src/utils/numbers.test.d.ts +2 -0
  183. package/dist/src/utils/numbers.test.d.ts.map +1 -0
  184. package/dist/src/utils/numbers.test.js +54 -0
  185. package/dist/src/utils/numbers.test.js.map +1 -0
  186. package/package.json +10 -4
  187. package/src/api/auth.ts +104 -129
  188. package/src/api/client.ts +78 -0
  189. package/src/api/fetch-error.ts +8 -0
  190. package/src/api/index.ts +2 -0
  191. package/src/api/portal.ts +56 -0
  192. package/src/assets/DefaultAvatar.tsx +74 -0
  193. package/src/components/Dropdown/AccountAddress.tsx +130 -0
  194. package/src/components/Dropdown/AccountAssets.tsx +110 -0
  195. package/src/components/Dropdown/AccountBalance.tsx +38 -0
  196. package/src/components/Dropdown/ConnectedTrigger.tsx +56 -0
  197. package/src/components/Dropdown/Content.tsx +148 -0
  198. package/src/components/Dropdown/DisconnectedTrigger.tsx +36 -0
  199. package/src/components/Dropdown/Dropdown.tsx +111 -0
  200. package/src/components/Dropdown/README.md +51 -0
  201. package/src/components/Dropdown/WelcomeBlock.tsx +92 -0
  202. package/src/components/Dropdown/index.ts +2 -0
  203. package/src/components/index.ts +1 -0
  204. package/src/hooks/constants.ts +4 -4
  205. package/src/hooks/index.ts +1 -4
  206. package/src/hooks/useAssetsUSDConversion.ts +31 -0
  207. package/src/hooks/useAuthenticateWithWallet.ts +98 -0
  208. package/src/hooks/useCreateAccount.ts +20 -11
  209. package/src/hooks/useCreateSession.ts +24 -18
  210. package/src/hooks/useDropdownData.ts +131 -0
  211. package/src/hooks/useEnsureNoSessionAndFetchNonce.ts +46 -0
  212. package/src/hooks/useGetAccountByAddress.ts +11 -5
  213. package/src/hooks/useGetAccountByMezoId.ts +11 -5
  214. package/src/hooks/useGetCurrentAccount.ts +53 -7
  215. package/src/hooks/useGetSession.ts +10 -3
  216. package/src/hooks/useLinkAccount.ts +19 -31
  217. package/src/hooks/usePortalApiClient.ts +6 -0
  218. package/src/hooks/useSignInWithDiscord.ts +19 -28
  219. package/src/hooks/useSignInWithWallet.ts +14 -54
  220. package/src/hooks/useSignOut.ts +22 -6
  221. package/src/hooks/useSignUpWithWallet.ts +21 -0
  222. package/src/hooks/useUpdateMezoId.ts +20 -12
  223. package/src/hooks/useWalletAccount.ts +53 -0
  224. package/src/index.ts +1 -0
  225. package/src/provider.ts +30 -37
  226. package/src/utils/address.test.ts +38 -0
  227. package/src/utils/address.ts +43 -0
  228. package/src/utils/cryptoAssets.test.ts +61 -0
  229. package/src/utils/cryptoAssets.ts +93 -0
  230. package/src/utils/currency.test.ts +38 -0
  231. package/src/utils/currency.ts +32 -0
  232. package/src/utils/numbers.test.ts +73 -0
  233. package/src/utils/numbers.ts +55 -0
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Formats a number or bigint into a human-readable string with K, M, B, or T suffix.
3
+ * @param value The number or bigint to format.
4
+ * @param decimals Number of decimal places to include (ignored for bigint to avoid floating point issues).
5
+ * @returns The formatted number as a string.
6
+ */
7
+ export function formatHumanReadableNumber(value, decimals = 4) {
8
+ return value.toLocaleString("en-US", {
9
+ notation: "compact",
10
+ compactDisplay: "short",
11
+ maximumFractionDigits: decimals,
12
+ });
13
+ }
14
+ /**
15
+ * Returns a number rounded up to the desired number of decimals.
16
+ * @param amount The number to round up.
17
+ * @param desiredDecimals The number of decimals to round up to.
18
+ * @returns The rounded up number.
19
+ * @see https://github.com/thesis/acre/blob/2078d339f4ddce79e69c78529c9d72910dd2640a/dapp/src/utils/numbersUtils.ts#L1-L3
20
+ */
21
+ export function roundUpNumber(amount, desiredDecimals = 2) {
22
+ return Math.ceil(amount * 10 ** desiredDecimals) / 10 ** desiredDecimals;
23
+ }
24
+ /**
25
+ * Convert a fixed point bigint with precision `fixedPointDecimals` to a
26
+ * floating point number truncated to `desiredDecimals`.
27
+ * @param fixedPoint The fixed point bigint to convert.
28
+ * @param fixedPointDecimals The number of decimals in the fixed point bigint.
29
+ * @param desiredDecimals The number of decimals in the output number.
30
+ * @returns The floating point number truncated to `desiredDecimals`.
31
+ * @see https://github.com/tahowallet/extension/blob/f6b26b7cb4a0a0278b1fccc6319626638f1a6dfa/background/lib/fixed-point.ts#L216-L239
32
+ */
33
+ export function fromFixedPoint(fixedPoint, fixedPointDecimals, desiredDecimals = 2) {
34
+ const fixedPointDesiredDecimalsAmount = fixedPoint /
35
+ 10n ** BigInt(Math.max(1, fixedPointDecimals - desiredDecimals));
36
+ const formattedAmount = Number(fixedPointDesiredDecimalsAmount) /
37
+ 10 ** Math.min(desiredDecimals, fixedPointDecimals);
38
+ return formattedAmount;
39
+ }
40
+ //# sourceMappingURL=numbers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"numbers.js","sourceRoot":"","sources":["../../../src/utils/numbers.ts"],"names":[],"mappings":"AAGA;;;;;GAKG;AACH,MAAM,UAAU,yBAAyB,CACvC,KAAsB,EACtB,QAAQ,GAAG,CAAC;IAEZ,OAAO,KAAK,CAAC,cAAc,CAAC,OAAO,EAAE;QACnC,QAAQ,EAAE,SAAS;QACnB,cAAc,EAAE,OAAO;QACvB,qBAAqB,EAAE,QAA2B;KACnD,CAAC,CAAA;AACJ,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,aAAa,CAAC,MAAc,EAAE,eAAe,GAAG,CAAC;IAC/D,OAAO,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,EAAE,IAAI,eAAe,CAAC,GAAG,EAAE,IAAI,eAAe,CAAA;AAC1E,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,cAAc,CAC5B,UAAkB,EAClB,kBAA0B,EAC1B,eAAe,GAAG,CAAC;IAEnB,MAAM,+BAA+B,GACnC,UAAU;QACV,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,kBAAkB,GAAG,eAAe,CAAC,CAAC,CAAA;IAElE,MAAM,eAAe,GACnB,MAAM,CAAC,+BAA+B,CAAC;QACvC,EAAE,IAAI,IAAI,CAAC,GAAG,CAAC,eAAe,EAAE,kBAAkB,CAAC,CAAA;IAErD,OAAO,eAAe,CAAA;AACxB,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=numbers.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"numbers.test.d.ts","sourceRoot":"","sources":["../../../src/utils/numbers.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,54 @@
1
+ import { formatHumanReadableNumber, fromFixedPoint, roundUpNumber, } from "./numbers";
2
+ describe("formatHumanReadableNumber", () => {
3
+ it("formats number less than 1000 with decimals", () => {
4
+ expect(formatHumanReadableNumber(123.456789, 2)).toBe("123.46");
5
+ });
6
+ it("formats thousands with K", () => {
7
+ expect(formatHumanReadableNumber(12_345, 2)).toBe("12.35K");
8
+ });
9
+ it("formats millions with M", () => {
10
+ expect(formatHumanReadableNumber(12_345_678, 2)).toBe("12.35M");
11
+ });
12
+ it("formats billions with B", () => {
13
+ expect(formatHumanReadableNumber(12_345_678_901, 2)).toBe("12.35B");
14
+ });
15
+ it("formats trillions with T", () => {
16
+ expect(formatHumanReadableNumber(12_345_678_901_234, 2)).toBe("12.35T");
17
+ });
18
+ it("formats bigint as readable number", () => {
19
+ expect(formatHumanReadableNumber(1234567890123456789n, 2)).toBe("1,234,567.89T");
20
+ });
21
+ it("caps at > 1000T for huge numbers", () => {
22
+ const hugeNumber = 10 ** 18; // 1e18
23
+ expect(formatHumanReadableNumber(hugeNumber, 2)).toBe("1,000,000T");
24
+ });
25
+ it("formats numbers with trailing zeros correctly", () => {
26
+ expect(formatHumanReadableNumber(1_000, 2)).toBe("1K");
27
+ });
28
+ });
29
+ describe("roundUpNumber", () => {
30
+ it("rounds up to 2 decimals", () => {
31
+ expect(roundUpNumber(1.234)).toBe(1.24);
32
+ });
33
+ it("rounds up to 3 decimals", () => {
34
+ expect(roundUpNumber(9.87654, 3)).toBe(9.877);
35
+ });
36
+ it("rounds up exact decimal", () => {
37
+ expect(roundUpNumber(2.5, 1)).toBe(2.5);
38
+ });
39
+ });
40
+ describe("fromFixedPoint", () => {
41
+ it("converts fixed point with 18 decimals to float", () => {
42
+ const raw = 1234567890000000000n; // 1.23456789 ETH in 18 decimals
43
+ expect(fromFixedPoint(raw, 18, 4)).toBe(1.2345);
44
+ });
45
+ it("converts with truncation to 2 decimals", () => {
46
+ const raw = 987654321000000000n; // ~0.987654321 ETH
47
+ expect(fromFixedPoint(raw, 18, 2)).toBe(0.98);
48
+ });
49
+ it("handles large fixedPoint decimals", () => {
50
+ const raw = 1000000000000000000000000n; // 1M with 6 decimals
51
+ expect(fromFixedPoint(raw, 6, 2)).toBe(1000000000000000000);
52
+ });
53
+ });
54
+ //# sourceMappingURL=numbers.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"numbers.test.js","sourceRoot":"","sources":["../../../src/utils/numbers.test.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,yBAAyB,EACzB,cAAc,EACd,aAAa,GACd,MAAM,WAAW,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AAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IAC/C,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,mCAAmC,EAAE,GAAG,EAAE;QAC3C,MAAM,GAAG,GAAG,0BAAkC,CAAA,CAAC,qBAAqB;QACpE,MAAM,CAAC,cAAc,CAAC,GAAG,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,mBAAmB,CAAC,CAAA;IAC7D,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA"}
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "@mezo-org/passport",
3
- "version": "0.4.0-dev.1",
3
+ "version": "0.4.0-dev.11",
4
4
  "main": "dist/src/index.js",
5
5
  "license": "GPL-3.0-only",
6
6
  "scripts": {
7
7
  "build": "tsc",
8
+ "dev": "tsc --watch",
8
9
  "format": "npm run lint:js && npm run lint:config",
9
10
  "format:fix": "npm run lint:js:fix && npm run lint:config:fix",
10
11
  "lint:js": "eslint .",
@@ -23,24 +24,29 @@
23
24
  "directory": "typescript/passport"
24
25
  },
25
26
  "devDependencies": {
26
- "@babel/preset-env": "^7.23.7",
27
27
  "@thesis-co/eslint-config": "github:thesis/eslint-config#7b9bc8c",
28
28
  "@types/jest": "^29.5.12",
29
29
  "@types/node": "^20.11.30",
30
30
  "@types/react": "^18.2.64",
31
31
  "eslint": "^8.57.1",
32
32
  "jest": "^29.7.0",
33
+ "jest-environment-jsdom": "^29.7.0",
33
34
  "prettier": "^3.2.5",
34
35
  "ts-jest": "^29.1.2",
35
36
  "ts-node": "^10.9.2",
36
37
  "typescript": "^5.4.3"
37
38
  },
38
39
  "dependencies": {
39
- "@mezo-org/orangekit": "1.0.0-beta.40-dev.0",
40
+ "@mezo-org/mezo-clay": "0.1.0-dev.18",
41
+ "@mezo-org/orangekit": "1.0.0-beta.40-dev.1",
42
+ "@mezo-org/sign-in-with-wallet": "1.0.0-beta.8"
43
+ },
44
+ "peerDependencies": {
40
45
  "@rainbow-me/rainbowkit": "2.0.2",
41
46
  "@tanstack/react-query": "^5.28.4",
42
- "@mezo-org/sign-in-with-wallet": "1.0.0-beta.8",
43
47
  "react": "^18.2.0",
48
+ "styletron-engine-monolithic": "^1.0.0",
49
+ "usehooks-ts": "^3.1.1",
44
50
  "viem": "2.22.8",
45
51
  "wagmi": "2.5.12"
46
52
  }
package/src/api/auth.ts CHANGED
@@ -1,152 +1,110 @@
1
- type RequestHandlerOptions = Omit<RequestInit, "credentials" | "body"> & {
2
- body?: Record<string, unknown>
3
- queryParams?: Record<string, string | undefined>
4
- }
5
- type PassportIdentityProvider = "wallet" | "discord" | "email"
1
+ import { ApiClient } from "./client"
2
+ import { FetchError } from "./fetch-error"
6
3
 
7
- type AccountPublicData = {
8
- mezoId?: string
9
- btcAddress: string
4
+ type AuthProvider = "discord"
5
+
6
+ export type LinkedWalletAccount = {
7
+ type: "wallet"
8
+ btcAddress?: string
10
9
  evmAddress: string
11
10
  }
12
11
 
12
+ export type LinkedProviderAccount = {
13
+ type: AuthProvider
14
+ name: string
15
+ id: string
16
+ avatarUrl: string
17
+ }
18
+
19
+ export type LinkedAccount = LinkedWalletAccount | LinkedProviderAccount
20
+
13
21
  type Account = {
14
- app_metadata: {
15
- provider: PassportIdentityProvider
16
- providers: PassportIdentityProvider[]
17
- }
18
- user_metadata: AccountPublicData
22
+ mezoId: string
23
+ hasModifiedMezoId: boolean
24
+ linkedAccounts: LinkedAccount[]
19
25
  }
20
26
 
21
- type Session = {
22
- access_token: string
23
- token_type: string
24
- expires_in: number
25
- expires_at: number
26
- refresh_token: string
27
- user: Account
27
+ export type Session = {
28
+ expiresIn: number
29
+ expiresAt: number
30
+ sessionLinks: {
31
+ _links: {
32
+ account: string
33
+ }
34
+ }
28
35
  }
29
36
 
30
- type AuthenticationProviderRedirectResponse = {
37
+ export type AuthenticationProviderRedirectResponse = {
31
38
  redirectTo: string
32
39
  }
33
40
 
34
- type WalletAccountCredentials = {
35
- type: "wallet"
41
+ type SignatureData = {
36
42
  message: string
37
43
  signature: string
38
44
  }
39
45
 
46
+ // Request and Response types
47
+ export type GetSessionResponse = Session | { nonce: string }
48
+
40
49
  export type CreateSessionRequest =
41
- | WalletAccountCredentials
50
+ | ({ type: "wallet" } & SignatureData)
42
51
  | { type: "discord" }
52
+ export type CreateSessionResponse =
53
+ | Session
54
+ | AuthenticationProviderRedirectResponse
43
55
 
44
- export type LinkAccountRequest = CreateSessionRequest
56
+ export type DeleteSessionResponse = { message: string }
45
57
 
46
- export type CreateAccountRequest = WalletAccountCredentials
58
+ export type GetCurrentAccountResponse = Partial<Account> | null
47
59
 
48
- export const API_ENDPOINTS_BY_ENV = {
49
- mainnet: "https://api.mezo.org",
50
- testnet: "https://api.test.mezo.org",
60
+ export type GetAccountByMezoIdOrAddressResponse = {
61
+ mezoId?: string
62
+ linkedAccounts?: Extract<LinkedAccount, { type: "wallet" }>[]
51
63
  }
52
64
 
53
- const DEFAULT_API_ENDPOINT = API_ENDPOINTS_BY_ENV.mainnet
54
-
55
- export class AuthApiClient {
56
- private apiUrl: string
57
-
58
- constructor(apiUrl?: string) {
59
- this.apiUrl = apiUrl ?? DEFAULT_API_ENDPOINT
60
- }
61
-
62
- /**
63
- * The request handler for the Passport API. It accepts an API endpoint and
64
- * request options.
65
- * @dev Generic type `D` is a type of returned object from the function
66
- * @param endpoint - The API endpoint to request. Should start with
67
- * forward slash ("/")
68
- * @param {RequestHandlerOptions<B>} options - The request options, the
69
- * `RequestInit` type with additional type-safe properties
70
- * @returns The promise of the API response
71
- */
72
- protected async handleRequest<D>(
73
- endpoint: `/${string}`,
74
- options: RequestHandlerOptions,
75
- ): Promise<D> {
76
- const {
77
- method,
78
- headers,
79
- body,
80
- queryParams = {},
81
- ...restOptions
82
- } = options || {}
83
-
84
- // If last character in apiUrl is forward slash we are removing it
85
- const authApiUrlRoute = this.apiUrl.replace(/\/$/, "")
86
- const url = new URL(`${authApiUrlRoute}${endpoint}`)
87
-
88
- // Removes falsy values (e.g. null, undefined etc.) from query params and
89
- // appends remaining ones to the URL
90
- Object.entries(queryParams)
91
- .filter(([, value]) => value) // Remove falsy values
92
- .forEach(([key, value]) => url.searchParams.append(key, value!))
93
-
94
- try {
95
- const response = await fetch(url, {
96
- method,
97
- credentials: "include",
98
- headers: {
99
- "Content-Type": "application/json",
100
- ...headers,
101
- },
102
- body: body ? JSON.stringify(body) : undefined,
103
- ...restOptions,
104
- })
65
+ export type LinkAccountRequest = CreateSessionRequest
66
+ export type LinkAccountResponse =
67
+ | Account
68
+ | AuthenticationProviderRedirectResponse
105
69
 
106
- const data = await response.json()
70
+ export type CreateAccountRequest = { type: "wallet" } & SignatureData
71
+ export type CreateAccountResponse = Account
107
72
 
108
- if (!response.ok || data?.error) {
109
- const error = data?.error || "An error occurred"
110
- throw new Error(error)
111
- }
73
+ export type UpdateMezoIdResponse = Account
112
74
 
113
- return data as D
114
- } catch (error) {
115
- const errorMessage =
116
- error instanceof Error ? error.message : "An error occurred"
75
+ export const AUTH_API_ENDPOINTS_BY_ENV = {
76
+ mainnet: "https://api.mezo.org",
77
+ testnet: "https://api.test.mezo.org",
78
+ }
117
79
 
118
- throw new Error(errorMessage)
119
- }
80
+ export class AuthApiClient extends ApiClient {
81
+ constructor(
82
+ environment: keyof typeof AUTH_API_ENDPOINTS_BY_ENV = "mainnet",
83
+ apiUrl?: string,
84
+ ) {
85
+ super(apiUrl ?? AUTH_API_ENDPOINTS_BY_ENV[environment])
120
86
  }
121
87
 
122
88
  async getSession(code?: string) {
123
- const data = await this.handleRequest<Session | { nonce: string }>(
124
- "/session",
125
- {
126
- method: "GET",
127
- queryParams: { code },
128
- },
129
- )
89
+ const data = await this.handleRequest<GetSessionResponse>("/session", {
90
+ method: "GET",
91
+ queryParams: { code },
92
+ })
130
93
 
131
94
  return data
132
95
  }
133
96
 
134
97
  async createSession(createSessionRequest: CreateSessionRequest) {
135
- const data = await this.handleRequest<Session | { redirectTo: string }>(
136
- "/session",
137
- {
138
- method: "POST",
139
- body: createSessionRequest,
140
- },
141
- )
98
+ const data = await this.handleRequest<CreateSessionResponse>("/session", {
99
+ method: "POST",
100
+ body: createSessionRequest,
101
+ })
142
102
 
143
103
  return data
144
104
  }
145
105
 
146
106
  async deleteSession() {
147
- const data = await this.handleRequest<{
148
- message: string
149
- }>("/session", {
107
+ const data = await this.handleRequest<DeleteSessionResponse>("/session", {
150
108
  method: "DELETE",
151
109
  })
152
110
 
@@ -154,37 +112,51 @@ export class AuthApiClient {
154
112
  }
155
113
 
156
114
  async getCurrentAccount() {
157
- const data = await this.handleRequest<Account>("/session/account", {
158
- method: "GET",
159
- })
115
+ const data = await this.handleRequest<GetCurrentAccountResponse>(
116
+ "/session/account",
117
+ {
118
+ method: "GET",
119
+ },
120
+ )
160
121
 
161
122
  return data
162
123
  }
163
124
 
164
125
  async getAccountByMezoIdOrAddress(mezoIdOrAddress: string) {
165
- const data = await this.handleRequest<AccountPublicData>(
166
- `/accounts/${mezoIdOrAddress}`,
167
- {
168
- method: "GET",
169
- },
170
- )
126
+ let data
127
+ try {
128
+ data = await this.handleRequest<GetAccountByMezoIdOrAddressResponse>(
129
+ `/accounts/${mezoIdOrAddress}`,
130
+ {
131
+ method: "GET",
132
+ },
133
+ )
134
+ } catch (error) {
135
+ // Return null if account is not found instead of throwing an error
136
+ if (error instanceof FetchError && error.status === 404) {
137
+ data = null
138
+ } else {
139
+ throw error
140
+ }
141
+ }
171
142
 
172
143
  return data
173
144
  }
174
145
 
175
146
  async linkAccount(linkAccountRequest: LinkAccountRequest) {
176
- const data = await this.handleRequest<
177
- Account | AuthenticationProviderRedirectResponse
178
- >("/session/account/linked-accounts", {
179
- method: "POST",
180
- body: linkAccountRequest,
181
- })
147
+ const data = await this.handleRequest<LinkAccountResponse>(
148
+ "/session/account/linked-accounts",
149
+ {
150
+ method: "POST",
151
+ body: linkAccountRequest,
152
+ },
153
+ )
182
154
 
183
155
  return data
184
156
  }
185
157
 
186
158
  async createAccount(createAccountRequest: CreateAccountRequest) {
187
- const data = await this.handleRequest<Account>("/accounts", {
159
+ const data = await this.handleRequest<CreateAccountResponse>("/accounts", {
188
160
  method: "POST",
189
161
  body: createAccountRequest,
190
162
  })
@@ -193,10 +165,13 @@ export class AuthApiClient {
193
165
  }
194
166
 
195
167
  async updateMezoId(newMezoId: string) {
196
- const data = await this.handleRequest<Account>("/session/account", {
197
- method: "PATCH",
198
- body: { newMezoId },
199
- })
168
+ const data = await this.handleRequest<UpdateMezoIdResponse>(
169
+ "/session/account",
170
+ {
171
+ method: "PATCH",
172
+ body: { mezoId: newMezoId },
173
+ },
174
+ )
200
175
 
201
176
  return data
202
177
  }
@@ -0,0 +1,78 @@
1
+ import { FetchError } from "./fetch-error"
2
+
3
+ type RequestHandlerOptions = Omit<RequestInit, "credentials" | "body"> & {
4
+ body?: Record<string, unknown>
5
+ queryParams?: Record<string, string | undefined>
6
+ }
7
+
8
+ export abstract class ApiClient {
9
+ private apiUrl: string
10
+
11
+ constructor(apiUrl: string) {
12
+ this.apiUrl = apiUrl
13
+ }
14
+
15
+ /**
16
+ * The request handler for the Passport API. It accepts an API endpoint and
17
+ * request options.
18
+ * @dev Generic type `D` is a type of returned object from the function
19
+ * @dev WARNING: This function hardcodes `credentials: "include"`, meaning
20
+ * cookies and authentication headers will be sent with every request. Ensure
21
+ * this is safe if you plan to use it in other contexts than auth api worker.
22
+ * @param endpoint - The API endpoint to request. Should start with
23
+ * forward slash ("/")
24
+ * @param options - The request options, the `RequestInit` type with
25
+ * additional type-safe properties
26
+ * @returns The promise of the API response
27
+ */
28
+ protected async handleRequest<D>(
29
+ endpoint: `/${string}`,
30
+ options: RequestHandlerOptions = {},
31
+ ): Promise<D> {
32
+ try {
33
+ const { method, body, queryParams = {}, ...restOptions } = options
34
+
35
+ // If last character in apiUrl is forward slash we are removing it
36
+ const authApiUrlRoute = this.apiUrl.replace(/\/$/, "")
37
+ const url = new URL(`${authApiUrlRoute}${endpoint}`)
38
+
39
+ // Removes falsy values (e.g. null, undefined etc.) from query params and
40
+ // appends remaining ones to the URL
41
+ Object.entries(queryParams)
42
+ .filter(([, value]) => value) // Remove falsy values
43
+ .forEach(([key, value]) => url.searchParams.append(key, value!))
44
+
45
+ const response = await fetch(url, {
46
+ method,
47
+ credentials: "include",
48
+ body: body ? JSON.stringify(body) : undefined,
49
+ ...restOptions,
50
+ })
51
+
52
+ const data = await response.json()
53
+
54
+ if (!response.ok || data?.error) {
55
+ const error =
56
+ data?.error ||
57
+ `An error occurred when handling API request: ${JSON.stringify(data)}`
58
+ throw new FetchError(
59
+ response.status,
60
+ `Error [${response.status}]: ${error}`,
61
+ )
62
+ }
63
+
64
+ return data as D
65
+ } catch (error) {
66
+ if (error instanceof FetchError) {
67
+ throw error
68
+ }
69
+
70
+ const errorMessage =
71
+ error instanceof Error ? error.message : JSON.stringify(error)
72
+ throw new FetchError(
73
+ 500,
74
+ `Error [500]: An error occurred when handling API request: ${errorMessage}`,
75
+ )
76
+ }
77
+ }
78
+ }
@@ -0,0 +1,8 @@
1
+ export class FetchError extends Error {
2
+ constructor(
3
+ public status: number,
4
+ message?: string,
5
+ ) {
6
+ super(message)
7
+ }
8
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./auth"
2
+ export * from "./portal"
@@ -0,0 +1,56 @@
1
+ import { ApiClient } from "./client"
2
+
3
+ type PortalStatistics = {
4
+ hodlers: number
5
+ depositedBitcoin: string
6
+ tvl: number
7
+ currentUsdPerBtc: string
8
+ currentUsdPerEth: string
9
+ usdPerBtc24hChange: string
10
+ }
11
+
12
+ type PortalMats = {
13
+ address: string
14
+ totalMats: number
15
+ matsFromDeposits: number
16
+ matsFromReferrals: number
17
+ matsFromSecondaryReferrals: number
18
+ netMatsTransferred: number
19
+ dailyMats: number
20
+ dailyMatsFromDeposits: number
21
+ dailyMatsFromReferrals: number
22
+ dailyMatsFromSecondaryReferrals: number
23
+ }
24
+
25
+ export const PORTAL_API_ENDPOINTS_BY_ENV = {
26
+ mainnet: "https://api.mezo.org/api/v1",
27
+ testnet: "https://api.test.mezo.org/api/v1",
28
+ }
29
+
30
+ export class PortalApiClient extends ApiClient {
31
+ constructor(
32
+ environment: keyof typeof PORTAL_API_ENDPOINTS_BY_ENV = "mainnet",
33
+ apiUrl?: string,
34
+ ) {
35
+ super(apiUrl ?? PORTAL_API_ENDPOINTS_BY_ENV[environment])
36
+ }
37
+
38
+ async getPortalStatistics() {
39
+ const data = await this.handleRequest<PortalStatistics>("/statistics", {
40
+ method: "GET",
41
+ })
42
+
43
+ return data
44
+ }
45
+
46
+ async getPortalMats(address: string) {
47
+ const data = await this.handleRequest<PortalMats>(
48
+ `/accounts/${address}/mats`,
49
+ {
50
+ method: "GET",
51
+ },
52
+ )
53
+
54
+ return data
55
+ }
56
+ }