Skip to content

Commit

Permalink
Merge pull request #52 from Web3Auth/feat/add-userinfo-method
Browse files Browse the repository at this point in the history
Exposed getUserInfo() method
  • Loading branch information
chaitanyapotti authored Apr 17, 2023
2 parents fb2a4fa + adc36d3 commit bbff689
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 12 deletions.
2 changes: 2 additions & 0 deletions app/src/main/java/com/web3auth/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
reRender(loginResponse)
println("PrivKey: " + web3Auth.getPrivkey())
println("ed25519PrivKey: " + web3Auth.getEd25519PrivKey())
println("Web3Auth UserInfo" + web3Auth.getUserInfo())
} else {
Log.d("MainActivity_Web3Auth", error.message ?: "Something went wrong")
}
Expand Down Expand Up @@ -137,6 +138,7 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
reRender(loginResponse)
println("PrivKey: " + web3Auth.getPrivkey())
println("ed25519PrivKey: " + web3Auth.getEd25519PrivKey())
println("Web3Auth UserInfo" + web3Auth.getUserInfo())
} else {
Log.d("MainActivity_Web3Auth", error.message ?: "Something went wrong")
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<resources>
<string name="app_name">Torus Web3Auth</string>
<string name="web3auth_project_id">BASdZxp5tWqPGkzbOU4VMJsnz8QOpJ2pyxf6kZcuxI8AJ1gsOw5MgbW96RpAAx_Kk-5p4rhOopPhCa9kR_9mKpE</string>
<string name="web3auth_project_id">BEaGnq-mY0ZOXk2UT1ivWUe0PZ_iJX4Vyb6MtpOp7RMBu_6ErTrATlfuK3IaFcvHJr27h6L1T4owkBH6srLphIw</string>
<string name="sign_in">Sign in</string>
<string name="not_logged_in">Not logged in</string>
<string name="sign_out">Sign out</string>
Expand Down
35 changes: 26 additions & 9 deletions core/src/main/java/com/web3auth/core/Web3Auth.kt
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) {
if (web3AuthResponse.error?.isNotBlank() == true) {
loginCompletableFuture.completeExceptionally(
UnKnownException(
web3AuthResponse.error ?: "Something went wrong"
web3AuthResponse.error ?: Web3AuthError.getError(ErrorCode.SOMETHING_WENT_WRONG)
)
)
}
Expand Down Expand Up @@ -225,7 +225,6 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) {
messageObj,
ShareMetadata::class.java
)
println("shareMetadata$shareMetadata")

KeyStoreManagerUtils.savePreferenceData(
KeyStoreManagerUtils.EPHEM_PUBLIC_Key,
Expand Down Expand Up @@ -260,7 +259,9 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) {
Handler(Looper.getMainLooper()).postDelayed(10) {
sessionCompletableFuture.completeExceptionally(
UnKnownException(
web3AuthResponse.error ?: "Something went wrong"
web3AuthResponse.error ?: Web3AuthError.getError(
ErrorCode.SOMETHING_WENT_WRONG
)
)
)
}
Expand Down Expand Up @@ -334,23 +335,39 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) {
}

fun getPrivkey(): String {
val privKey: String = if (web3AuthOption.useCoreKitKey == true) {
web3AuthResponse.coreKitKey.toString()
val privKey: String = if (web3AuthResponse == null) {
throw Error(Web3AuthError.getError(ErrorCode.NOUSERFOUND))
} else {
web3AuthResponse.privKey.toString()
if (web3AuthOption.useCoreKitKey == true) {
web3AuthResponse.coreKitEd25519PrivKey.toString()
} else {
web3AuthResponse.ed25519PrivKey.toString()
}
}
return privKey
}

fun getEd25519PrivKey(): String {
val ed25519Key: String = if (web3AuthOption.useCoreKitKey == true) {
web3AuthResponse.coreKitEd25519PrivKey.toString()
val ed25519Key: String = if (web3AuthResponse == null) {
throw Error(Web3AuthError.getError(ErrorCode.NOUSERFOUND))
} else {
web3AuthResponse.ed25519PrivKey.toString()
if (web3AuthOption.useCoreKitKey == true) {
web3AuthResponse.coreKitEd25519PrivKey.toString()
} else {
web3AuthResponse.ed25519PrivKey.toString()
}
}
return ed25519Key
}

fun getUserInfo(): UserInfo? {
if (web3AuthResponse == null || web3AuthResponse.userInfo == null) {
throw Error(Web3AuthError.getError(ErrorCode.NOUSERFOUND))
} else {
return web3AuthResponse.userInfo
}
}

fun logout(
redirectUrl: Uri? = null,
appState: String? = null
Expand Down
36 changes: 36 additions & 0 deletions core/src/main/java/com/web3auth/core/types/Web3AuthError.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.web3auth.core.types

object Web3AuthError {

fun getError(errorCode: ErrorCode): String {
return when (errorCode) {
ErrorCode.NOUSERFOUND -> {
"No user found, please login again!"
}
ErrorCode.ENCODING_ERROR -> {
"Encoding Error"
}
ErrorCode.DECODING_ERROR -> {
"Decoding Error"
}
ErrorCode.SOMETHING_WENT_WRONG -> {
"Something went wrong!"
}
ErrorCode.RUNTIME_ERROR -> {
"Runtime Error"
}
ErrorCode.APP_CANCELLED -> {
"App Cancelled"
}
}
}
}

enum class ErrorCode {
NOUSERFOUND,
ENCODING_ERROR,
DECODING_ERROR,
RUNTIME_ERROR,
APP_CANCELLED,
SOMETHING_WENT_WRONG,
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import androidx.annotation.Keep

@Keep
data class Web3AuthResponse(
var privKey: String? = null,
var ed25519PrivKey: String? = null,
val privKey: String? = null,
val ed25519PrivKey: String? = null,
val userInfo: UserInfo? = null,
val error: String? = null,
val sessionId: String? = null,
Expand Down

0 comments on commit bbff689

Please sign in to comment.