Skip to content

Commit

Permalink
Adds class to manage bluetooth transport (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
w4ll3 authored Mar 7, 2024
1 parent b8669a6 commit 7052c57
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
}
Expand Down
103 changes: 103 additions & 0 deletions WalletSdk/src/main/java/com/spruceid/wallet/sdk/BleSessionManager.kt
Original file line number Diff line number Diff line change
@@ -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<String, Any>)
}

public class BLESessionManager {

val callback: BLESessionStateDelegate
val uuid: UUID
var state: String = ""
var sessionManager: SessionManager? = null
var itemsRequests: List<ItemsRequest> = 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<String, Map<String, List<String>>>) {
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)))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,5 @@ class CredentialsViewModel : ViewModel() {
_currState.value = PresentmentState.ERROR
throw e
}

}


}

0 comments on commit 7052c57

Please sign in to comment.