@mezo-org/passport 0.4.0-dev.0 → 0.4.0-dev.10

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 (252) hide show
  1. package/dist/src/api/auth.d.ts +78 -0
  2. package/dist/src/api/auth.d.ts.map +1 -0
  3. package/dist/src/api/auth.js +77 -0
  4. package/dist/src/api/auth.js.map +1 -0
  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 +7 -0
  66. package/dist/src/hooks/constants.d.ts.map +1 -0
  67. package/dist/src/hooks/constants.js +7 -0
  68. package/dist/src/hooks/constants.js.map +1 -0
  69. package/dist/src/hooks/index.d.ts +10 -0
  70. package/dist/src/hooks/index.d.ts.map +1 -1
  71. package/dist/src/hooks/index.js +10 -0
  72. package/dist/src/hooks/index.js.map +1 -1
  73. package/dist/src/hooks/useAssetsUSDConversion.d.ts +8 -0
  74. package/dist/src/hooks/useAssetsUSDConversion.d.ts.map +1 -0
  75. package/dist/src/hooks/useAssetsUSDConversion.js +21 -0
  76. package/dist/src/hooks/useAssetsUSDConversion.js.map +1 -0
  77. package/dist/src/hooks/useAuthApiClient.d.ts +2 -0
  78. package/dist/src/hooks/useAuthApiClient.d.ts.map +1 -0
  79. package/dist/src/hooks/useAuthApiClient.js +6 -0
  80. package/dist/src/hooks/useAuthApiClient.js.map +1 -0
  81. package/dist/src/hooks/useAuthenticateWithWallet.d.ts +73 -0
  82. package/dist/src/hooks/useAuthenticateWithWallet.d.ts.map +1 -0
  83. package/dist/src/hooks/useAuthenticateWithWallet.js +69 -0
  84. package/dist/src/hooks/useAuthenticateWithWallet.js.map +1 -0
  85. package/dist/src/hooks/useCreateAccount.d.ts +108 -0
  86. package/dist/src/hooks/useCreateAccount.d.ts.map +1 -0
  87. package/dist/src/hooks/useCreateAccount.js +21 -0
  88. package/dist/src/hooks/useCreateAccount.js.map +1 -0
  89. package/dist/src/hooks/useCreateSession.d.ts +76 -0
  90. package/dist/src/hooks/useCreateSession.d.ts.map +1 -0
  91. package/dist/src/hooks/useCreateSession.js +29 -0
  92. package/dist/src/hooks/useCreateSession.js.map +1 -0
  93. package/dist/src/hooks/useDropdownData.d.ts +45 -0
  94. package/dist/src/hooks/useDropdownData.d.ts.map +1 -0
  95. package/dist/src/hooks/useDropdownData.js +74 -0
  96. package/dist/src/hooks/useDropdownData.js.map +1 -0
  97. package/dist/src/hooks/useEnsureNoSessionAndFetchNonce.d.ts +5 -0
  98. package/dist/src/hooks/useEnsureNoSessionAndFetchNonce.d.ts.map +1 -0
  99. package/dist/src/hooks/useEnsureNoSessionAndFetchNonce.js +34 -0
  100. package/dist/src/hooks/useEnsureNoSessionAndFetchNonce.js.map +1 -0
  101. package/dist/src/hooks/useGetAccountByAddress.d.ts +4 -0
  102. package/dist/src/hooks/useGetAccountByAddress.d.ts.map +1 -0
  103. package/dist/src/hooks/useGetAccountByAddress.js +17 -0
  104. package/dist/src/hooks/useGetAccountByAddress.js.map +1 -0
  105. package/dist/src/hooks/useGetAccountByMezoId.d.ts +4 -0
  106. package/dist/src/hooks/useGetAccountByMezoId.d.ts.map +1 -0
  107. package/dist/src/hooks/useGetAccountByMezoId.js +17 -0
  108. package/dist/src/hooks/useGetAccountByMezoId.js.map +1 -0
  109. package/dist/src/hooks/useGetCurrentAccount.d.ts +18 -0
  110. package/dist/src/hooks/useGetCurrentAccount.d.ts.map +1 -0
  111. package/dist/src/hooks/useGetCurrentAccount.js +37 -0
  112. package/dist/src/hooks/useGetCurrentAccount.js.map +1 -0
  113. package/dist/src/hooks/useGetSession.d.ts +4 -0
  114. package/dist/src/hooks/useGetSession.d.ts.map +1 -0
  115. package/dist/src/hooks/useGetSession.js +15 -0
  116. package/dist/src/hooks/useGetSession.js.map +1 -0
  117. package/dist/src/hooks/useLinkAccount.d.ts +72 -0
  118. package/dist/src/hooks/useLinkAccount.d.ts.map +1 -0
  119. package/dist/src/hooks/useLinkAccount.js +19 -0
  120. package/dist/src/hooks/useLinkAccount.js.map +1 -0
  121. package/dist/src/hooks/usePassportContext.d.ts +3 -0
  122. package/dist/src/hooks/usePassportContext.d.ts.map +1 -0
  123. package/dist/src/hooks/usePassportContext.js +10 -0
  124. package/dist/src/hooks/usePassportContext.js.map +1 -0
  125. package/dist/src/hooks/usePortalApiClient.d.ts +2 -0
  126. package/dist/src/hooks/usePortalApiClient.d.ts.map +1 -0
  127. package/dist/src/hooks/usePortalApiClient.js +6 -0
  128. package/dist/src/hooks/usePortalApiClient.js.map +1 -0
  129. package/dist/src/hooks/useSignInWithDiscord.d.ts +73 -0
  130. package/dist/src/hooks/useSignInWithDiscord.d.ts.map +1 -0
  131. package/dist/src/hooks/useSignInWithDiscord.js +25 -0
  132. package/dist/src/hooks/useSignInWithDiscord.js.map +1 -0
  133. package/dist/src/hooks/useSignInWithWallet.d.ts +73 -0
  134. package/dist/src/hooks/useSignInWithWallet.d.ts.map +1 -0
  135. package/dist/src/hooks/useSignInWithWallet.js +11 -0
  136. package/dist/src/hooks/useSignInWithWallet.js.map +1 -0
  137. package/dist/src/hooks/useSignOut.d.ts +72 -0
  138. package/dist/src/hooks/useSignOut.d.ts.map +1 -0
  139. package/dist/src/hooks/useSignOut.js +22 -0
  140. package/dist/src/hooks/useSignOut.js.map +1 -0
  141. package/dist/src/hooks/useSignUpWithWallet.d.ts +73 -0
  142. package/dist/src/hooks/useSignUpWithWallet.d.ts.map +1 -0
  143. package/dist/src/hooks/useSignUpWithWallet.js +11 -0
  144. package/dist/src/hooks/useSignUpWithWallet.js.map +1 -0
  145. package/dist/src/hooks/useUpdateMezoId.d.ts +108 -0
  146. package/dist/src/hooks/useUpdateMezoId.d.ts.map +1 -0
  147. package/dist/src/hooks/useUpdateMezoId.js +19 -0
  148. package/dist/src/hooks/useUpdateMezoId.js.map +1 -0
  149. package/dist/src/hooks/useWalletAccount.d.ts +12 -0
  150. package/dist/src/hooks/useWalletAccount.d.ts.map +1 -0
  151. package/dist/src/hooks/useWalletAccount.js +28 -0
  152. package/dist/src/hooks/useWalletAccount.js.map +1 -0
  153. package/dist/src/index.d.ts +2 -0
  154. package/dist/src/index.d.ts.map +1 -1
  155. package/dist/src/index.js +2 -0
  156. package/dist/src/index.js.map +1 -1
  157. package/dist/src/provider.d.ts +17 -0
  158. package/dist/src/provider.d.ts.map +1 -0
  159. package/dist/src/provider.js +16 -0
  160. package/dist/src/provider.js.map +1 -0
  161. package/dist/src/utils/address.d.ts +15 -0
  162. package/dist/src/utils/address.d.ts.map +1 -0
  163. package/dist/src/utils/address.js +35 -0
  164. package/dist/src/utils/address.js.map +1 -0
  165. package/dist/src/utils/address.test.d.ts +2 -0
  166. package/dist/src/utils/address.test.d.ts.map +1 -0
  167. package/dist/src/utils/address.test.js +32 -0
  168. package/dist/src/utils/address.test.js.map +1 -0
  169. package/dist/src/utils/cryptoAssets.d.ts +28 -0
  170. package/dist/src/utils/cryptoAssets.d.ts.map +1 -0
  171. package/dist/src/utils/cryptoAssets.js +73 -0
  172. package/dist/src/utils/cryptoAssets.js.map +1 -0
  173. package/dist/src/utils/cryptoAssets.test.d.ts +2 -0
  174. package/dist/src/utils/cryptoAssets.test.d.ts.map +1 -0
  175. package/dist/src/utils/cryptoAssets.test.js +50 -0
  176. package/dist/src/utils/cryptoAssets.test.js.map +1 -0
  177. package/dist/src/utils/currency.d.ts +14 -0
  178. package/dist/src/utils/currency.d.ts.map +1 -0
  179. package/dist/src/utils/currency.js +27 -0
  180. package/dist/src/utils/currency.js.map +1 -0
  181. package/dist/src/utils/currency.test.d.ts +2 -0
  182. package/dist/src/utils/currency.test.d.ts.map +1 -0
  183. package/dist/src/utils/currency.test.js +34 -0
  184. package/dist/src/utils/currency.test.js.map +1 -0
  185. package/dist/src/utils/numbers.d.ts +26 -0
  186. package/dist/src/utils/numbers.d.ts.map +1 -0
  187. package/dist/src/utils/numbers.js +40 -0
  188. package/dist/src/utils/numbers.js.map +1 -0
  189. package/dist/src/utils/numbers.test.d.ts +2 -0
  190. package/dist/src/utils/numbers.test.d.ts.map +1 -0
  191. package/dist/src/utils/numbers.test.js +54 -0
  192. package/dist/src/utils/numbers.test.js.map +1 -0
  193. package/dist/src/utils/siww.d.ts +4 -0
  194. package/dist/src/utils/siww.d.ts.map +1 -0
  195. package/dist/src/utils/siww.js +19 -0
  196. package/dist/src/utils/siww.js.map +1 -0
  197. package/dist/src/utils/time.d.ts +5 -0
  198. package/dist/src/utils/time.d.ts.map +1 -0
  199. package/dist/src/utils/time.js +5 -0
  200. package/dist/src/utils/time.js.map +1 -0
  201. package/package.json +13 -5
  202. package/src/api/auth.ts +178 -0
  203. package/src/api/client.ts +78 -0
  204. package/src/api/fetch-error.ts +8 -0
  205. package/src/api/index.ts +2 -0
  206. package/src/api/portal.ts +56 -0
  207. package/src/assets/DefaultAvatar.tsx +74 -0
  208. package/src/components/Dropdown/AccountAddress.tsx +130 -0
  209. package/src/components/Dropdown/AccountAssets.tsx +110 -0
  210. package/src/components/Dropdown/AccountBalance.tsx +38 -0
  211. package/src/components/Dropdown/ConnectedTrigger.tsx +56 -0
  212. package/src/components/Dropdown/Content.tsx +148 -0
  213. package/src/components/Dropdown/DisconnectedTrigger.tsx +36 -0
  214. package/src/components/Dropdown/Dropdown.tsx +111 -0
  215. package/src/components/Dropdown/README.md +51 -0
  216. package/src/components/Dropdown/WelcomeBlock.tsx +92 -0
  217. package/src/components/Dropdown/index.ts +2 -0
  218. package/src/components/index.ts +1 -0
  219. package/src/hooks/constants.ts +6 -0
  220. package/src/hooks/index.ts +10 -0
  221. package/src/hooks/useAssetsUSDConversion.ts +31 -0
  222. package/src/hooks/useAuthApiClient.ts +6 -0
  223. package/src/hooks/useAuthenticateWithWallet.ts +98 -0
  224. package/src/hooks/useCreateAccount.ts +36 -0
  225. package/src/hooks/useCreateSession.ts +44 -0
  226. package/src/hooks/useDropdownData.ts +131 -0
  227. package/src/hooks/useEnsureNoSessionAndFetchNonce.ts +46 -0
  228. package/src/hooks/useGetAccountByAddress.ts +25 -0
  229. package/src/hooks/useGetAccountByMezoId.ts +25 -0
  230. package/src/hooks/useGetCurrentAccount.ts +62 -0
  231. package/src/hooks/useGetSession.ts +22 -0
  232. package/src/hooks/useLinkAccount.ts +33 -0
  233. package/src/hooks/usePassportContext.ts +11 -0
  234. package/src/hooks/usePortalApiClient.ts +6 -0
  235. package/src/hooks/useSignInWithDiscord.ts +35 -0
  236. package/src/hooks/useSignInWithWallet.ts +21 -0
  237. package/src/hooks/useSignOut.ts +35 -0
  238. package/src/hooks/useSignUpWithWallet.ts +21 -0
  239. package/src/hooks/useUpdateMezoId.ts +33 -0
  240. package/src/hooks/useWalletAccount.ts +53 -0
  241. package/src/index.ts +2 -0
  242. package/src/provider.ts +50 -0
  243. package/src/utils/address.test.ts +38 -0
  244. package/src/utils/address.ts +43 -0
  245. package/src/utils/cryptoAssets.test.ts +61 -0
  246. package/src/utils/cryptoAssets.ts +93 -0
  247. package/src/utils/currency.test.ts +38 -0
  248. package/src/utils/currency.ts +32 -0
  249. package/src/utils/numbers.test.ts +73 -0
  250. package/src/utils/numbers.ts +55 -0
  251. package/src/utils/siww.ts +31 -0
  252. package/src/utils/time.ts +4 -0
@@ -0,0 +1,11 @@
1
+ import { useContext } from "react"
2
+ import { PassportContext, PassportContextValue } from "../provider"
3
+
4
+ export function usePassportContext(): PassportContextValue {
5
+ const passportContext = useContext(PassportContext)
6
+ if (!passportContext) {
7
+ throw new Error("usePassportContext must be used within PassportProvider!")
8
+ }
9
+
10
+ return passportContext
11
+ }
@@ -0,0 +1,6 @@
1
+ import { usePassportContext } from "./usePassportContext"
2
+
3
+ export function usePortalApiClient() {
4
+ const { portalApiClient } = usePassportContext()
5
+ return portalApiClient
6
+ }
@@ -0,0 +1,35 @@
1
+ import { MutationOptions, useMutation } from "@tanstack/react-query"
2
+ import { useCreateSession } from "./useCreateSession"
3
+ import { useEnsureNoSessionAndFetchNonce } from "./useEnsureNoSessionAndFetchNonce"
4
+ import type { AuthenticationProviderRedirectResponse } from "../api"
5
+
6
+ function useSignInWithDiscord(
7
+ mutationOptions: Omit<
8
+ MutationOptions<AuthenticationProviderRedirectResponse>,
9
+ "mutationFn" | "mutationKey"
10
+ > = {},
11
+ ) {
12
+ const { ensureNoSessionAndFetchNonce } = useEnsureNoSessionAndFetchNonce()
13
+ const { createSessionAsync } = useCreateSession()
14
+
15
+ const { mutate, mutateAsync, ...signInMutationRestParameters } = useMutation({
16
+ mutationFn: async (): Promise<AuthenticationProviderRedirectResponse> => {
17
+ await ensureNoSessionAndFetchNonce()
18
+
19
+ return (await createSessionAsync({
20
+ type: "discord",
21
+ })) as AuthenticationProviderRedirectResponse
22
+ // ^ By passing type: "discord" we know for sure the return type is
23
+ // AuthenticationProviderRedirectResponse
24
+ },
25
+ ...mutationOptions,
26
+ })
27
+
28
+ return {
29
+ signInWithDiscord: mutate,
30
+ signInWithDiscordAsync: mutateAsync,
31
+ ...signInMutationRestParameters,
32
+ }
33
+ }
34
+
35
+ export { useSignInWithDiscord }
@@ -0,0 +1,21 @@
1
+ import { MutationOptions } from "@tanstack/react-query"
2
+ import { useAuthenticateWithWallet } from "./useAuthenticateWithWallet"
3
+ import type { Session } from "../api"
4
+
5
+ function useSignInWithWallet(
6
+ mutationOptions: Omit<
7
+ MutationOptions<Session>,
8
+ "mutationFn" | "mutationKey"
9
+ > = {},
10
+ ) {
11
+ const { authenticateWithWallet, authenticateWithWalletAsync, ...rest } =
12
+ useAuthenticateWithWallet(false, mutationOptions)
13
+
14
+ return {
15
+ signInWithWallet: authenticateWithWallet,
16
+ signInWithWalletAsync: authenticateWithWalletAsync,
17
+ ...rest,
18
+ }
19
+ }
20
+
21
+ export { useSignInWithWallet }
@@ -0,0 +1,35 @@
1
+ import {
2
+ MutationOptions,
3
+ useMutation,
4
+ useQueryClient,
5
+ } from "@tanstack/react-query"
6
+ import { useAuthApiClient } from "./useAuthApiClient"
7
+ import { QUERY_KEYS } from "./constants"
8
+ import type { DeleteSessionResponse } from "../api"
9
+
10
+ export function useSignOut(
11
+ mutationOptions: Omit<
12
+ MutationOptions<DeleteSessionResponse>,
13
+ "mutationFn" | "mutationKey"
14
+ > = {},
15
+ ) {
16
+ const queryClient = useQueryClient()
17
+ const authApiClient = useAuthApiClient()
18
+
19
+ const { onSuccess: customOnSuccess, ...restMutationOptions } = mutationOptions
20
+
21
+ const { mutate, mutateAsync, ...rest } = useMutation({
22
+ mutationFn: () => authApiClient.deleteSession(),
23
+ onSuccess: (data, variables, context) => {
24
+ queryClient.resetQueries({ queryKey: [QUERY_KEYS.SESSION] })
25
+ queryClient.resetQueries({
26
+ queryKey: [QUERY_KEYS.ACCOUNT, QUERY_KEYS.CURRENT],
27
+ })
28
+
29
+ if (customOnSuccess) customOnSuccess(data, variables, context)
30
+ },
31
+ ...restMutationOptions,
32
+ })
33
+
34
+ return { signOut: mutate, signOutAsync: mutateAsync, ...rest }
35
+ }
@@ -0,0 +1,21 @@
1
+ import { MutationOptions } from "@tanstack/react-query"
2
+ import { useAuthenticateWithWallet } from "./useAuthenticateWithWallet"
3
+ import type { Session } from "../api"
4
+
5
+ function useSignUpWithWallet(
6
+ mutationOptions: Omit<
7
+ MutationOptions<Session>,
8
+ "mutationFn" | "mutationKey"
9
+ > = {},
10
+ ) {
11
+ const { authenticateWithWallet, authenticateWithWalletAsync, ...rest } =
12
+ useAuthenticateWithWallet(true, mutationOptions)
13
+
14
+ return {
15
+ signUpWithWallet: authenticateWithWallet,
16
+ signUpWithWalletAsync: authenticateWithWalletAsync,
17
+ ...rest,
18
+ }
19
+ }
20
+
21
+ export { useSignUpWithWallet }
@@ -0,0 +1,33 @@
1
+ import {
2
+ useQueryClient,
3
+ useMutation,
4
+ MutationOptions,
5
+ DefaultError,
6
+ } from "@tanstack/react-query"
7
+ import { useAuthApiClient } from "./useAuthApiClient"
8
+ import { QUERY_KEYS } from "./constants"
9
+ import type { UpdateMezoIdResponse } from "../api"
10
+
11
+ export function useUpdateMezoId(
12
+ mutationOptions: Omit<
13
+ MutationOptions<UpdateMezoIdResponse, DefaultError, string>,
14
+ "mutationFn" | "mutationKey"
15
+ > = {},
16
+ ) {
17
+ const queryClient = useQueryClient()
18
+ const authApiClient = useAuthApiClient()
19
+
20
+ const { onSuccess: customOnSuccess, ...restMutationOptions } = mutationOptions
21
+
22
+ const { mutate, mutateAsync, ...rest } = useMutation({
23
+ mutationFn: (newMezoId: string) => authApiClient.updateMezoId(newMezoId),
24
+ onSuccess: (data, variables, context) => {
25
+ queryClient.resetQueries({ queryKey: [QUERY_KEYS.ACCOUNT] })
26
+
27
+ if (customOnSuccess) customOnSuccess(data, variables, context)
28
+ },
29
+ ...restMutationOptions,
30
+ })
31
+
32
+ return { updateMezoId: mutate, updateMezoIdAsync: mutateAsync, ...rest }
33
+ }
@@ -0,0 +1,53 @@
1
+ import { OrangeKitBitcoinProvider } from "@mezo-org/orangekit"
2
+ import { useQuery } from "@tanstack/react-query"
3
+ import { useMemo } from "react"
4
+ import { Address } from "viem"
5
+ import { Connector, useAccount } from "wagmi"
6
+
7
+ type UseWalletAccountReturn = {
8
+ address?: string | Address
9
+ isConnected: boolean
10
+ networkFamily: "bitcoin" | "evm"
11
+ connector?: Connector
12
+ chainId?: number
13
+ }
14
+
15
+ type OrangeKitConnector = {
16
+ getBitcoinProvider: () => OrangeKitBitcoinProvider
17
+ }
18
+
19
+ export default function useWalletAccount(): UseWalletAccountReturn {
20
+ const { address: evmAddress, connector, chainId } = useAccount()
21
+
22
+ const networkFamily = useMemo(
23
+ () => (connector?.type !== "orangekit" ? "evm" : "bitcoin"),
24
+ [connector],
25
+ )
26
+
27
+ const { data: btcData } = useQuery({
28
+ queryKey: [connector?.id],
29
+ queryFn: async () => {
30
+ const bitcoinProvider = (
31
+ connector as unknown as OrangeKitConnector
32
+ ).getBitcoinProvider()
33
+
34
+ const address = await bitcoinProvider.getAddress()
35
+ const balance = await bitcoinProvider.getBalance()
36
+
37
+ return {
38
+ address,
39
+ balance,
40
+ }
41
+ },
42
+ })
43
+
44
+ const { address: btcAddress } = btcData || {}
45
+
46
+ return {
47
+ address: networkFamily === "bitcoin" ? btcAddress : evmAddress,
48
+ isConnected: networkFamily === "bitcoin" ? !!btcAddress : !!evmAddress,
49
+ networkFamily,
50
+ connector,
51
+ chainId,
52
+ } as const
53
+ }
package/src/index.ts CHANGED
@@ -1,4 +1,6 @@
1
+ export * from "./components"
1
2
  export * from "./config"
2
3
  export * from "./constants"
3
4
  export * from "./hooks"
5
+ export * from "./provider"
4
6
  export * from "./wallet"
@@ -0,0 +1,50 @@
1
+ import { createContext, createElement } from "react"
2
+ import { AuthApiClient, PortalApiClient } from "./api"
3
+
4
+ export interface PassportContextValue {
5
+ authApiClient: AuthApiClient
6
+ portalApiClient: PortalApiClient
7
+ environment?: Environment
8
+ }
9
+
10
+ export const PassportContext = createContext<PassportContextValue | undefined>(
11
+ undefined,
12
+ )
13
+
14
+ type Environment = "mainnet" | "testnet"
15
+
16
+ type PassportProviderProps = {
17
+ children: React.ReactNode
18
+ // eslint-disable-next-line react/require-default-props
19
+ environment?: Environment
20
+ // eslint-disable-next-line react/require-default-props
21
+ authApiUrl?: string
22
+ // eslint-disable-next-line react/require-default-props
23
+ portalApiUrl?: string
24
+ }
25
+
26
+ export function PassportProvider({
27
+ environment,
28
+ authApiUrl,
29
+ portalApiUrl,
30
+ children,
31
+ }: PassportProviderProps) {
32
+ if (environment && !["mainnet", "testnet"].includes(environment)) {
33
+ throw new Error("Wrong environment passed to PassportProvider.")
34
+ }
35
+
36
+ return createElement(
37
+ PassportContext.Provider,
38
+ {
39
+ value: {
40
+ authApiClient: new AuthApiClient(environment ?? "mainnet", authApiUrl),
41
+ portalApiClient: new PortalApiClient(
42
+ environment ?? "mainnet",
43
+ portalApiUrl,
44
+ ),
45
+ environment,
46
+ },
47
+ },
48
+ children,
49
+ )
50
+ }
@@ -0,0 +1,38 @@
1
+ import { getAddressExplorerUrl, trimAddress } from "./address"
2
+
3
+ describe("trimAddress", () => {
4
+ it("returns full address if length is less than 11", () => {
5
+ expect(trimAddress("0x12345")).toBe("0x12345")
6
+ })
7
+
8
+ it("returns trimmed address if length is 11 or more", () => {
9
+ const input = "0x123456789abcdef"
10
+ const expected = "0x123...bcdef"
11
+ expect(trimAddress(input)).toBe(expected)
12
+ })
13
+ })
14
+
15
+ describe("getAddressExplorerUrl", () => {
16
+ const btcAddress = "bc1qxyz123"
17
+ const evmAddress = "0x1234567890abcdef"
18
+
19
+ it("returns correct mainnet Bitcoin explorer URL", () => {
20
+ const url = getAddressExplorerUrl(btcAddress, "bitcoin", false)
21
+ expect(url).toBe(`https://mempool.space/address/${btcAddress}`)
22
+ })
23
+
24
+ it("returns correct testnet Bitcoin explorer URL", () => {
25
+ const url = getAddressExplorerUrl(btcAddress, "bitcoin", true)
26
+ expect(url).toBe(`https://mempool.space/testnet/address/${btcAddress}`)
27
+ })
28
+
29
+ it("returns correct mainnet EVM explorer URL", () => {
30
+ const url = getAddressExplorerUrl(evmAddress, "evm", false)
31
+ expect(url).toBe(`https://etherscan.io/address/${evmAddress}`)
32
+ })
33
+
34
+ it("returns correct testnet EVM explorer URL", () => {
35
+ const url = getAddressExplorerUrl(evmAddress, "evm", true)
36
+ expect(url).toBe(`https://sepolia.etherscan.io/address/${evmAddress}`)
37
+ })
38
+ })
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Trims the address to the first 5 and last 5 characters.
3
+ * @param address The address to trim.
4
+ * @returns The trimmed address.
5
+ */
6
+ export function trimAddress(address: string): string {
7
+ if (address.length < 11) return address
8
+
9
+ const trimmedAddress = `${address.slice(0, 5)}...${address.slice(-5)}`
10
+
11
+ return trimmedAddress
12
+ }
13
+
14
+ const MAINNET_EXPLORER_URLS: Record<"bitcoin" | "evm", string> = {
15
+ bitcoin: "https://mempool.space",
16
+ evm: "https://etherscan.io",
17
+ }
18
+
19
+ const TESTNET_EXPLORER_URLS: Record<"bitcoin" | "evm", string> = {
20
+ bitcoin: "https://mempool.space/testnet",
21
+ evm: "https://sepolia.etherscan.io",
22
+ }
23
+
24
+ /**
25
+ * Get the URL of the block explorer page for an address.
26
+ * @param address - The address to link to.
27
+ * @param chainType - The chain type of the address.
28
+ * @param isTestnet - Whether the address is on testnet.
29
+ * @returns The URL of the block explorer page for the address.
30
+ */
31
+ export function getAddressExplorerUrl(
32
+ address: string,
33
+ chainType: "bitcoin" | "evm",
34
+ isTestnet: boolean,
35
+ ) {
36
+ const EXPLORER_URLS = isTestnet
37
+ ? TESTNET_EXPLORER_URLS
38
+ : MAINNET_EXPLORER_URLS
39
+ const baseUrl = EXPLORER_URLS[chainType]
40
+ const endpoint = `address/${address}`
41
+
42
+ return `${baseUrl}/${endpoint}`
43
+ }
@@ -0,0 +1,61 @@
1
+ import {
2
+ BitcoinCircle,
3
+ EthCircle,
4
+ MUsdCircle,
5
+ TBtcCircle,
6
+ } from "@mezo-org/mezo-clay"
7
+ import { getCryptoAsset } from "./cryptoAssets"
8
+
9
+ // FIXME: Test fails if run in full suite (pnpm test) but works when run in isolation (pnpm test cryptoAssets)
10
+
11
+ describe("getCryptoAsset", () => {
12
+ it("returns the correct asset for 'btc'", () => {
13
+ const asset = getCryptoAsset("btc")
14
+ expect(asset.name).toBe("Bitcoin")
15
+ expect(asset.symbol).toBe("BTC")
16
+ expect(asset.decimals).toBe(8)
17
+ expect(asset.icon).toBe(BitcoinCircle)
18
+ })
19
+
20
+ it("returns the correct asset for 'eth'", () => {
21
+ const asset = getCryptoAsset("eth")
22
+ expect(asset.name).toBe("Ethereum")
23
+ expect(asset.symbol).toBe("ETH")
24
+ expect(asset.decimals).toBe(18)
25
+ expect(asset.icon).toBe(EthCircle)
26
+ })
27
+
28
+ it("returns the correct asset for 'tbtc'", () => {
29
+ const asset = getCryptoAsset("tbtc")
30
+ expect(asset.name).toBe("Threshold Bitcoin")
31
+ expect(asset.symbol).toBe("tBTC")
32
+ expect(asset.decimals).toBe(18)
33
+ expect(asset.icon).toBe(TBtcCircle)
34
+ })
35
+
36
+ it("returns the correct asset for 'wbtc'", () => {
37
+ const asset = getCryptoAsset("wbtc")
38
+ expect(asset.name).toBe("Wrapped Bitcoin")
39
+ expect(asset.symbol).toBe("wBTC")
40
+ expect(asset.decimals).toBe(8)
41
+ // FIXME: Test should expect the exact icon component
42
+ expect(asset.icon).toEqual(expect.any(Function))
43
+ })
44
+
45
+ it("returns the correct asset for 'mbtc'", () => {
46
+ const asset = getCryptoAsset("mbtc")
47
+ expect(asset.name).toBe("Matsnet Bitcoin")
48
+ expect(asset.symbol).toBe("BTC")
49
+ expect(asset.decimals).toBe(18)
50
+ // FIXME: Test should expect the exact icon component
51
+ expect(asset.icon).toEqual(expect.any(Function))
52
+ })
53
+
54
+ it("returns the correct asset for 'musd'", () => {
55
+ const asset = getCryptoAsset("musd")
56
+ expect(asset.name).toBe("Mezo USD")
57
+ expect(asset.symbol).toBe("MUSD")
58
+ expect(asset.decimals).toBe(18)
59
+ expect(asset.icon).toBe(MUsdCircle)
60
+ })
61
+ })
@@ -0,0 +1,93 @@
1
+ import {
2
+ BitcoinCircle,
3
+ EthCircle,
4
+ IconProps,
5
+ MUsdCircle,
6
+ TBtcCircle,
7
+ } from "@mezo-org/mezo-clay"
8
+ import { createElement, FC } from "react"
9
+
10
+ export type CryptoAssetKey = "btc" | "eth" | "tbtc" | "wbtc" | "mbtc" | "musd"
11
+
12
+ export type CryptoAsset = {
13
+ name: string
14
+ symbol: string
15
+ icon: FC<IconProps>
16
+ decimals: number
17
+ }
18
+
19
+ const CRYPTO_ASSETS: Record<CryptoAssetKey, CryptoAsset> = {
20
+ btc: {
21
+ name: "Bitcoin",
22
+ symbol: "BTC",
23
+ icon: BitcoinCircle,
24
+ decimals: 8,
25
+ },
26
+ eth: {
27
+ name: "Ethereum",
28
+ symbol: "ETH",
29
+ icon: EthCircle,
30
+ decimals: 18,
31
+ },
32
+ tbtc: {
33
+ name: "Threshold Bitcoin",
34
+ symbol: "tBTC",
35
+ icon: TBtcCircle,
36
+ decimals: 18,
37
+ },
38
+ wbtc: {
39
+ name: "Wrapped Bitcoin",
40
+ symbol: "wBTC",
41
+ icon: (props) =>
42
+ createElement(BitcoinCircle, {
43
+ ...props,
44
+ symbolColor: "#FF8A00",
45
+ color: "#000000",
46
+ }),
47
+ decimals: 8,
48
+ },
49
+ mbtc: {
50
+ name: "Matsnet Bitcoin",
51
+ symbol: "BTC",
52
+ icon: (props) =>
53
+ createElement(BitcoinCircle, {
54
+ ...props,
55
+ symbolColor: "#000000",
56
+ color: "#FFC907",
57
+ }),
58
+ decimals: 18,
59
+ },
60
+ musd: {
61
+ name: "Mezo USD",
62
+ symbol: "MUSD",
63
+ icon: MUsdCircle,
64
+ decimals: 18,
65
+ },
66
+ }
67
+
68
+ /**
69
+ * Gets details of given crypto asset
70
+ * @param key The key of crypto asset
71
+ * @returns The crypto asset details
72
+ */
73
+ export function getCryptoAsset(key: CryptoAssetKey) {
74
+ return CRYPTO_ASSETS[key]
75
+ }
76
+
77
+ /**
78
+ * Checks if given crypto asset is Bitcoin-like
79
+ * @param key The key of crypto asset
80
+ * @returns True if crypto asset is Bitcoin-like
81
+ */
82
+ export function isBitcoinLikeCryptoAsset(key: CryptoAssetKey) {
83
+ return ["btc", "tbtc", "wbtc", "mbtc"].includes(key)
84
+ }
85
+
86
+ /**
87
+ * Checks if given crypto asset is USD-like
88
+ * @param key The key of crypto asset
89
+ * @returns True if crypto asset is USD-like
90
+ */
91
+ export function isUsdLikeCryptoAsset(key: CryptoAssetKey) {
92
+ return ["musd"].includes(key)
93
+ }
@@ -0,0 +1,38 @@
1
+ import { formatCurrency, formatUsd } from "./currency"
2
+
3
+ describe("formatCurrency", () => {
4
+ it("formats a number as currency with default options", () => {
5
+ const result = formatCurrency(1234.5, { currency: "EUR" })
6
+ expect(result).toBe("€1,234.50")
7
+ })
8
+
9
+ it("respects minimumFractionDigits (min = 3, max = 3)", () => {
10
+ const result = formatCurrency(1.2, {
11
+ currency: "USD",
12
+ minimumFractionDigits: 3,
13
+ maximumFractionDigits: 3,
14
+ })
15
+ expect(result).toBe("$1.200")
16
+ })
17
+
18
+ it("respects maximumFractionDigits (min = 0, max = 1)", () => {
19
+ const result = formatCurrency(1.234567, {
20
+ currency: "USD",
21
+ minimumFractionDigits: 0,
22
+ maximumFractionDigits: 1,
23
+ })
24
+ expect(result).toBe("$1.2")
25
+ })
26
+ })
27
+
28
+ describe("formatUsd", () => {
29
+ it("formats a number as USD", () => {
30
+ const result = formatUsd(99.99)
31
+ expect(result).toBe("$99.99")
32
+ })
33
+
34
+ it("formats zero", () => {
35
+ const result = formatUsd(0)
36
+ expect(result).toBe("$0.00")
37
+ })
38
+ })
@@ -0,0 +1,32 @@
1
+ const DEFAULT_FORMAT_OPTIONS: Intl.NumberFormatOptions = {
2
+ style: "currency",
3
+ minimumFractionDigits: 2,
4
+ maximumFractionDigits: 2,
5
+ }
6
+
7
+ /**
8
+ * Formats a number as a currency
9
+ * @param value The value to format
10
+ * @param options The options to pass to the formatter
11
+ * @returns The formatted currency
12
+ */
13
+ export function formatCurrency(
14
+ value: number,
15
+ options: Omit<Intl.NumberFormatOptions, "style"> = {},
16
+ ): string {
17
+ const formatter = new Intl.NumberFormat("en-US", {
18
+ ...DEFAULT_FORMAT_OPTIONS,
19
+ ...options,
20
+ })
21
+
22
+ return formatter.format(value)
23
+ }
24
+
25
+ /**
26
+ * Formats a number as a USD currency
27
+ * @param value The value to format
28
+ * @returns The formatted currency
29
+ */
30
+ export function formatUsd(value: number): string {
31
+ return formatCurrency(value, { currency: "USD" })
32
+ }
@@ -0,0 +1,73 @@
1
+ import {
2
+ formatHumanReadableNumber,
3
+ fromFixedPoint,
4
+ roundUpNumber,
5
+ } from "./numbers"
6
+
7
+ describe("formatHumanReadableNumber", () => {
8
+ it("formats number less than 1000 with decimals", () => {
9
+ expect(formatHumanReadableNumber(123.456789, 2)).toBe("123.46")
10
+ })
11
+
12
+ it("formats thousands with K", () => {
13
+ expect(formatHumanReadableNumber(12_345, 2)).toBe("12.35K")
14
+ })
15
+
16
+ it("formats millions with M", () => {
17
+ expect(formatHumanReadableNumber(12_345_678, 2)).toBe("12.35M")
18
+ })
19
+
20
+ it("formats billions with B", () => {
21
+ expect(formatHumanReadableNumber(12_345_678_901, 2)).toBe("12.35B")
22
+ })
23
+
24
+ it("formats trillions with T", () => {
25
+ expect(formatHumanReadableNumber(12_345_678_901_234, 2)).toBe("12.35T")
26
+ })
27
+
28
+ it("formats bigint as readable number", () => {
29
+ expect(formatHumanReadableNumber(1234567890123456789n, 2)).toBe(
30
+ "1,234,567.89T",
31
+ )
32
+ })
33
+
34
+ it("caps at > 1000T for huge numbers", () => {
35
+ const hugeNumber = 10 ** 18 // 1e18
36
+ expect(formatHumanReadableNumber(hugeNumber, 2)).toBe("1,000,000T")
37
+ })
38
+
39
+ it("formats numbers with trailing zeros correctly", () => {
40
+ expect(formatHumanReadableNumber(1_000, 2)).toBe("1K")
41
+ })
42
+ })
43
+
44
+ describe("roundUpNumber", () => {
45
+ it("rounds up to 2 decimals", () => {
46
+ expect(roundUpNumber(1.234)).toBe(1.24)
47
+ })
48
+
49
+ it("rounds up to 3 decimals", () => {
50
+ expect(roundUpNumber(9.87654, 3)).toBe(9.877)
51
+ })
52
+
53
+ it("rounds up exact decimal", () => {
54
+ expect(roundUpNumber(2.5, 1)).toBe(2.5)
55
+ })
56
+ })
57
+
58
+ describe("fromFixedPoint", () => {
59
+ it("converts fixed point with 18 decimals to float", () => {
60
+ const raw = 1234567890000000000n // 1.23456789 ETH in 18 decimals
61
+ expect(fromFixedPoint(raw, 18, 4)).toBe(1.2345)
62
+ })
63
+
64
+ it("converts with truncation to 2 decimals", () => {
65
+ const raw = 987654321000000000n // ~0.987654321 ETH
66
+ expect(fromFixedPoint(raw, 18, 2)).toBe(0.98)
67
+ })
68
+
69
+ it("handles large fixedPoint decimals", () => {
70
+ const raw = 1_000_000_000_000_000_000_000_000n // 1M with 6 decimals
71
+ expect(fromFixedPoint(raw, 6, 2)).toBe(1000000000000000000)
72
+ })
73
+ })