forked from MystenLabs/sui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wallet-ext: ui hooks for qredo (MystenLabs#12018)
* update bg client for new messages * use hooks for api and data from bg service - use queries for that data that invalidate using messages from bg service * allow queries bypass persisted data using metadata part of APPS-760
- Loading branch information
1 parent
1c604c2
commit e4ab6d1
Showing
7 changed files
with
227 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { useEffect, useState } from 'react'; | ||
|
||
import { useBackgroundClient } from './useBackgroundClient'; | ||
import { useQredoInfo } from './useQredoInfo'; | ||
import { QredoAPI } from '_src/shared/qredo-api'; | ||
|
||
const API_INSTANCES: Record<string, QredoAPI> = {}; | ||
|
||
export function useQredoAPI(qredoID?: string) { | ||
const backgroundClient = useBackgroundClient(); | ||
const { data, isLoading, error } = useQredoInfo(qredoID); | ||
const [api, setAPI] = useState( | ||
() => (qredoID && API_INSTANCES[qredoID]) || null | ||
); | ||
useEffect(() => { | ||
if ( | ||
data?.qredoInfo?.apiUrl && | ||
data?.qredoInfo?.accessToken && | ||
qredoID | ||
) { | ||
const instance = API_INSTANCES[qredoID]; | ||
if ( | ||
instance && | ||
instance.accessToken !== data.qredoInfo.accessToken | ||
) { | ||
instance.accessToken = data.qredoInfo.accessToken; | ||
} else if (!instance) { | ||
API_INSTANCES[qredoID] = new QredoAPI( | ||
qredoID, | ||
data.qredoInfo.apiUrl, | ||
{ | ||
accessTokenRenewalFN: async (qredoID) => | ||
( | ||
await backgroundClient.getQredoConnectionInfo( | ||
qredoID, | ||
true | ||
) | ||
).qredoInfo?.accessToken || null, | ||
accessToken: data.qredoInfo.accessToken, | ||
} | ||
); | ||
} | ||
} | ||
setAPI((qredoID && API_INSTANCES[qredoID]) || null); | ||
}, [ | ||
backgroundClient, | ||
data?.qredoInfo?.apiUrl, | ||
data?.qredoInfo?.accessToken, | ||
qredoID, | ||
]); | ||
return [api, isLoading, error] as const; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { useQuery } from '@tanstack/react-query'; | ||
|
||
import { useBackgroundClient } from './useBackgroundClient'; | ||
|
||
export function useQredoInfo(qredoID?: string) { | ||
const backgroundClient = useBackgroundClient(); | ||
return useQuery({ | ||
queryKey: ['qredo', 'info', qredoID], | ||
queryFn: async () => backgroundClient.getQredoConnectionInfo(qredoID!), | ||
enabled: !!qredoID, | ||
staleTime: 0, | ||
refetchInterval: 1000, | ||
meta: { skipPersistedCache: true }, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { useQuery } from '@tanstack/react-query'; | ||
|
||
import { useBackgroundClient } from '../../hooks/useBackgroundClient'; | ||
import { useQredoAPI } from '../../hooks/useQredoAPI'; | ||
import { type GetWalletsParams } from '_src/shared/qredo-api'; | ||
|
||
export function useQredoUIPendingRequest(requestID?: string) { | ||
const backgroundClient = useBackgroundClient(); | ||
return useQuery({ | ||
queryKey: ['qredo-connect', 'pending-request', requestID], | ||
queryFn: async () => | ||
await backgroundClient.fetchPendingQredoConnectRequest(requestID!), | ||
staleTime: 0, | ||
refetchInterval: 1000, | ||
enabled: !!requestID, | ||
meta: { skipPersistedCache: true }, | ||
}); | ||
} | ||
|
||
export function useFetchQredoAccounts( | ||
qredoID: string, | ||
enabled?: boolean, | ||
params?: GetWalletsParams | ||
) { | ||
const [api, isAPILoading, apiInitError] = useQredoAPI(qredoID); | ||
return useQuery({ | ||
queryKey: ['qredo', 'fetch', 'accounts', qredoID, api, apiInitError], | ||
queryFn: async () => { | ||
if (api) { | ||
return (await api.getWallets(params)).wallets; | ||
} | ||
throw apiInitError | ||
? apiInitError | ||
: new Error('Qredo API initialization failed'); | ||
}, | ||
enabled: | ||
!!qredoID && | ||
(enabled ?? true) && | ||
!isAPILoading && | ||
!!(api || apiInitError), | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { type UIQredoPendingRequest } from '_src/background/qredo/types'; | ||
|
||
export function isUntrustedQredoConnect({ | ||
apiUrl, | ||
origin, | ||
}: UIQredoPendingRequest) { | ||
try { | ||
return ( | ||
new URL(origin).protocol !== 'https:' || | ||
new URL(apiUrl).protocol !== 'https:' | ||
); | ||
} catch (e) { | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters