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

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 (227) hide show
  1. package/dist/src/api/auth.d.ts +36 -59
  2. package/dist/src/api/auth.d.ts.map +1 -1
  3. package/dist/src/api/auth.js +21 -49
  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 +58 -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 +8 -0
  42. package/dist/src/components/Dropdown/Content.d.ts.map +1 -0
  43. package/dist/src/components/Dropdown/Content.js +65 -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 +18 -0
  50. package/dist/src/components/Dropdown/Dropdown.d.ts.map +1 -0
  51. package/dist/src/components/Dropdown/Dropdown.js +42 -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 +1 -0
  66. package/dist/src/hooks/constants.d.ts.map +1 -1
  67. package/dist/src/hooks/constants.js +1 -0
  68. package/dist/src/hooks/constants.js.map +1 -1
  69. package/dist/src/hooks/index.d.ts +1 -1
  70. package/dist/src/hooks/index.d.ts.map +1 -1
  71. package/dist/src/hooks/index.js +1 -1
  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 +1 -1
  78. package/dist/src/hooks/useAuthApiClient.d.ts.map +1 -1
  79. package/dist/src/hooks/useAuthenticateWithWallet.d.ts +73 -0
  80. package/dist/src/hooks/useAuthenticateWithWallet.d.ts.map +1 -0
  81. package/dist/src/hooks/useAuthenticateWithWallet.js +70 -0
  82. package/dist/src/hooks/useAuthenticateWithWallet.js.map +1 -0
  83. package/dist/src/hooks/useCreateAccount.d.ts +23 -173
  84. package/dist/src/hooks/useCreateAccount.d.ts.map +1 -1
  85. package/dist/src/hooks/useCreateAccount.js +7 -4
  86. package/dist/src/hooks/useCreateAccount.js.map +1 -1
  87. package/dist/src/hooks/useCreateSession.d.ts +12 -101
  88. package/dist/src/hooks/useCreateSession.d.ts.map +1 -1
  89. package/dist/src/hooks/useCreateSession.js +10 -8
  90. package/dist/src/hooks/useCreateSession.js.map +1 -1
  91. package/dist/src/hooks/useDropdownData.d.ts +44 -0
  92. package/dist/src/hooks/useDropdownData.d.ts.map +1 -0
  93. package/dist/src/hooks/useDropdownData.js +73 -0
  94. package/dist/src/hooks/useDropdownData.js.map +1 -0
  95. package/dist/src/hooks/useEnsureNoSessionAndFetchNonce.d.ts.map +1 -1
  96. package/dist/src/hooks/useEnsureNoSessionAndFetchNonce.js +1 -0
  97. package/dist/src/hooks/useEnsureNoSessionAndFetchNonce.js.map +1 -1
  98. package/dist/src/hooks/useGetAccountByAddress.d.ts +2 -2
  99. package/dist/src/hooks/useGetAccountByAddress.d.ts.map +1 -1
  100. package/dist/src/hooks/useGetAccountByMezoId.d.ts +2 -2
  101. package/dist/src/hooks/useGetAccountByMezoId.d.ts.map +1 -1
  102. package/dist/src/hooks/useGetCurrentAccount.d.ts +16 -2
  103. package/dist/src/hooks/useGetCurrentAccount.d.ts.map +1 -1
  104. package/dist/src/hooks/useGetCurrentAccount.js +25 -3
  105. package/dist/src/hooks/useGetCurrentAccount.js.map +1 -1
  106. package/dist/src/hooks/useGetSession.d.ts +1 -1
  107. package/dist/src/hooks/useGetSession.d.ts.map +1 -1
  108. package/dist/src/hooks/useLinkAccount.d.ts +15 -175
  109. package/dist/src/hooks/useLinkAccount.d.ts.map +1 -1
  110. package/dist/src/hooks/useLinkAccount.js +7 -4
  111. package/dist/src/hooks/useLinkAccount.js.map +1 -1
  112. package/dist/src/hooks/usePortalApiClient.d.ts +2 -0
  113. package/dist/src/hooks/usePortalApiClient.d.ts.map +1 -0
  114. package/dist/src/hooks/usePortalApiClient.js +6 -0
  115. package/dist/src/hooks/usePortalApiClient.js.map +1 -0
  116. package/dist/src/hooks/useSignInWithDiscord.d.ts +12 -100
  117. package/dist/src/hooks/useSignInWithDiscord.d.ts.map +1 -1
  118. package/dist/src/hooks/useSignInWithDiscord.js +6 -3
  119. package/dist/src/hooks/useSignInWithDiscord.js.map +1 -1
  120. package/dist/src/hooks/useSignInWithWallet.d.ts +12 -100
  121. package/dist/src/hooks/useSignInWithWallet.d.ts.map +1 -1
  122. package/dist/src/hooks/useSignInWithWallet.js +6 -34
  123. package/dist/src/hooks/useSignInWithWallet.js.map +1 -1
  124. package/dist/src/hooks/useSignOut.d.ts +12 -28
  125. package/dist/src/hooks/useSignOut.d.ts.map +1 -1
  126. package/dist/src/hooks/useSignOut.js +7 -4
  127. package/dist/src/hooks/useSignOut.js.map +1 -1
  128. package/dist/src/hooks/useSignUpWithWallet.d.ts +73 -0
  129. package/dist/src/hooks/useSignUpWithWallet.d.ts.map +1 -0
  130. package/dist/src/hooks/useSignUpWithWallet.js +11 -0
  131. package/dist/src/hooks/useSignUpWithWallet.js.map +1 -0
  132. package/dist/src/hooks/useUpdateMezoId.d.ts +30 -136
  133. package/dist/src/hooks/useUpdateMezoId.d.ts.map +1 -1
  134. package/dist/src/hooks/useUpdateMezoId.js +7 -4
  135. package/dist/src/hooks/useUpdateMezoId.js.map +1 -1
  136. package/dist/src/hooks/useWalletAccount.d.ts +12 -0
  137. package/dist/src/hooks/useWalletAccount.d.ts.map +1 -0
  138. package/dist/src/hooks/useWalletAccount.js +25 -0
  139. package/dist/src/hooks/useWalletAccount.js.map +1 -0
  140. package/dist/src/index.d.ts +1 -0
  141. package/dist/src/index.d.ts.map +1 -1
  142. package/dist/src/index.js +1 -0
  143. package/dist/src/index.js.map +1 -1
  144. package/dist/src/provider.d.ts +10 -13
  145. package/dist/src/provider.d.ts.map +1 -1
  146. package/dist/src/provider.js +11 -20
  147. package/dist/src/provider.js.map +1 -1
  148. package/dist/src/utils/address.d.ts +15 -0
  149. package/dist/src/utils/address.d.ts.map +1 -0
  150. package/dist/src/utils/address.js +35 -0
  151. package/dist/src/utils/address.js.map +1 -0
  152. package/dist/src/utils/address.test.d.ts +2 -0
  153. package/dist/src/utils/address.test.d.ts.map +1 -0
  154. package/dist/src/utils/address.test.js +37 -0
  155. package/dist/src/utils/address.test.js.map +1 -0
  156. package/dist/src/utils/cryptoAssets.d.ts +28 -0
  157. package/dist/src/utils/cryptoAssets.d.ts.map +1 -0
  158. package/dist/src/utils/cryptoAssets.js +73 -0
  159. package/dist/src/utils/cryptoAssets.js.map +1 -0
  160. package/dist/src/utils/cryptoAssets.test.d.ts +2 -0
  161. package/dist/src/utils/cryptoAssets.test.d.ts.map +1 -0
  162. package/dist/src/utils/cryptoAssets.test.js +49 -0
  163. package/dist/src/utils/cryptoAssets.test.js.map +1 -0
  164. package/dist/src/utils/currency.d.ts +14 -0
  165. package/dist/src/utils/currency.d.ts.map +1 -0
  166. package/dist/src/utils/currency.js +27 -0
  167. package/dist/src/utils/currency.js.map +1 -0
  168. package/dist/src/utils/currency.test.d.ts +2 -0
  169. package/dist/src/utils/currency.test.d.ts.map +1 -0
  170. package/dist/src/utils/currency.test.js +34 -0
  171. package/dist/src/utils/currency.test.js.map +1 -0
  172. package/dist/src/utils/numbers.d.ts +43 -0
  173. package/dist/src/utils/numbers.d.ts.map +1 -0
  174. package/dist/src/utils/numbers.js +81 -0
  175. package/dist/src/utils/numbers.js.map +1 -0
  176. package/dist/src/utils/numbers.test.d.ts +2 -0
  177. package/dist/src/utils/numbers.test.d.ts.map +1 -0
  178. package/dist/src/utils/numbers.test.js +68 -0
  179. package/dist/src/utils/numbers.test.js.map +1 -0
  180. package/package.json +9 -3
  181. package/src/api/auth.ts +72 -129
  182. package/src/api/client.ts +78 -0
  183. package/src/api/fetch-error.ts +8 -0
  184. package/src/api/index.ts +2 -0
  185. package/src/api/portal.ts +56 -0
  186. package/src/assets/DefaultAvatar.tsx +74 -0
  187. package/src/components/Dropdown/AccountAddress.tsx +111 -0
  188. package/src/components/Dropdown/AccountAssets.tsx +110 -0
  189. package/src/components/Dropdown/AccountBalance.tsx +38 -0
  190. package/src/components/Dropdown/ConnectedTrigger.tsx +56 -0
  191. package/src/components/Dropdown/Content.tsx +142 -0
  192. package/src/components/Dropdown/DisconnectedTrigger.tsx +35 -0
  193. package/src/components/Dropdown/Dropdown.tsx +81 -0
  194. package/src/components/Dropdown/README.md +46 -0
  195. package/src/components/Dropdown/WelcomeBlock.tsx +92 -0
  196. package/src/components/Dropdown/index.ts +2 -0
  197. package/src/components/index.ts +1 -0
  198. package/src/hooks/constants.ts +1 -0
  199. package/src/hooks/index.ts +1 -1
  200. package/src/hooks/useAssetsUSDConversion.ts +31 -0
  201. package/src/hooks/useAuthenticateWithWallet.ts +105 -0
  202. package/src/hooks/useCreateAccount.ts +19 -5
  203. package/src/hooks/useCreateSession.ts +22 -9
  204. package/src/hooks/useDropdownData.ts +130 -0
  205. package/src/hooks/useEnsureNoSessionAndFetchNonce.ts +1 -0
  206. package/src/hooks/useGetAccountByAddress.ts +2 -2
  207. package/src/hooks/useGetAccountByMezoId.ts +2 -2
  208. package/src/hooks/useGetCurrentAccount.ts +46 -6
  209. package/src/hooks/useGetSession.ts +1 -1
  210. package/src/hooks/useLinkAccount.ts +18 -5
  211. package/src/hooks/usePortalApiClient.ts +6 -0
  212. package/src/hooks/useSignInWithDiscord.ts +14 -5
  213. package/src/hooks/useSignInWithWallet.ts +14 -42
  214. package/src/hooks/useSignOut.ts +18 -4
  215. package/src/hooks/useSignUpWithWallet.ts +21 -0
  216. package/src/hooks/useUpdateMezoId.ts +19 -4
  217. package/src/hooks/useWalletAccount.ts +41 -0
  218. package/src/index.ts +1 -0
  219. package/src/provider.ts +30 -37
  220. package/src/utils/address.test.ts +44 -0
  221. package/src/utils/address.ts +43 -0
  222. package/src/utils/cryptoAssets.test.ts +59 -0
  223. package/src/utils/cryptoAssets.ts +93 -0
  224. package/src/utils/currency.test.ts +38 -0
  225. package/src/utils/currency.ts +32 -0
  226. package/src/utils/numbers.test.ts +89 -0
  227. package/src/utils/numbers.ts +110 -0
@@ -0,0 +1,105 @@
1
+ import { useSignMessage } from "wagmi"
2
+ import { useBitcoinAccount } from "@mezo-org/orangekit"
3
+ import { MutationOptions, useMutation } from "@tanstack/react-query"
4
+ import { useMemo } from "react"
5
+ import { useCreateAccount } from "./useCreateAccount"
6
+ import { useCreateSession } from "./useCreateSession"
7
+ import { useEnsureNoSessionAndFetchNonce } from "./useEnsureNoSessionAndFetchNonce"
8
+ import { createSignInWithWalletMessage } from "../utils/siww"
9
+ import { useGetAccountByAddress } from "./useGetAccountByAddress"
10
+ import type { Session } from "../api"
11
+ import useWalletAccount from "./useWalletAccount"
12
+
13
+ function useAuthenticateWithWallet(
14
+ shouldCreateAccount = false,
15
+ mutationOptions: Omit<
16
+ MutationOptions<Session>,
17
+ "mutationFn" | "mutationKey"
18
+ > = {},
19
+ ) {
20
+ const { address, chainId, connector, networkFamily } = useWalletAccount()
21
+ const { btcAddress } = useBitcoinAccount()
22
+ const { ensureNoSessionAndFetchNonce } = useEnsureNoSessionAndFetchNonce()
23
+ const { createSessionAsync } = useCreateSession()
24
+ const { createAccountAsync } = useCreateAccount()
25
+ const { signMessageAsync } = useSignMessage()
26
+ const walletAddress = useMemo(
27
+ () => (connector?.type === "orangekit" ? btcAddress : address),
28
+ [btcAddress, address, connector],
29
+ )
30
+
31
+ const { refetch: getAccountByAddress } = useGetAccountByAddress(
32
+ walletAddress,
33
+ { enabled: false, retry: false },
34
+ )
35
+
36
+ const { mutate, mutateAsync, ...signInMutationRestParameters } = useMutation({
37
+ mutationFn: async () => {
38
+ if (!address) {
39
+ throw new Error("Sign in error: Wallet not connected!")
40
+ }
41
+
42
+ const nonce = await ensureNoSessionAndFetchNonce()
43
+
44
+ const messageResult = createSignInWithWalletMessage(
45
+ btcAddress || address,
46
+ nonce,
47
+ networkFamily,
48
+ chainId,
49
+ )
50
+ const signatureResult = await signMessageAsync({
51
+ message: messageResult,
52
+ connector,
53
+ })
54
+
55
+ if (shouldCreateAccount) {
56
+ const getAccountByAddressResult = await getAccountByAddress()
57
+
58
+ if (getAccountByAddressResult.data) {
59
+ throw new Error(
60
+ "Wallet authentication failed: Account creation failed. Account already exists.",
61
+ )
62
+ }
63
+
64
+ if (getAccountByAddressResult.error) {
65
+ throw new Error(
66
+ `Wallet authentication failed: Account creation failed. Could not verify if account already exists: ${getAccountByAddressResult.error}`,
67
+ )
68
+ }
69
+
70
+ // Fetch was successful, but no account was found
71
+ try {
72
+ const createAccountResult = await createAccountAsync({
73
+ type: "wallet",
74
+ message: messageResult,
75
+ signature: signatureResult,
76
+ })
77
+
78
+ if (!createAccountResult) {
79
+ throw new Error("Failed to create new account")
80
+ }
81
+ } catch (error) {
82
+ throw new Error(
83
+ `Wallet authentication failed: Account creation failed: ${error}`,
84
+ )
85
+ }
86
+ }
87
+
88
+ return (await createSessionAsync({
89
+ type: "wallet",
90
+ message: messageResult,
91
+ signature: signatureResult,
92
+ })) as Session
93
+ // ^ By passing type: "wallet" we know for sure the return type is Session
94
+ },
95
+ ...mutationOptions,
96
+ })
97
+
98
+ return {
99
+ authenticateWithWallet: mutate,
100
+ authenticateWithWalletAsync: mutateAsync,
101
+ ...signInMutationRestParameters,
102
+ }
103
+ }
104
+
105
+ export { useAuthenticateWithWallet }
@@ -1,21 +1,35 @@
1
- import { useQueryClient, useMutation } from "@tanstack/react-query"
1
+ import {
2
+ useQueryClient,
3
+ useMutation,
4
+ MutationOptions,
5
+ DefaultError,
6
+ } from "@tanstack/react-query"
2
7
  import { useAuthApiClient } from "./useAuthApiClient"
3
8
  import { QUERY_KEYS } from "./constants"
4
- import { CreateAccountRequest } from "../api/auth"
9
+ import type { CreateAccountRequest, CreateAccountResponse } from "../api"
5
10
 
6
- export function useCreateAccount(useMutationOptions = {}) {
11
+ export function useCreateAccount(
12
+ mutationOptions: Omit<
13
+ MutationOptions<CreateAccountResponse, DefaultError, CreateAccountRequest>,
14
+ "mutationFn" | "mutationKey"
15
+ > = {},
16
+ ) {
7
17
  const queryClient = useQueryClient()
8
18
  const authApiClient = useAuthApiClient()
9
19
 
20
+ const { onSuccess: customOnSuccess, ...restMutationOptions } = mutationOptions
21
+
10
22
  const { mutate, mutateAsync, ...rest } = useMutation({
11
23
  mutationFn: (createAccountRequest: CreateAccountRequest) =>
12
24
  authApiClient.createAccount(createAccountRequest),
13
- onSuccess: () => {
25
+ onSuccess: (data, variables, context) => {
14
26
  queryClient.resetQueries({
15
27
  queryKey: [QUERY_KEYS.ACCOUNT],
16
28
  })
29
+
30
+ if (customOnSuccess) customOnSuccess(data, variables, context)
17
31
  },
18
- ...useMutationOptions,
32
+ ...restMutationOptions,
19
33
  })
20
34
 
21
35
  return { createAccount: mutate, createAccountAsync: mutateAsync, ...rest }
@@ -1,30 +1,43 @@
1
- import { useQueryClient, useMutation } from "@tanstack/react-query"
1
+ import {
2
+ useQueryClient,
3
+ useMutation,
4
+ MutationOptions,
5
+ DefaultError,
6
+ } from "@tanstack/react-query"
2
7
  import { useAuthApiClient } from "./useAuthApiClient"
3
- import { CreateSessionRequest } from "../api/auth"
8
+ import type { CreateSessionRequest, CreateSessionResponse } from "../api"
4
9
  import { QUERY_KEYS } from "./constants"
5
10
 
6
11
  /**
7
12
  * This hook is not exposed for external consumers. For creating session they
8
13
  * should use `useSignIn` hook related to the authentication method.
9
14
  */
10
- export function useCreateSession(useMutationOptions = {}) {
15
+ export function useCreateSession(
16
+ mutationOptions: Omit<
17
+ MutationOptions<CreateSessionResponse, DefaultError, CreateSessionRequest>,
18
+ "mutationFn" | "mutationKey"
19
+ > = {},
20
+ ) {
11
21
  const queryClient = useQueryClient()
12
22
  const authApiClient = useAuthApiClient()
13
23
 
24
+ const { onSuccess: customOnSuccess, ...restMutationOptions } = mutationOptions
25
+
14
26
  const { mutate, mutateAsync, ...rest } = useMutation({
15
27
  mutationFn: (createSessionRequest: CreateSessionRequest) =>
16
28
  authApiClient.createSession(createSessionRequest),
17
- onSuccess: () => {
18
- // Since `getSession` query also has code in it's query keys we can't
19
- // set query data for a specific query, because we don't know if the
20
- // code was used or not. Because of that we just reset all getSession
21
- // queries
29
+ onSuccess: (data, variables, context) => {
30
+ // Since `getSession` query also has code in its query keys we can't set
31
+ // query data for a specific query, because we don't know if the code was
32
+ // used or not. Because of that we just reset all getSession queries
22
33
  queryClient.resetQueries({ queryKey: [QUERY_KEYS.SESSION] })
23
34
  queryClient.resetQueries({
24
35
  queryKey: [QUERY_KEYS.ACCOUNT, QUERY_KEYS.CURRENT],
25
36
  })
37
+
38
+ if (customOnSuccess) customOnSuccess(data, variables, context)
26
39
  },
27
- ...useMutationOptions,
40
+ ...restMutationOptions,
28
41
  })
29
42
 
30
43
  return { createSession: mutate, createSessionAsync: mutateAsync, ...rest }
@@ -0,0 +1,130 @@
1
+ import { useBitcoinAccount } from "@mezo-org/orangekit"
2
+ import { useGetCurrentAccount } from "."
3
+ import {
4
+ CryptoAssetKey,
5
+ getCryptoAsset,
6
+ isBitcoinLikeCryptoAsset,
7
+ isUsdLikeCryptoAsset,
8
+ } from "../utils/cryptoAssets"
9
+ import { fromFixedPoint } from "../utils/numbers"
10
+ import { useAssetsUsdConversion } from "./useAssetsUSDConversion"
11
+ import useWalletAccount from "./useWalletAccount"
12
+
13
+ /**
14
+ * Formats crypto asset to balance details (balance in USD and decimals)
15
+ * @param type The type of crypto asset
16
+ * @param balance The balance of crypto asset
17
+ * @param btcUsdConversionRate The USD conversion rate
18
+ * @returns The balance details
19
+ */
20
+ export function formatCryptoAsset(
21
+ type: CryptoAssetKey,
22
+ balance: bigint,
23
+ btcUsdConversionRate: number,
24
+ ) {
25
+ const { decimals } = getCryptoAsset(type)
26
+
27
+ const nativeBalance = fromFixedPoint(balance, decimals, 4)
28
+ let balanceInUsd = 0
29
+ if (isBitcoinLikeCryptoAsset(type)) {
30
+ balanceInUsd = nativeBalance * btcUsdConversionRate
31
+ }
32
+ if (isUsdLikeCryptoAsset(type)) {
33
+ balanceInUsd = nativeBalance
34
+ }
35
+
36
+ return {
37
+ type,
38
+ balance: nativeBalance,
39
+ balanceInUsd,
40
+ decimals,
41
+ }
42
+ }
43
+
44
+ export type DropdownCryptoAsset<T = CryptoAssetKey> = {
45
+ type: T
46
+ balance: bigint
47
+ }
48
+
49
+ type DropdownDetailedCryptoAsset = ReturnType<typeof formatCryptoAsset>
50
+
51
+ export type DropdownData = {
52
+ mezoId: string
53
+ address: string
54
+ walletType: "bitcoin" | "evm"
55
+ totalBalanceInUsd: number
56
+ formattedNativeAssets: DropdownDetailedCryptoAsset[]
57
+ matsnet: {
58
+ mats: number
59
+ formattedAssets: DropdownDetailedCryptoAsset[]
60
+ }
61
+ }
62
+
63
+ type UseDropdownDataOptions = {
64
+ // This is workaround. It should be read from contracts or fetched from API.
65
+ evmNativeAssets?: DropdownCryptoAsset[]
66
+ matsnetAssets?: DropdownCryptoAsset[]
67
+ }
68
+
69
+ /**
70
+ * Aggregates dropdown data for Dropdown component.
71
+ * @param options - Options for the dropdown data.
72
+ * @param options.evmNativeAssets - Native assets for EVM wallet.
73
+ * @param options.matsnetAssets - Matsnet tokens. Rendered as separate section.
74
+ * @returns Dropdown data.
75
+ */
76
+ export default function useDropdownData(
77
+ options?: UseDropdownDataOptions,
78
+ ): DropdownData | null {
79
+ const { evmNativeAssets = [], matsnetAssets = [] } = options || {}
80
+
81
+ const { btcBalance } = useBitcoinAccount()
82
+ const { address, networkFamily, isConnected } = useWalletAccount()
83
+ const { data: passportAccount } = useGetCurrentAccount()
84
+ const { data: assetsUsdConversion } = useAssetsUsdConversion()
85
+
86
+ if (!address || !isConnected || !passportAccount) {
87
+ return null
88
+ }
89
+
90
+ const mezoId = passportAccount.mezoId!
91
+
92
+ const nativeAssets: DropdownCryptoAsset[] =
93
+ networkFamily === "bitcoin"
94
+ ? [
95
+ {
96
+ type: "btc",
97
+ balance: BigInt(btcBalance?.total ?? 0),
98
+ },
99
+ ]
100
+ : evmNativeAssets
101
+
102
+ const btcUsdConversionRate = Number(assetsUsdConversion?.btcUsd ?? 0)
103
+
104
+ const formattedNativeAssets = nativeAssets.map((asset) =>
105
+ formatCryptoAsset(asset.type, asset.balance, btcUsdConversionRate),
106
+ )
107
+
108
+ const totalBalanceInUsd = formattedNativeAssets.reduce(
109
+ (sum, value) => sum + value.balanceInUsd,
110
+ 0,
111
+ )
112
+
113
+ const matsBalance = passportAccount.mats.totalMats
114
+
115
+ const matsnetFormattedAssets = matsnetAssets.map((asset) =>
116
+ formatCryptoAsset(asset.type, asset.balance, btcUsdConversionRate),
117
+ )
118
+
119
+ return {
120
+ mezoId,
121
+ address,
122
+ walletType: networkFamily,
123
+ totalBalanceInUsd,
124
+ formattedNativeAssets,
125
+ matsnet: {
126
+ mats: matsBalance,
127
+ formattedAssets: matsnetFormattedAssets,
128
+ },
129
+ }
130
+ }
@@ -5,6 +5,7 @@ import { useSignOut } from "./useSignOut"
5
5
  function useEnsureNoSessionAndFetchNonce() {
6
6
  const { refetch: getSession } = useGetSession(undefined, {
7
7
  enabled: false,
8
+ retry: false,
8
9
  })
9
10
  const { signOutAsync } = useSignOut()
10
11
 
@@ -2,12 +2,12 @@ import { useQuery, skipToken, UseBaseQueryOptions } from "@tanstack/react-query"
2
2
  import { useAuthApiClient } from "./useAuthApiClient"
3
3
  import { QUERY_KEYS } from "./constants"
4
4
  import { ONE_MINUTE_MS } from "../utils/time"
5
- import { GetAccountByMezoIdOrAddressResponse } from "../api/auth"
5
+ import type { GetAccountByMezoIdOrAddressResponse } from "../api"
6
6
 
7
7
  export function useGetAccountByAddress(
8
8
  address?: string,
9
9
  queryOptions: Omit<
10
- UseBaseQueryOptions<GetAccountByMezoIdOrAddressResponse>,
10
+ UseBaseQueryOptions<GetAccountByMezoIdOrAddressResponse | null>,
11
11
  "queryKey" | "queryFn"
12
12
  > = {},
13
13
  ) {
@@ -2,12 +2,12 @@ import { useQuery, skipToken, UseBaseQueryOptions } from "@tanstack/react-query"
2
2
  import { useAuthApiClient } from "./useAuthApiClient"
3
3
  import { QUERY_KEYS } from "./constants"
4
4
  import { ONE_MINUTE_MS } from "../utils/time"
5
- import { GetAccountByMezoIdOrAddressResponse } from "../api/auth"
5
+ import type { GetAccountByMezoIdOrAddressResponse } from "../api"
6
6
 
7
7
  export function useGetAccountByMezoId(
8
8
  mezoId?: string,
9
9
  queryOptions: Omit<
10
- UseBaseQueryOptions<GetAccountByMezoIdOrAddressResponse>,
10
+ UseBaseQueryOptions<GetAccountByMezoIdOrAddressResponse | null>,
11
11
  "queryKey" | "queryFn"
12
12
  > = {},
13
13
  ) {
@@ -1,20 +1,60 @@
1
- import { useQuery, UseBaseQueryOptions } from "@tanstack/react-query"
2
- import { useAuthApiClient } from "./useAuthApiClient"
3
- import { QUERY_KEYS } from "./constants"
1
+ import { UseBaseQueryOptions, useQuery } from "@tanstack/react-query"
4
2
  import { ONE_MINUTE_MS } from "../utils/time"
5
- import { GetCurrentAccountResponse } from "../api/auth"
3
+ import { QUERY_KEYS } from "./constants"
4
+ import { useAuthApiClient } from "./useAuthApiClient"
5
+ import { usePortalApiClient } from "./usePortalApiClient"
6
+ import type { GetCurrentAccountResponse } from "../api"
7
+
8
+ type GetCurrentAccountResponseWithMats = GetCurrentAccountResponse & {
9
+ mats: {
10
+ totalMats: number
11
+ }
12
+ }
6
13
 
7
14
  export function useGetCurrentAccount(
8
15
  queryOptions: Omit<
9
- UseBaseQueryOptions<GetCurrentAccountResponse>,
16
+ UseBaseQueryOptions<GetCurrentAccountResponseWithMats>,
10
17
  "queryKey" | "queryFn"
11
18
  > = {},
12
19
  ) {
13
20
  const authApiClient = useAuthApiClient()
21
+ const portalApiClient = usePortalApiClient()
14
22
 
15
23
  return useQuery({
16
24
  queryKey: [QUERY_KEYS.ACCOUNT, QUERY_KEYS.CURRENT],
17
- queryFn: () => authApiClient.getCurrentAccount(),
25
+ queryFn: async () => {
26
+ const currentAccount = await authApiClient.getCurrentAccount()
27
+ const linkedWallets = currentAccount?.linkedAccounts?.filter(
28
+ (account) => account.type === "wallet",
29
+ )
30
+ if (!linkedWallets || linkedWallets.length === 0) {
31
+ return {
32
+ ...currentAccount,
33
+ mats: {
34
+ totalMats: 0,
35
+ },
36
+ }
37
+ }
38
+
39
+ const addresses = linkedWallets.map(
40
+ (account) => account.btcAddress || account.evmAddress,
41
+ )
42
+ const mats = await Promise.all(
43
+ addresses.map((address) => portalApiClient.getPortalMats(address)),
44
+ )
45
+
46
+ const totalMats = mats.reduce(
47
+ (sumMats, currentMats) => sumMats + currentMats.totalMats,
48
+ 0,
49
+ )
50
+
51
+ return {
52
+ ...currentAccount,
53
+ mats: {
54
+ totalMats,
55
+ },
56
+ }
57
+ },
18
58
  staleTime: ONE_MINUTE_MS,
19
59
  retry: 1,
20
60
  ...queryOptions,
@@ -2,7 +2,7 @@ import { useQuery, UseBaseQueryOptions } from "@tanstack/react-query"
2
2
  import { useAuthApiClient } from "./useAuthApiClient"
3
3
  import { QUERY_KEYS } from "./constants"
4
4
  import { ONE_MINUTE_MS } from "../utils/time"
5
- import { GetSessionResponse } from "../api/auth"
5
+ import type { GetSessionResponse } from "../api"
6
6
 
7
7
  export function useGetSession(
8
8
  code?: string,
@@ -1,19 +1,32 @@
1
- import { useQueryClient, useMutation } from "@tanstack/react-query"
1
+ import {
2
+ useQueryClient,
3
+ useMutation,
4
+ MutationOptions,
5
+ DefaultError,
6
+ } from "@tanstack/react-query"
2
7
  import { useAuthApiClient } from "./useAuthApiClient"
3
- import { LinkAccountRequest } from "../api/auth"
8
+ import type { LinkAccountRequest, LinkAccountResponse } from "../api"
4
9
  import { QUERY_KEYS } from "./constants"
5
10
 
6
- export function useLinkAccount(useMutationOptions = {}) {
11
+ export function useLinkAccount(
12
+ mutationOptions: Omit<
13
+ MutationOptions<LinkAccountResponse, DefaultError, LinkAccountRequest>,
14
+ "mutationFn" | "mutationKey"
15
+ > = {},
16
+ ) {
7
17
  const queryClient = useQueryClient()
8
18
  const authApiClient = useAuthApiClient()
9
19
 
20
+ const { onSuccess: customOnSuccess, ...restMutationOptions } = mutationOptions
21
+
10
22
  const { mutate, mutateAsync, ...rest } = useMutation({
11
23
  mutationFn: (linkAccountRequest: LinkAccountRequest) =>
12
24
  authApiClient.linkAccount(linkAccountRequest),
13
- onSuccess: () => {
25
+ onSuccess: (data, variables, context) => {
14
26
  queryClient.resetQueries({ queryKey: [QUERY_KEYS.ACCOUNT] })
27
+ if (customOnSuccess) customOnSuccess(data, variables, context)
15
28
  },
16
- ...useMutationOptions,
29
+ ...restMutationOptions,
17
30
  })
18
31
 
19
32
  return { linkAccount: mutate, linkAccountAsync: mutateAsync, ...rest }
@@ -0,0 +1,6 @@
1
+ import { usePassportContext } from "./usePassportContext"
2
+
3
+ export function usePortalApiClient() {
4
+ const { portalApiClient } = usePassportContext()
5
+ return portalApiClient
6
+ }
@@ -1,19 +1,28 @@
1
- import { useMutation } from "@tanstack/react-query"
1
+ import { MutationOptions, useMutation } from "@tanstack/react-query"
2
2
  import { useCreateSession } from "./useCreateSession"
3
3
  import { useEnsureNoSessionAndFetchNonce } from "./useEnsureNoSessionAndFetchNonce"
4
+ import type { AuthenticationProviderRedirectResponse } from "../api"
4
5
 
5
- function useSignInWithDiscord() {
6
+ function useSignInWithDiscord(
7
+ mutationOptions: Omit<
8
+ MutationOptions<AuthenticationProviderRedirectResponse>,
9
+ "mutationFn" | "mutationKey"
10
+ > = {},
11
+ ) {
6
12
  const { ensureNoSessionAndFetchNonce } = useEnsureNoSessionAndFetchNonce()
7
13
  const { createSessionAsync } = useCreateSession()
8
14
 
9
15
  const { mutate, mutateAsync, ...signInMutationRestParameters } = useMutation({
10
- mutationFn: async () => {
16
+ mutationFn: async (): Promise<AuthenticationProviderRedirectResponse> => {
11
17
  await ensureNoSessionAndFetchNonce()
12
18
 
13
- return createSessionAsync({
19
+ return (await createSessionAsync({
14
20
  type: "discord",
15
- })
21
+ })) as AuthenticationProviderRedirectResponse
22
+ // ^ By passing type: "discord" we know for sure the return type is
23
+ // AuthenticationProviderRedirectResponse
16
24
  },
25
+ ...mutationOptions,
17
26
  })
18
27
 
19
28
  return {
@@ -1,48 +1,20 @@
1
- import { useAccount, useSignMessage } from "wagmi"
2
- import { useBitcoinAccount } from "@mezo-org/orangekit"
3
- import { useMutation } from "@tanstack/react-query"
4
- import { useCreateSession } from "./useCreateSession"
5
- import { createSignInWithWalletMessage } from "../utils/siww"
6
- import { useEnsureNoSessionAndFetchNonce } from "./useEnsureNoSessionAndFetchNonce"
1
+ import { MutationOptions } from "@tanstack/react-query"
2
+ import { useAuthenticateWithWallet } from "./useAuthenticateWithWallet"
3
+ import type { Session } from "../api"
7
4
 
8
- function useSignInWithWallet() {
9
- const { chainId, address, connector } = useAccount()
10
- const { btcAddress } = useBitcoinAccount()
11
- const { ensureNoSessionAndFetchNonce } = useEnsureNoSessionAndFetchNonce()
12
- const { createSessionAsync } = useCreateSession()
13
- const { signMessageAsync } = useSignMessage()
14
-
15
- const { mutate, mutateAsync, ...signInMutationRestParameters } = useMutation({
16
- mutationFn: async () => {
17
- if (!address) {
18
- throw new Error("Sign in error: Wallet not connected!")
19
- }
20
-
21
- const nonce = await ensureNoSessionAndFetchNonce()
22
-
23
- const networkFamily = btcAddress ? "bitcoin" : "evm"
24
- const messageResult = createSignInWithWalletMessage(
25
- btcAddress ? btcAddress! : address!,
26
- nonce,
27
- networkFamily,
28
- chainId,
29
- )
30
- const signatureResult = await signMessageAsync({
31
- message: messageResult,
32
- connector,
33
- })
34
- return createSessionAsync({
35
- type: "wallet",
36
- message: messageResult,
37
- signature: signatureResult,
38
- })
39
- },
40
- })
5
+ function useSignInWithWallet(
6
+ mutationOptions: Omit<
7
+ MutationOptions<Session>,
8
+ "mutationFn" | "mutationKey"
9
+ > = {},
10
+ ) {
11
+ const { authenticateWithWallet, authenticateWithWalletAsync, ...rest } =
12
+ useAuthenticateWithWallet(false, mutationOptions)
41
13
 
42
14
  return {
43
- signInWithWallet: mutate,
44
- signInWithWalletAsync: mutateAsync,
45
- ...signInMutationRestParameters,
15
+ signInWithWallet: authenticateWithWallet,
16
+ signInWithWalletAsync: authenticateWithWalletAsync,
17
+ ...rest,
46
18
  }
47
19
  }
48
20
 
@@ -1,20 +1,34 @@
1
- import { useMutation, useQueryClient } from "@tanstack/react-query"
1
+ import {
2
+ MutationOptions,
3
+ useMutation,
4
+ useQueryClient,
5
+ } from "@tanstack/react-query"
2
6
  import { useAuthApiClient } from "./useAuthApiClient"
3
7
  import { QUERY_KEYS } from "./constants"
8
+ import type { DeleteSessionResponse } from "../api"
4
9
 
5
- export function useSignOut(useMutationOptions = {}) {
10
+ export function useSignOut(
11
+ mutationOptions: Omit<
12
+ MutationOptions<DeleteSessionResponse>,
13
+ "mutationFn" | "mutationKey"
14
+ > = {},
15
+ ) {
6
16
  const queryClient = useQueryClient()
7
17
  const authApiClient = useAuthApiClient()
8
18
 
19
+ const { onSuccess: customOnSuccess, ...restMutationOptions } = mutationOptions
20
+
9
21
  const { mutate, mutateAsync, ...rest } = useMutation({
10
22
  mutationFn: () => authApiClient.deleteSession(),
11
- onSuccess: () => {
23
+ onSuccess: (data, variables, context) => {
12
24
  queryClient.resetQueries({ queryKey: [QUERY_KEYS.SESSION] })
13
25
  queryClient.resetQueries({
14
26
  queryKey: [QUERY_KEYS.ACCOUNT, QUERY_KEYS.CURRENT],
15
27
  })
28
+
29
+ if (customOnSuccess) customOnSuccess(data, variables, context)
16
30
  },
17
- ...useMutationOptions,
31
+ ...restMutationOptions,
18
32
  })
19
33
 
20
34
  return { signOut: mutate, signOutAsync: mutateAsync, ...rest }
@@ -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 }