|
1 | 1 | package com.credman.cmwallet.ui |
2 | 2 |
|
3 | | -import android.content.Context |
4 | | -import android.os.Bundle |
5 | | -import android.util.Base64 |
6 | 3 | import android.util.Log |
7 | | -import androidx.credentials.DigitalCredential |
| 4 | +import androidx.credentials.CreateDigitalCredentialRequest |
| 5 | +import androidx.credentials.CreateDigitalCredentialResponse |
| 6 | +import androidx.credentials.CredentialManager |
8 | 7 | import androidx.credentials.ExperimentalDigitalCredentialApi |
9 | 8 | import androidx.lifecycle.ViewModel |
10 | 9 | import androidx.lifecycle.viewModelScope |
11 | 10 | import com.credman.cmwallet.CmWalletApplication |
12 | 11 | import com.credman.cmwallet.CmWalletApplication.Companion.TAG |
13 | 12 | import com.credman.cmwallet.MainActivity |
14 | 13 | import com.credman.cmwallet.data.model.CredentialItem |
15 | | -import com.credman.cmwallet.loadECPrivateKey |
16 | | -import com.credman.cmwallet.openid4vci.OpenId4VCI |
17 | | -import com.credman.cmwallet.openid4vci.data.AuthorizationDetailResponseOpenIdCredential |
18 | | -import com.credman.cmwallet.openid4vci.data.CredentialRequest |
19 | | -import com.credman.cmwallet.openid4vci.data.TokenRequest |
20 | | -import com.google.android.gms.identitycredentials.CreateCredentialRequest |
21 | | -import com.google.android.gms.identitycredentials.IdentityCredentialManager |
22 | 14 | import kotlinx.coroutines.flow.MutableStateFlow |
23 | 15 | import kotlinx.coroutines.flow.StateFlow |
24 | 16 | import kotlinx.coroutines.flow.asStateFlow |
25 | 17 | import kotlinx.coroutines.flow.update |
26 | 18 | import kotlinx.coroutines.launch |
27 | 19 | import org.json.JSONObject |
28 | | -import java.security.KeyFactory |
29 | | -import java.security.interfaces.ECPrivateKey |
30 | | -import java.security.spec.X509EncodedKeySpec |
31 | 20 |
|
32 | 21 | data class HomeScreenUiState( |
33 | 22 | val credentials: List<CredentialItem> |
@@ -57,25 +46,26 @@ class HomeViewModel : ViewModel() { |
57 | 46 | fun testIssuance(activity: MainActivity) { |
58 | 47 | Log.i("HomeViewModel", "testIssuance") |
59 | 48 | val credOfferJson = JSONObject(CmWalletApplication.credentialRepo.openId4VCITestRequestJson) |
60 | | - val requestJson = JSONObject().put("protocol", "openid4vci1.0").put("data", credOfferJson.toString()).toString() |
| 49 | + val requestJson = |
| 50 | + JSONObject().put("protocol", "openid4vci1.0").put("data", credOfferJson.toString()) |
| 51 | + .toString() |
61 | 52 |
|
62 | | - // Note: Long term this will be wrapped by a simple structured Jetpack API that |
63 | | - // automatically handles the request Bundle formation and pending intent launching |
64 | | - IdentityCredentialManager.getClient(activity).createCredential( |
65 | | - CreateCredentialRequest( |
66 | | - type = DigitalCredential.TYPE_DIGITAL_CREDENTIAL, |
67 | | - credentialData = Bundle().apply { putString("androidx.credentials.BUNDLE_KEY_REQUEST_JSON", requestJson) }, |
68 | | - candidateQueryData = Bundle.EMPTY, |
69 | | - origin = null, |
70 | | - requestJson = requestJson, |
71 | | - resultReceiver = null |
72 | | - ) |
73 | | - ).addOnSuccessListener { |
74 | | - Log.d(TAG, "Issuance pending intent received ${it.pendingIntent}") |
75 | | - assert(it.pendingIntent != null) { "PendingIntent must be non-null in this case" } |
76 | | - activity.launchIssuanceIntent(it.pendingIntent!!) |
77 | | - }.addOnFailureListener { |
78 | | - Log.e(TAG, "Issuance failure", it) |
| 53 | + viewModelScope.launch { |
| 54 | + val response = try { |
| 55 | + CredentialManager.create(activity).createCredential( |
| 56 | + activity, |
| 57 | + CreateDigitalCredentialRequest( |
| 58 | + origin = null, |
| 59 | + requestJson = requestJson, |
| 60 | + ) |
| 61 | + ) |
| 62 | + } catch (e: Exception) { |
| 63 | + Log.e(TAG, "Issuance failure", e) |
| 64 | + null |
| 65 | + } |
| 66 | + (response as? CreateDigitalCredentialResponse)?.let { |
| 67 | + Log.d(TAG, "Issuance response ${it.responseJson}") |
| 68 | + } |
79 | 69 | } |
80 | 70 |
|
81 | 71 | // val openId4VCI = OpenId4VCI(requestJson) |
|
0 commit comments