@mezo-org/passport 0.4.0-dev.1 → 0.4.0-dev.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/api/auth.d.ts +57 -52
- package/dist/src/api/auth.d.ts.map +1 -1
- package/dist/src/api/auth.js +22 -53
- package/dist/src/api/auth.js.map +1 -1
- package/dist/src/api/client.d.ts +24 -0
- package/dist/src/api/client.d.ts.map +1 -0
- package/dist/src/api/client.js +54 -0
- package/dist/src/api/client.js.map +1 -0
- package/dist/src/api/fetch-error.d.ts +5 -0
- package/dist/src/api/fetch-error.d.ts.map +1 -0
- package/dist/src/api/fetch-error.js +8 -0
- package/dist/src/api/fetch-error.js.map +1 -0
- package/dist/src/api/index.d.ts +3 -0
- package/dist/src/api/index.d.ts.map +1 -0
- package/dist/src/api/index.js +3 -0
- package/dist/src/api/index.js.map +1 -0
- package/dist/src/api/portal.d.ts +32 -0
- package/dist/src/api/portal.d.ts.map +1 -0
- package/dist/src/api/portal.js +23 -0
- package/dist/src/api/portal.js.map +1 -0
- package/dist/src/assets/DefaultAvatar.d.ts +5 -0
- package/dist/src/assets/DefaultAvatar.d.ts.map +1 -0
- package/dist/src/assets/DefaultAvatar.js +21 -0
- package/dist/src/assets/DefaultAvatar.js.map +1 -0
- package/dist/src/components/Dropdown/AccountAddress.d.ts +8 -0
- package/dist/src/components/Dropdown/AccountAddress.d.ts.map +1 -0
- package/dist/src/components/Dropdown/AccountAddress.js +66 -0
- package/dist/src/components/Dropdown/AccountAddress.js.map +1 -0
- package/dist/src/components/Dropdown/AccountAssets.d.ts +14 -0
- package/dist/src/components/Dropdown/AccountAssets.d.ts.map +1 -0
- package/dist/src/components/Dropdown/AccountAssets.js +44 -0
- package/dist/src/components/Dropdown/AccountAssets.js.map +1 -0
- package/dist/src/components/Dropdown/AccountBalance.d.ts +7 -0
- package/dist/src/components/Dropdown/AccountBalance.d.ts.map +1 -0
- package/dist/src/components/Dropdown/AccountBalance.js +18 -0
- package/dist/src/components/Dropdown/AccountBalance.js.map +1 -0
- package/dist/src/components/Dropdown/ConnectedTrigger.d.ts +7 -0
- package/dist/src/components/Dropdown/ConnectedTrigger.d.ts.map +1 -0
- package/dist/src/components/Dropdown/ConnectedTrigger.js +30 -0
- package/dist/src/components/Dropdown/ConnectedTrigger.js.map +1 -0
- package/dist/src/components/Dropdown/Content.d.ts +9 -0
- package/dist/src/components/Dropdown/Content.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Content.js +69 -0
- package/dist/src/components/Dropdown/Content.js.map +1 -0
- package/dist/src/components/Dropdown/DisconnectedTrigger.d.ts +7 -0
- package/dist/src/components/Dropdown/DisconnectedTrigger.d.ts.map +1 -0
- package/dist/src/components/Dropdown/DisconnectedTrigger.js +13 -0
- package/dist/src/components/Dropdown/DisconnectedTrigger.js.map +1 -0
- package/dist/src/components/Dropdown/Dropdown.d.ts +20 -0
- package/dist/src/components/Dropdown/Dropdown.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Dropdown.js +64 -0
- package/dist/src/components/Dropdown/Dropdown.js.map +1 -0
- package/dist/src/components/Dropdown/WelcomeBlock.d.ts +8 -0
- package/dist/src/components/Dropdown/WelcomeBlock.d.ts.map +1 -0
- package/dist/src/components/Dropdown/WelcomeBlock.js +44 -0
- package/dist/src/components/Dropdown/WelcomeBlock.js.map +1 -0
- package/dist/src/components/Dropdown/index.d.ts +3 -0
- package/dist/src/components/Dropdown/index.d.ts.map +1 -0
- package/dist/src/components/Dropdown/index.js +2 -0
- package/dist/src/components/Dropdown/index.js.map +1 -0
- package/dist/src/components/index.d.ts +2 -0
- package/dist/src/components/index.d.ts.map +1 -0
- package/dist/src/components/index.js +2 -0
- package/dist/src/components/index.js.map +1 -0
- package/dist/src/hooks/constants.d.ts +3 -3
- package/dist/src/hooks/constants.js +4 -4
- package/dist/src/hooks/constants.js.map +1 -1
- package/dist/src/hooks/index.d.ts +1 -4
- package/dist/src/hooks/index.d.ts.map +1 -1
- package/dist/src/hooks/index.js +1 -4
- package/dist/src/hooks/index.js.map +1 -1
- package/dist/src/hooks/useAssetsUSDConversion.d.ts +8 -0
- package/dist/src/hooks/useAssetsUSDConversion.d.ts.map +1 -0
- package/dist/src/hooks/useAssetsUSDConversion.js +21 -0
- package/dist/src/hooks/useAssetsUSDConversion.js.map +1 -0
- package/dist/src/hooks/useAuthApiClient.d.ts +1 -1
- package/dist/src/hooks/useAuthApiClient.d.ts.map +1 -1
- package/dist/src/hooks/useAuthenticateWithWallet.d.ts +73 -0
- package/dist/src/hooks/useAuthenticateWithWallet.d.ts.map +1 -0
- package/dist/src/hooks/useAuthenticateWithWallet.js +69 -0
- package/dist/src/hooks/useAuthenticateWithWallet.js.map +1 -0
- package/dist/src/hooks/useCreateAccount.d.ts +41 -137
- package/dist/src/hooks/useCreateAccount.d.ts.map +1 -1
- package/dist/src/hooks/useCreateAccount.js +8 -10
- package/dist/src/hooks/useCreateAccount.js.map +1 -1
- package/dist/src/hooks/useCreateSession.d.ts +12 -182
- package/dist/src/hooks/useCreateSession.d.ts.map +1 -1
- package/dist/src/hooks/useCreateSession.js +12 -18
- package/dist/src/hooks/useCreateSession.js.map +1 -1
- package/dist/src/hooks/useDropdownData.d.ts +45 -0
- package/dist/src/hooks/useDropdownData.d.ts.map +1 -0
- package/dist/src/hooks/useDropdownData.js +74 -0
- package/dist/src/hooks/useDropdownData.js.map +1 -0
- package/dist/src/hooks/useEnsureNoSessionAndFetchNonce.d.ts +5 -0
- package/dist/src/hooks/useEnsureNoSessionAndFetchNonce.d.ts.map +1 -0
- package/dist/src/hooks/useEnsureNoSessionAndFetchNonce.js +34 -0
- package/dist/src/hooks/useEnsureNoSessionAndFetchNonce.js.map +1 -0
- package/dist/src/hooks/useGetAccountByAddress.d.ts +3 -5
- package/dist/src/hooks/useGetAccountByAddress.d.ts.map +1 -1
- package/dist/src/hooks/useGetAccountByAddress.js +3 -4
- package/dist/src/hooks/useGetAccountByAddress.js.map +1 -1
- package/dist/src/hooks/useGetAccountByMezoId.d.ts +3 -5
- package/dist/src/hooks/useGetAccountByMezoId.d.ts.map +1 -1
- package/dist/src/hooks/useGetAccountByMezoId.js +3 -4
- package/dist/src/hooks/useGetAccountByMezoId.js.map +1 -1
- package/dist/src/hooks/useGetCurrentAccount.d.ts +14 -8
- package/dist/src/hooks/useGetCurrentAccount.d.ts.map +1 -1
- package/dist/src/hooks/useGetCurrentAccount.js +28 -6
- package/dist/src/hooks/useGetCurrentAccount.js.map +1 -1
- package/dist/src/hooks/useGetSession.d.ts +3 -20
- package/dist/src/hooks/useGetSession.d.ts.map +1 -1
- package/dist/src/hooks/useGetSession.js +2 -2
- package/dist/src/hooks/useGetSession.js.map +1 -1
- package/dist/src/hooks/useLinkAccount.d.ts +15 -121
- package/dist/src/hooks/useLinkAccount.d.ts.map +1 -1
- package/dist/src/hooks/useLinkAccount.js +8 -21
- package/dist/src/hooks/useLinkAccount.js.map +1 -1
- package/dist/src/hooks/usePortalApiClient.d.ts +2 -0
- package/dist/src/hooks/usePortalApiClient.d.ts.map +1 -0
- package/dist/src/hooks/usePortalApiClient.js +6 -0
- package/dist/src/hooks/usePortalApiClient.js.map +1 -0
- package/dist/src/hooks/useSignInWithDiscord.d.ts +12 -181
- package/dist/src/hooks/useSignInWithDiscord.d.ts.map +1 -1
- package/dist/src/hooks/useSignInWithDiscord.js +11 -22
- package/dist/src/hooks/useSignInWithDiscord.js.map +1 -1
- package/dist/src/hooks/useSignInWithWallet.d.ts +12 -181
- package/dist/src/hooks/useSignInWithWallet.d.ts.map +1 -1
- package/dist/src/hooks/useSignInWithWallet.js +6 -43
- package/dist/src/hooks/useSignInWithWallet.js.map +1 -1
- package/dist/src/hooks/useSignOut.d.ts +12 -28
- package/dist/src/hooks/useSignOut.d.ts.map +1 -1
- package/dist/src/hooks/useSignOut.js +11 -6
- package/dist/src/hooks/useSignOut.js.map +1 -1
- package/dist/src/hooks/useSignUpWithWallet.d.ts +73 -0
- package/dist/src/hooks/useSignUpWithWallet.d.ts.map +1 -0
- package/dist/src/hooks/useSignUpWithWallet.js +11 -0
- package/dist/src/hooks/useSignUpWithWallet.js.map +1 -0
- package/dist/src/hooks/useUpdateMezoId.d.ts +30 -82
- package/dist/src/hooks/useUpdateMezoId.d.ts.map +1 -1
- package/dist/src/hooks/useUpdateMezoId.js +8 -12
- package/dist/src/hooks/useUpdateMezoId.js.map +1 -1
- package/dist/src/hooks/useWalletAccount.d.ts +12 -0
- package/dist/src/hooks/useWalletAccount.d.ts.map +1 -0
- package/dist/src/hooks/useWalletAccount.js +28 -0
- package/dist/src/hooks/useWalletAccount.js.map +1 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +1 -0
- package/dist/src/index.js.map +1 -1
- package/dist/src/provider.d.ts +10 -13
- package/dist/src/provider.d.ts.map +1 -1
- package/dist/src/provider.js +11 -20
- package/dist/src/provider.js.map +1 -1
- package/dist/src/utils/address.d.ts +15 -0
- package/dist/src/utils/address.d.ts.map +1 -0
- package/dist/src/utils/address.js +35 -0
- package/dist/src/utils/address.js.map +1 -0
- package/dist/src/utils/address.test.d.ts +2 -0
- package/dist/src/utils/address.test.d.ts.map +1 -0
- package/dist/src/utils/address.test.js +32 -0
- package/dist/src/utils/address.test.js.map +1 -0
- package/dist/src/utils/cryptoAssets.d.ts +28 -0
- package/dist/src/utils/cryptoAssets.d.ts.map +1 -0
- package/dist/src/utils/cryptoAssets.js +73 -0
- package/dist/src/utils/cryptoAssets.js.map +1 -0
- package/dist/src/utils/cryptoAssets.test.d.ts +2 -0
- package/dist/src/utils/cryptoAssets.test.d.ts.map +1 -0
- package/dist/src/utils/cryptoAssets.test.js +50 -0
- package/dist/src/utils/cryptoAssets.test.js.map +1 -0
- package/dist/src/utils/currency.d.ts +14 -0
- package/dist/src/utils/currency.d.ts.map +1 -0
- package/dist/src/utils/currency.js +27 -0
- package/dist/src/utils/currency.js.map +1 -0
- package/dist/src/utils/currency.test.d.ts +2 -0
- package/dist/src/utils/currency.test.d.ts.map +1 -0
- package/dist/src/utils/currency.test.js +34 -0
- package/dist/src/utils/currency.test.js.map +1 -0
- package/dist/src/utils/numbers.d.ts +26 -0
- package/dist/src/utils/numbers.d.ts.map +1 -0
- package/dist/src/utils/numbers.js +40 -0
- package/dist/src/utils/numbers.js.map +1 -0
- package/dist/src/utils/numbers.test.d.ts +2 -0
- package/dist/src/utils/numbers.test.d.ts.map +1 -0
- package/dist/src/utils/numbers.test.js +54 -0
- package/dist/src/utils/numbers.test.js.map +1 -0
- package/package.json +10 -4
- package/src/api/auth.ts +104 -129
- package/src/api/client.ts +78 -0
- package/src/api/fetch-error.ts +8 -0
- package/src/api/index.ts +2 -0
- package/src/api/portal.ts +56 -0
- package/src/assets/DefaultAvatar.tsx +74 -0
- package/src/components/Dropdown/AccountAddress.tsx +130 -0
- package/src/components/Dropdown/AccountAssets.tsx +110 -0
- package/src/components/Dropdown/AccountBalance.tsx +38 -0
- package/src/components/Dropdown/ConnectedTrigger.tsx +56 -0
- package/src/components/Dropdown/Content.tsx +148 -0
- package/src/components/Dropdown/DisconnectedTrigger.tsx +36 -0
- package/src/components/Dropdown/Dropdown.tsx +111 -0
- package/src/components/Dropdown/README.md +51 -0
- package/src/components/Dropdown/WelcomeBlock.tsx +92 -0
- package/src/components/Dropdown/index.ts +2 -0
- package/src/components/index.ts +1 -0
- package/src/hooks/constants.ts +4 -4
- package/src/hooks/index.ts +1 -4
- package/src/hooks/useAssetsUSDConversion.ts +31 -0
- package/src/hooks/useAuthenticateWithWallet.ts +98 -0
- package/src/hooks/useCreateAccount.ts +20 -11
- package/src/hooks/useCreateSession.ts +24 -18
- package/src/hooks/useDropdownData.ts +131 -0
- package/src/hooks/useEnsureNoSessionAndFetchNonce.ts +46 -0
- package/src/hooks/useGetAccountByAddress.ts +11 -5
- package/src/hooks/useGetAccountByMezoId.ts +11 -5
- package/src/hooks/useGetCurrentAccount.ts +53 -7
- package/src/hooks/useGetSession.ts +10 -3
- package/src/hooks/useLinkAccount.ts +19 -31
- package/src/hooks/usePortalApiClient.ts +6 -0
- package/src/hooks/useSignInWithDiscord.ts +19 -28
- package/src/hooks/useSignInWithWallet.ts +14 -54
- package/src/hooks/useSignOut.ts +22 -6
- package/src/hooks/useSignUpWithWallet.ts +21 -0
- package/src/hooks/useUpdateMezoId.ts +20 -12
- package/src/hooks/useWalletAccount.ts +53 -0
- package/src/index.ts +1 -0
- package/src/provider.ts +30 -37
- package/src/utils/address.test.ts +38 -0
- package/src/utils/address.ts +43 -0
- package/src/utils/cryptoAssets.test.ts +61 -0
- package/src/utils/cryptoAssets.ts +93 -0
- package/src/utils/currency.test.ts +38 -0
- package/src/utils/currency.ts +32 -0
- package/src/utils/numbers.test.ts +73 -0
- package/src/utils/numbers.ts +55 -0
|
@@ -1,16 +1,62 @@
|
|
|
1
|
-
import { useQuery } 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"
|
|
3
|
+
import { QUERY_KEYS } from "./constants"
|
|
4
|
+
import { useAuthApiClient } from "./useAuthApiClient"
|
|
5
|
+
import { usePortalApiClient } from "./usePortalApiClient"
|
|
6
|
+
import type { GetCurrentAccountResponse } from "../api"
|
|
5
7
|
|
|
6
|
-
|
|
8
|
+
type GetCurrentAccountResponseWithMats = GetCurrentAccountResponse & {
|
|
9
|
+
mats: {
|
|
10
|
+
totalMats: number
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function useGetCurrentAccount(
|
|
15
|
+
queryOptions: Omit<
|
|
16
|
+
UseBaseQueryOptions<GetCurrentAccountResponseWithMats>,
|
|
17
|
+
"queryKey" | "queryFn"
|
|
18
|
+
> = {},
|
|
19
|
+
) {
|
|
7
20
|
const authApiClient = useAuthApiClient()
|
|
21
|
+
const portalApiClient = usePortalApiClient()
|
|
8
22
|
|
|
9
23
|
return useQuery({
|
|
10
|
-
queryKey: [QUERY_KEYS.
|
|
11
|
-
queryFn: () =>
|
|
24
|
+
queryKey: [QUERY_KEYS.ACCOUNT, QUERY_KEYS.CURRENT],
|
|
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
|
+
},
|
|
12
58
|
staleTime: ONE_MINUTE_MS,
|
|
13
59
|
retry: 1,
|
|
14
|
-
...
|
|
60
|
+
...queryOptions,
|
|
15
61
|
})
|
|
16
62
|
}
|
|
@@ -1,15 +1,22 @@
|
|
|
1
|
-
import { useQuery } from "@tanstack/react-query"
|
|
1
|
+
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 type { GetSessionResponse } from "../api"
|
|
5
6
|
|
|
6
|
-
export function useGetSession(
|
|
7
|
+
export function useGetSession(
|
|
8
|
+
code?: string,
|
|
9
|
+
queryOptions: Omit<
|
|
10
|
+
UseBaseQueryOptions<GetSessionResponse>,
|
|
11
|
+
"queryKey" | "queryFn"
|
|
12
|
+
> = {},
|
|
13
|
+
) {
|
|
7
14
|
const authApiClient = useAuthApiClient()
|
|
8
15
|
return useQuery({
|
|
9
16
|
queryKey: [QUERY_KEYS.SESSION, code],
|
|
10
17
|
queryFn: () => authApiClient.getSession(code),
|
|
11
18
|
staleTime: ONE_MINUTE_MS,
|
|
12
19
|
retry: 1,
|
|
13
|
-
...
|
|
20
|
+
...queryOptions,
|
|
14
21
|
})
|
|
15
22
|
}
|
|
@@ -1,44 +1,32 @@
|
|
|
1
|
-
import {
|
|
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
|
|
8
|
+
import type { LinkAccountRequest, LinkAccountResponse } from "../api"
|
|
4
9
|
import { QUERY_KEYS } from "./constants"
|
|
5
10
|
|
|
6
|
-
export function useLinkAccount(
|
|
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: (data) => {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
queryClient.setQueryData(
|
|
17
|
-
[QUERY_KEYS.ACCOUNT_BY_MEZO_ID, data.user_metadata.mezoId],
|
|
18
|
-
data,
|
|
19
|
-
)
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
queryClient.setQueryData(
|
|
23
|
-
[QUERY_KEYS.ACCOUNT_BY_ADDRESS, data.user_metadata.btcAddress],
|
|
24
|
-
data,
|
|
25
|
-
)
|
|
26
|
-
queryClient.setQueryData(
|
|
27
|
-
[QUERY_KEYS.ACCOUNT_BY_ADDRESS, data.user_metadata.evmAddress],
|
|
28
|
-
data,
|
|
29
|
-
)
|
|
30
|
-
} else {
|
|
31
|
-
queryClient.resetQueries({
|
|
32
|
-
queryKey: [QUERY_KEYS.ACCOUNT_BY_MEZO_ID],
|
|
33
|
-
exact: false,
|
|
34
|
-
})
|
|
35
|
-
queryClient.resetQueries({
|
|
36
|
-
queryKey: [QUERY_KEYS.ACCOUNT_BY_ADDRESS],
|
|
37
|
-
exact: false,
|
|
38
|
-
})
|
|
39
|
-
}
|
|
25
|
+
onSuccess: (data, variables, context) => {
|
|
26
|
+
queryClient.resetQueries({ queryKey: [QUERY_KEYS.ACCOUNT] })
|
|
27
|
+
if (customOnSuccess) customOnSuccess(data, variables, context)
|
|
40
28
|
},
|
|
41
|
-
...
|
|
29
|
+
...restMutationOptions,
|
|
42
30
|
})
|
|
43
31
|
|
|
44
32
|
return { linkAccount: mutate, linkAccountAsync: mutateAsync, ...rest }
|
|
@@ -1,42 +1,33 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { useMutation } from "@tanstack/react-query"
|
|
3
|
-
import { useGetSession } from "./useGetSession"
|
|
1
|
+
import { MutationOptions, useMutation } from "@tanstack/react-query"
|
|
4
2
|
import { useCreateSession } from "./useCreateSession"
|
|
3
|
+
import { useEnsureNoSessionAndFetchNonce } from "./useEnsureNoSessionAndFetchNonce"
|
|
4
|
+
import type { AuthenticationProviderRedirectResponse } from "../api"
|
|
5
5
|
|
|
6
|
-
function useSignInWithDiscord(
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
6
|
+
function useSignInWithDiscord(
|
|
7
|
+
mutationOptions: Omit<
|
|
8
|
+
MutationOptions<AuthenticationProviderRedirectResponse>,
|
|
9
|
+
"mutationFn" | "mutationKey"
|
|
10
|
+
> = {},
|
|
11
|
+
) {
|
|
12
|
+
const { ensureNoSessionAndFetchNonce } = useEnsureNoSessionAndFetchNonce()
|
|
11
13
|
const { createSessionAsync } = useCreateSession()
|
|
12
14
|
|
|
13
15
|
const { mutate, mutateAsync, ...signInMutationRestParameters } = useMutation({
|
|
14
|
-
mutationFn: async () => {
|
|
15
|
-
|
|
16
|
-
throw new Error("Sign in error: User not connected!")
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const getSessionResult = await getSession()
|
|
20
|
-
|
|
21
|
-
// TODO: We should create a separate endpoint that will always return nonce
|
|
22
|
-
if (!getSessionResult.data || !("nonce" in getSessionResult.data)) {
|
|
23
|
-
if (getSessionResult.error) {
|
|
24
|
-
throw new Error(`Sign in error: ${getSessionResult.error}`)
|
|
25
|
-
}
|
|
26
|
-
throw new Error(
|
|
27
|
-
"Sign in error: Nonce not available! Remove the session first.",
|
|
28
|
-
)
|
|
29
|
-
}
|
|
16
|
+
mutationFn: async (): Promise<AuthenticationProviderRedirectResponse> => {
|
|
17
|
+
await ensureNoSessionAndFetchNonce()
|
|
30
18
|
|
|
31
|
-
return createSessionAsync({
|
|
19
|
+
return (await createSessionAsync({
|
|
32
20
|
type: "discord",
|
|
33
|
-
})
|
|
21
|
+
})) as AuthenticationProviderRedirectResponse
|
|
22
|
+
// ^ By passing type: "discord" we know for sure the return type is
|
|
23
|
+
// AuthenticationProviderRedirectResponse
|
|
34
24
|
},
|
|
25
|
+
...mutationOptions,
|
|
35
26
|
})
|
|
36
27
|
|
|
37
28
|
return {
|
|
38
|
-
|
|
39
|
-
|
|
29
|
+
signInWithDiscord: mutate,
|
|
30
|
+
signInWithDiscordAsync: mutateAsync,
|
|
40
31
|
...signInMutationRestParameters,
|
|
41
32
|
}
|
|
42
33
|
}
|
|
@@ -1,60 +1,20 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { useGetSession } from "./useGetSession"
|
|
5
|
-
import { useCreateSession } from "./useCreateSession"
|
|
6
|
-
import { createSignInWithWalletMessage } from "../utils/siww"
|
|
1
|
+
import { MutationOptions } from "@tanstack/react-query"
|
|
2
|
+
import { useAuthenticateWithWallet } from "./useAuthenticateWithWallet"
|
|
3
|
+
import type { Session } from "../api"
|
|
7
4
|
|
|
8
|
-
function useSignInWithWallet(
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const { mutate, mutateAsync, ...signInMutationRestParameters } = useMutation({
|
|
18
|
-
mutationFn: async () => {
|
|
19
|
-
if (!address) {
|
|
20
|
-
throw new Error("Sign in error: User not connected!")
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const getSessionResult = await getSession()
|
|
24
|
-
|
|
25
|
-
// TODO: We should create a separate endpoint that will always return nonce
|
|
26
|
-
if (!getSessionResult.data || !("nonce" in getSessionResult.data)) {
|
|
27
|
-
if (getSessionResult.error) {
|
|
28
|
-
throw new Error(`Sign in error: ${getSessionResult.error}`)
|
|
29
|
-
}
|
|
30
|
-
throw new Error(
|
|
31
|
-
"Sign in error: Nonce not available! Remove the session first.",
|
|
32
|
-
)
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const networkFamily = btcAddress ? "bitcoin" : "evm"
|
|
36
|
-
const messageResult = createSignInWithWalletMessage(
|
|
37
|
-
btcAddress ? btcAddress! : address!,
|
|
38
|
-
getSessionResult.data.nonce,
|
|
39
|
-
networkFamily,
|
|
40
|
-
chainId,
|
|
41
|
-
)
|
|
42
|
-
const signatureResult = await signMessageAsync({
|
|
43
|
-
message: messageResult,
|
|
44
|
-
connector,
|
|
45
|
-
})
|
|
46
|
-
return createSessionAsync({
|
|
47
|
-
type: "wallet",
|
|
48
|
-
message: messageResult,
|
|
49
|
-
signature: signatureResult,
|
|
50
|
-
})
|
|
51
|
-
},
|
|
52
|
-
})
|
|
5
|
+
function useSignInWithWallet(
|
|
6
|
+
mutationOptions: Omit<
|
|
7
|
+
MutationOptions<Session>,
|
|
8
|
+
"mutationFn" | "mutationKey"
|
|
9
|
+
> = {},
|
|
10
|
+
) {
|
|
11
|
+
const { authenticateWithWallet, authenticateWithWalletAsync, ...rest } =
|
|
12
|
+
useAuthenticateWithWallet(false, mutationOptions)
|
|
53
13
|
|
|
54
14
|
return {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
...
|
|
15
|
+
signInWithWallet: authenticateWithWallet,
|
|
16
|
+
signInWithWalletAsync: authenticateWithWalletAsync,
|
|
17
|
+
...rest,
|
|
58
18
|
}
|
|
59
19
|
}
|
|
60
20
|
|
package/src/hooks/useSignOut.ts
CHANGED
|
@@ -1,18 +1,34 @@
|
|
|
1
|
-
import {
|
|
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(
|
|
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: () => {
|
|
12
|
-
queryClient.resetQueries({ queryKey: [QUERY_KEYS.SESSION]
|
|
13
|
-
queryClient.resetQueries({
|
|
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)
|
|
14
30
|
},
|
|
15
|
-
...
|
|
31
|
+
...restMutationOptions,
|
|
16
32
|
})
|
|
17
33
|
|
|
18
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 }
|
|
@@ -1,24 +1,32 @@
|
|
|
1
|
-
import {
|
|
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"
|
|
9
|
+
import type { UpdateMezoIdResponse } from "../api"
|
|
4
10
|
|
|
5
|
-
export function useUpdateMezoId(
|
|
11
|
+
export function useUpdateMezoId(
|
|
12
|
+
mutationOptions: Omit<
|
|
13
|
+
MutationOptions<UpdateMezoIdResponse, DefaultError, string>,
|
|
14
|
+
"mutationFn" | "mutationKey"
|
|
15
|
+
> = {},
|
|
16
|
+
) {
|
|
6
17
|
const queryClient = useQueryClient()
|
|
7
18
|
const authApiClient = useAuthApiClient()
|
|
8
19
|
|
|
20
|
+
const { onSuccess: customOnSuccess, ...restMutationOptions } = mutationOptions
|
|
21
|
+
|
|
9
22
|
const { mutate, mutateAsync, ...rest } = useMutation({
|
|
10
23
|
mutationFn: (newMezoId: string) => authApiClient.updateMezoId(newMezoId),
|
|
11
|
-
onSuccess: () => {
|
|
12
|
-
queryClient.resetQueries({
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
})
|
|
16
|
-
queryClient.resetQueries({
|
|
17
|
-
queryKey: [QUERY_KEYS.ACCOUNT_BY_MEZO_ID],
|
|
18
|
-
exact: false,
|
|
19
|
-
})
|
|
24
|
+
onSuccess: (data, variables, context) => {
|
|
25
|
+
queryClient.resetQueries({ queryKey: [QUERY_KEYS.ACCOUNT] })
|
|
26
|
+
|
|
27
|
+
if (customOnSuccess) customOnSuccess(data, variables, context)
|
|
20
28
|
},
|
|
21
|
-
...
|
|
29
|
+
...restMutationOptions,
|
|
22
30
|
})
|
|
23
31
|
|
|
24
32
|
return { updateMezoId: mutate, updateMezoIdAsync: mutateAsync, ...rest }
|
|
@@ -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
package/src/provider.ts
CHANGED
|
@@ -1,56 +1,49 @@
|
|
|
1
|
-
import { createContext, createElement
|
|
2
|
-
import {
|
|
1
|
+
import { createContext, createElement } from "react"
|
|
2
|
+
import { AuthApiClient, PortalApiClient } from "./api"
|
|
3
3
|
|
|
4
4
|
export interface PassportContextValue {
|
|
5
5
|
authApiClient: AuthApiClient
|
|
6
|
+
portalApiClient: PortalApiClient
|
|
7
|
+
environment?: Environment
|
|
6
8
|
}
|
|
7
9
|
|
|
8
10
|
export const PassportContext = createContext<PassportContextValue | undefined>(
|
|
9
11
|
undefined,
|
|
10
12
|
)
|
|
11
13
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
environment?: keyof typeof API_ENDPOINTS_BY_ENV
|
|
16
|
-
apiUrl?: string
|
|
17
|
-
} & (
|
|
18
|
-
| {
|
|
19
|
-
environment: keyof typeof API_ENDPOINTS_BY_ENV
|
|
20
|
-
}
|
|
21
|
-
| {
|
|
22
|
-
apiUrl: string
|
|
23
|
-
}
|
|
24
|
-
)
|
|
14
|
+
type Environment = "mainnet" | "testnet"
|
|
15
|
+
|
|
16
|
+
type PassportProviderProps = {
|
|
25
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
|
|
26
24
|
}
|
|
27
25
|
|
|
28
|
-
export function PassportProvider({
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
if (apiUrl) return apiUrl
|
|
38
|
-
|
|
39
|
-
if (!(environment! in API_ENDPOINTS_BY_ENV)) {
|
|
40
|
-
throw new Error("Wrong environment passed to PassportProvider.")
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
return API_ENDPOINTS_BY_ENV[environment!]
|
|
44
|
-
}, [environment, apiUrl])
|
|
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
|
+
}
|
|
45
35
|
|
|
46
36
|
return createElement(
|
|
47
37
|
PassportContext.Provider,
|
|
48
38
|
{
|
|
49
|
-
value:
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
39
|
+
value: {
|
|
40
|
+
authApiClient: new AuthApiClient(environment ?? "mainnet", authApiUrl),
|
|
41
|
+
portalApiClient: new PortalApiClient(
|
|
42
|
+
environment ?? "mainnet",
|
|
43
|
+
portalApiUrl,
|
|
44
|
+
),
|
|
45
|
+
environment,
|
|
46
|
+
},
|
|
54
47
|
},
|
|
55
48
|
children,
|
|
56
49
|
)
|
|
@@ -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
|
+
}
|