This repository has been archived by the owner on Feb 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
uPortLoginActivity.kt
121 lines (100 loc) · 4.13 KB
/
uPortLoginActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package me.uport.sdk.demoapp.request_flows
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import com.uport.sdk.signer.KPSigner
import kotlinx.android.synthetic.main.activity_uport_login.*
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import me.uport.sdk.core.UI
import me.uport.sdk.credentials.Credentials
import me.uport.sdk.credentials.SelectiveDisclosureRequestParams
import me.uport.sdk.demoapp.R
import me.uport.sdk.jwt.JWTTools
import me.uport.sdk.transport.*
/**
* This allows the users initiate a uPort login using [SelectiveDisclosureRequest]
* and then receive the deeplink response via [onActivityResult]
*/
class uPortLoginActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_uport_login)
// create a signer
val signer = KPSigner("0x1234")
// create a DID
val issuerDID = "did:ethr:${signer.getAddress()}"
// create the request JWT
val cred = Credentials(issuerDID, signer)
val params = SelectiveDisclosureRequestParams(
requested = listOf("name"),
callbackUrl = "https://uport-project.github.io/uport-android-sdk/callbacks"
)
request_details.text = """
Selective disclosure
Requesting `name` (and implicit DID) from uPort app user.
""".trimIndent()
// make the request
btn_send_request.setOnClickListener {
progress.visibility = View.VISIBLE
GlobalScope.launch {
val requestJWT = cred.createDisclosureRequest(params)
// Send a signed request to uport via Transports
@Suppress("LabeledExpression")
Transports().sendExpectingResult(this@uPortLoginActivity, requestJWT)
withContext(UI) {
progress.visibility = View.GONE
}
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
val response: UriResponse? = ResponseParser.parseActivityResult(requestCode, resultCode, data)
when (response) {
is JWTUriResponse -> {
val (_, payloadMap, _) = JWTTools().decodeRaw(response.token)
response_details.text = """
profile: ${payloadMap["own"]}
uPort app user DID: ${payloadMap["iss"]}
""".trimIndent()
@Suppress("UnsafeCast")
createRequestFlowOptions((payloadMap["iss"] as String))
}
is ErrorUriResponse -> {
response_details.text = "error: ${response.message}"
}
null -> {
//process your other domain specific responses
}
}
}
private fun createRequestFlowOptions(did: String) {
btn_verified_claim.visibility = View.VISIBLE
btn_verified_claim.setOnClickListener {
val intent = Intent(this, VerifiedClaimRequestActivity::class.java)
intent.putExtra("sub", did)
startActivity(intent)
}
btn_personal_signature.visibility = View.VISIBLE
btn_personal_signature.setOnClickListener {
val intent = Intent(this, PersonalSignRequestActivity::class.java)
intent.putExtra("riss", did)
startActivity(intent)
}
btn_typed_data.visibility = View.VISIBLE
btn_typed_data.setOnClickListener {
val intent = Intent(this, TypedDataRequestActivity::class.java)
intent.putExtra("riss", did)
startActivity(intent)
}
btn_ethereum_transaction.visibility = View.VISIBLE
btn_ethereum_transaction.setOnClickListener {
val intent = Intent(this, EthereumTransactionActivity::class.java)
intent.putExtra("receiver", did)
startActivity(intent)
}
}
}