Skip to content

Commit

Permalink
feat: Update session-manager-android and code related changes
Browse files Browse the repository at this point in the history
Signed-off-by: Gaurav Goel <[email protected]>
  • Loading branch information
grvgoel81 authored and Gaurav Goel committed Sep 16, 2024
1 parent 55bf50f commit 9a5f4ba
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 33 deletions.
4 changes: 2 additions & 2 deletions app/src/main/java/com/web3auth/sfaexample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class MainActivity : AppCompatActivity() {
val idToken = JwtUtils.generateIdToken(TORUS_TEST_EMAIL)
sfaParams =
SFAParams(Web3AuthNetwork.SAPPHIRE_MAINNET, "YOUR_CLIENT_ID", null, 0)
singleFactorAuth = SingleFactorAuth(sfaParams, this)
singleFactorAuth = SingleFactorAuth(sfaParams, this, 86400, this.packageName)
loginParams = LoginParams(TEST_VERIFIER, TORUS_TEST_EMAIL, idToken)

if (singleFactorAuth.isSessionIdExists()) {
Expand All @@ -47,7 +47,7 @@ class MainActivity : AppCompatActivity() {
val idToken = JwtUtils.generateIdToken(TORUS_TEST_EMAIL)
loginParams = LoginParams(TEST_VERIFIER, TORUS_TEST_EMAIL, idToken)
val sfakey =
singleFactorAuth.connect(loginParams, 86400, this.applicationContext)
singleFactorAuth.connect(loginParams, this.applicationContext)
if (sfakey != null) {
val text =
"Public Address: ${sfakey.getPublicAddress()} , Private Key: ${sfakey.getPrivateKey()}"
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.3.0'
//TODO(): Update version
implementation 'com.github.Web3Auth:session-manager-android:feat~constructor-updates-SNAPSHOT'

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 @@ -37,7 +37,7 @@ class AquaTest {

val context = getInstrumentation().context
sfaParams = SFAParams(Web3AuthNetwork.AQUA, "YOUR_CLIENT_ID", null, 0)
singleFactorAuth = SingleFactorAuth(sfaParams, context)
singleFactorAuth = SingleFactorAuth(sfaParams, context, 86400, context.packageName)
val privateKey = readPrivateKeyFromReader(
WellKnownSecret.pem(),
"EC"
Expand All @@ -51,7 +51,7 @@ 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, 86400, context)
val sfaKey = singleFactorAuth.connect(loginParams, context)
singleFactorAuth.initialize(context)
val requiredPrivateKey =
BigInteger("d8204e9f8c270647294c54acd8d49ee208789f981a7503158e122527d38626d8", 16)
Expand All @@ -68,7 +68,7 @@ class AquaTest {
fun shouldAggregrateGetTorusKey() {
sfaParams = SFAParams(Web3AuthNetwork.AQUA, "YOUR_CLIENT_ID")
val context = getInstrumentation().context
singleFactorAuth = SingleFactorAuth(sfaParams, context)
singleFactorAuth = SingleFactorAuth(sfaParams, context, 86400, context.packageName)
val privateKey = readPrivateKeyFromReader(
WellKnownSecret.pem(),
"EC"
Expand All @@ -88,7 +88,7 @@ class AquaTest {
)
)
)
val sfaKey = singleFactorAuth.connect(loginParams, 86400, context)
val sfaKey = singleFactorAuth.connect(loginParams, context)
singleFactorAuth.initialize(context)
val requiredPrivateKey =
BigInteger("6f8b884f19975fb0d138ed21b22a6a7e1b79e37f611d0a29f1442b34efc6bacd", 16)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class CyanTest {
fun shouldGetTorusKey() {
val context = InstrumentationRegistry.getInstrumentation().context
sfaParams = SFAParams(Web3AuthNetwork.CYAN, "YOUR_CLIENT_ID", null, 0)
singleFactorAuth = SingleFactorAuth(sfaParams,context)
singleFactorAuth = SingleFactorAuth(sfaParams, context, 86400, context.packageName)
val privateKey = readPrivateKeyFromReader(
WellKnownSecret.pem(),
"EC"
Expand All @@ -47,7 +47,7 @@ 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, 86400, context)
val sfakey = singleFactorAuth.connect(loginParams, context)
if (sfakey != null) {
assert("0x6b902fBCEb0E0374e5eB9eDFe68cD4B888c32150" == sfakey.getPublicAddress())
val requiredPrivateKey =
Expand All @@ -63,7 +63,7 @@ class CyanTest {
fun shouldAggregrateGetTorusKey() {
val context = InstrumentationRegistry.getInstrumentation().context
sfaParams = SFAParams(Web3AuthNetwork.CYAN, "YOUR_CLIENT_ID")
singleFactorAuth = SingleFactorAuth(sfaParams, context)
singleFactorAuth = SingleFactorAuth(sfaParams, context, 86400, context.packageName)
val privateKey = readPrivateKeyFromReader(
WellKnownSecret.pem(),
"EC"
Expand All @@ -83,7 +83,7 @@ class CyanTest {
)
)
)
val sfakey = singleFactorAuth.connect(loginParams, 86400, context)
val sfakey = singleFactorAuth.connect(loginParams, context)
if (sfakey != null) {
val requiredPrivateKey =
BigInteger("66af498ea82c95d52fdb8c8dedd44cf2f758424a0eecab7ac3dd8721527ea2d4", 16)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class SapphireDevnetTest {
Web3AuthNetwork.SAPPHIRE_DEVNET,
"CLIENT ID", null, 0
)
singleFactorAuth = SingleFactorAuth(sfaParams, context)
singleFactorAuth = SingleFactorAuth(sfaParams, context, 86400, context.packageName)
val privateKey = readPrivateKeyFromReader(
WellKnownSecret.pem(),
"EC"
Expand All @@ -50,7 +50,7 @@ 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, 86400, context)
val sfakey = singleFactorAuth.connect(loginParams, context)
if (sfakey != null) {
assert("0x462A8BF111A55C9354425F875F89B22678c0Bc44" == sfakey.getPublicAddress())
val requiredPrivateKey =
Expand All @@ -69,7 +69,7 @@ class SapphireDevnetTest {
Web3AuthNetwork.SAPPHIRE_DEVNET,
"CLIENT_ID"
)
singleFactorAuth = SingleFactorAuth(sfaParams,context)
singleFactorAuth = SingleFactorAuth(sfaParams, context, 86400, context.packageName)
val privateKey = readPrivateKeyFromReader(
WellKnownSecret.pem(),
"EC"
Expand All @@ -89,7 +89,7 @@ class SapphireDevnetTest {
)
)
)
val sfakey = singleFactorAuth.connect(loginParams, 86400, context)
val sfakey = singleFactorAuth.connect(loginParams, context)
val requiredPrivateKey =
BigInteger("edef171853fdf23ed3cfc702d32cf46f181b475a449d2f7b636924cabecd81d4", 16)
if (sfakey != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class SapphireMainnetTest {
"CLIENT ID",
null, 0
)
singleFactorAuth = SingleFactorAuth(sfaParams, context)
singleFactorAuth = SingleFactorAuth(sfaParams, context, 86400, context.packageName)
val privateKey = readPrivateKeyFromReader(
WellKnownSecret.pem(),
"EC"
Expand All @@ -51,7 +51,7 @@ class SapphireMainnetTest {
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, 86400, context)
val sfakey = singleFactorAuth.connect(loginParams, context)
if (sfakey != null) {
assert("0x0934d844a0a6db37CF75aF0269436ae1b2Ae5D36" == sfakey.getPublicAddress())
val requiredPrivateKey =
Expand All @@ -70,7 +70,7 @@ class SapphireMainnetTest {
Web3AuthNetwork.SAPPHIRE_MAINNET,
"CLIENT_ID"
)
singleFactorAuth = SingleFactorAuth(sfaParams, context)
singleFactorAuth = SingleFactorAuth(sfaParams, context, 86400, context.packageName)
val privateKey = readPrivateKeyFromReader(
WellKnownSecret.pem(),
"EC"
Expand All @@ -90,7 +90,7 @@ class SapphireMainnetTest {
)
)
)
val sfakey = singleFactorAuth.connect(loginParams, 86400, context)
val sfakey = singleFactorAuth.connect(loginParams, context)
val requiredPrivateKey =
BigInteger("0c724bb285560dc41e585b91aa2ded94fdd703c2e7133dcc64b1361b0d1fd105", 16)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class SingleFactorAuthTest {
fun shouldGetTorusKey() {
val context = InstrumentationRegistry.getInstrumentation().context
sfaParams = SFAParams(Web3AuthNetwork.MAINNET, "CLIENT_ID", null, 0)
singleFactorAuth = SingleFactorAuth(sfaParams, context)
singleFactorAuth = SingleFactorAuth(sfaParams, context, 86400, context.packageName)
val privateKey = readPrivateKeyFromReader(
WellKnownSecret.pem(),
"EC"
Expand All @@ -47,7 +47,7 @@ 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, 86400, context)
val sfakey = singleFactorAuth.connect(loginParams, context)
if (sfakey != null) {
assert("0x90A926b698047b4A87265ba1E9D8b512E8489067" == sfakey.getPublicAddress())
val requiredPrivateKey =
Expand All @@ -63,7 +63,7 @@ class SingleFactorAuthTest {
fun shouldAggregrateGetTorusKey() {
val context = InstrumentationRegistry.getInstrumentation().context
sfaParams = SFAParams(Web3AuthNetwork.MAINNET, "YOUR_CLIENT_ID")
singleFactorAuth = SingleFactorAuth(sfaParams, context)
singleFactorAuth = SingleFactorAuth(sfaParams, context, 86400, context.packageName)
val privateKey = readPrivateKeyFromReader(
WellKnownSecret.pem(),
"EC"
Expand All @@ -83,7 +83,7 @@ class SingleFactorAuthTest {
)
)
)
val sfakey = singleFactorAuth.connect(loginParams, 86400, context)
val sfakey = singleFactorAuth.connect(loginParams, context)
val requiredPrivateKey =
BigInteger("68390578bbdab74e9883de58d3919c176662852bdd42a783bc3a08f1a1024e0c", 16)
if (sfakey != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ import org.torusresearch.torusutils.types.common.TorusOptions
import org.torusresearch.torusutils.types.common.TorusPublicKey
import org.web3j.crypto.Hash

class SingleFactorAuth(sfaParams: SFAParams, ctx: Context) {
class SingleFactorAuth(
sfaParams: SFAParams,
ctx: Context,
sessionTime: Long,
allowedOrigin: String
) {
private var nodeDetailManager: FetchNodeDetails =
FetchNodeDetails(sfaParams.getNetwork())
private val torusUtils: TorusUtils
Expand All @@ -30,15 +35,13 @@ class SingleFactorAuth(sfaParams: SFAParams, ctx: Context) {
private var network: Web3AuthNetwork

init {
val torusOptions = sfaParams.getClientId()?.let {
TorusOptions(
it, sfaParams.getNetwork(),
sfaParams.getNetworkUrl(), sfaParams.getServerTimeOffset(), true
)
}
val torusOptions = TorusOptions(
sfaParams.getClientId(), sfaParams.getNetwork(),
sfaParams.getNetworkUrl(), sfaParams.getServerTimeOffset(), true
)
network = sfaParams.getNetwork()
torusUtils = TorusUtils(torusOptions)
sessionManager = SessionManager(ctx)
sessionManager = SessionManager(ctx, sessionTime, allowedOrigin)
}

fun initialize(ctx: Context): SFAKey {
Expand Down Expand Up @@ -97,7 +100,6 @@ class SingleFactorAuth(sfaParams: SFAParams, ctx: Context) {

fun connect(
loginParams: LoginParams,
sessionTime: Long,
ctx: Context
): SFAKey? {
val torusKey = getTorusKey(loginParams)
Expand All @@ -119,7 +121,7 @@ class SingleFactorAuth(sfaParams: SFAParams, ctx: Context) {
json.put("publicAddress", torusSFAKey.getPublicAddress())
}

sessionManager.createSession(json.toString(), sessionTime, ctx, ctx.packageName).get()
sessionManager.createSession(json.toString(), ctx).get()
return torusSFAKey
}
}

0 comments on commit 9a5f4ba

Please sign in to comment.