Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update UI based on websocket interactions #34

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions frontend/src/flows/Authentication/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,28 @@ export const Authentication: React.FC<IAuthenticationProps> = ({
serviceAPI,
}) => {
const [description, setDescription] = useState<string>('Lorem ipsum')
const [msg, setMsg] = useState<string | undefined>()

const startAuth = async () => {
const resp: {
qr: string
err: string
id: string | undefined
} = await serviceAPI.sendRPC(RpcRoutes.authnInterxn, { description })
console.log('resp', resp)
if (resp.id) {
receiveUpdate(resp)
}
return resp
}

const receiveUpdate = async (resp: any) => {
const processedRes = await resp.originalMsg.followUps[1].processed
const responderDid = processedRes.participants.responder.didDocument.id
setMsg(responderDid)
console.log('processedRes', processedRes)
}

return (
<InteractionTemplate
startText="Start Authentication Interaction"
Expand All @@ -31,6 +44,16 @@ export const Authentication: React.FC<IAuthenticationProps> = ({
value={description}
setValue={setDescription}
/>
<p>
{msg && (
<>
<br />
<b>Result: </b>
The user successfully authenticate using his DID:
<b> {msg}</b>
</>
)}
</p>
</InteractionTemplate>
)
}
64 changes: 56 additions & 8 deletions frontend/src/flows/CredentialOffer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export const CredentialOffer: React.FC<ICredentialOfferProps> = ({
const [issuedCredentials, setIssued] = useState<Array<string>>([])
const [invalidCredentials, setInvalid] = useState<Array<string>>([])
const availableIssueCredentials = credTypes
const [msg, setMsg] = useState<string | undefined>()
const [err, setErr] = useState<string | undefined>()

const handleSelect = (array: string[], item: string) => {
return !array.includes(item)
Expand All @@ -27,20 +29,48 @@ export const CredentialOffer: React.FC<ICredentialOfferProps> = ({
if (issuedCredentials.length === 0 && credTypes && credTypes.length > 0) {
setIssued(credTypes.slice(0, 1))
}
})
}, [issuedCredentials.length, credTypes])

const startCredOffer = async () => {
const resp: { qr: string; err: string } = await serviceAPI.sendRPC(
RpcRoutes.offerCred,
{
types: Array.from(new Set(issuedCredentials)),
invalid: Array.from(new Set(invalidCredentials)),
},
)
const resp: {
qr: string
err: string
id: string
} = await serviceAPI.sendRPC(RpcRoutes.offerCred, {
types: Array.from(new Set(issuedCredentials)),
invalid: Array.from(new Set(invalidCredentials)),
})
console.log(resp)
if (resp.id) {
receiveUpdate(resp)
}

return resp
}

const receiveUpdate = async (resp: any) => {
const processedRes = await resp.originalMsg.followUps[1].processed

const successfulCredentials = processedRes.credentials
.filter((c: any) => c.claim.id !== 'INVALID')
.map((c: any) => c.name)
.join(', ')
const failedCredentials = processedRes.credentials
.filter((c: any) => c.claim.id === 'INVALID')
.map((c: any) => c.name)
.join(', ')

setMsg(`${successfulCredentials}`)
if (failedCredentials.length > 0) setErr(`${failedCredentials}`)

receiveFinal(resp)
}

const receiveFinal = async (resp: any) => {
const processed = await resp.originalMsg.followUps[2].processed
console.log('The final update has been recived from server', processed)
}

return (
<InteractionTemplate
startText="Start Credential Offer"
Expand All @@ -59,6 +89,24 @@ export const CredentialOffer: React.FC<ICredentialOfferProps> = ({
onSelect={type => setInvalid(handleSelect(invalidCredentials, type))}
selectedItems={invalidCredentials}
/>
<p>
{msg && (
<>
<b>Result: </b>
The user successfully got the Credentials:
<b> {msg}</b>
</>
)}
</p>
<p>
{err && (
<>
<b>Error: </b>
The user failed to get the Credentials:
<b> {err}</b>
</>
)}
</p>
</InteractionTemplate>
)
}
2 changes: 1 addition & 1 deletion frontend/src/flows/CredentialOfferCustom/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const CredentialOfferCustom = ({

useEffect(() => {
handleSetInitialInputs(credType)
}, [])
}, [credType])

const [credentialsToBeIssued, setCredentialsToBeIssued] = useState<
Array<ICredential>
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/flows/CredentialRequest/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const CredentialRequest: React.FC<ICredentialShareProps> = ({
) {
setRequested(credTypes.slice(0, 1))
}
})
}, [requestedCredentials.length, credTypes])

const startCredRequest = async () => {
const resp: { qr: string; err: string } = await serviceAPI.sendRPC(
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/flows/EstablishChannel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const EstablishChannel = ({
const [identifier, setIdentifier] = useState<string>('')
const [qr, setQr] = useState<string>('')
const [jwt, setJwt] = useState<string>('')
const [err, setErr] = useState<boolean>(false)
const [err] = useState<boolean>(false)

const [encryptInput, setEncryptInput] = useState('')
//const [decryptInput, setDecryptInput] = useState('')
Expand All @@ -31,7 +31,7 @@ export const EstablishChannel = ({
window.location.search.split('&').forEach(p => {
try {
const [k, v] = p.split('=')
if (k == 'id') chId = v
if (k === 'id') chId = v
} catch {}
})
if (!chId) {
Expand Down Expand Up @@ -97,7 +97,7 @@ export const EstablishChannel = ({

<InteractionQR jwt={jwt ? `${jwtCommand} ${jwt}` : undefined} />

{!err && qr && <img src={qr} />}
{!err && qr && <img src={qr} alt="QR" />}

{!!encryptOutput.length && (
<>
Expand Down
38 changes: 35 additions & 3 deletions service_agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,20 @@ export const init = async () => {
{ createInteractionCallbackURL, wrapJWT },
) => {
const callbackURL = createInteractionCallbackURL(
async (jwt: string) => {
async (jwt: string, websocket: WebSocket) => {
const interxn = await jolo.processJWT(jwt)
console.log('auth request handled for', interxn.participants)

websocket.send(
JSON.stringify({
id: interxn.id,
status: 'update',
response: {
id: interxn.id,
participants: interxn.participants,
},
}),
)
},
)
return wrapJWT(
Expand Down Expand Up @@ -288,7 +299,7 @@ export const init = async () => {
const invalidTypes = req.invalid || []

const callbackURL = createInteractionCallbackURL(
async (jwt: string) => {
async (jwt: string, websocket: WebSocket) => {
const interxn = await jolo.processJWT(jwt)
console.log('offerCred called back for', interxn.id)

Expand Down Expand Up @@ -322,6 +333,16 @@ export const init = async () => {
'credentials issued',
credentials.map((c) => last(c.type)),
)
websocket.send(
JSON.stringify({
id: interxn.id,
status: 'update',
response: {
id: interxn.id,
credentials: credentials,
},
}),
)
return interxn.createCredentialReceiveToken(credentials)
},
)
Expand Down Expand Up @@ -349,9 +370,20 @@ export const init = async () => {
)

const callbackURL = createInteractionCallbackURL(
async (jwt: string) => {
async (jwt: string, websocket: WebSocket) => {
const interxn = await jolo.processJWT(jwt)
console.log('credShareRequest called back for', interxn.id)

websocket.send(
JSON.stringify({
id: interxn.id,
status: 'update',
response: {
id: interxn.id,
state: interxn.flow.state,
},
}),
)
},
)

Expand Down