Skip to content

Commit 0e748db

Browse files
Merge pull request #73 from Web3Auth/feat/no-mutable-map
Feat/no mutable map
2 parents ddf1950 + dfaad6b commit 0e748db

File tree

4 files changed

+57
-45
lines changed

4 files changed

+57
-45
lines changed

app/src/main/java/com/web3auth/app/MainActivity.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
121121
val options = Web3AuthOptions(
122122
context = this,
123123
clientId = getString(R.string.web3auth_project_id),
124-
network = Network.SAPPHIRE_DEVNET,
124+
network = Network.SAPPHIRE_MAINNET,
125125
redirectUrl = Uri.parse("torusapp://org.torusresearch.web3authexample"),
126126
whiteLabel = WhiteLabelData(
127127
"Web3Auth Sample App", null, null, null,
@@ -138,6 +138,7 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
138138
clientId = ""
139139
)
140140
),
141+
buildEnv = BuildEnv.TESTING,
141142
sessionTime = 86400
142143
)
143144

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<resources>
22
<string name="app_name">Torus Web3Auth</string>
3-
<string name="web3auth_project_id">BG4pe3aBso5SjVbpotFQGnXVHgxhgOxnqnNBKyjfEJ3izFvIVWUaMIzoCrAfYag8O6t6a6AOvdLcS4JR2sQMjR4</string>
3+
<string name="web3auth_project_id">BPi5PB_UiIZ-cPz1GtV5i1I2iOSOHuimiXBI0e-Oe_u6X3oVAbCiAZOTEBtTXw4tsluTITPqA8zMsfxIKMjiqNQ</string>
44
<string name="sign_in">Sign in</string>
55
<string name="not_logged_in">Not logged in</string>
66
<string name="sign_out">Sign out</string>

core/src/main/java/com/web3auth/core/Web3Auth.kt

Lines changed: 53 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -27,58 +27,72 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) {
2727
}
2828

2929
private fun request(
30-
path: String, params: LoginParams? = null, extraParams: Map<String, Any>? = null
30+
path: String, params: LoginParams, extraParams: Map<String, Any>? = null
3131
) {
3232
val sdkUrl = Uri.parse(web3AuthOption.sdkUrl)
3333
val context = web3AuthOption.context
3434

35-
val initOptions = mutableMapOf(
36-
"clientId" to web3AuthOption.clientId,
37-
"network" to web3AuthOption.network.name.lowercase(Locale.ROOT)
35+
val initOptions = JSONObject()
36+
initOptions.put("clientId", web3AuthOption.clientId)
37+
initOptions.put("network", web3AuthOption.network.name.lowercase(Locale.ROOT))
38+
if (web3AuthOption.redirectUrl != null) initOptions.put(
39+
"redirectUrl", web3AuthOption.redirectUrl.toString()
3840
)
39-
if (web3AuthOption.redirectUrl != null) initOptions["redirectUrl"] =
40-
web3AuthOption.redirectUrl.toString()
41-
if (web3AuthOption.whiteLabel != null) initOptions["whiteLabel"] =
42-
gson.toJson(web3AuthOption.whiteLabel)
43-
if (web3AuthOption.loginConfig != null) initOptions["loginConfig"] =
44-
gson.toJson(web3AuthOption.loginConfig)
45-
if (web3AuthOption.buildEnv != null) initOptions["buildEnv"] =
46-
web3AuthOption.buildEnv.toString().lowercase()
47-
if (web3AuthOption.mfaSettings != null) initOptions["mfaSettings"] =
48-
gson.toJson(web3AuthOption.mfaSettings)
49-
if (web3AuthOption.sessionTime != null) initOptions["sessionTime"] =
50-
web3AuthOption.sessionTime.toString()
51-
52-
val initParams = mutableMapOf(
53-
"loginProvider" to params?.loginProvider,
54-
"extraLoginOptions" to params?.extraLoginOptions,
55-
"redirectUrl" to if (params?.redirectUrl != null) params.redirectUrl.toString() else initOptions["redirectUrl"].toString(),
56-
"mfaLevel" to params?.mfaLevel,
57-
"curve" to params?.curve,
58-
"dappShare" to params?.dappShare
41+
if (web3AuthOption.whiteLabel != null) initOptions.put(
42+
"whiteLabel", gson.toJson(web3AuthOption.whiteLabel)
5943
)
44+
if (web3AuthOption.loginConfig != null) initOptions.put(
45+
"loginConfig", gson.toJson(web3AuthOption.loginConfig)
46+
)
47+
if (web3AuthOption.buildEnv != null) initOptions.put(
48+
"buildEnv", web3AuthOption.buildEnv?.name?.lowercase(Locale.ROOT)
49+
)
50+
if (web3AuthOption.mfaSettings != null) initOptions.put(
51+
"mfaSettings", gson.toJson(web3AuthOption.mfaSettings)
52+
)
53+
if (web3AuthOption.sessionTime != null) initOptions.put(
54+
"sessionTime", web3AuthOption.sessionTime
55+
)
56+
57+
val initParams = JSONObject()
58+
initParams.put("loginProvider", params.loginProvider.name.lowercase(Locale.ROOT))
59+
if (params.extraLoginOptions != null) initParams.put(
60+
"extraLoginOptions",
61+
gson.toJson(params.extraLoginOptions)
62+
)
63+
initParams.put(
64+
"redirectUrl",
65+
if (params.redirectUrl != null) params.redirectUrl.toString() else initOptions["redirectUrl"].toString()
66+
)
67+
if (params.mfaLevel != null) initParams.put(
68+
"mfaLevel",
69+
params.mfaLevel.name.lowercase(Locale.ROOT)
70+
)
71+
if (params.curve != null) initParams.put("curve", params.curve.name.lowercase(Locale.ROOT))
72+
if (params.dappShare != null) initParams.put("dappShare", params.dappShare)
73+
6074

61-
val paramMap = mapOf(
62-
"options" to initOptions, "params" to initParams, "actionType" to path
75+
val paramMap = JSONObject()
76+
paramMap.put(
77+
"options", initOptions
6378
)
79+
paramMap.put("params", initParams)
80+
paramMap.put("actionType", path)
6481

65-
extraParams?.let { paramMap.plus("params" to extraParams) }
66-
val validParams = paramMap.filterValues { it != null }
82+
extraParams?.let { paramMap.put("params", extraParams) }
6783

68-
val loginIdCf = getLoginId(validParams)
84+
val loginIdCf = getLoginId(paramMap)
6985

7086
loginIdCf.whenComplete { loginId, error ->
7187
if (error == null) {
72-
val jsonObject = mapOf(
73-
"loginId" to loginId
74-
)
88+
val jsonObject = mapOf("loginId" to loginId)
7589
val hash = "b64Params=" + gson.toJson(jsonObject).toByteArray(Charsets.UTF_8)
7690
.toBase64URLString()
7791

7892
val url =
7993
Uri.Builder().scheme(sdkUrl.scheme).encodedAuthority(sdkUrl.encodedAuthority)
8094
.encodedPath(sdkUrl.encodedPath).appendPath("start").fragment(hash).build()
81-
95+
print("url: => $url")
8296
val defaultBrowser = context.getDefaultBrowser()
8397
val customTabsBrowsers = context.getCustomTabsBrowsers()
8498

@@ -126,14 +140,15 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) {
126140
loginCompletableFuture.completeExceptionally(UserCancelledException())
127141
return
128142
}
143+
val hashUri = Uri.parse(uri.host + "?" + uri.fragment)
129144
val error = uri.getQueryParameter("error")
130145
if (error != null) {
131146
loginCompletableFuture.completeExceptionally(UnKnownException(error))
132147
}
133148

134-
val sessionId = hash.split("&")[0].split("=")[1]
149+
val sessionId = hashUri.getQueryParameter("sessionId")
135150

136-
if (sessionId != null) {
151+
if (!sessionId.isNullOrBlank() && sessionId.isNotEmpty()) {
137152
sessionManager.saveSessionId(sessionId)
138153

139154
//Rehydrate Session
@@ -223,8 +238,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) {
223238
sessionResponse.whenComplete { response, error ->
224239
if (error == null) {
225240
val tempJson = JSONObject(response)
226-
web3AuthResponse =
227-
gson.fromJson(tempJson.toString(), Web3AuthResponse::class.java)
241+
web3AuthResponse = gson.fromJson(tempJson.toString(), Web3AuthResponse::class.java)
228242
if (web3AuthResponse?.error?.isNotBlank() == true) {
229243
sessionCompletableFuture.completeExceptionally(
230244
UnKnownException(
@@ -255,13 +269,10 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) {
255269
return sessionCompletableFuture
256270
}
257271

258-
private fun getLoginId(jsonObject: Map<String, Any?>): CompletableFuture<String> {
272+
private fun getLoginId(jsonObject: JSONObject): CompletableFuture<String> {
259273
val createSessionCompletableFuture: CompletableFuture<String> = CompletableFuture()
260-
if (this.sessionManager == null) {
261-
createSessionCompletableFuture.completeExceptionally(Exception("Session Manager is not initialized"))
262-
}
263274
val sessionResponse: CompletableFuture<String> =
264-
sessionManager.createSession(gson.toJson(jsonObject), 600)
275+
sessionManager.createSession(jsonObject.toString(), 600)
265276
sessionResponse.whenComplete { response, error ->
266277
if (error == null) {
267278
createSessionCompletableFuture.complete(response)

core/src/main/java/com/web3auth/core/types/LoginParams.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ data class LoginParams(
99
@Transient var redirectUrl: Uri? = null,
1010
val appState: String? = null,
1111
val mfaLevel: MFALevel? = null,
12-
val curve: Curve? = null
12+
val curve: Curve? = Curve.SECP256K1
1313
)

0 commit comments

Comments
 (0)