From 652d1f6a42adae15970da6efb0644d08862eca6a Mon Sep 17 00:00:00 2001 From: Gaurav Goel Date: Fri, 8 Nov 2024 08:51:33 +0530 Subject: [PATCH 1/4] feat: Update manifest.xml Signed-off-by: Gaurav Goel --- singlefactorauth/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/singlefactorauth/build.gradle b/singlefactorauth/build.gradle index 7ebae56..d1d940e 100644 --- a/singlefactorauth/build.gradle +++ b/singlefactorauth/build.gradle @@ -39,7 +39,7 @@ dependencies { implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'org.web3j:crypto:4.8.8-android' - implementation 'org.torusresearch:torus-utils-java:4.0.3' + api 'org.torusresearch:torus-utils-java:4.0.3' api 'org.torusresearch:fetch-node-details-java:5.0.0' implementation 'com.google.code.gson:gson:2.10.1' implementation 'org.slf4j:slf4j-simple:2.0.3' From 2bd91fb8eb522c0ebc98e07cdc2065aa22ae90a5 Mon Sep 17 00:00:00 2001 From: Gaurav Goel Date: Fri, 8 Nov 2024 10:14:55 +0530 Subject: [PATCH 2/4] feat: Update logout function Signed-off-by: Gaurav Goel --- .../com/web3auth/singlefactorauth/SingleFactorAuth.kt | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/singlefactorauth/src/main/java/com/web3auth/singlefactorauth/SingleFactorAuth.kt b/singlefactorauth/src/main/java/com/web3auth/singlefactorauth/SingleFactorAuth.kt index fea9662..8d8fc73 100644 --- a/singlefactorauth/src/main/java/com/web3auth/singlefactorauth/SingleFactorAuth.kt +++ b/singlefactorauth/src/main/java/com/web3auth/singlefactorauth/SingleFactorAuth.kt @@ -214,12 +214,17 @@ class SingleFactorAuth( } } - fun logout(context: Context) { - sessionManager.invalidateSession(context).whenComplete { res, _ -> + fun logout(context: Context): CompletableFuture { + val logoutCF = CompletableFuture() + sessionManager.invalidateSession(context).whenComplete { res, err -> if (res) { SessionManager.deleteSessionIdFromStorage() this.state = null + logoutCF.complete(null) + } else { + logoutCF.completeExceptionally(err) } } + return logoutCF } } \ No newline at end of file From 3fd8be38bf9dfd98738b8ec4ae606e4096b3feac Mon Sep 17 00:00:00 2001 From: Gaurav Goel Date: Mon, 11 Nov 2024 16:20:40 +0530 Subject: [PATCH 3/4] feat: Refactor initialize function Signed-off-by: Gaurav Goel --- .../main/java/com/web3auth/sfaexample/MainActivity.kt | 9 +++------ .../com/web3auth/singlefactorauth/SingleFactorAuth.kt | 6 +++--- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/web3auth/sfaexample/MainActivity.kt b/app/src/main/java/com/web3auth/sfaexample/MainActivity.kt index f301190..668183d 100644 --- a/app/src/main/java/com/web3auth/sfaexample/MainActivity.kt +++ b/app/src/main/java/com/web3auth/sfaexample/MainActivity.kt @@ -34,14 +34,11 @@ class MainActivity : AppCompatActivity() { singleFactorAuth = SingleFactorAuth(web3AuthOptions, this) loginParams = LoginParams(TEST_VERIFIER, TORUS_TEST_EMAIL, idToken) - val sfakey = singleFactorAuth.initialize(this.applicationContext) - sfakey.whenComplete { response, error -> - if (response != null) { + singleFactorAuth.initialize(this.applicationContext).whenComplete { res, err -> + if (err == null) { val text = - "Public Address: ${response?.publicAddress} , Private Key: ${response?.privateKey}" + "Public Address: ${singleFactorAuth.getSessionData()?.publicAddress} , Private Key: ${singleFactorAuth.getSessionData()?.privateKey}" tv.text = text - } else { - tv.text = error.message } } diff --git a/singlefactorauth/src/main/java/com/web3auth/singlefactorauth/SingleFactorAuth.kt b/singlefactorauth/src/main/java/com/web3auth/singlefactorauth/SingleFactorAuth.kt index 8d8fc73..4b9c92f 100644 --- a/singlefactorauth/src/main/java/com/web3auth/singlefactorauth/SingleFactorAuth.kt +++ b/singlefactorauth/src/main/java/com/web3auth/singlefactorauth/SingleFactorAuth.kt @@ -50,7 +50,7 @@ class SingleFactorAuth( sessionManager = SessionManager(ctx, web3AuthOptions.getSessionTime(), ctx.packageName) } - fun initialize(ctx: Context): CompletableFuture { + fun initialize(ctx: Context): CompletableFuture { return CompletableFuture.supplyAsync { val savedSessionId = SessionManager.getSessionIdFromStorage() sessionManager.setSessionId(savedSessionId) @@ -85,8 +85,8 @@ class SingleFactorAuth( userInfo = finalUserInfo ) state - }.thenApplyAsync({ sessionData -> - sessionData + }.thenApplyAsync({ _ -> + null }, { Handler(Looper.getMainLooper()).post(it) }).exceptionally { ex -> throw Exception("Initialization failed", ex) } From fb316531b06ec652387a1f43e4387ab269b08daf Mon Sep 17 00:00:00 2001 From: Gaurav Goel Date: Wed, 13 Nov 2024 08:46:36 +0530 Subject: [PATCH 4/4] feat: Update all unit-tests and example. Signed-off-by: Gaurav Goel --- .../com/web3auth/sfaexample/MainActivity.kt | 4 ++-- .../com/web3auth/singlefactorauth/AquaTest.kt | 16 ++++++------- .../com/web3auth/singlefactorauth/CyanTest.kt | 18 +++++++-------- .../singlefactorauth/SapphireDevnetTest.kt | 16 ++++++------- .../singlefactorauth/SapphireMainnetTest.kt | 18 +++++++-------- .../singlefactorauth/SingleFactorAuthTest.kt | 23 +++++++++++-------- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/app/src/main/java/com/web3auth/sfaexample/MainActivity.kt b/app/src/main/java/com/web3auth/sfaexample/MainActivity.kt index 668183d..0641d55 100644 --- a/app/src/main/java/com/web3auth/sfaexample/MainActivity.kt +++ b/app/src/main/java/com/web3auth/sfaexample/MainActivity.kt @@ -26,7 +26,7 @@ class MainActivity : AppCompatActivity() { tv = findViewById(R.id.tv) btnTorusKey.setOnClickListener { - getSFAKey() + getSessionData() } val idToken = JwtUtils.generateIdToken(TORUS_TEST_EMAIL) val web3AuthOptions = @@ -44,7 +44,7 @@ class MainActivity : AppCompatActivity() { } - private fun getSFAKey() { + private fun getSessionData() { val idToken = JwtUtils.generateIdToken(TORUS_TEST_EMAIL) loginParams = LoginParams(TEST_VERIFIER, TORUS_TEST_EMAIL, idToken) val sfakey = diff --git a/singlefactorauth/src/androidTest/java/com/web3auth/singlefactorauth/AquaTest.kt b/singlefactorauth/src/androidTest/java/com/web3auth/singlefactorauth/AquaTest.kt index 88e6168..1a0962e 100644 --- a/singlefactorauth/src/androidTest/java/com/web3auth/singlefactorauth/AquaTest.kt +++ b/singlefactorauth/src/androidTest/java/com/web3auth/singlefactorauth/AquaTest.kt @@ -51,13 +51,13 @@ class AquaTest { algorithmRs = Algorithm.ECDSA256(publicKey, privateKey) val idToken: String = generateIdToken(TORUS_TEST_EMAIL, algorithmRs) loginParams = LoginParams(TEST_VERIFIER, TORUS_TEST_EMAIL, idToken) - val sfaKey = singleFactorAuth.connect(loginParams, context) + val sessionData = singleFactorAuth.connect(loginParams, context) singleFactorAuth.initialize(context) val requiredPrivateKey = BigInteger("d8204e9f8c270647294c54acd8d49ee208789f981a7503158e122527d38626d8", 16) - if (sfaKey != null) { - assert(requiredPrivateKey.toString(16) == sfaKey.privateKey) - assert("0x8b32926cD9224fec3B296aA7250B049029434807" == sfaKey.publicAddress) + if (sessionData != null) { + assert(requiredPrivateKey.toString(16) == sessionData.privateKey) + assert("0x8b32926cD9224fec3B296aA7250B049029434807" == sessionData.publicAddress) } else { fail() } @@ -88,13 +88,13 @@ class AquaTest { ) ) ) - val sfaKey = singleFactorAuth.connect(loginParams, context) + val sessionData = singleFactorAuth.connect(loginParams, context) singleFactorAuth.initialize(context) val requiredPrivateKey = BigInteger("6f8b884f19975fb0d138ed21b22a6a7e1b79e37f611d0a29f1442b34efc6bacd", 16) - if (sfaKey != null) { - assert(requiredPrivateKey.toString(16) == sfaKey.privateKey) - assert("0x62BaCa60f48C2b2b7e3074f7B7b4795EeF2afD2e" == sfaKey.publicAddress) + if (sessionData != null) { + assert(requiredPrivateKey.toString(16) == sessionData.privateKey) + assert("0x62BaCa60f48C2b2b7e3074f7B7b4795EeF2afD2e" == sessionData.publicAddress) } else { fail() } diff --git a/singlefactorauth/src/androidTest/java/com/web3auth/singlefactorauth/CyanTest.kt b/singlefactorauth/src/androidTest/java/com/web3auth/singlefactorauth/CyanTest.kt index d0620fd..3384e7e 100644 --- a/singlefactorauth/src/androidTest/java/com/web3auth/singlefactorauth/CyanTest.kt +++ b/singlefactorauth/src/androidTest/java/com/web3auth/singlefactorauth/CyanTest.kt @@ -47,12 +47,12 @@ class CyanTest { algorithmRs = Algorithm.ECDSA256(publicKey, privateKey) val idToken: String = generateIdToken(TORUS_TEST_EMAIL, algorithmRs) loginParams = LoginParams(TEST_VERIFIER, TORUS_TEST_EMAIL, idToken) - val sfakey = singleFactorAuth.connect(loginParams, context) - if (sfakey != null) { - assert("0x6b902fBCEb0E0374e5eB9eDFe68cD4B888c32150" == sfakey.publicAddress) + val sessionData = singleFactorAuth.connect(loginParams, context) + if (sessionData != null) { + assert("0x6b902fBCEb0E0374e5eB9eDFe68cD4B888c32150" == sessionData.publicAddress) val requiredPrivateKey = - BigInteger("223d982054fa1ad27d1497560521e4cce5b8c6438c38533c7bad27ff21ce0546", 16) - assert(requiredPrivateKey.toString(16) == sfakey.privateKey) + BigInteger("223d982054fa1ad27d1497560521e4cce5b8c6438c38533c7bad27ff21ce0546", 16) + assert(requiredPrivateKey.toString(16) == sessionData.privateKey) } else { fail() } @@ -83,12 +83,12 @@ class CyanTest { ) ) ) - val sfakey = singleFactorAuth.connect(loginParams, context) - if (sfakey != null) { + val sessionData = singleFactorAuth.connect(loginParams, context) + if (sessionData != null) { val requiredPrivateKey = BigInteger("66af498ea82c95d52fdb8c8dedd44cf2f758424a0eecab7ac3dd8721527ea2d4", 16) - assert(requiredPrivateKey.toString(16) == sfakey.privateKey) - assert("0xFF4c4A0Aa5D633302B5711C3047D7D5967884521" == sfakey.publicAddress) + assert(requiredPrivateKey.toString(16) == sessionData.privateKey) + assert("0xFF4c4A0Aa5D633302B5711C3047D7D5967884521" == sessionData.publicAddress) } else { fail() } diff --git a/singlefactorauth/src/androidTest/java/com/web3auth/singlefactorauth/SapphireDevnetTest.kt b/singlefactorauth/src/androidTest/java/com/web3auth/singlefactorauth/SapphireDevnetTest.kt index 5f433e0..dac4cac 100644 --- a/singlefactorauth/src/androidTest/java/com/web3auth/singlefactorauth/SapphireDevnetTest.kt +++ b/singlefactorauth/src/androidTest/java/com/web3auth/singlefactorauth/SapphireDevnetTest.kt @@ -49,12 +49,12 @@ class SapphireDevnetTest { algorithmRs = Algorithm.ECDSA256(publicKey, privateKey) val idToken: String = generateIdToken(TORUS_TEST_EMAIL, algorithmRs) loginParams = LoginParams(TEST_VERIFIER, TORUS_TEST_EMAIL, idToken) - val sfakey = singleFactorAuth.connect(loginParams, context) - if (sfakey != null) { - assert("0x462A8BF111A55C9354425F875F89B22678c0Bc44" == sfakey.publicAddress) + val sessionData = singleFactorAuth.connect(loginParams, context) + if (sessionData != null) { + assert("0x462A8BF111A55C9354425F875F89B22678c0Bc44" == sessionData.publicAddress) val requiredPrivateKey = BigInteger("230dad9f42039569e891e6b066ff5258b14e9764ef5176d74aeb594d1a744203", 16) - assert(requiredPrivateKey.toString(16) == sfakey.privateKey) + assert(requiredPrivateKey.toString(16) == sessionData.privateKey) } else { fail() } @@ -85,12 +85,12 @@ class SapphireDevnetTest { ) ) ) - val sfakey = singleFactorAuth.connect(loginParams, context) + val sessionData = singleFactorAuth.connect(loginParams, context) val requiredPrivateKey = BigInteger("edef171853fdf23ed3cfc702d32cf46f181b475a449d2f7b636924cabecd81d4", 16) - if (sfakey != null) { - assert(requiredPrivateKey.toString(16) == sfakey.privateKey) - assert("0xfC58EB0475F1E3fa05877eE2e1350f6A619E2d78" == sfakey.publicAddress) + if (sessionData != null) { + assert(requiredPrivateKey.toString(16) == sessionData.privateKey) + assert("0xfC58EB0475F1E3fa05877eE2e1350f6A619E2d78" == sessionData.publicAddress) } else { fail() } diff --git a/singlefactorauth/src/androidTest/java/com/web3auth/singlefactorauth/SapphireMainnetTest.kt b/singlefactorauth/src/androidTest/java/com/web3auth/singlefactorauth/SapphireMainnetTest.kt index f185fc0..e6c0c83 100644 --- a/singlefactorauth/src/androidTest/java/com/web3auth/singlefactorauth/SapphireMainnetTest.kt +++ b/singlefactorauth/src/androidTest/java/com/web3auth/singlefactorauth/SapphireMainnetTest.kt @@ -49,13 +49,13 @@ class SapphireMainnetTest { ) as ECPublicKey algorithmRs = Algorithm.ECDSA256(publicKey, privateKey) val idToken: String = generateIdToken(TORUS_TEST_EMAIL, algorithmRs) - loginParams = LoginParams(TEST_VERIFIER,TORUS_TEST_EMAIL, idToken) - val sfakey = singleFactorAuth.connect(loginParams, context) - if (sfakey != null) { - assert("0x0934d844a0a6db37CF75aF0269436ae1b2Ae5D36" == sfakey.publicAddress) + loginParams = LoginParams(TEST_VERIFIER, TORUS_TEST_EMAIL, idToken) + val sessionData = singleFactorAuth.connect(loginParams, context) + if (sessionData != null) { + assert("0x0934d844a0a6db37CF75aF0269436ae1b2Ae5D36" == sessionData.publicAddress) val requiredPrivateKey = BigInteger("2c4b346a91ecd11fe8a02d111d00bd921bf9b543f0a1e811face91b5f28947d6", 16) - assert(requiredPrivateKey.toString(16) == sfakey.privateKey) + assert(requiredPrivateKey.toString(16) == sessionData.privateKey) } else { fail() } @@ -89,15 +89,15 @@ class SapphireMainnetTest { ) ) ) - val sfakey = singleFactorAuth.connect(loginParams, context) + val sessionData = singleFactorAuth.connect(loginParams, context) val requiredPrivateKey = BigInteger("0c724bb285560dc41e585b91aa2ded94fdd703c2e7133dcc64b1361b0d1fd105", 16) - if (sfakey != null) { - assert(requiredPrivateKey.toString(16).padStart(64, '0') == sfakey.privateKey) + if (sessionData != null) { + assert(requiredPrivateKey.toString(16).padStart(64, '0') == sessionData.privateKey) assert( "0xA92E2C756B5b2abABc127907b02D4707dc085612" == - sfakey.publicAddress + sessionData.publicAddress ) } else { fail() diff --git a/singlefactorauth/src/androidTest/java/com/web3auth/singlefactorauth/SingleFactorAuthTest.kt b/singlefactorauth/src/androidTest/java/com/web3auth/singlefactorauth/SingleFactorAuthTest.kt index d28cad7..de3b807 100644 --- a/singlefactorauth/src/androidTest/java/com/web3auth/singlefactorauth/SingleFactorAuthTest.kt +++ b/singlefactorauth/src/androidTest/java/com/web3auth/singlefactorauth/SingleFactorAuthTest.kt @@ -49,12 +49,15 @@ class SingleFactorAuthTest { algorithmRs = Algorithm.ECDSA256(publicKey, privateKey) val idToken: String = generateIdToken(TORUS_TEST_EMAIL, algorithmRs) loginParams = LoginParams(TEST_VERIFIER, TORUS_TEST_EMAIL, idToken) - val sfakey = singleFactorAuth.connect(loginParams, context) - if (sfakey != null) { - assert("0x90A926b698047b4A87265ba1E9D8b512E8489067" == sfakey.publicAddress) + val sessionData = singleFactorAuth.connect(loginParams, context) + if (sessionData != null) { + assert("0x90A926b698047b4A87265ba1E9D8b512E8489067" == sessionData.publicAddress) val requiredPrivateKey = - BigInteger("0129494416ab5d5f674692b39fa49680e07d3aac01b9683ee7650e40805d4c44", 16).toString(16).padStart(64,'0') - assert(requiredPrivateKey == sfakey.privateKey) + BigInteger( + "0129494416ab5d5f674692b39fa49680e07d3aac01b9683ee7650e40805d4c44", + 16 + ).toString(16).padStart(64, '0') + assert(requiredPrivateKey == sessionData.privateKey) } else { fail() } @@ -85,13 +88,13 @@ class SingleFactorAuthTest { ) ) ) - val sfakey = singleFactorAuth.connect(loginParams, context) + val sessionData = singleFactorAuth.connect(loginParams, context) val requiredPrivateKey = BigInteger("68390578bbdab74e9883de58d3919c176662852bdd42a783bc3a08f1a1024e0c", 16) - if (sfakey != null) { - assert(requiredPrivateKey.toString(16) == sfakey.privateKey) - assert("0x86129bC541b03B6B42A76E9e002eE88F81E0aadD" == sfakey.publicAddress) - } else { + if (sessionData != null) { + assert(requiredPrivateKey.toString(16) == sessionData.privateKey) + assert("0x86129bC541b03B6B42A76E9e002eE88F81E0aadD" == sessionData.publicAddress) + } else { fail() } }