-
Notifications
You must be signed in to change notification settings - Fork 13
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 <timo@animo.id>
- Loading branch information
1 parent
d8aa3a1
commit 5330961
Showing
26 changed files
with
664 additions
and
218 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
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 './hasFinishedOnboarding' | ||
|
||
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,88 @@ | ||
import { 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 { credentials } = usePidCredential() | ||
const [isRefreshing, setIsRefreshing] = useState(false) | ||
const hasInternet = useHasInternetConnection() | ||
const shouldUseCloudHsm = useShouldUseCloudHsm() | ||
const { agent } = useAppAgent() | ||
|
||
const { sdJwt, mdoc } = useMemo(() => { | ||
if (!shouldUseCloudHsm) return {} | ||
const sdJwt = credentials?.find((c) => c.record instanceof SdJwtVcRecord)?.record as SdJwtVcRecord | undefined | ||
const mdoc = credentials?.find((c) => c.record instanceof MdocRecord)?.record as MdocRecord | undefined | ||
|
||
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 { | ||
sdJwt: sdJwt | ||
? { | ||
record: sdJwt, | ||
shouldRefresh: shouldRefreshSdJwt, | ||
} | ||
: undefined, | ||
mdoc: mdoc | ||
? { | ||
record: mdoc, | ||
shouldRefresh: shouldRefreshMdoc, | ||
} | ||
: undefined, | ||
} | ||
}, [credentials, batchThreshold, shouldUseCloudHsm]) | ||
|
||
useEffect(() => { | ||
if (isRefreshing || !hasInternet || !shouldUseCloudHsm) return | ||
|
||
if (sdJwt?.shouldRefresh || mdoc?.shouldRefresh) { | ||
setIsRefreshing(true) | ||
|
||
refreshPid({ | ||
agent, | ||
sdJwt: sdJwt?.shouldRefresh ? sdJwt?.record : undefined, | ||
mdoc: mdoc?.shouldRefresh ? mdoc?.record : undefined, | ||
}).finally(() => setIsRefreshing(false)) | ||
} | ||
}, [ | ||
sdJwt?.shouldRefresh, | ||
mdoc?.shouldRefresh, | ||
hasInternet, | ||
agent, | ||
isRefreshing, | ||
mdoc?.record, | ||
sdJwt?.record, | ||
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
Oops, something went wrong.