-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Timo Glastra <[email protected]>
- Loading branch information
1 parent
ad2f84b
commit 71fd2ed
Showing
27 changed files
with
653 additions
and
230 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
5 changes: 2 additions & 3 deletions
5
apps/easypid/src/features/onboarding/hasFinishedOnboarding.ts
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
13 changes: 13 additions & 0 deletions
13
apps/easypid/src/features/onboarding/useShouldUseCloudHsm.ts
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,13 @@ | ||
import { useMMKVBoolean } from 'react-native-mmkv' | ||
import { mmkv } from '../../storage/mmkv' | ||
|
||
export function getShouldUseCloudHsm() { | ||
return mmkv.getBoolean('shouldUseCloudHsm') | ||
} | ||
|
||
export function useShouldUseCloudHsm() { | ||
return useMMKVBoolean('shouldUseCloudHsm', mmkv) | ||
} | ||
export function removeShouldUseCloudHsm() { | ||
mmkv.delete('shouldUseCloudHsm') | ||
} |
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,9 @@ | ||
import type { PropsWithChildren } from 'react' | ||
import { useBackgroundPidRefresh } from '../../hooks/useBackgroundPidRefresh' | ||
|
||
export function WithBackgroundPidRefresh({ children }: PropsWithChildren) { | ||
// Refresh PID once it reaches 1 | ||
useBackgroundPidRefresh(1) | ||
|
||
return children | ||
} |
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,67 @@ | ||
import type { MdocRecord, SdJwtVcRecord } from '@credo-ts/core' | ||
import { getBatchCredentialMetadata } from '@package/agent/src/openid4vc/batchMetadata' | ||
import { useHasInternetConnection } from '@package/app' | ||
import { useEffect, useMemo, useState } from 'react' | ||
import { type AppAgent, useAppAgent } from '../agent' | ||
import { useShouldUseCloudHsm } from '../features/onboarding/useShouldUseCloudHsm' | ||
import { RefreshPidUseCase } from '../use-cases/RefreshPidUseCase.ts' | ||
import { usePidCredential } from './usePidCredential' | ||
|
||
async function refreshPid({ agent, sdJwt, mdoc }: { agent: AppAgent; sdJwt?: SdJwtVcRecord; mdoc?: MdocRecord }) { | ||
console.log('refreshing PID') | ||
const useCase = await RefreshPidUseCase.initialize({ | ||
agent, | ||
}) | ||
|
||
await useCase.retrieveCredentialsUsingExistingRecords({ | ||
sdJwt, | ||
mdoc, | ||
}) | ||
} | ||
|
||
export function useBackgroundPidRefresh(batchThreshold: number) { | ||
const { sdJwt, mdoc } = usePidCredential() | ||
const [isRefreshing, setIsRefreshing] = useState(false) | ||
const hasInternet = useHasInternetConnection() | ||
const shouldUseCloudHsm = useShouldUseCloudHsm() | ||
const { agent } = useAppAgent() | ||
|
||
const { shouldRefreshMdoc, shouldRefreshSdJwt } = useMemo(() => { | ||
if (!shouldUseCloudHsm) return {} | ||
|
||
let shouldRefreshSdJwt = false | ||
if (sdJwt) { | ||
const sdJwtBatch = getBatchCredentialMetadata(sdJwt) | ||
if (sdJwtBatch) { | ||
shouldRefreshSdJwt = sdJwtBatch.additionalCredentials.length <= batchThreshold | ||
} | ||
} | ||
|
||
let shouldRefreshMdoc = false | ||
if (mdoc) { | ||
const mdocBatch = getBatchCredentialMetadata(mdoc) | ||
if (mdocBatch) { | ||
shouldRefreshMdoc = mdocBatch.additionalCredentials.length <= batchThreshold | ||
} | ||
} | ||
|
||
return { | ||
shouldRefreshSdJwt, | ||
shouldRefreshMdoc, | ||
} | ||
}, [sdJwt, mdoc, batchThreshold, shouldUseCloudHsm]) | ||
|
||
useEffect(() => { | ||
if (isRefreshing || !hasInternet || !shouldUseCloudHsm) return | ||
|
||
if (shouldRefreshMdoc || shouldRefreshSdJwt) { | ||
setIsRefreshing(true) | ||
|
||
refreshPid({ | ||
agent, | ||
sdJwt: shouldRefreshSdJwt ? sdJwt : undefined, | ||
mdoc: shouldRefreshMdoc ? mdoc : undefined, | ||
}).finally(() => setIsRefreshing(false)) | ||
} | ||
}, [shouldRefreshMdoc, shouldRefreshSdJwt, hasInternet, agent, isRefreshing, mdoc, sdJwt, shouldUseCloudHsm]) | ||
} |
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,3 @@ | ||
import { MMKV } from 'react-native-mmkv' | ||
|
||
export const mmkv = new MMKV() |
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
Oops, something went wrong.