Skip to content

Commit

Permalink
refactor: add clientid to SingleFactorAuthArgs and update unit tests.
Browse files Browse the repository at this point in the history
Signed-off-by: Gaurav Goel <gaurav@tor.us>
  • Loading branch information
Gaurav Goel committed Jul 8, 2024
1 parent c0a15ef commit ad9dc05
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 23 deletions.
3 changes: 1 addition & 2 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ plugins {

android {
namespace 'com.web3auth.sfaexample'
compileSdk 33
compileSdk 34

defaultConfig {
applicationId "com.web3auth.sfaexample"
minSdk 24
targetSdk 33
targetSdk 34
versionCode 1
versionName "1.0"

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/web3auth/sfaexample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class MainActivity : AppCompatActivity() {
getTorusKey()
}

singleFactorAuthArgs = SingleFactorAuthArgs(TorusNetwork.TESTNET)
singleFactorAuthArgs = SingleFactorAuthArgs(TorusNetwork.TESTNET, "YOUR_CLIENT_ID")
singleFactorAuth = SingleFactorAuth(singleFactorAuthArgs)
val sessionResponse: CompletableFuture<TorusKey> = singleFactorAuth.initialize(this.applicationContext)
sessionResponse.whenComplete { torusKey, error ->
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '7.3.1' apply false
id 'com.android.library' version '7.3.1' apply false
id 'com.android.application' version '7.4.2' apply false
id 'com.android.library' version '7.4.2' apply false
id 'org.jetbrains.kotlin.android' version '1.7.20' apply false
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Fri May 05 11:39:31 IST 2023
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
6 changes: 3 additions & 3 deletions singlefactorauth/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ plugins {

android {
namespace 'com.web3auth.singlefactorauth'
compileSdk 33
compileSdk 34

defaultConfig {
minSdk 24
targetSdk 33
targetSdk 34

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
Expand All @@ -37,7 +37,7 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.6.1'

implementation 'org.web3j:crypto:4.9.5'
implementation 'org.torusresearch:torus-utils-java:3.1.2'
implementation 'com.github.torusresearch:torus-utils-java:3.2.1'
implementation 'org.torusresearch:fetch-node-details-java:3.1.0'
implementation 'com.google.code.gson:gson:2.9.1'
implementation 'org.slf4j:slf4j-simple:2.0.3'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
package com.web3auth.singlefactorauth

import android.content.Context
import android.os.Build
import androidx.annotation.RequiresApi
import com.google.gson.GsonBuilder
import com.web3auth.session_manager_android.SessionManager
import com.web3auth.singlefactorauth.types.*
import com.web3auth.singlefactorauth.types.AggregateVerifierParams
import com.web3auth.singlefactorauth.types.ErrorCode
import com.web3auth.singlefactorauth.types.LoginParams
import com.web3auth.singlefactorauth.types.SFAError
import com.web3auth.singlefactorauth.types.SingleFactorAuthArgs
import com.web3auth.singlefactorauth.types.TorusKey
import com.web3auth.singlefactorauth.types.TorusSubVerifierInfo
import org.json.JSONObject
import org.torusresearch.fetchnodedetails.FetchNodeDetails
import org.torusresearch.fetchnodedetails.types.NodeDetails
import org.torusresearch.torusutils.TorusUtils
import org.torusresearch.torusutils.helpers.Utils
import org.torusresearch.torusutils.types.*
import org.torusresearch.torusutils.types.RetrieveSharesResponse
import org.torusresearch.torusutils.types.TorusCtorOptions
import org.torusresearch.torusutils.types.TorusPublicKey
import org.torusresearch.torusutils.types.TypeOfUser
import org.torusresearch.torusutils.types.VerifierArgs
import org.web3j.crypto.Hash
import java.util.concurrent.CompletableFuture
import java.util.concurrent.ExecutionException
Expand All @@ -32,7 +44,7 @@ class SingleFactorAuth(singleFactorAuthArgs: SingleFactorAuthArgs) {
SingleFactorAuthArgs.CONTRACT_MAP[singleFactorAuthArgs.getNetwork()]
)
}
val opts = TorusCtorOptions("single-factor-auth-android")
val opts = TorusCtorOptions("single-factor-auth-android", singleFactorAuthArgs.clientid)
opts.isEnableOneKey = true
opts.network = singleFactorAuthArgs.getNetwork().toString()
opts.signerHost =
Expand All @@ -42,6 +54,7 @@ class SingleFactorAuth(singleFactorAuthArgs: SingleFactorAuthArgs) {
torusUtils = TorusUtils(opts)
}

@RequiresApi(Build.VERSION_CODES.O)
@Throws(ExecutionException::class, InterruptedException::class)
fun getKey(
loginParams: LoginParams,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package com.web3auth.singlefactorauth.types

import org.torusresearch.fetchnodedetails.FetchNodeDetails
import org.torusresearch.fetchnodedetails.types.TorusNetwork
import java.util.HashMap

class SingleFactorAuthArgs(network: TorusNetwork) {
class SingleFactorAuthArgs(network: TorusNetwork, clientid: String) {
private var network: TorusNetwork
var clientid: String
var networkUrl: String? = null

init {
this.network = network
this.clientid = clientid
}

fun getNetwork(): TorusNetwork {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class AquaTest {
@Test
@Throws(ExecutionException::class, InterruptedException::class)
fun shouldGetTorusKey() {
singleFactorAuthArgs = SingleFactorAuthArgs(TorusNetwork.AQUA)
singleFactorAuthArgs = SingleFactorAuthArgs(TorusNetwork.AQUA, "YOUR_CLIENT_ID")
singleFactorAuth = SingleFactorAuth(singleFactorAuthArgs)
val privateKey = PemUtils.readPrivateKeyFromFile(
"src/test/java/com/web3Auth/singlefactorauth/keys/key.pem",
Expand All @@ -58,7 +58,7 @@ class AquaTest {
@Test
@Throws(ExecutionException::class, InterruptedException::class)
fun shouldAggregrateGetTorusKey() {
singleFactorAuthArgs = SingleFactorAuthArgs(TorusNetwork.AQUA)
singleFactorAuthArgs = SingleFactorAuthArgs(TorusNetwork.AQUA, "YOUR_CLIENT_ID")
singleFactorAuth = SingleFactorAuth(singleFactorAuthArgs)
val privateKey = PemUtils.readPrivateKeyFromFile(
"src/test/java/com/web3Auth/singlefactorauth/keys/key.pem",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class CyanTest {
@Test
@Throws(ExecutionException::class, InterruptedException::class)
fun shouldGetTorusKey() {
singleFactorAuthArgs = SingleFactorAuthArgs(TorusNetwork.CYAN)
singleFactorAuthArgs = SingleFactorAuthArgs(TorusNetwork.CYAN, "YOUR_CLIENT_ID")
singleFactorAuth = SingleFactorAuth(singleFactorAuthArgs)
val privateKey = PemUtils.readPrivateKeyFromFile(
"src/test/java/com/web3Auth/singlefactorauth/keys/key.pem",
Expand All @@ -48,17 +48,17 @@ class CyanTest {
val idToken: String = JwtUtils.generateIdToken(TORUS_TEST_EMAIL, algorithmRs)
loginParams = LoginParams(TEST_VERIFIER, TORUS_TEST_EMAIL, idToken)
val torusKey: TorusKey = singleFactorAuth.getKey(loginParams).get()
assertEquals("0x6b902fBCEb0E0374e5eB9eDFe68cD4B888c32150", torusKey.publicAddress)
val requiredPrivateKey =
BigInteger("44ca9a8ac5167ff11e0b48731f7bfde141fbbb0711d0abb54d5da554fb6fd40a", 16)
assert(requiredPrivateKey == torusKey.privateKey)
assertEquals("0x1bbc291d4a8DCcb55fd969568D56b72a4BF62be8", torusKey.publicAddress)
}

@DisplayName("Test Aggregate getTorusKey")
@Test
@Throws(ExecutionException::class, InterruptedException::class)
fun shouldAggregrateGetTorusKey() {
singleFactorAuthArgs = SingleFactorAuthArgs(TorusNetwork.CYAN)
singleFactorAuthArgs = SingleFactorAuthArgs(TorusNetwork.CYAN, "YOUR_CLIENT_ID")
singleFactorAuth = SingleFactorAuth(singleFactorAuthArgs)
val privateKey = PemUtils.readPrivateKeyFromFile(
"src/test/java/com/web3Auth/singlefactorauth/keys/key.pem",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class SingleFactorAuthTest {
@Test
@Throws(ExecutionException::class, InterruptedException::class)
fun shouldGetTorusKey() {
singleFactorAuthArgs = SingleFactorAuthArgs(TorusNetwork.TESTNET)
singleFactorAuthArgs = SingleFactorAuthArgs(TorusNetwork.TESTNET, "YOUR_CLIENT_ID")
singleFactorAuth = SingleFactorAuth(singleFactorAuthArgs)
val privateKey = PemUtils.readPrivateKeyFromFile(
"src/test/java/com/web3Auth/singlefactorauth/keys/key.pem",
Expand All @@ -58,7 +58,7 @@ class SingleFactorAuthTest {
@Test
@Throws(ExecutionException::class, InterruptedException::class)
fun shouldAggregrateGetTorusKey() {
singleFactorAuthArgs = SingleFactorAuthArgs(TorusNetwork.TESTNET)
singleFactorAuthArgs = SingleFactorAuthArgs(TorusNetwork.TESTNET, "YOUR_CLIENT_ID")
singleFactorAuth = SingleFactorAuth(singleFactorAuthArgs)
val privateKey = PemUtils.readPrivateKeyFromFile(
"src/test/java/com/web3Auth/singlefactorauth/keys/key.pem",
Expand Down

0 comments on commit ad9dc05

Please sign in to comment.