-
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.
feat(mdoc): proximity flow for Android functionality (#202)
Signed-off-by: Berend Sliedrecht <sliedrecht@berend.io>
- Loading branch information
1 parent
6802572
commit 4b5f277
Showing
15 changed files
with
5,732 additions
and
1,980 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ yarn-error.log* | |
.env.development.local | ||
.env.test.local | ||
.env.production.local | ||
.env | ||
|
||
# vercel | ||
.vercel | ||
|
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 @@ | ||
export * from './mdocProximity' |
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,95 @@ | ||
import { mdocDataTransfer } from '@animo-id/expo-mdoc-data-transfer' | ||
import { TypedArrayEncoder } from '@credo-ts/core' | ||
import { getMdocContext } from '@credo-ts/core/build/modules/mdoc/MdocContext' | ||
import type { EasyPIDAppAgent } from '@package/agent' | ||
import { | ||
COSEKey, | ||
DeviceRequest, | ||
DeviceResponse, | ||
MDoc, | ||
type MdocContext, | ||
parseIssuerSigned, | ||
} from '@protokoll/mdoc-client' | ||
import { type Permission, PermissionsAndroid, Platform } from 'react-native' | ||
|
||
type ShareDeviceResponseOptions = { | ||
sessionTranscript: Uint8Array | ||
deviceRequest: Uint8Array | ||
agent: EasyPIDAppAgent | ||
} | ||
|
||
const PERMISSIONS = [ | ||
'android.permission.ACCESS_FINE_LOCATION', | ||
'android.permission.BLUETOOTH_CONNECT', | ||
'android.permission.BLUETOOTH_SCAN', | ||
'android.permission.BLUETOOTH_ADVERTISE', | ||
'android.permission.ACCESS_COARSE_LOCATION', | ||
] as const as Permission[] | ||
|
||
export const requestMdocPermissions = async () => { | ||
if (Platform.OS !== 'android') return | ||
await PermissionsAndroid.requestMultiple(PERMISSIONS) | ||
} | ||
|
||
export const getMdocQrCode = async () => { | ||
const mdt = mdocDataTransfer.instance() | ||
const qrData = await mdt.startQrEngagement() | ||
return qrData | ||
} | ||
|
||
/** | ||
* | ||
* Wait for the device request | ||
* | ||
* Returns the device request and session transcript | ||
* | ||
*/ | ||
export const waitForDeviceRequest = async () => { | ||
const mdt = mdocDataTransfer.instance() | ||
return await mdt.waitForDeviceRequest() | ||
} | ||
|
||
/** | ||
* | ||
* Naive way to share the device response based on the device request | ||
* | ||
* Optimalisations: | ||
* | ||
* 1. pre-filter the `agent.mdoc.getAll()` based on the `deviceRequest` | ||
* 2. Allow the user to pick which specific mdoc is being used | ||
* | ||
*/ | ||
export const shareDeviceResponse = async (options: ShareDeviceResponseOptions) => { | ||
const mdocs = await options.agent.mdoc.getAll() | ||
const issuerSignedDocuments = mdocs.map((mdoc) => { | ||
const docType = mdoc.getTag('DocType') as string | ||
return parseIssuerSigned(TypedArrayEncoder.fromBase64(mdoc.base64Url), docType) | ||
}) | ||
|
||
const mdoc = new MDoc(issuerSignedDocuments) | ||
|
||
const mdocContext = getMdocContext(options.agent.context) as unknown as { | ||
cose: MdocContext['cose'] | ||
crypto: MdocContext['crypto'] | ||
} | ||
|
||
const mdt = mdocDataTransfer.instance() | ||
|
||
const mso = mdoc.documents[0].issuerSigned.issuerAuth.decodedPayload | ||
const deviceKeyInfo = mso.deviceKeyInfo | ||
if (!deviceKeyInfo?.deviceKey) { | ||
throw new Error('Device key info is missing') | ||
} | ||
|
||
const publicDeviceJwk = COSEKey.import(deviceKeyInfo.deviceKey).toJWK() | ||
|
||
const deviceRequest = DeviceRequest.parse(options.deviceRequest) | ||
|
||
const deviceResponse = await DeviceResponse.from(mdoc) | ||
.usingSessionTranscriptBytes(new Uint8Array(options.sessionTranscript)) | ||
.usingDeviceRequest(deviceRequest) | ||
.authenticateWithSignature(publicDeviceJwk, 'ES256') | ||
.sign(mdocContext) | ||
|
||
await mdt.sendDeviceResponse(deviceResponse.encode()) | ||
} |
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 |
---|---|---|
|
@@ -68,6 +68,7 @@ const config = { | |
}, | ||
plugins: [ | ||
[ | ||
'expo-router', | ||
'expo-font', | ||
{ | ||
fonts: [ | ||
|
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 |
---|---|---|
|
@@ -25,6 +25,7 @@ | |
"../../node_modules/@expo-google-fonts/open-sans/OpenSans_700Bold.ttf" | ||
] | ||
} | ||
] | ||
], | ||
"expo-router" | ||
] | ||
} |
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
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.