-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathFunkeOpenIdPresentationNotificationScreen.tsx
200 lines (175 loc) · 7.36 KB
/
FunkeOpenIdPresentationNotificationScreen.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import {
BiometricAuthenticationCancelledError,
type CredentialsForProofRequest,
type FormattedSubmissionEntrySatisfied,
getCredentialsForProofRequest,
getDisclosedAttributeNamesForDisplay,
shareProof,
} from '@package/agent'
import { useToastController } from '@package/ui'
import { useLocalSearchParams } from 'expo-router'
import { useCallback, useEffect, useState } from 'react'
import { useAppAgent } from '@easypid/agent'
import { InvalidPinError } from '@easypid/crypto/error'
import { useOverAskingAi } from '@easypid/hooks'
import { useDevelopmentMode } from '@easypid/hooks'
import { usePushToWallet } from '@package/app/src/hooks/usePushToWallet'
import { setWalletServiceProviderPin } from '../../crypto/WalletServiceProviderClient'
import { useShouldUsePinForSubmission } from '../../hooks/useShouldUsePinForPresentation'
import { addSharedActivityForCredentialsForRequest, useActivities } from '../activity/activityRecord'
import { FunkePresentationNotificationScreen } from './FunkePresentationNotificationScreen'
import type { onPinSubmitProps } from './slides/PinSlide'
type Query = { uri?: string; data?: string }
export function FunkeOpenIdPresentationNotificationScreen() {
const toast = useToastController()
const params = useLocalSearchParams<Query>()
const pushToWallet = usePushToWallet()
const { agent } = useAppAgent()
const [isDevelopmentModeEnabled] = useDevelopmentMode()
const [credentialsForRequest, setCredentialsForRequest] = useState<CredentialsForProofRequest>()
const [isSharing, setIsSharing] = useState(false)
const { activities } = useActivities({
filters: { entityId: credentialsForRequest?.verifier.entityId ?? 'NO MATCH' },
})
const lastInteractionDate = activities?.[0]?.date
const shouldUsePin = useShouldUsePinForSubmission(credentialsForRequest?.formattedSubmission)
useEffect(() => {
if (credentialsForRequest) return
getCredentialsForProofRequest({
agent,
data: params.data,
uri: params.uri,
})
.then(setCredentialsForRequest)
.catch((error) => {
toast.show('Presentation information could not be extracted.', {
message:
error instanceof Error && isDevelopmentModeEnabled ? `Development mode error: ${error.message}` : undefined,
customData: { preset: 'danger' },
})
agent.config.logger.error('Error getting credentials for request', {
error,
})
pushToWallet()
})
}, [credentialsForRequest, params.data, params.uri, toast.show, agent, pushToWallet, toast, isDevelopmentModeEnabled])
const { checkForOverAsking, isProcessingOverAsking, overAskingResponse, stopOverAsking } = useOverAskingAi()
useEffect(() => {
if (!credentialsForRequest?.formattedSubmission || !credentialsForRequest?.formattedSubmission.areAllSatisfied) {
return
}
if (isProcessingOverAsking || overAskingResponse) {
// Already generating or already has result
return
}
const submission = credentialsForRequest.formattedSubmission
const requestedCards = submission.entries
.filter((entry): entry is FormattedSubmissionEntrySatisfied => entry.isSatisfied)
.flatMap((entry) => entry.credentials)
void checkForOverAsking({
verifier: {
name: credentialsForRequest.verifier.name ?? 'No name provided',
domain: credentialsForRequest.verifier.hostName ?? 'No domain provided',
},
name: submission.name ?? 'No name provided',
purpose: submission.purpose ?? 'No purpose provided',
cards: requestedCards.map((credential) => ({
name: credential.credential.display.name ?? 'Card name',
subtitle: credential.credential.display.description ?? 'Card description',
requestedAttributes: getDisclosedAttributeNamesForDisplay(credential),
})),
})
}, [credentialsForRequest, checkForOverAsking, isProcessingOverAsking, overAskingResponse])
const handleError = useCallback(
({ reason, description, redirect = true }: { reason: string; description?: string; redirect?: boolean }) => {
setIsSharing(false)
toast.show(reason, { message: description, customData: { preset: 'danger' } })
if (redirect) pushToWallet()
return
},
[toast, pushToWallet]
)
const onProofAccept = useCallback(
async ({ pin, onPinComplete, onPinError }: onPinSubmitProps = {}) => {
stopOverAsking()
if (!credentialsForRequest) return handleError({ reason: 'No credentials selected' })
setIsSharing(true)
if (shouldUsePin) {
if (!pin) {
setIsSharing(false)
return handleError({ reason: 'PIN authentication failed' })
}
// TODO: maybe provide to shareProof method?
try {
await setWalletServiceProviderPin(pin.split('').map(Number))
} catch (e) {
setIsSharing(false)
if (e instanceof InvalidPinError) {
onPinError?.()
return handleError({ reason: 'Invalid PIN entered', redirect: false })
}
return handleError({
reason: 'Authentication failed',
description:
e instanceof Error && isDevelopmentModeEnabled ? `Development mode error: ${e.message}` : undefined,
redirect: true,
})
}
}
try {
await shareProof({
agent,
resolvedRequest: credentialsForRequest,
selectedCredentials: {},
})
onPinComplete?.()
await addSharedActivityForCredentialsForRequest(agent, credentialsForRequest, 'success').catch(console.error)
} catch (error) {
setIsSharing(false)
if (error instanceof BiometricAuthenticationCancelledError) {
return handleError({ reason: 'Biometric authentication cancelled' })
}
if (credentialsForRequest) {
await addSharedActivityForCredentialsForRequest(agent, credentialsForRequest, 'failed')
}
agent.config.logger.error('Error accepting presentation', {
error,
})
return handleError({
reason: 'Presentation could not be shared.',
description:
error instanceof Error && isDevelopmentModeEnabled ? `Development mode error: ${error.message}` : undefined,
})
}
},
[credentialsForRequest, agent, shouldUsePin, stopOverAsking, isDevelopmentModeEnabled, handleError]
)
const onProofDecline = async () => {
stopOverAsking()
if (credentialsForRequest) {
await addSharedActivityForCredentialsForRequest(
agent,
credentialsForRequest,
credentialsForRequest.formattedSubmission.areAllSatisfied ? 'stopped' : 'failed'
)
}
pushToWallet()
toast.show('Information request has been declined.', { customData: { preset: 'danger' } })
}
return (
<FunkePresentationNotificationScreen
usePin={shouldUsePin ?? false}
onAccept={onProofAccept}
onDecline={onProofDecline}
submission={credentialsForRequest?.formattedSubmission}
isAccepting={isSharing}
entityId={credentialsForRequest?.verifier.entityId as string}
verifierName={credentialsForRequest?.verifier.name}
logo={credentialsForRequest?.verifier.logo}
trustedEntities={credentialsForRequest?.verifier.trustedEntities}
lastInteractionDate={lastInteractionDate}
onComplete={() => pushToWallet('replace')}
overAskingResponse={overAskingResponse}
/>
)
}