Skip to content

Commit

Permalink
Merge pull request #11 from Web3Auth/update-isSessionIdExists-method
Browse files Browse the repository at this point in the history
feat: removed isSessionIdExists method and modified tests.
  • Loading branch information
grvgoel81 authored Nov 6, 2024
2 parents 907870c + 6559637 commit 6f85454
Show file tree
Hide file tree
Showing 16 changed files with 290 additions and 206 deletions.
32 changes: 15 additions & 17 deletions app/src/main/java/com/web3auth/sfaexample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.web3auth.singlefactorauth.SingleFactorAuth
import com.web3auth.singlefactorauth.types.LoginParams
import com.web3auth.singlefactorauth.types.SFAParams
import com.web3auth.singlefactorauth.types.Web3AuthOptions
import org.torusresearch.fetchnodedetails.types.Web3AuthNetwork

class MainActivity : AppCompatActivity() {

private lateinit var btnTorusKey: Button
private lateinit var tv: TextView
lateinit var singleFactorAuth: SingleFactorAuth
private lateinit var sfaParams: SFAParams
lateinit var loginParams: LoginParams
private lateinit var loginParams: LoginParams
var TEST_VERIFIER = "torus-test-health"
var TORUS_TEST_EMAIL = "[email protected]"

Expand All @@ -30,23 +29,22 @@ class MainActivity : AppCompatActivity() {
getSFAKey()
}
val idToken = JwtUtils.generateIdToken(TORUS_TEST_EMAIL)
sfaParams =
SFAParams(Web3AuthNetwork.SAPPHIRE_MAINNET, "YOUR_CLIENT_ID", 86400, null, 0)
singleFactorAuth = SingleFactorAuth(sfaParams, this)
val web3AuthOptions =
Web3AuthOptions("YOUR_CLIENT_ID", Web3AuthNetwork.SAPPHIRE_MAINNET, 86400)
singleFactorAuth = SingleFactorAuth(web3AuthOptions, this)
loginParams = LoginParams(TEST_VERIFIER, TORUS_TEST_EMAIL, idToken)

if (singleFactorAuth.isSessionIdExists()) {
val sfakey = singleFactorAuth.initialize(this.applicationContext)
sfakey.whenComplete { response, error ->
if (error == null) {
val text =
"Public Address: ${response.getPublicAddress()} , Private Key: ${response.getPrivateKey()}"
tv.text = text
} else {
tv.text = error.message
}
val sfakey = singleFactorAuth.initialize(this.applicationContext)
sfakey.whenComplete { response, error ->
if (response != null) {
val text =
"Public Address: ${response?.publicAddress} , Private Key: ${response?.privateKey}"
tv.text = text
} else {
tv.text = error.message
}
}

}

private fun getSFAKey() {
Expand All @@ -56,7 +54,7 @@ class MainActivity : AppCompatActivity() {
singleFactorAuth.connect(loginParams, this.applicationContext)
if (sfakey != null) {
val text =
"Public Address: ${sfakey.getPublicAddress()} , Private Key: ${sfakey.getPrivateKey()}"
"Public Address: ${sfakey.publicAddress} , Private Key: ${sfakey.privateKey}"
tv.text = text
}
}
Expand Down
3 changes: 2 additions & 1 deletion singlefactorauth/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ dependencies {
implementation 'com.google.code.gson:gson:2.10.1'
implementation 'org.slf4j:slf4j-simple:2.0.3'
//session-manager-sdk
implementation 'com.github.Web3Auth:session-manager-android:2.4.1'
implementation 'com.github.Web3Auth:session-manager-android:3.0.0'
implementation 'com.auth0.android:jwtdecode:2.0.1'

testImplementation 'junit:junit:4.13.2'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation
import com.auth0.jwt.algorithms.Algorithm
import com.web3auth.singlefactorauth.types.LoginParams
import com.web3auth.singlefactorauth.types.SFAParams
import com.web3auth.singlefactorauth.types.TorusSubVerifierInfo
import com.web3auth.singlefactorauth.types.Web3AuthOptions
import com.web3auth.singlefactorauth.utils.JwtUtils.generateIdToken
import com.web3auth.singlefactorauth.utils.PemUtils.readPrivateKeyFromReader
import com.web3auth.singlefactorauth.utils.WellKnownSecret
Expand All @@ -24,7 +24,7 @@ import java.util.concurrent.ExecutionException
class AquaTest {

lateinit var singleFactorAuth: SingleFactorAuth
private lateinit var sfaParams: SFAParams
private lateinit var web3AuthOptions: Web3AuthOptions
lateinit var loginParams: LoginParams
lateinit var algorithmRs: Algorithm
var TEST_VERIFIER = "torus-test-health"
Expand All @@ -36,8 +36,8 @@ class AquaTest {
fun shouldGetTorusKey() {

val context = getInstrumentation().context
sfaParams = SFAParams(Web3AuthNetwork.AQUA, "YOUR_CLIENT_ID", 86400, null, 0)
singleFactorAuth = SingleFactorAuth(sfaParams, context)
web3AuthOptions = Web3AuthOptions("YOUR_CLIENT_ID", Web3AuthNetwork.AQUA, 86400)
singleFactorAuth = SingleFactorAuth(web3AuthOptions, context)
val privateKey = readPrivateKeyFromReader(
WellKnownSecret.pem(),
"EC"
Expand All @@ -56,8 +56,8 @@ class AquaTest {
val requiredPrivateKey =
BigInteger("d8204e9f8c270647294c54acd8d49ee208789f981a7503158e122527d38626d8", 16)
if (sfaKey != null) {
assert(requiredPrivateKey.toString(16) == sfaKey.getPrivateKey())
assert("0x8b32926cD9224fec3B296aA7250B049029434807" == sfaKey.getPublicAddress())
assert(requiredPrivateKey.toString(16) == sfaKey.privateKey)
assert("0x8b32926cD9224fec3B296aA7250B049029434807" == sfaKey.publicAddress)
} else {
fail()
}
Expand All @@ -66,9 +66,9 @@ class AquaTest {
@Test
@Throws(ExecutionException::class, InterruptedException::class)
fun shouldAggregrateGetTorusKey() {
sfaParams = SFAParams(Web3AuthNetwork.AQUA, "YOUR_CLIENT_ID")
web3AuthOptions = Web3AuthOptions("YOUR_CLIENT_ID", Web3AuthNetwork.AQUA)
val context = getInstrumentation().context
singleFactorAuth = SingleFactorAuth(sfaParams, context)
singleFactorAuth = SingleFactorAuth(web3AuthOptions, context)
val privateKey = readPrivateKeyFromReader(
WellKnownSecret.pem(),
"EC"
Expand All @@ -93,8 +93,8 @@ class AquaTest {
val requiredPrivateKey =
BigInteger("6f8b884f19975fb0d138ed21b22a6a7e1b79e37f611d0a29f1442b34efc6bacd", 16)
if (sfaKey != null) {
assert(requiredPrivateKey.toString(16) == sfaKey.getPrivateKey())
assert("0x62BaCa60f48C2b2b7e3074f7B7b4795EeF2afD2e" == sfaKey.getPublicAddress())
assert(requiredPrivateKey.toString(16) == sfaKey.privateKey)
assert("0x62BaCa60f48C2b2b7e3074f7B7b4795EeF2afD2e" == sfaKey.publicAddress)
} else {
fail()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package com.web3auth.singlefactorauth
import androidx.test.platform.app.InstrumentationRegistry
import com.auth0.jwt.algorithms.Algorithm
import com.web3auth.singlefactorauth.types.LoginParams
import com.web3auth.singlefactorauth.types.SFAParams
import com.web3auth.singlefactorauth.types.TorusSubVerifierInfo
import com.web3auth.singlefactorauth.types.Web3AuthOptions
import com.web3auth.singlefactorauth.utils.JwtUtils.generateIdToken
import com.web3auth.singlefactorauth.utils.PemUtils.readPrivateKeyFromReader
import com.web3auth.singlefactorauth.utils.WellKnownSecret
Expand All @@ -21,7 +21,7 @@ import java.util.concurrent.ExecutionException
class CyanTest {

lateinit var singleFactorAuth: SingleFactorAuth
private lateinit var sfaParams: SFAParams
private lateinit var web3AuthOptions: Web3AuthOptions
lateinit var loginParams: LoginParams
lateinit var algorithmRs: Algorithm
var TEST_VERIFIER = "torus-test-health"
Expand All @@ -32,8 +32,8 @@ class CyanTest {
@Throws(ExecutionException::class, InterruptedException::class)
fun shouldGetTorusKey() {
val context = InstrumentationRegistry.getInstrumentation().context
sfaParams = SFAParams(Web3AuthNetwork.CYAN, "YOUR_CLIENT_ID", 86400, null, 0)
singleFactorAuth = SingleFactorAuth(sfaParams, context)
web3AuthOptions = Web3AuthOptions("YOUR_CLIENT_ID", Web3AuthNetwork.CYAN, 86400)
singleFactorAuth = SingleFactorAuth(web3AuthOptions, context)
val privateKey = readPrivateKeyFromReader(
WellKnownSecret.pem(),
"EC"
Expand All @@ -49,10 +49,10 @@ class CyanTest {
loginParams = LoginParams(TEST_VERIFIER, TORUS_TEST_EMAIL, idToken)
val sfakey = singleFactorAuth.connect(loginParams, context)
if (sfakey != null) {
assert("0x6b902fBCEb0E0374e5eB9eDFe68cD4B888c32150" == sfakey.getPublicAddress())
assert("0x6b902fBCEb0E0374e5eB9eDFe68cD4B888c32150" == sfakey.publicAddress)
val requiredPrivateKey =
BigInteger("223d982054fa1ad27d1497560521e4cce5b8c6438c38533c7bad27ff21ce0546", 16)
assert(requiredPrivateKey.toString(16) == sfakey.getPrivateKey())
assert(requiredPrivateKey.toString(16) == sfakey.privateKey)
} else {
fail()
}
Expand All @@ -62,8 +62,8 @@ class CyanTest {
@Throws(ExecutionException::class, InterruptedException::class)
fun shouldAggregrateGetTorusKey() {
val context = InstrumentationRegistry.getInstrumentation().context
sfaParams = SFAParams(Web3AuthNetwork.CYAN, "YOUR_CLIENT_ID")
singleFactorAuth = SingleFactorAuth(sfaParams, context)
web3AuthOptions = Web3AuthOptions("YOUR_CLIENT_ID", Web3AuthNetwork.CYAN)
singleFactorAuth = SingleFactorAuth(web3AuthOptions, context)
val privateKey = readPrivateKeyFromReader(
WellKnownSecret.pem(),
"EC"
Expand All @@ -87,8 +87,8 @@ class CyanTest {
if (sfakey != null) {
val requiredPrivateKey =
BigInteger("66af498ea82c95d52fdb8c8dedd44cf2f758424a0eecab7ac3dd8721527ea2d4", 16)
assert(requiredPrivateKey.toString(16) == sfakey.getPrivateKey())
assert("0xFF4c4A0Aa5D633302B5711C3047D7D5967884521" == sfakey.getPublicAddress())
assert(requiredPrivateKey.toString(16) == sfakey.privateKey)
assert("0xFF4c4A0Aa5D633302B5711C3047D7D5967884521" == sfakey.publicAddress)
} else {
fail()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package com.web3auth.singlefactorauth
import androidx.test.platform.app.InstrumentationRegistry
import com.auth0.jwt.algorithms.Algorithm
import com.web3auth.singlefactorauth.types.LoginParams
import com.web3auth.singlefactorauth.types.SFAParams
import com.web3auth.singlefactorauth.types.TorusSubVerifierInfo
import com.web3auth.singlefactorauth.types.Web3AuthOptions
import com.web3auth.singlefactorauth.utils.JwtUtils.generateIdToken
import com.web3auth.singlefactorauth.utils.PemUtils.readPrivateKeyFromReader
import com.web3auth.singlefactorauth.utils.WellKnownSecret
Expand All @@ -21,7 +21,7 @@ import java.util.concurrent.ExecutionException
class SapphireDevnetTest {

lateinit var singleFactorAuth: SingleFactorAuth
private lateinit var sfaParams: SFAParams
private lateinit var web3AuthOptions: Web3AuthOptions
lateinit var loginParams: LoginParams
lateinit var algorithmRs: Algorithm
var TEST_VERIFIER = "torus-test-health"
Expand All @@ -32,11 +32,10 @@ class SapphireDevnetTest {
@Throws(ExecutionException::class, InterruptedException::class)
fun shouldGetTorusKey() {
val context = InstrumentationRegistry.getInstrumentation().context
sfaParams = SFAParams(
Web3AuthNetwork.SAPPHIRE_DEVNET,
"CLIENT ID", 86400, null, 0
web3AuthOptions = Web3AuthOptions(
"CLIENT ID", Web3AuthNetwork.SAPPHIRE_DEVNET, 86400
)
singleFactorAuth = SingleFactorAuth(sfaParams, context)
singleFactorAuth = SingleFactorAuth(web3AuthOptions, context)
val privateKey = readPrivateKeyFromReader(
WellKnownSecret.pem(),
"EC"
Expand All @@ -52,10 +51,10 @@ class SapphireDevnetTest {
loginParams = LoginParams(TEST_VERIFIER, TORUS_TEST_EMAIL, idToken)
val sfakey = singleFactorAuth.connect(loginParams, context)
if (sfakey != null) {
assert("0x462A8BF111A55C9354425F875F89B22678c0Bc44" == sfakey.getPublicAddress())
assert("0x462A8BF111A55C9354425F875F89B22678c0Bc44" == sfakey.publicAddress)
val requiredPrivateKey =
BigInteger("230dad9f42039569e891e6b066ff5258b14e9764ef5176d74aeb594d1a744203", 16)
assert(requiredPrivateKey.toString(16) == sfakey.getPrivateKey())
assert(requiredPrivateKey.toString(16) == sfakey.privateKey)
} else {
fail()
}
Expand All @@ -65,11 +64,8 @@ class SapphireDevnetTest {
@Throws(ExecutionException::class, InterruptedException::class)
fun shouldAggregrateGetTorusKey() {
val context = InstrumentationRegistry.getInstrumentation().context
sfaParams = SFAParams(
Web3AuthNetwork.SAPPHIRE_DEVNET,
"CLIENT_ID"
)
singleFactorAuth = SingleFactorAuth(sfaParams, context)
web3AuthOptions = Web3AuthOptions("CLIENT ID", Web3AuthNetwork.SAPPHIRE_DEVNET)
singleFactorAuth = SingleFactorAuth(web3AuthOptions, context)
val privateKey = readPrivateKeyFromReader(
WellKnownSecret.pem(),
"EC"
Expand All @@ -93,8 +89,8 @@ class SapphireDevnetTest {
val requiredPrivateKey =
BigInteger("edef171853fdf23ed3cfc702d32cf46f181b475a449d2f7b636924cabecd81d4", 16)
if (sfakey != null) {
assert(requiredPrivateKey.toString(16) == sfakey.getPrivateKey())
assert("0xfC58EB0475F1E3fa05877eE2e1350f6A619E2d78" == sfakey.getPublicAddress())
assert(requiredPrivateKey.toString(16) == sfakey.privateKey)
assert("0xfC58EB0475F1E3fa05877eE2e1350f6A619E2d78" == sfakey.publicAddress)
} else {
fail()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package com.web3auth.singlefactorauth
import androidx.test.platform.app.InstrumentationRegistry
import com.auth0.jwt.algorithms.Algorithm
import com.web3auth.singlefactorauth.types.LoginParams
import com.web3auth.singlefactorauth.types.SFAParams
import com.web3auth.singlefactorauth.types.TorusSubVerifierInfo
import com.web3auth.singlefactorauth.types.Web3AuthOptions
import com.web3auth.singlefactorauth.utils.JwtUtils.generateIdToken
import com.web3auth.singlefactorauth.utils.PemUtils.readPrivateKeyFromReader
import com.web3auth.singlefactorauth.utils.WellKnownSecret
Expand All @@ -21,7 +21,7 @@ import java.util.concurrent.ExecutionException
class SapphireMainnetTest {

lateinit var singleFactorAuth: SingleFactorAuth
private lateinit var sfaParams: SFAParams
private lateinit var web3AuthOptions: Web3AuthOptions
lateinit var loginParams: LoginParams
lateinit var algorithmRs: Algorithm
var TEST_VERIFIER = "torus-test-health"
Expand All @@ -32,12 +32,11 @@ class SapphireMainnetTest {
@Throws(ExecutionException::class, InterruptedException::class)
fun shouldGetTorusKey() {
val context = InstrumentationRegistry.getInstrumentation().context
sfaParams = SFAParams(
Web3AuthNetwork.SAPPHIRE_MAINNET,
"CLIENT ID", 86400,
null, 0
web3AuthOptions = Web3AuthOptions(
"CLIENT ID", Web3AuthNetwork.SAPPHIRE_MAINNET,
86400
)
singleFactorAuth = SingleFactorAuth(sfaParams, context)
singleFactorAuth = SingleFactorAuth(web3AuthOptions, context)
val privateKey = readPrivateKeyFromReader(
WellKnownSecret.pem(),
"EC"
Expand All @@ -53,10 +52,10 @@ class SapphireMainnetTest {
loginParams = LoginParams(TEST_VERIFIER,TORUS_TEST_EMAIL, idToken)
val sfakey = singleFactorAuth.connect(loginParams, context)
if (sfakey != null) {
assert("0x0934d844a0a6db37CF75aF0269436ae1b2Ae5D36" == sfakey.getPublicAddress())
assert("0x0934d844a0a6db37CF75aF0269436ae1b2Ae5D36" == sfakey.publicAddress)
val requiredPrivateKey =
BigInteger("2c4b346a91ecd11fe8a02d111d00bd921bf9b543f0a1e811face91b5f28947d6", 16)
assert(requiredPrivateKey.toString(16) == sfakey.getPrivateKey())
assert(requiredPrivateKey.toString(16) == sfakey.privateKey)
} else {
fail()
}
Expand All @@ -66,11 +65,11 @@ class SapphireMainnetTest {
@Throws(ExecutionException::class, InterruptedException::class)
fun shouldAggregrateGetTorusKey() {
val context = InstrumentationRegistry.getInstrumentation().context
sfaParams = SFAParams(
Web3AuthNetwork.SAPPHIRE_MAINNET,
"CLIENT_ID"
web3AuthOptions = Web3AuthOptions(
"CLIENT ID",
Web3AuthNetwork.SAPPHIRE_MAINNET
)
singleFactorAuth = SingleFactorAuth(sfaParams, context)
singleFactorAuth = SingleFactorAuth(web3AuthOptions, context)
val privateKey = readPrivateKeyFromReader(
WellKnownSecret.pem(),
"EC"
Expand All @@ -95,10 +94,10 @@ class SapphireMainnetTest {
BigInteger("0c724bb285560dc41e585b91aa2ded94fdd703c2e7133dcc64b1361b0d1fd105", 16)

if (sfakey != null) {
assert(requiredPrivateKey.toString(16).padStart(64, '0') == sfakey.getPrivateKey())
assert(requiredPrivateKey.toString(16).padStart(64, '0') == sfakey.privateKey)
assert(
"0xA92E2C756B5b2abABc127907b02D4707dc085612" ==
sfakey.getPublicAddress()
sfakey.publicAddress
)
} else {
fail()
Expand Down
Loading

0 comments on commit 6f85454

Please sign in to comment.