@mezo-org/passport 0.4.0-dev.4 → 0.4.0-dev.41

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 (303) hide show
  1. package/README.md +18 -22
  2. package/dist/src/api/auth.d.ts +36 -59
  3. package/dist/src/api/auth.d.ts.map +1 -1
  4. package/dist/src/api/auth.js +21 -49
  5. package/dist/src/api/auth.js.map +1 -1
  6. package/dist/src/api/client.d.ts +24 -0
  7. package/dist/src/api/client.d.ts.map +1 -0
  8. package/dist/src/api/client.js +54 -0
  9. package/dist/src/api/client.js.map +1 -0
  10. package/dist/src/api/fetch-error.d.ts +5 -0
  11. package/dist/src/api/fetch-error.d.ts.map +1 -0
  12. package/dist/src/api/fetch-error.js +8 -0
  13. package/dist/src/api/fetch-error.js.map +1 -0
  14. package/dist/src/api/index.d.ts +3 -0
  15. package/dist/src/api/index.d.ts.map +1 -0
  16. package/dist/src/api/index.js +3 -0
  17. package/dist/src/api/index.js.map +1 -0
  18. package/dist/src/api/portal.d.ts +33 -0
  19. package/dist/src/api/portal.d.ts.map +1 -0
  20. package/dist/src/api/portal.js +23 -0
  21. package/dist/src/api/portal.js.map +1 -0
  22. package/dist/src/assets/DefaultAvatar.d.ts +5 -0
  23. package/dist/src/assets/DefaultAvatar.d.ts.map +1 -0
  24. package/dist/src/assets/DefaultAvatar.js +21 -0
  25. package/dist/src/assets/DefaultAvatar.js.map +1 -0
  26. package/dist/src/components/Dropdown/ConnectedTrigger.d.ts +8 -0
  27. package/dist/src/components/Dropdown/ConnectedTrigger.d.ts.map +1 -0
  28. package/dist/src/components/Dropdown/ConnectedTrigger.js +39 -0
  29. package/dist/src/components/Dropdown/ConnectedTrigger.js.map +1 -0
  30. package/dist/src/components/Dropdown/Content.d.ts +23 -0
  31. package/dist/src/components/Dropdown/Content.d.ts.map +1 -0
  32. package/dist/src/components/Dropdown/Content.js +27 -0
  33. package/dist/src/components/Dropdown/Content.js.map +1 -0
  34. package/dist/src/components/Dropdown/DisconnectedTrigger.d.ts +7 -0
  35. package/dist/src/components/Dropdown/DisconnectedTrigger.d.ts.map +1 -0
  36. package/dist/src/components/Dropdown/DisconnectedTrigger.js +13 -0
  37. package/dist/src/components/Dropdown/DisconnectedTrigger.js.map +1 -0
  38. package/dist/src/components/Dropdown/Dropdown.d.ts +27 -0
  39. package/dist/src/components/Dropdown/Dropdown.d.ts.map +1 -0
  40. package/dist/src/components/Dropdown/Dropdown.js +68 -0
  41. package/dist/src/components/Dropdown/Dropdown.js.map +1 -0
  42. package/dist/src/components/Dropdown/ListingItem.d.ts +13 -0
  43. package/dist/src/components/Dropdown/ListingItem.d.ts.map +1 -0
  44. package/dist/src/components/Dropdown/ListingItem.js +34 -0
  45. package/dist/src/components/Dropdown/ListingItem.js.map +1 -0
  46. package/dist/src/components/Dropdown/NestedViewLayout.d.ts +7 -0
  47. package/dist/src/components/Dropdown/NestedViewLayout.d.ts.map +1 -0
  48. package/dist/src/components/Dropdown/NestedViewLayout.js +34 -0
  49. package/dist/src/components/Dropdown/NestedViewLayout.js.map +1 -0
  50. package/dist/src/components/Dropdown/Receive/Receive.d.ts +7 -0
  51. package/dist/src/components/Dropdown/Receive/Receive.d.ts.map +1 -0
  52. package/dist/src/components/Dropdown/Receive/Receive.js +53 -0
  53. package/dist/src/components/Dropdown/Receive/Receive.js.map +1 -0
  54. package/dist/src/components/Dropdown/Root/AccountAddressActions.d.ts +7 -0
  55. package/dist/src/components/Dropdown/Root/AccountAddressActions.d.ts.map +1 -0
  56. package/dist/src/components/Dropdown/Root/AccountAddressActions.js +48 -0
  57. package/dist/src/components/Dropdown/Root/AccountAddressActions.js.map +1 -0
  58. package/dist/src/components/Dropdown/Root/AccountAssetItem.d.ts +11 -0
  59. package/dist/src/components/Dropdown/Root/AccountAssetItem.d.ts.map +1 -0
  60. package/dist/src/components/Dropdown/Root/AccountAssetItem.js +9 -0
  61. package/dist/src/components/Dropdown/Root/AccountAssetItem.js.map +1 -0
  62. package/dist/src/components/Dropdown/Root/AccountBalance.d.ts +8 -0
  63. package/dist/src/components/Dropdown/Root/AccountBalance.d.ts.map +1 -0
  64. package/dist/src/components/Dropdown/Root/AccountBalance.js +15 -0
  65. package/dist/src/components/Dropdown/Root/AccountBalance.js.map +1 -0
  66. package/dist/src/components/Dropdown/Root/AccountOtherAssets.d.ts +9 -0
  67. package/dist/src/components/Dropdown/Root/AccountOtherAssets.d.ts.map +1 -0
  68. package/dist/src/components/Dropdown/Root/AccountOtherAssets.js +48 -0
  69. package/dist/src/components/Dropdown/Root/AccountOtherAssets.js.map +1 -0
  70. package/dist/src/components/Dropdown/Root/Root.d.ts +23 -0
  71. package/dist/src/components/Dropdown/Root/Root.d.ts.map +1 -0
  72. package/dist/src/components/Dropdown/Root/Root.js +57 -0
  73. package/dist/src/components/Dropdown/Root/Root.js.map +1 -0
  74. package/dist/src/components/Dropdown/Root/WalletAddress.d.ts +8 -0
  75. package/dist/src/components/Dropdown/Root/WalletAddress.d.ts.map +1 -0
  76. package/dist/src/components/Dropdown/Root/WalletAddress.js +64 -0
  77. package/dist/src/components/Dropdown/Root/WalletAddress.js.map +1 -0
  78. package/dist/src/components/Dropdown/Root/WelcomeBlock.d.ts +8 -0
  79. package/dist/src/components/Dropdown/Root/WelcomeBlock.d.ts.map +1 -0
  80. package/dist/src/components/Dropdown/Root/WelcomeBlock.js +44 -0
  81. package/dist/src/components/Dropdown/Root/WelcomeBlock.js.map +1 -0
  82. package/dist/src/components/Dropdown/TestnetTopBanner.d.ts +3 -0
  83. package/dist/src/components/Dropdown/TestnetTopBanner.d.ts.map +1 -0
  84. package/dist/src/components/Dropdown/TestnetTopBanner.js +14 -0
  85. package/dist/src/components/Dropdown/TestnetTopBanner.js.map +1 -0
  86. package/dist/src/components/Dropdown/index.d.ts +3 -0
  87. package/dist/src/components/Dropdown/index.d.ts.map +1 -0
  88. package/dist/src/components/Dropdown/index.js +2 -0
  89. package/dist/src/components/Dropdown/index.js.map +1 -0
  90. package/dist/src/components/index.d.ts +2 -0
  91. package/dist/src/components/index.d.ts.map +1 -0
  92. package/dist/src/components/index.js +2 -0
  93. package/dist/src/components/index.js.map +1 -0
  94. package/dist/src/config.d.ts +41 -0
  95. package/dist/src/config.d.ts.map +1 -1
  96. package/dist/src/config.js +20 -5
  97. package/dist/src/config.js.map +1 -1
  98. package/dist/src/constants.d.ts +6 -2
  99. package/dist/src/constants.d.ts.map +1 -1
  100. package/dist/src/constants.js +8 -4
  101. package/dist/src/constants.js.map +1 -1
  102. package/dist/src/hooks/constants.d.ts +1 -0
  103. package/dist/src/hooks/constants.d.ts.map +1 -1
  104. package/dist/src/hooks/constants.js +1 -0
  105. package/dist/src/hooks/constants.js.map +1 -1
  106. package/dist/src/hooks/index.d.ts +4 -1
  107. package/dist/src/hooks/index.d.ts.map +1 -1
  108. package/dist/src/hooks/index.js +4 -1
  109. package/dist/src/hooks/index.js.map +1 -1
  110. package/dist/src/hooks/useAssetsConversionRates.d.ts +7 -0
  111. package/dist/src/hooks/useAssetsConversionRates.d.ts.map +1 -0
  112. package/dist/src/hooks/useAssetsConversionRates.js +24 -0
  113. package/dist/src/hooks/useAssetsConversionRates.js.map +1 -0
  114. package/dist/src/hooks/useAuthApiClient.d.ts +1 -1
  115. package/dist/src/hooks/useAuthApiClient.d.ts.map +1 -1
  116. package/dist/src/hooks/useAuthenticateWithWallet.d.ts +73 -0
  117. package/dist/src/hooks/useAuthenticateWithWallet.d.ts.map +1 -0
  118. package/dist/src/hooks/useAuthenticateWithWallet.js +69 -0
  119. package/dist/src/hooks/useAuthenticateWithWallet.js.map +1 -0
  120. package/dist/src/hooks/useBorrowData.d.ts +60 -0
  121. package/dist/src/hooks/useBorrowData.d.ts.map +1 -0
  122. package/dist/src/hooks/useBorrowData.js +122 -0
  123. package/dist/src/hooks/useBorrowData.js.map +1 -0
  124. package/dist/src/hooks/useCreateAccount.d.ts +23 -173
  125. package/dist/src/hooks/useCreateAccount.d.ts.map +1 -1
  126. package/dist/src/hooks/useCreateAccount.js +7 -4
  127. package/dist/src/hooks/useCreateAccount.js.map +1 -1
  128. package/dist/src/hooks/useCreateSession.d.ts +12 -101
  129. package/dist/src/hooks/useCreateSession.d.ts.map +1 -1
  130. package/dist/src/hooks/useCreateSession.js +10 -8
  131. package/dist/src/hooks/useCreateSession.js.map +1 -1
  132. package/dist/src/hooks/useDropdownData.d.ts +47 -0
  133. package/dist/src/hooks/useDropdownData.d.ts.map +1 -0
  134. package/dist/src/hooks/useDropdownData.js +95 -0
  135. package/dist/src/hooks/useDropdownData.js.map +1 -0
  136. package/dist/src/hooks/useEnsureNoSessionAndFetchNonce.d.ts.map +1 -1
  137. package/dist/src/hooks/useEnsureNoSessionAndFetchNonce.js +1 -0
  138. package/dist/src/hooks/useEnsureNoSessionAndFetchNonce.js.map +1 -1
  139. package/dist/src/hooks/useGetAccountByAddress.d.ts +2 -2
  140. package/dist/src/hooks/useGetAccountByAddress.d.ts.map +1 -1
  141. package/dist/src/hooks/useGetAccountByMezoId.d.ts +2 -2
  142. package/dist/src/hooks/useGetAccountByMezoId.d.ts.map +1 -1
  143. package/dist/src/hooks/useGetCurrentAccount.d.ts +16 -2
  144. package/dist/src/hooks/useGetCurrentAccount.d.ts.map +1 -1
  145. package/dist/src/hooks/useGetCurrentAccount.js +25 -3
  146. package/dist/src/hooks/useGetCurrentAccount.js.map +1 -1
  147. package/dist/src/hooks/useGetSession.d.ts +1 -1
  148. package/dist/src/hooks/useGetSession.d.ts.map +1 -1
  149. package/dist/src/hooks/useLinkAccount.d.ts +15 -175
  150. package/dist/src/hooks/useLinkAccount.d.ts.map +1 -1
  151. package/dist/src/hooks/useLinkAccount.js +7 -4
  152. package/dist/src/hooks/useLinkAccount.js.map +1 -1
  153. package/dist/src/hooks/usePortalApiClient.d.ts +2 -0
  154. package/dist/src/hooks/usePortalApiClient.d.ts.map +1 -0
  155. package/dist/src/hooks/usePortalApiClient.js +6 -0
  156. package/dist/src/hooks/usePortalApiClient.js.map +1 -0
  157. package/dist/src/hooks/useRefreshPassport.d.ts +19 -0
  158. package/dist/src/hooks/useRefreshPassport.d.ts.map +1 -0
  159. package/dist/src/hooks/useRefreshPassport.js +44 -0
  160. package/dist/src/hooks/useRefreshPassport.js.map +1 -0
  161. package/dist/src/hooks/useSignInWithDiscord.d.ts +12 -100
  162. package/dist/src/hooks/useSignInWithDiscord.d.ts.map +1 -1
  163. package/dist/src/hooks/useSignInWithDiscord.js +6 -3
  164. package/dist/src/hooks/useSignInWithDiscord.js.map +1 -1
  165. package/dist/src/hooks/useSignInWithWallet.d.ts +12 -100
  166. package/dist/src/hooks/useSignInWithWallet.d.ts.map +1 -1
  167. package/dist/src/hooks/useSignInWithWallet.js +6 -34
  168. package/dist/src/hooks/useSignInWithWallet.js.map +1 -1
  169. package/dist/src/hooks/useSignOut.d.ts +12 -28
  170. package/dist/src/hooks/useSignOut.d.ts.map +1 -1
  171. package/dist/src/hooks/useSignOut.js +7 -4
  172. package/dist/src/hooks/useSignOut.js.map +1 -1
  173. package/dist/src/hooks/useSignUpWithWallet.d.ts +73 -0
  174. package/dist/src/hooks/useSignUpWithWallet.d.ts.map +1 -0
  175. package/dist/src/hooks/useSignUpWithWallet.js +11 -0
  176. package/dist/src/hooks/useSignUpWithWallet.js.map +1 -0
  177. package/dist/src/hooks/useTokensBalances.d.ts +74 -0
  178. package/dist/src/hooks/useTokensBalances.d.ts.map +1 -0
  179. package/dist/src/hooks/useTokensBalances.js +140 -0
  180. package/dist/src/hooks/useTokensBalances.js.map +1 -0
  181. package/dist/src/hooks/useUpdateMezoId.d.ts +30 -136
  182. package/dist/src/hooks/useUpdateMezoId.d.ts.map +1 -1
  183. package/dist/src/hooks/useUpdateMezoId.js +7 -4
  184. package/dist/src/hooks/useUpdateMezoId.js.map +1 -1
  185. package/dist/src/hooks/useWalletAccount.d.ts +13 -0
  186. package/dist/src/hooks/useWalletAccount.d.ts.map +1 -0
  187. package/dist/src/hooks/useWalletAccount.js +29 -0
  188. package/dist/src/hooks/useWalletAccount.js.map +1 -0
  189. package/dist/src/hooks/useWatchTransferEvents.d.ts +5 -0
  190. package/dist/src/hooks/useWatchTransferEvents.d.ts.map +1 -0
  191. package/dist/src/hooks/useWatchTransferEvents.js +63 -0
  192. package/dist/src/hooks/useWatchTransferEvents.js.map +1 -0
  193. package/dist/src/index.d.ts +2 -1
  194. package/dist/src/index.d.ts.map +1 -1
  195. package/dist/src/index.js +2 -1
  196. package/dist/src/index.js.map +1 -1
  197. package/dist/src/lib/contracts/index.d.ts +13 -0
  198. package/dist/src/lib/contracts/index.d.ts.map +1 -0
  199. package/dist/src/lib/contracts/index.js +57 -0
  200. package/dist/src/lib/contracts/index.js.map +1 -0
  201. package/dist/src/provider.d.ts +10 -13
  202. package/dist/src/provider.d.ts.map +1 -1
  203. package/dist/src/provider.js +11 -20
  204. package/dist/src/provider.js.map +1 -1
  205. package/dist/src/stores/dropdownStore.d.ts +12 -0
  206. package/dist/src/stores/dropdownStore.d.ts.map +1 -0
  207. package/dist/src/stores/dropdownStore.js +13 -0
  208. package/dist/src/stores/dropdownStore.js.map +1 -0
  209. package/dist/src/utils/address.d.ts +15 -0
  210. package/dist/src/utils/address.d.ts.map +1 -0
  211. package/dist/src/utils/address.js +37 -0
  212. package/dist/src/utils/address.js.map +1 -0
  213. package/dist/src/utils/address.test.d.ts +2 -0
  214. package/dist/src/utils/address.test.d.ts.map +1 -0
  215. package/dist/src/utils/address.test.js +40 -0
  216. package/dist/src/utils/address.test.js.map +1 -0
  217. package/dist/src/utils/cryptoAssets.d.ts +44 -0
  218. package/dist/src/utils/cryptoAssets.d.ts.map +1 -0
  219. package/dist/src/utils/cryptoAssets.js +131 -0
  220. package/dist/src/utils/cryptoAssets.js.map +1 -0
  221. package/dist/src/utils/cryptoAssets.test.d.ts +2 -0
  222. package/dist/src/utils/cryptoAssets.test.d.ts.map +1 -0
  223. package/dist/src/utils/cryptoAssets.test.js +67 -0
  224. package/dist/src/utils/cryptoAssets.test.js.map +1 -0
  225. package/dist/src/utils/currency.d.ts +14 -0
  226. package/dist/src/utils/currency.d.ts.map +1 -0
  227. package/dist/src/utils/currency.js +27 -0
  228. package/dist/src/utils/currency.js.map +1 -0
  229. package/dist/src/utils/currency.test.d.ts +2 -0
  230. package/dist/src/utils/currency.test.d.ts.map +1 -0
  231. package/dist/src/utils/currency.test.js +34 -0
  232. package/dist/src/utils/currency.test.js.map +1 -0
  233. package/dist/src/utils/numbers.d.ts +58 -0
  234. package/dist/src/utils/numbers.d.ts.map +1 -0
  235. package/dist/src/utils/numbers.js +132 -0
  236. package/dist/src/utils/numbers.js.map +1 -0
  237. package/dist/src/utils/numbers.test.d.ts +2 -0
  238. package/dist/src/utils/numbers.test.d.ts.map +1 -0
  239. package/dist/src/utils/numbers.test.js +170 -0
  240. package/dist/src/utils/numbers.test.js.map +1 -0
  241. package/package.json +13 -5
  242. package/src/api/auth.ts +72 -129
  243. package/src/api/client.ts +78 -0
  244. package/src/api/fetch-error.ts +8 -0
  245. package/src/api/index.ts +2 -0
  246. package/src/api/portal.ts +57 -0
  247. package/src/assets/DefaultAvatar.tsx +74 -0
  248. package/src/components/Dropdown/ConnectedTrigger.tsx +76 -0
  249. package/src/components/Dropdown/Content.tsx +98 -0
  250. package/src/components/Dropdown/DisconnectedTrigger.tsx +36 -0
  251. package/src/components/Dropdown/Dropdown.tsx +148 -0
  252. package/src/components/Dropdown/ListingItem.tsx +80 -0
  253. package/src/components/Dropdown/NestedViewLayout.tsx +75 -0
  254. package/src/components/Dropdown/README.md +41 -0
  255. package/src/components/Dropdown/Receive/Receive.tsx +119 -0
  256. package/src/components/Dropdown/Root/AccountAddressActions.tsx +101 -0
  257. package/src/components/Dropdown/Root/AccountAssetItem.tsx +26 -0
  258. package/src/components/Dropdown/Root/AccountBalance.tsx +30 -0
  259. package/src/components/Dropdown/Root/AccountOtherAssets.tsx +68 -0
  260. package/src/components/Dropdown/Root/Root.tsx +147 -0
  261. package/src/components/Dropdown/Root/WalletAddress.tsx +128 -0
  262. package/src/components/Dropdown/Root/WelcomeBlock.tsx +91 -0
  263. package/src/components/Dropdown/TestnetTopBanner.tsx +32 -0
  264. package/src/components/Dropdown/index.ts +2 -0
  265. package/src/components/index.ts +1 -0
  266. package/src/config.ts +27 -6
  267. package/src/constants.ts +8 -4
  268. package/src/hooks/constants.ts +1 -0
  269. package/src/hooks/index.ts +7 -1
  270. package/src/hooks/useAssetsConversionRates.ts +32 -0
  271. package/src/hooks/useAuthenticateWithWallet.ts +102 -0
  272. package/src/hooks/useBorrowData.ts +143 -0
  273. package/src/hooks/useCreateAccount.ts +19 -5
  274. package/src/hooks/useCreateSession.ts +22 -9
  275. package/src/hooks/useDropdownData.ts +139 -0
  276. package/src/hooks/useEnsureNoSessionAndFetchNonce.ts +1 -0
  277. package/src/hooks/useGetAccountByAddress.ts +2 -2
  278. package/src/hooks/useGetAccountByMezoId.ts +2 -2
  279. package/src/hooks/useGetCurrentAccount.ts +46 -6
  280. package/src/hooks/useGetSession.ts +1 -1
  281. package/src/hooks/useLinkAccount.ts +18 -5
  282. package/src/hooks/usePortalApiClient.ts +6 -0
  283. package/src/hooks/useRefreshPassport.ts +56 -0
  284. package/src/hooks/useSignInWithDiscord.ts +14 -5
  285. package/src/hooks/useSignInWithWallet.ts +14 -42
  286. package/src/hooks/useSignOut.ts +18 -4
  287. package/src/hooks/useSignUpWithWallet.ts +21 -0
  288. package/src/hooks/useTokensBalances.ts +187 -0
  289. package/src/hooks/useUpdateMezoId.ts +19 -4
  290. package/src/hooks/useWalletAccount.ts +55 -0
  291. package/src/hooks/useWatchTransferEvents.ts +74 -0
  292. package/src/index.ts +9 -1
  293. package/src/lib/contracts/index.ts +88 -0
  294. package/src/provider.ts +30 -37
  295. package/src/stores/dropdownStore.ts +20 -0
  296. package/src/utils/address.test.ts +48 -0
  297. package/src/utils/address.ts +45 -0
  298. package/src/utils/cryptoAssets.test.ts +79 -0
  299. package/src/utils/cryptoAssets.ts +173 -0
  300. package/src/utils/currency.test.ts +38 -0
  301. package/src/utils/currency.ts +32 -0
  302. package/src/utils/numbers.test.ts +220 -0
  303. package/src/utils/numbers.ts +188 -0
@@ -0,0 +1,132 @@
1
+ /**
2
+ * Formats a number or bigint into compact 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 formatNumberToCompactString(value, decimals = 4) {
8
+ return value.toLocaleString("en-US", {
9
+ notation: "compact",
10
+ compactDisplay: "short",
11
+ maximumFractionDigits: decimals,
12
+ });
13
+ }
14
+ /**
15
+ * Formats a number or string into a locale-sensitive string with the desired number of decimals.
16
+ * @param value The number or string to format.
17
+ * @param desiredDecimals The number of decimal places to include.
18
+ * @returns The formatted number as a string.
19
+ * @see https://github.com/thesis/acre/blob/2078d339f4ddce79e69c78529c9d72910dd2640a/dapp/src/utils/numbersUtils.ts#L5-L19
20
+ */
21
+ export function formatNumberToLocaleString(value, desiredDecimals = 0) {
22
+ const number = typeof value === "number" ? value : parseFloat(value);
23
+ if (number === 0 && desiredDecimals === 0)
24
+ return "0";
25
+ if (number === 0)
26
+ return `0.${"0".repeat(desiredDecimals)}`;
27
+ return number.toLocaleString("en-US", {
28
+ minimumFractionDigits: desiredDecimals,
29
+ maximumFractionDigits: desiredDecimals,
30
+ });
31
+ }
32
+ /**
33
+ * Returns a number rounded up to the desired number of decimals.
34
+ * @param amount The number to round up.
35
+ * @param desiredDecimals The number of decimals to round up to.
36
+ * @returns The rounded up number.
37
+ * @see https://github.com/thesis/acre/blob/2078d339f4ddce79e69c78529c9d72910dd2640a/dapp/src/utils/numbersUtils.ts#L1-L3
38
+ */
39
+ export function roundUpNumber(amount, desiredDecimals = 2) {
40
+ return Math.ceil(amount * 10 ** desiredDecimals) / 10 ** desiredDecimals;
41
+ }
42
+ /**
43
+ * Convert a fixed point bigint with precision `fixedPointDecimals` to a
44
+ * floating point number truncated to `desiredDecimals`.
45
+ * @param fixedPoint The fixed point bigint to convert.
46
+ * @param fixedPointDecimals The number of decimals in the fixed point bigint.
47
+ * @param desiredDecimals The number of decimals in the output number.
48
+ * @returns The floating point number truncated to `desiredDecimals`.
49
+ * @see https://github.com/tahowallet/extension/blob/f6b26b7cb4a0a0278b1fccc6319626638f1a6dfa/background/lib/fixed-point.ts#L216-L239
50
+ */
51
+ export function fromFixedPoint(fixedPoint, fixedPointDecimals, desiredDecimals = 4) {
52
+ const fixedPointDesiredDecimalsAmount = fixedPoint /
53
+ 10n ** BigInt(Math.max(1, fixedPointDecimals - desiredDecimals));
54
+ const formattedAmount = Number(fixedPointDesiredDecimalsAmount) /
55
+ 10 ** Math.min(desiredDecimals, fixedPointDecimals);
56
+ return formattedAmount;
57
+ }
58
+ /**
59
+ * Display a token amount correctly with desired decimals.
60
+ * The function returns a string with a language-sensitive representation of this number.
61
+ * @param amount The fixed point number to convert.
62
+ * @param decimals The number of decimals in the fixed point number.
63
+ * @param desiredDecimals The number of decimals to display.
64
+ * @param withRoundUp Whether to round up the number.
65
+ * @returns The formatted number as a string.
66
+ * @see https://github.com/thesis/acre/blob/2078d339f4ddce79e69c78529c9d72910dd2640a/dapp/src/utils/numbersUtils.ts#L57-L87
67
+ */
68
+ export const fromFixedPointToString = (amount, decimals = 18, desiredDecimals = 4, withRoundUp = false) => {
69
+ const fixedPoint = BigInt(amount);
70
+ if (fixedPoint === 0n) {
71
+ return `0.${"0".repeat(desiredDecimals)}`;
72
+ }
73
+ const formattedAmount = fromFixedPoint(fixedPoint, decimals,
74
+ // To round the amount up, let's increase the precision by one.
75
+ // The desired decimal numbers will be set later anyway.
76
+ withRoundUp ? desiredDecimals + 1 : desiredDecimals);
77
+ const minAmountToDisplay = 1 / 10 ** Math.min(desiredDecimals, decimals);
78
+ if (minAmountToDisplay > formattedAmount) {
79
+ return `<0.${"0".repeat(desiredDecimals - 1)}1`;
80
+ }
81
+ const finalFormattedAmount = withRoundUp
82
+ ? Math.ceil(formattedAmount * 10 ** desiredDecimals) / 10 ** desiredDecimals
83
+ : formattedAmount;
84
+ return formatNumberToLocaleString(finalFormattedAmount, desiredDecimals);
85
+ };
86
+ /**
87
+ * Converts a floating-point number or string to a bigint representation
88
+ * scaled by the specified number of decimal places (default: 18).
89
+ *
90
+ * This function avoids floating-point precision issues by manually
91
+ * parsing and scaling the number.
92
+ *
93
+ * @param amount - A number or string containing a floating point number.
94
+ * @param decimals - Number of decimals to scale to (default: 18).
95
+ * @returns A bigint representation scaled to the given decimal places.
96
+ * @see https://github.com/thesis/mezo-portal/blob/3bd4f39cb77c39948af2886a6ce7415a0c60307a/dapp/src/shared/utils/numbers.ts#L55
97
+ */
98
+ export const fromFloatToBigInt = (amount, decimals = 18) => {
99
+ const FLOATING_POINT_REGEX = /^-?\d{1,3}(,\d{3})*(\.\d+)?$|^-?\d+(\.\d+)?$/;
100
+ const parseToFixedPointNumber = (floatingPointString) => {
101
+ if (!floatingPointString.match(FLOATING_POINT_REGEX)) {
102
+ return undefined;
103
+ }
104
+ const [whole, decimalsPart, ...extra] = floatingPointString.split(".");
105
+ if (extra.length > 0)
106
+ return undefined;
107
+ const noThousandsSeparatorWhole = whole.replace(/,/g, "");
108
+ const setDecimals = decimalsPart ?? "";
109
+ try {
110
+ return {
111
+ amount: BigInt([noThousandsSeparatorWhole, setDecimals].join("")),
112
+ decimals: setDecimals.length,
113
+ };
114
+ }
115
+ catch {
116
+ return undefined;
117
+ }
118
+ };
119
+ const convertFixedPoint = (fixedPoint, fixedPointDecimals, targetDecimals) => {
120
+ if (fixedPointDecimals >= targetDecimals) {
121
+ return fixedPoint / 10n ** BigInt(fixedPointDecimals - targetDecimals);
122
+ }
123
+ return fixedPoint * 10n ** BigInt(targetDecimals - fixedPointDecimals);
124
+ };
125
+ const parsedAmount = amount.toString();
126
+ const fixedPointAmount = parseToFixedPointNumber(parsedAmount);
127
+ if (typeof fixedPointAmount === "undefined") {
128
+ return 0n;
129
+ }
130
+ return convertFixedPoint(fixedPointAmount.amount, fixedPointAmount.decimals, decimals);
131
+ };
132
+ //# 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,2BAA2B,CACzC,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,0BAA0B,CACxC,KAAsB,EACtB,eAAe,GAAG,CAAC;IAEnB,MAAM,MAAM,GAAG,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,UAAU,CAAC,KAAK,CAAC,CAAA;IAEpE,IAAI,MAAM,KAAK,CAAC,IAAI,eAAe,KAAK,CAAC;QAAE,OAAO,GAAG,CAAA;IAErD,IAAI,MAAM,KAAK,CAAC;QAAE,OAAO,KAAK,GAAG,CAAC,MAAM,CAAC,eAAe,CAAC,EAAE,CAAA;IAE3D,OAAO,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE;QACpC,qBAAqB,EAAE,eAAe;QACtC,qBAAqB,EAAE,eAAe;KACvC,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,MA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CAAA;IACtC,MAAM,gBAAgB,GAAG,uBAAuB,CAAC,YAAY,CAAC,CAAA;IAE9D,IAAI,OAAO,gBAAgB,KAAK,WAAW,EAAE,CAAC;QAC5C,OAAO,EAAE,CAAA;IACX,CAAC;IAED,OAAO,iBAAiB,CACtB,gBAAgB,CAAC,MAAM,EACvB,gBAAgB,CAAC,QAAQ,EACzB,QAAQ,CACT,CAAA;AACH,CAAC,CAAA"}
@@ -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,170 @@
1
+ import { formatNumberToCompactString, fromFixedPoint, roundUpNumber, formatNumberToLocaleString, fromFixedPointToString, } from "./numbers";
2
+ describe("formatNumberToCompactString", () => {
3
+ it("formats number less than 1000 with decimals", () => {
4
+ expect(formatNumberToCompactString(123.456789, 2)).toBe("123.46");
5
+ });
6
+ it("formats thousands with K", () => {
7
+ expect(formatNumberToCompactString(12_345, 2)).toBe("12.35K");
8
+ });
9
+ it("formats millions with M", () => {
10
+ expect(formatNumberToCompactString(12_345_678, 2)).toBe("12.35M");
11
+ });
12
+ it("formats billions with B", () => {
13
+ expect(formatNumberToCompactString(12_345_678_901, 2)).toBe("12.35B");
14
+ });
15
+ it("formats trillions with T", () => {
16
+ expect(formatNumberToCompactString(12_345_678_901_234, 2)).toBe("12.35T");
17
+ });
18
+ it("formats bigint as readable number", () => {
19
+ expect(formatNumberToCompactString(1234567890123456789n, 2)).toBe("1,234,567.89T");
20
+ });
21
+ it("caps at > 1000T for huge numbers", () => {
22
+ const hugeNumber = 10 ** 18; // 1e18
23
+ expect(formatNumberToCompactString(hugeNumber, 2)).toBe("1,000,000T");
24
+ });
25
+ it("formats numbers with trailing zeros correctly", () => {
26
+ expect(formatNumberToCompactString(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)).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
+ describe("formatNumberToLocaleString", () => {
55
+ it("formats zero with 0 decimals", () => {
56
+ expect(formatNumberToLocaleString(0, 0)).toBe("0");
57
+ });
58
+ it("formats zero with >0 decimals", () => {
59
+ expect(formatNumberToLocaleString(0, 2)).toBe("0.00");
60
+ expect(formatNumberToLocaleString(0, 4)).toBe("0.0000");
61
+ });
62
+ it("formats small decimal number", () => {
63
+ expect(formatNumberToLocaleString(0.123456, 2)).toBe("0.12");
64
+ expect(formatNumberToLocaleString(0.123456, 4)).toBe("0.1235");
65
+ });
66
+ it("formats number just above 1", () => {
67
+ expect(formatNumberToLocaleString(1.000001, 3)).toBe("1.000");
68
+ expect(formatNumberToLocaleString(1.234567, 2)).toBe("1.23");
69
+ });
70
+ it("formats whole number", () => {
71
+ expect(formatNumberToLocaleString(1234, 0)).toBe("1,234");
72
+ expect(formatNumberToLocaleString(1000, 2)).toBe("1,000.00");
73
+ });
74
+ it("formats number with trailing decimal zeroes", () => {
75
+ expect(formatNumberToLocaleString(10.1, 3)).toBe("10.100");
76
+ expect(formatNumberToLocaleString(5.0, 2)).toBe("5.00");
77
+ });
78
+ it("formats string input", () => {
79
+ expect(formatNumberToLocaleString("123.456", 2)).toBe("123.46");
80
+ expect(formatNumberToLocaleString("0.00123", 4)).toBe("0.0012");
81
+ });
82
+ it("formats negative number", () => {
83
+ expect(formatNumberToLocaleString(-123.456, 2)).toBe("-123.46");
84
+ expect(formatNumberToLocaleString("-0.00999", 3)).toBe("-0.010");
85
+ });
86
+ it("formats large number with commas", () => {
87
+ expect(formatNumberToLocaleString(12345678.9, 2)).toBe("12,345,678.90");
88
+ });
89
+ it("handles max decimals safely", () => {
90
+ expect(formatNumberToLocaleString(1.123456789, 8)).toBe("1.12345679");
91
+ expect(formatNumberToLocaleString("0.000000000000000001", 18)).toBe("0.000000000000000001");
92
+ });
93
+ it("defaults to 0 decimals if not passed", () => {
94
+ expect(formatNumberToLocaleString(1234.567)).toBe("1,235"); // rounds up by default
95
+ });
96
+ });
97
+ describe("fromFixedPointToString", () => {
98
+ const toFixedPoint = (num) => BigInt(Math.round(num * 1e18));
99
+ const cases = [
100
+ { input: 2.0, expected: "2.000" },
101
+ { input: 2.009, expected: "2.009" },
102
+ { input: 2.01, expected: "2.010" },
103
+ { input: 2.015, expected: "2.016" },
104
+ { input: 2.05, expected: "2.050" },
105
+ { input: 2.09, expected: "2.090" },
106
+ ];
107
+ describe("withRoundUp: true", () => {
108
+ cases.forEach(({ input, expected }) => {
109
+ it(`formats ${input} → ${expected}`, () => {
110
+ const fixed = toFixedPoint(input);
111
+ expect(fromFixedPointToString(fixed, 18, 3, true)).toBe(expected);
112
+ });
113
+ });
114
+ });
115
+ describe("withRoundUp: false", () => {
116
+ const noRoundCases = [
117
+ { input: 2.0099, expected: "2.009" },
118
+ { input: 2.0159, expected: "2.015" },
119
+ { input: 2.0101, expected: "2.010" },
120
+ { input: 2.0199, expected: "2.019" },
121
+ ];
122
+ noRoundCases.forEach(({ input, expected }) => {
123
+ it(`formats ${input} → ${expected} (no rounding)`, () => {
124
+ const fixed = toFixedPoint(input);
125
+ expect(fromFixedPointToString(fixed, 18, 3, false)).toBe(expected);
126
+ });
127
+ });
128
+ });
129
+ describe("zero handling", () => {
130
+ it("formats 0 → 0.000 (3 decimals)", () => {
131
+ expect(fromFixedPointToString(0n, 18, 3)).toBe("0.000");
132
+ });
133
+ it("formats 0 → 0.0000 (4 decimals)", () => {
134
+ expect(fromFixedPointToString(0n, 18, 4)).toBe("0.0000");
135
+ });
136
+ });
137
+ describe("very small numbers below display threshold", () => {
138
+ it("returns <0.0001 for small number (3 decimals)", () => {
139
+ const small = BigInt(123); // ~1.23e-16
140
+ expect(fromFixedPointToString(small, 18, 4)).toBe("<0.0001");
141
+ });
142
+ it("returns <0.01 for small number with only 2 decimals", () => {
143
+ const tiny = BigInt(9999999999999); // <0.01
144
+ expect(fromFixedPointToString(tiny, 18, 2)).toBe("<0.01");
145
+ });
146
+ });
147
+ describe("input types", () => {
148
+ it("accepts bigint", () => {
149
+ const amount = 2100000000000000000n;
150
+ expect(fromFixedPointToString(amount, 18, 3)).toBe("2.100");
151
+ });
152
+ it("accepts string", () => {
153
+ expect(fromFixedPointToString("2000000000000000000", 18, 3)).toBe("2.000");
154
+ });
155
+ it("accepts number", () => {
156
+ expect(fromFixedPointToString(Number("1000000000000000000"), 18, 3)).toBe("1.000");
157
+ });
158
+ });
159
+ describe("high values", () => {
160
+ it("formats 1_000_000.123456789 correctly", () => {
161
+ const big = toFixedPoint(1_000_000.123456789);
162
+ expect(fromFixedPointToString(big, 18, 6)).toBe("1,000,000.123456");
163
+ });
164
+ it("formats 1e12 ETH", () => {
165
+ const big = BigInt("1000000000000000000000000000000");
166
+ expect(fromFixedPointToString(big, 18, 2)).toBe("1,000,000,000,000.00");
167
+ });
168
+ });
169
+ });
170
+ //# sourceMappingURL=numbers.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"numbers.test.js","sourceRoot":"","sources":["../../../src/ut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CAAC,OAAO,CAAC,CAAA;QAC7D,CAAC,CAAC,CAAA;QAEF,EAAE,CAAC,gBAAgB,EAAE,GAAG,EAAE;YACxB,MAAM,CAAC,sBAAsB,CAAC,qBAAqB,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;QAC5E,CAAC,CAAC,CAAA;QAEF,EAAE,CAAC,gBAAgB,EAAE,GAAG,EAAE;YACxB,MAAM,CAAC,sBAAsB,CAAC,MAAM,CAAC,qBAAqB,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CACvE,OAAO,CACR,CAAA;QACH,CAAC,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;IAEF,QAAQ,CAAC,aAAa,EAAE,GAAG,EAAE;QAC3B,EAAE,CAAC,uCAAuC,EAAE,GAAG,EAAE;YAC/C,MAAM,GAAG,GAAG,YAAY,CAAC,mBAAmB,CAAC,CAAA;YAC7C,MAAM,CAAC,sBAAsB,CAAC,GAAG,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,kBAAkB,CAAC,CAAA;QACrE,CAAC,CAAC,CAAA;QAEF,EAAE,CAAC,kBAAkB,EAAE,GAAG,EAAE;YAC1B,MAAM,GAAG,GAAG,MAAM,CAAC,iCAAiC,CAAC,CAAA;YACrD,MAAM,CAAC,sBAAsB,CAAC,GAAG,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,sBAAsB,CAAC,CAAA;QACzE,CAAC,CAAC,CAAA;IACJ,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.4",
3
+ "version": "0.4.0-dev.41",
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,21 +24,28 @@
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
- "typescript": "^5.4.3"
37
+ "typescript": "^5.8.2"
37
38
  },
38
39
  "dependencies": {
39
- "@mezo-org/orangekit": "1.0.0-beta.40-dev.0",
40
- "@mezo-org/sign-in-with-wallet": "1.0.0-beta.8"
40
+ "@mezo-org/mezo-clay": "0.1.0-dev.27",
41
+ "@mezo-org/mezod-contracts": "^1.0.0",
42
+ "@mezo-org/musd-contracts": "^1.0.1",
43
+ "@mezo-org/orangekit": "1.0.0-beta.40-dev.3",
44
+ "@mezo-org/sign-in-with-wallet": "1.0.0-beta.8",
45
+ "qrcode.react": "^4.2.0",
46
+ "styletron-engine-monolithic": "^1.0.0",
47
+ "usehooks-ts": "^3.1.1",
48
+ "zustand": "^5.0.3"
41
49
  },
42
50
  "peerDependencies": {
43
51
  "@rainbow-me/rainbowkit": "2.0.2",
package/src/api/auth.ts CHANGED
@@ -1,151 +1,88 @@
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
 
13
- type AuthUser = {
12
+ export type LinkedProviderAccount = {
13
+ type: AuthProvider
14
+ name: string
14
15
  id: string
15
- app_metadata: {
16
- provider: PassportIdentityProvider
17
- providers: PassportIdentityProvider[]
18
- }
19
- user_metadata: AccountPublicData
16
+ avatarUrl: string
20
17
  }
21
18
 
22
- type AuthProvider = "discord"
23
-
24
- export type LinkedAccount =
25
- | {
26
- type: "wallet"
27
- btcAddress?: string
28
- evmAddress: string
29
- }
30
- | {
31
- type: AuthProvider
32
- name: string
33
- id: string
34
- avatarUrl: string
35
- }
19
+ export type LinkedAccount = LinkedWalletAccount | LinkedProviderAccount
36
20
 
37
21
  type Account = {
38
22
  mezoId: string
39
23
  hasModifiedMezoId: boolean
40
24
  linkedAccounts: LinkedAccount[]
41
- authUser: AuthUser
42
25
  }
43
26
 
44
- type Session = {
27
+ export type Session = {
45
28
  expiresIn: number
46
29
  expiresAt: number
47
30
  sessionLinks: {
48
31
  _links: {
49
- account: "/session/account"
32
+ account: string
50
33
  }
51
34
  }
52
35
  }
53
36
 
54
- type AuthenticationProviderRedirectResponse = {
37
+ export type AuthenticationProviderRedirectResponse = {
55
38
  redirectTo: string
56
39
  }
57
40
 
58
- type WalletAccountCredentials = {
59
- type: "wallet"
41
+ type SignatureData = {
60
42
  message: string
61
43
  signature: string
62
44
  }
63
45
 
46
+ // Request and Response types
47
+ export type GetSessionResponse = Session | { nonce: string }
48
+
64
49
  export type CreateSessionRequest =
65
- | WalletAccountCredentials
50
+ | ({ type: "wallet" } & SignatureData)
66
51
  | { type: "discord" }
52
+ export type CreateSessionResponse =
53
+ | Session
54
+ | AuthenticationProviderRedirectResponse
67
55
 
68
- export type LinkAccountRequest = CreateSessionRequest
69
-
70
- export type CreateAccountRequest = WalletAccountCredentials
71
-
72
- export type GetSessionResponse = Session | { nonce: string }
56
+ export type DeleteSessionResponse = { message: string }
73
57
 
74
58
  export type GetCurrentAccountResponse = Partial<Account> | null
75
59
 
76
60
  export type GetAccountByMezoIdOrAddressResponse = {
77
61
  mezoId?: string
78
- linkedAccounts?: LinkedAccount[]
62
+ linkedAccounts?: Extract<LinkedAccount, { type: "wallet" }>[]
79
63
  }
80
64
 
81
- export const API_ENDPOINTS_BY_ENV = {
82
- mainnet: "https://api.mezo.org",
83
- testnet: "https://api.test.mezo.org",
84
- }
65
+ export type LinkAccountRequest = CreateSessionRequest
66
+ export type LinkAccountResponse =
67
+ | Account
68
+ | AuthenticationProviderRedirectResponse
85
69
 
86
- export class AuthApiClient {
87
- private apiUrl: string
70
+ export type CreateAccountRequest = { type: "wallet" } & SignatureData
71
+ export type CreateAccountResponse = Account
88
72
 
89
- constructor(apiUrl?: string) {
90
- this.apiUrl = apiUrl ?? API_ENDPOINTS_BY_ENV.mainnet
91
- }
73
+ export type UpdateMezoIdResponse = Account
92
74
 
93
- /**
94
- * The request handler for the Passport API. It accepts an API endpoint and
95
- * request options.
96
- * @dev Generic type `D` is a type of returned object from the function
97
- * @dev WARNING: This function hardcodes `credentials: "include"`, meaning
98
- * cookies and authentication headers will be sent with every request. Ensure
99
- * this is safe if you plan to use it in other contexts than auth api worker.
100
- * @param endpoint - The API endpoint to request. Should start with
101
- * forward slash ("/")
102
- * @param options - The request options, the `RequestInit` type with
103
- * additional type-safe properties
104
- * @returns The promise of the API response
105
- */
106
- protected async handleRequest<D>(
107
- endpoint: `/${string}`,
108
- options: RequestHandlerOptions,
109
- ): Promise<D> {
110
- const {
111
- method,
112
- headers,
113
- body,
114
- queryParams = {},
115
- ...restOptions
116
- } = options || {}
117
-
118
- // If last character in apiUrl is forward slash we are removing it
119
- const authApiUrlRoute = this.apiUrl.replace(/\/$/, "")
120
- const url = new URL(`${authApiUrlRoute}${endpoint}`)
121
-
122
- // Removes falsy values (e.g. null, undefined etc.) from query params and
123
- // appends remaining ones to the URL
124
- Object.entries(queryParams)
125
- .filter(([, value]) => value) // Remove falsy values
126
- .forEach(([key, value]) => url.searchParams.append(key, value!))
127
-
128
- const response = await fetch(url, {
129
- method,
130
- credentials: "include",
131
- headers: {
132
- "Content-Type": "application/json",
133
- ...headers,
134
- },
135
- body: body ? JSON.stringify(body) : undefined,
136
- ...restOptions,
137
- })
138
-
139
- const data = await response.json()
140
-
141
- if (!response.ok || data?.error) {
142
- const error =
143
- data?.error ||
144
- `An error occurred when handling API request: ${JSON.stringify(data)}`
145
- throw new Error(`Error [${response.status}]: ${error}`)
146
- }
75
+ export const AUTH_API_ENDPOINTS_BY_ENV = {
76
+ mainnet: "https://api.mezo.org",
77
+ testnet: "https://api.test.mezo.org",
78
+ }
147
79
 
148
- return data as D
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])
149
86
  }
150
87
 
151
88
  async getSession(code?: string) {
@@ -158,21 +95,16 @@ export class AuthApiClient {
158
95
  }
159
96
 
160
97
  async createSession(createSessionRequest: CreateSessionRequest) {
161
- const data = await this.handleRequest<Session | { redirectTo: string }>(
162
- "/session",
163
- {
164
- method: "POST",
165
- body: createSessionRequest,
166
- },
167
- )
98
+ const data = await this.handleRequest<CreateSessionResponse>("/session", {
99
+ method: "POST",
100
+ body: createSessionRequest,
101
+ })
168
102
 
169
103
  return data
170
104
  }
171
105
 
172
106
  async deleteSession() {
173
- const data = await this.handleRequest<{
174
- message: string
175
- }>("/session", {
107
+ const data = await this.handleRequest<DeleteSessionResponse>("/session", {
176
108
  method: "DELETE",
177
109
  })
178
110
 
@@ -191,29 +123,40 @@ export class AuthApiClient {
191
123
  }
192
124
 
193
125
  async getAccountByMezoIdOrAddress(mezoIdOrAddress: string) {
194
- const data = await this.handleRequest<GetAccountByMezoIdOrAddressResponse>(
195
- `/accounts/${mezoIdOrAddress}`,
196
- {
197
- method: "GET",
198
- },
199
- )
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
+ }
200
142
 
201
143
  return data
202
144
  }
203
145
 
204
146
  async linkAccount(linkAccountRequest: LinkAccountRequest) {
205
- const data = await this.handleRequest<
206
- Account | AuthenticationProviderRedirectResponse
207
- >("/session/account/linked-accounts", {
208
- method: "POST",
209
- body: linkAccountRequest,
210
- })
147
+ const data = await this.handleRequest<LinkAccountResponse>(
148
+ "/session/account/linked-accounts",
149
+ {
150
+ method: "POST",
151
+ body: linkAccountRequest,
152
+ },
153
+ )
211
154
 
212
155
  return data
213
156
  }
214
157
 
215
158
  async createAccount(createAccountRequest: CreateAccountRequest) {
216
- const data = await this.handleRequest<Account>("/accounts", {
159
+ const data = await this.handleRequest<CreateAccountResponse>("/accounts", {
217
160
  method: "POST",
218
161
  body: createAccountRequest,
219
162
  })
@@ -222,7 +165,7 @@ export class AuthApiClient {
222
165
  }
223
166
 
224
167
  async updateMezoId(newMezoId: string) {
225
- const data = await this.handleRequest<Partial<Account> | null>(
168
+ const data = await this.handleRequest<UpdateMezoIdResponse>(
226
169
  "/session/account",
227
170
  {
228
171
  method: "PATCH",