From 7052c57076004dd342dc6549a8969b6d955df425 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=C3=B3rio=20Granado=20Magalh=C3=A3es?= Date: Thu, 7 Mar 2024 00:47:01 -0300 Subject: [PATCH] Adds class to manage bluetooth transport (#5) --- .../com/spruceid/wallet/sdk/BaseCredential.kt | 4 + .../spruceid/wallet/sdk/BleSessionManager.kt | 103 ++++++++++++++++++ .../wallet/sdk/CredentialsViewModel.kt | 3 - 3 files changed, 107 insertions(+), 3 deletions(-) create mode 100644 WalletSdk/src/main/java/com/spruceid/wallet/sdk/BleSessionManager.kt diff --git a/WalletSdk/src/main/java/com/spruceid/wallet/sdk/BaseCredential.kt b/WalletSdk/src/main/java/com/spruceid/wallet/sdk/BaseCredential.kt index c3c8c5c..7d9e7b9 100644 --- a/WalletSdk/src/main/java/com/spruceid/wallet/sdk/BaseCredential.kt +++ b/WalletSdk/src/main/java/com/spruceid/wallet/sdk/BaseCredential.kt @@ -2,6 +2,10 @@ package com.spruceid.wallet.sdk open class BaseCredential constructor(private val id: String?) { + fun getId(): String? { + return this.id + } + override fun toString(): String { return "Credential($id)" } diff --git a/WalletSdk/src/main/java/com/spruceid/wallet/sdk/BleSessionManager.kt b/WalletSdk/src/main/java/com/spruceid/wallet/sdk/BleSessionManager.kt new file mode 100644 index 0000000..8eaaeab --- /dev/null +++ b/WalletSdk/src/main/java/com/spruceid/wallet/sdk/BleSessionManager.kt @@ -0,0 +1,103 @@ +package com.spruceid.wallet.sdk + +import android.bluetooth.BluetoothManager +import android.util.Log +import com.spruceid.wallet.sdk.rs.ItemsRequest +import com.spruceid.wallet.sdk.rs.SessionManager +import com.spruceid.wallet.sdk.rs.initialiseSession +import com.spruceid.wallet.sdk.rs.handleRequest +import com.spruceid.wallet.sdk.rs.submitResponse +import com.spruceid.wallet.sdk.rs.submitSignature +import java.security.KeyStore +import java.security.Signature +import java.util.UUID + +abstract class BLESessionStateDelegate { + abstract fun update(state: Map) +} + +public class BLESessionManager { + + val callback: BLESessionStateDelegate + val uuid: UUID + var state: String = "" + var sessionManager: SessionManager? = null + var itemsRequests: List = listOf() + val mdoc: MDoc + var bleManager: Transport? = null + + constructor( + mdoc: MDoc, + bluetoothManager: BluetoothManager, + callback: BLESessionStateDelegate, + ) { + this.callback = callback + this.uuid = UUID.randomUUID() + this.mdoc = mdoc + try { + val sessionData = initialiseSession(mdoc.inner, uuid.toString()) + this.state = sessionData.state + this.bleManager = Transport(bluetoothManager) + this.bleManager!! + .initialize( + "Holder", + this.uuid, + "BLE", + "Central", + sessionData.bleIdent.toByteArray(), + ::updateRequestData + ) + this.callback.update(mapOf(Pair("engagingQRCode", sessionData.qrCodeUri))) + } catch (e: Error) { + Log.e("BleSessionManager.constructor", e.toString()) + } + } + + fun cancel() { + this.bleManager?.terminate() + } + + fun submitNamespaces(items: Map>>) { + val responseData = submitResponse( + this.sessionManager!!, + this.itemsRequests, + items + ) + + val ks: KeyStore = KeyStore.getInstance( + "AndroidKeyStore" + ) + + ks.load( + null + ) + + val entry = ks.getEntry(this.mdoc.keyAlias, null) + if (entry !is KeyStore.PrivateKeyEntry) { + throw IllegalStateException("No such private key under the alias <${this.mdoc.keyAlias}>") + } + + try { + val signer = Signature.getInstance("ECDSA") + signer.initSign(entry.privateKey) + + signer.update(responseData.payload) + + val signature = signer.sign() + val signatureData = submitSignature(this.sessionManager!!, signature) + this.state = signatureData.state + this.bleManager!!.send(signatureData.response) + } catch (e: Error) { + Log.e("CredentialsViewModel.submitNamespaces", e.toString()) + this.callback.update(mapOf(Pair("error", e.toString()))) + throw e + } + } + + fun updateRequestData(data: ByteArray) { + val requestData = handleRequest(this.state, data) + this.sessionManager = requestData.sessionManager + this.itemsRequests = requestData.itemsRequests + this.callback.update(mapOf(Pair("selectNamespaces", requestData.itemsRequests))) + } +} diff --git a/WalletSdk/src/main/java/com/spruceid/wallet/sdk/CredentialsViewModel.kt b/WalletSdk/src/main/java/com/spruceid/wallet/sdk/CredentialsViewModel.kt index 79153d6..1c7d9da 100644 --- a/WalletSdk/src/main/java/com/spruceid/wallet/sdk/CredentialsViewModel.kt +++ b/WalletSdk/src/main/java/com/spruceid/wallet/sdk/CredentialsViewModel.kt @@ -128,8 +128,5 @@ class CredentialsViewModel : ViewModel() { _currState.value = PresentmentState.ERROR throw e } - } - - } \ No newline at end of file