-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds class to manage bluetooth transport (#5)
- Loading branch information
Showing
3 changed files
with
107 additions
and
3 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
103 changes: 103 additions & 0 deletions
103
WalletSdk/src/main/java/com/spruceid/wallet/sdk/BleSessionManager.kt
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,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))) | ||
} | ||
} |
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 |
---|---|---|
|
@@ -128,8 +128,5 @@ class CredentialsViewModel : ViewModel() { | |
_currState.value = PresentmentState.ERROR | ||
throw e | ||
} | ||
|
||
} | ||
|
||
|
||
} |