Skip to content

Commit

Permalink
feat: batch generate key pair
Browse files Browse the repository at this point in the history
Signed-off-by: Berend Sliedrecht <[email protected]>
  • Loading branch information
Berend Sliedrecht authored and berendsliedrecht committed Nov 26, 2024
1 parent a822967 commit 22f24ad
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 7 deletions.
7 changes: 6 additions & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@
"enabled": true
},
"linter": {
"enabled": true
"enabled": true,
"rules": {
"performance": {
"noAccumulatingSpread": "off"
}
}
},
"vcs": {
"useIgnoreFile": true,
Expand Down
49 changes: 49 additions & 0 deletions src/ExpoSecureEnvironmentModule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { requireNativeModule } from 'expo-modules-core'
import type { SecureEnvironment } from './SecureEnvironment'

type UnwrapPromiseReturnTypes<T, ExcludedKey extends keyof T = never> = {
[K in keyof T]: K extends ExcludedKey
? T[K]
: T[K] extends (...args: infer A) => Promise<infer R> | infer S
? (...args: A) => Promise<R> extends Promise<infer U> ? U : Promise<R> | S
: T[K]
}

const nativeExpoSecureEnvironment = requireNativeModule<
UnwrapPromiseReturnTypes<Omit<SecureEnvironment, 'batchGenerateKeyPair'>, 'sign'> & {
supportsSecureEnvironment: () => boolean
}
>('ExpoSecureEnvironment')

export const expoSecureEnvironment = {
generateKeypair: (keyId: string, biometricsBacked?: boolean): Uint8Array => {
nativeExpoSecureEnvironment.generateKeypair(keyId, biometricsBacked)
const publicKey = expoSecureEnvironment.getPublicBytesForKeyId(keyId)
return publicKey
},

batchGenerateKeyPair: (keyIds: Array<string>): Record<string, Uint8Array> => {
for (const keyId of keyIds) {
expoSecureEnvironment.generateKeypair(keyId)
}

return keyIds
.map((keyId) => ({
keyId,
publicKey: expoSecureEnvironment.getPublicBytesForKeyId(keyId),
}))
.reduce((prev, curr) => ({ ...prev, [curr.keyId]: curr.publicKey }), {})
},

getPublicBytesForKeyId: (keyId: string): Uint8Array => {
return nativeExpoSecureEnvironment.getPublicBytesForKeyId(keyId)
},

sign: (keyId: string, message: Uint8Array, biometricsBacked?: boolean): Promise<Uint8Array> => {
return nativeExpoSecureEnvironment.sign(keyId, message, biometricsBacked)
},

supportsSecureEnvironment: () => {
return nativeExpoSecureEnvironment.supportsSecureEnvironment()
},
}
20 changes: 14 additions & 6 deletions src/SecureEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,22 @@
*
*/

import { Platform, requireNativeModule } from 'expo-modules-core'

const expoSecureEnvironment = requireNativeModule<SecureEnvironment & { supportsSecureEnvironment: () => boolean }>(
'ExpoSecureEnvironment'
)
import { Platform } from 'expo-modules-core'
import { expoSecureEnvironment } from './ExpoSecureEnvironmentModule'

export interface SecureEnvironment {
generateKeypair(id: string, biometricsBacked?: boolean): Promise<void> | void
generateKeypair(keyId: string, biometricsBacked?: boolean): Promise<Uint8Array> | Uint8Array
/**
*
* @todo Might be better to make this optional
*
* Returns an object where each key is the `keyId` and the value is the public key bytes
*
*/
batchGenerateKeyPair(
keyIds: Array<string>,
biometricsBacked?: boolean
): Promise<Record<string, Uint8Array>> | Record<string, Uint8Array>
getPublicBytesForKeyId(keyId: string): Promise<Uint8Array> | Uint8Array
sign(keyId: string, message: Uint8Array, biometricsBacked?: boolean): Promise<Uint8Array>
}
Expand Down

0 comments on commit 22f24ad

Please sign in to comment.