From e13675aad7f16a9aabbb6b78e68912efb1d2f063 Mon Sep 17 00:00:00 2001 From: Gaurav Goel Date: Tue, 4 Apr 2023 15:33:38 +0530 Subject: [PATCH 1/6] Exposed getUserInfo() method Signed-off-by: Gaurav Goel --- app/src/main/java/com/web3auth/app/MainActivity.kt | 2 ++ core/src/main/java/com/web3auth/core/Web3Auth.kt | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/app/src/main/java/com/web3auth/app/MainActivity.kt b/app/src/main/java/com/web3auth/app/MainActivity.kt index 4a50871..4867b89 100644 --- a/app/src/main/java/com/web3auth/app/MainActivity.kt +++ b/app/src/main/java/com/web3auth/app/MainActivity.kt @@ -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") } @@ -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") } diff --git a/core/src/main/java/com/web3auth/core/Web3Auth.kt b/core/src/main/java/com/web3auth/core/Web3Auth.kt index 8d4d384..29498d4 100644 --- a/core/src/main/java/com/web3auth/core/Web3Auth.kt +++ b/core/src/main/java/com/web3auth/core/Web3Auth.kt @@ -351,6 +351,10 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) { return ed25519Key } + fun getUserInfo(): UserInfo { + return web3AuthResponse.userInfo!! + } + fun logout( redirectUrl: Uri? = null, appState: String? = null From 887532e026141f00e2b2a71ed62a7973f98e6ce9 Mon Sep 17 00:00:00 2001 From: Gaurav Goel Date: Thu, 6 Apr 2023 17:57:14 +0530 Subject: [PATCH 2/6] Updated clientId Signed-off-by: Gaurav Goel --- app/src/main/res/values/strings.xml | 2 +- .../src/main/java/com/web3auth/core/types/Web3AuthResponse.kt | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b8a8199..8dac64d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,6 +1,6 @@ Torus Web3Auth - BASdZxp5tWqPGkzbOU4VMJsnz8QOpJ2pyxf6kZcuxI8AJ1gsOw5MgbW96RpAAx_Kk-5p4rhOopPhCa9kR_9mKpE + BEaGnq-mY0ZOXk2UT1ivWUe0PZ_iJX4Vyb6MtpOp7RMBu_6ErTrATlfuK3IaFcvHJr27h6L1T4owkBH6srLphIw Sign in Not logged in Sign out diff --git a/core/src/main/java/com/web3auth/core/types/Web3AuthResponse.kt b/core/src/main/java/com/web3auth/core/types/Web3AuthResponse.kt index 2a744e0..1f1a7bd 100644 --- a/core/src/main/java/com/web3auth/core/types/Web3AuthResponse.kt +++ b/core/src/main/java/com/web3auth/core/types/Web3AuthResponse.kt @@ -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, From 3fabbcf00a7f8cb6c60a66ac8801496b7bc9a765 Mon Sep 17 00:00:00 2001 From: Gaurav Goel Date: Mon, 17 Apr 2023 11:08:48 +0530 Subject: [PATCH 3/6] Adding error checks when web3AuthResponse is null Signed-off-by: Gaurav Goel --- .../main/java/com/web3auth/core/Web3Auth.kt | 29 +++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/core/src/main/java/com/web3auth/core/Web3Auth.kt b/core/src/main/java/com/web3auth/core/Web3Auth.kt index 29498d4..fe1d7c9 100644 --- a/core/src/main/java/com/web3auth/core/Web3Auth.kt +++ b/core/src/main/java/com/web3auth/core/Web3Auth.kt @@ -225,7 +225,6 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) { messageObj, ShareMetadata::class.java ) - println("shareMetadata$shareMetadata") KeyStoreManagerUtils.savePreferenceData( KeyStoreManagerUtils.EPHEM_PUBLIC_Key, @@ -334,25 +333,37 @@ 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("No userInfo found, please login again") } 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("No userInfo found, please login again") } else { - web3AuthResponse.ed25519PrivKey.toString() + if (web3AuthOption.useCoreKitKey == true) { + web3AuthResponse.coreKitEd25519PrivKey.toString() + } else { + web3AuthResponse.ed25519PrivKey.toString() + } } return ed25519Key } - fun getUserInfo(): UserInfo { - return web3AuthResponse.userInfo!! + fun getUserInfo(): UserInfo? { + if (web3AuthResponse == null) { + throw Error("No userInfo found, please login again") + } else { + return web3AuthResponse.userInfo + } } fun logout( From 09d611914579bbec00bde9b5a15b021e42d25d93 Mon Sep 17 00:00:00 2001 From: Gaurav Goel Date: Mon, 17 Apr 2023 12:25:27 +0530 Subject: [PATCH 4/6] Adding error checks when web3AuthResponse is null Signed-off-by: Gaurav Goel --- core/src/main/java/com/web3auth/core/Web3Auth.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/com/web3auth/core/Web3Auth.kt b/core/src/main/java/com/web3auth/core/Web3Auth.kt index fe1d7c9..7b87dc0 100644 --- a/core/src/main/java/com/web3auth/core/Web3Auth.kt +++ b/core/src/main/java/com/web3auth/core/Web3Auth.kt @@ -334,7 +334,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) { fun getPrivkey(): String { val privKey: String = if (web3AuthResponse == null) { - throw Error("No userInfo found, please login again") + throw Error("No user found, please login again") } else { if (web3AuthOption.useCoreKitKey == true) { web3AuthResponse.coreKitEd25519PrivKey.toString() @@ -347,7 +347,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) { fun getEd25519PrivKey(): String { val ed25519Key: String = if (web3AuthResponse == null) { - throw Error("No userInfo found, please login again") + throw Error("No user found, please login again") } else { if (web3AuthOption.useCoreKitKey == true) { web3AuthResponse.coreKitEd25519PrivKey.toString() @@ -359,7 +359,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) { } fun getUserInfo(): UserInfo? { - if (web3AuthResponse == null) { + if (web3AuthResponse == null || web3AuthResponse.userInfo == null) { throw Error("No userInfo found, please login again") } else { return web3AuthResponse.userInfo From 644add1dfbbd81d731eaca2b50edc4df7786040f Mon Sep 17 00:00:00 2001 From: Gaurav Goel Date: Mon, 17 Apr 2023 14:44:53 +0530 Subject: [PATCH 5/6] Added error codes to generalize errors Signed-off-by: Gaurav Goel --- .../main/java/com/web3auth/core/Web3Auth.kt | 12 ++++--- .../com/web3auth/core/types/Web3AuthError.kt | 36 +++++++++++++++++++ 2 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 core/src/main/java/com/web3auth/core/types/Web3AuthError.kt diff --git a/core/src/main/java/com/web3auth/core/Web3Auth.kt b/core/src/main/java/com/web3auth/core/Web3Auth.kt index 7b87dc0..1ee2e88 100644 --- a/core/src/main/java/com/web3auth/core/Web3Auth.kt +++ b/core/src/main/java/com/web3auth/core/Web3Auth.kt @@ -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_ERROR) ) ) } @@ -259,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_ERROR + ) ) ) } @@ -334,7 +336,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) { fun getPrivkey(): String { val privKey: String = if (web3AuthResponse == null) { - throw Error("No user found, please login again") + throw Error(Web3AuthError.getError(ErrorCode.NOUSERFOUND)) } else { if (web3AuthOption.useCoreKitKey == true) { web3AuthResponse.coreKitEd25519PrivKey.toString() @@ -347,7 +349,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) { fun getEd25519PrivKey(): String { val ed25519Key: String = if (web3AuthResponse == null) { - throw Error("No user found, please login again") + throw Error(Web3AuthError.getError(ErrorCode.NOUSERFOUND)) } else { if (web3AuthOption.useCoreKitKey == true) { web3AuthResponse.coreKitEd25519PrivKey.toString() @@ -360,7 +362,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) { fun getUserInfo(): UserInfo? { if (web3AuthResponse == null || web3AuthResponse.userInfo == null) { - throw Error("No userInfo found, please login again") + throw Error(Web3AuthError.getError(ErrorCode.NOUSERFOUND)) } else { return web3AuthResponse.userInfo } diff --git a/core/src/main/java/com/web3auth/core/types/Web3AuthError.kt b/core/src/main/java/com/web3auth/core/types/Web3AuthError.kt new file mode 100644 index 0000000..107c90e --- /dev/null +++ b/core/src/main/java/com/web3auth/core/types/Web3AuthError.kt @@ -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_ERROR -> { + "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_ERROR, +} \ No newline at end of file From adc36d35239a60e6835dca41bf7e2a303503ab4e Mon Sep 17 00:00:00 2001 From: Gaurav Goel Date: Mon, 17 Apr 2023 14:55:37 +0530 Subject: [PATCH 6/6] Code refractoring Signed-off-by: Gaurav Goel --- core/src/main/java/com/web3auth/core/Web3Auth.kt | 4 ++-- core/src/main/java/com/web3auth/core/types/Web3AuthError.kt | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/com/web3auth/core/Web3Auth.kt b/core/src/main/java/com/web3auth/core/Web3Auth.kt index 1ee2e88..09dfed2 100644 --- a/core/src/main/java/com/web3auth/core/Web3Auth.kt +++ b/core/src/main/java/com/web3auth/core/Web3Auth.kt @@ -154,7 +154,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) { if (web3AuthResponse.error?.isNotBlank() == true) { loginCompletableFuture.completeExceptionally( UnKnownException( - web3AuthResponse.error ?: Web3AuthError.getError(ErrorCode.SOMETHING_WENT_ERROR) + web3AuthResponse.error ?: Web3AuthError.getError(ErrorCode.SOMETHING_WENT_WRONG) ) ) } @@ -260,7 +260,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) { sessionCompletableFuture.completeExceptionally( UnKnownException( web3AuthResponse.error ?: Web3AuthError.getError( - ErrorCode.SOMETHING_WENT_ERROR + ErrorCode.SOMETHING_WENT_WRONG ) ) ) diff --git a/core/src/main/java/com/web3auth/core/types/Web3AuthError.kt b/core/src/main/java/com/web3auth/core/types/Web3AuthError.kt index 107c90e..da222c3 100644 --- a/core/src/main/java/com/web3auth/core/types/Web3AuthError.kt +++ b/core/src/main/java/com/web3auth/core/types/Web3AuthError.kt @@ -13,7 +13,7 @@ object Web3AuthError { ErrorCode.DECODING_ERROR -> { "Decoding Error" } - ErrorCode.SOMETHING_WENT_ERROR -> { + ErrorCode.SOMETHING_WENT_WRONG -> { "Something went wrong!" } ErrorCode.RUNTIME_ERROR -> { @@ -32,5 +32,5 @@ enum class ErrorCode { DECODING_ERROR, RUNTIME_ERROR, APP_CANCELLED, - SOMETHING_WENT_ERROR, + SOMETHING_WENT_WRONG, } \ No newline at end of file