Skip to content

Commit

Permalink
feat: Update sfa android deps and code changes related to session man…
Browse files Browse the repository at this point in the history
…ager and sfa

Signed-off-by: Gaurav Goel <[email protected]>
  • Loading branch information
grvgoel81 authored and Gaurav Goel committed Sep 14, 2024
1 parent 9ac510b commit a54169b
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 100 deletions.
5 changes: 3 additions & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,15 @@ android {
}

defaultConfig {
minSdkVersion 24
minSdkVersion 26
}
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4'
implementation 'com.github.Web3Auth:single-factor-auth-android:0.4.0'
//implementation 'com.github.Web3Auth:single-factor-auth-android:0.4.0' //TODO(): Update version
implementation 'com.github.grvgoel81:single-factor-auth-android:4.0.0' // TODO(): remove this
implementation 'org.torusresearch:fetch-node-details-java:5.0.0'
implementation 'com.google.code.gson:gson:2.9.1'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.web3auth.single_factor_auth_flutter

data class SFAOptions(val network: String, var clientid: String)

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import androidx.annotation.NonNull
import com.google.gson.Gson
import com.web3auth.singlefactorauth.SingleFactorAuth
import com.web3auth.singlefactorauth.types.LoginParams
import com.web3auth.singlefactorauth.types.SingleFactorAuthArgs
import com.web3auth.singlefactorauth.types.TorusKey
import com.web3auth.singlefactorauth.types.SFAKey
import com.web3auth.singlefactorauth.types.SFAParams
import com.web3auth.singlefactorauth.types.TorusSubVerifierInfo
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall
Expand All @@ -27,7 +27,7 @@ class SingleFactorAuthFlutterPlugin : FlutterPlugin, MethodCallHandler {
private lateinit var channel: MethodChannel
private lateinit var context: Context
private lateinit var singleFactorAuth: SingleFactorAuth
private lateinit var singleFactorAuthArgs: SingleFactorAuthArgs
private lateinit var sfaParams: SFAParams
private lateinit var loginParams: LoginParams
private var gson: Gson = Gson()

Expand Down Expand Up @@ -73,19 +73,28 @@ class SingleFactorAuthFlutterPlugin : FlutterPlugin, MethodCallHandler {

"init" -> {
val initArgs = call.arguments<String>()
val params = gson.fromJson(initArgs, SFAParams::class.java)
singleFactorAuthArgs = SingleFactorAuthArgs(getNetwork(params.network), params.clientid)
singleFactorAuth = SingleFactorAuth(singleFactorAuthArgs)
val params = gson.fromJson(initArgs, SFAOptions::class.java)
sfaParams = SFAParams(getNetwork(params.network), params.clientid)
singleFactorAuth = SingleFactorAuth(sfaParams, context)
return null
}

"initialize" -> {
try {
val torusKeyCF = singleFactorAuth.initialize(context)
Log.d("${SingleFactorAuthFlutterPlugin::class.qualifiedName}", "#initialize")
return if (torusKeyCF.get() != null) {
val torusKey = torusKeyCF.get()
prepareResult(torusKey)
Log.d(
"${SingleFactorAuthFlutterPlugin::class.qualifiedName}",
"#initializeCalled"
)
Log.d("packageName:", context.packageName)
val sfaKey = singleFactorAuth.initialize(context)
Log.d("${SingleFactorAuthFlutterPlugin::class.qualifiedName}", "#_initialize")
return if (sfaKey != null) {
Log.d(
"${SingleFactorAuthFlutterPlugin::class.qualifiedName}",
sfaKey.toString()
)
Log.d("sfaKey", "reached here.")
prepareResultFromSFAkey(sfaKey)
} else {
""
}
Expand All @@ -94,18 +103,31 @@ class SingleFactorAuthFlutterPlugin : FlutterPlugin, MethodCallHandler {
}
}

"getTorusKey" -> {
"connect" -> {
try {
val initArgs = call.arguments<String>()
val params = gson.fromJson(initArgs, Web3AuthOptions::class.java)
loginParams = LoginParams(
params.verifier, params.verifierId,
params.idToken
)
val torusKeyCF = singleFactorAuth.getKey(loginParams, context)
Log.d("${SingleFactorAuthFlutterPlugin::class.qualifiedName}", "#getTorusKey")
val torusKey = torusKeyCF.get()
return prepareResult(torusKey)
if (params.aggregateVerifier.isNullOrEmpty()) {
loginParams = LoginParams(
params.verifier, params.verifierId,
params.idToken
)
} else {
loginParams = LoginParams(
params.aggregateVerifier, params.verifierId,
params.idToken,
arrayOf(
TorusSubVerifierInfo(
params.verifier,
params.idToken
)
)
)
}
val sfaKeyCF = singleFactorAuth.connect(loginParams, context)
Log.d("${SingleFactorAuthFlutterPlugin::class.qualifiedName}", "#connect")
val sfaKey = sfaKeyCF
return prepareResult(sfaKey)
} catch (e: Throwable) {
throw Error(e)
}
Expand All @@ -125,24 +147,35 @@ class SingleFactorAuthFlutterPlugin : FlutterPlugin, MethodCallHandler {
)
)
)
val torusKeyCF = singleFactorAuth.getKey(loginParams, context)
val sfaKey = singleFactorAuth.connect(loginParams, context)
Log.d(
"${SingleFactorAuthFlutterPlugin::class.qualifiedName}",
"#getAggregateTorusKey"
)
return prepareResult(torusKeyCF.get())
return prepareResult(sfaKey)
} catch (e: Throwable) {
throw Error(e)
}
}

"isSessionIdExists" -> {
return singleFactorAuth.isSessionIdExists()
}
}
throw NotImplementedError()
}

private fun prepareResult(torusKey: TorusKey): String {
private fun prepareResult(sfaKey: SFAKey?): String {
val hashMap: HashMap<String, String> = HashMap<String, String>(2)
hashMap["privateKey"] = sfaKey?.getPrivateKey() as String
hashMap["publicAddress"] = sfaKey?.getPublicAddress() as String
return gson.toJson(hashMap)
}

private fun prepareResultFromSFAkey(sfaKey: SFAKey): String {
val hashMap: HashMap<String, String> = HashMap<String, String>(2)
hashMap["privateKey"] = torusKey.privateKey?.toString(16) as String
hashMap["publicAddress"] = torusKey.publicAddress as String
hashMap["privateKey"] = sfaKey.getPrivateKey() as String ?: ""
hashMap["publicAddress"] = sfaKey.getPublicAddress() as String ?: ""
return gson.toJson(hashMap)
}
}
2 changes: 1 addition & 1 deletion example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ android {
applicationId "com.web3auth.single_factor_auth_flutter_example"
// You can update the following values to match your application needs.
// For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
minSdkVersion 24
minSdkVersion 26
targetSdkVersion flutter.targetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
Expand Down
42 changes: 16 additions & 26 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class _MyAppState extends State<MyApp> {
final _singleFactorAuthFlutterPlugin = SingleFactAuthFlutter();
String _result = '';
bool logoutVisible = false;
Web3AuthNetwork torusNetwork = Web3AuthNetwork.aqua;
Web3AuthNetwork torusNetwork = Web3AuthNetwork.mainnet;

@override
void initState() {
Expand All @@ -35,10 +35,14 @@ class _MyAppState extends State<MyApp> {
Future<void> initSdk() async {
if (Platform.isAndroid) {
await init();
initialize();
if (await _singleFactorAuthFlutterPlugin.isSessionIdExists()) {
initialize();
}
} else if (Platform.isIOS) {
await init();
initialize();
if (await _singleFactorAuthFlutterPlugin.isSessionIdExists()) {
initialize();
}
} else {}
}

Expand All @@ -49,12 +53,11 @@ class _MyAppState extends State<MyApp> {

Future<void> initialize() async {
log("initialize() called");
final TorusKey? torusKey =
await _singleFactorAuthFlutterPlugin.initialize();
if (torusKey != null) {
final SFAKey? sfaKey = await _singleFactorAuthFlutterPlugin.initialize();
if (sfaKey != null) {
setState(() {
_result =
"Public Add : ${torusKey.publicAddress} , Private Key : ${torusKey.privateKey}";
"Public Add : ${sfaKey.publicAddress} , Private Key : ${sfaKey.privateKey}";
});
}
}
Expand Down Expand Up @@ -134,10 +137,10 @@ class _MyAppState extends State<MyApp> {
);
}

VoidCallback _getKey(Future<TorusKey> Function() method) {
VoidCallback _getKey(Future<SFAKey> Function() method) {
return () async {
try {
final TorusKey response = await method();
final SFAKey response = await method();
setState(() {
_result =
"Public Add : ${response.publicAddress} , Private Key : ${response.privateKey}";
Expand All @@ -155,7 +158,7 @@ class _MyAppState extends State<MyApp> {

Future<void> _initialize() async {
try {
final TorusKey? response =
final SFAKey? response =
await _singleFactorAuthFlutterPlugin.initialize();
setState(() {
_result = "Private Key : ${response?.privateKey}";
Expand All @@ -168,24 +171,11 @@ class _MyAppState extends State<MyApp> {
}
}

Future<TorusKey> getKey() {
return _singleFactorAuthFlutterPlugin.getKey(
LoginParams(
Future<SFAKey> getKey() {
return _singleFactorAuthFlutterPlugin.connect(LoginParams(
verifier: 'torus-test-health',
verifierId: '[email protected]',
idToken: Utils().es256Token("[email protected]"),
),
);
}

Future<TorusKey> getAggregrateKey() {
return _singleFactorAuthFlutterPlugin.getAggregateKey(
LoginParams(
verifier: 'torus-test-health',
verifierId: '[email protected]',
idToken: Utils().es256Token("[email protected]"),
aggregateVerifier: 'torus-test-health-aggregate',
),
);
));
}
}
42 changes: 21 additions & 21 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,26 @@ packages:
dependency: transitive
description:
name: crypto
sha256: ff625774173754681d66daaf4a448684fb04b78f902da9cb3d308c19cc5e8bab
sha256: ec30d999af904f33454ba22ed9a86162b35e52b44ac4807d1d93c288041d7d27
url: "https://pub.dev"
source: hosted
version: "3.0.3"
version: "3.0.5"
cupertino_icons:
dependency: "direct main"
description:
name: cupertino_icons
sha256: d57953e10f9f8327ce64a508a355f0b1ec902193f66288e8cb5070e7c47eeb2d
sha256: ba631d1c7f7bef6b729a622b7b752645a2d076dba9976925b8f25725a30e1ee6
url: "https://pub.dev"
source: hosted
version: "1.0.6"
version: "1.0.8"
dart_jsonwebtoken:
dependency: "direct main"
description:
name: dart_jsonwebtoken
sha256: "40dc3a4788c02a44bc97ea0c8c4a078ae58c9a45acc2312ee6a689b0e8f5b5b9"
sha256: "346e9a21e4bf6e6a431e19ece00ebb2e3668e1e339cabdf6f46d18d88692a848"
url: "https://pub.dev"
source: hosted
version: "2.13.0"
version: "2.14.0"
ed25519_edwards:
dependency: transitive
description:
Expand Down Expand Up @@ -135,18 +135,18 @@ packages:
dependency: transitive
description:
name: leak_tracker
sha256: "7f0df31977cb2c0b88585095d168e689669a2cc9b97c309665e3386f3e9d341a"
sha256: "3f87a60e8c63aecc975dda1ceedbc8f24de75f09e4856ea27daf8958f2f0ce05"
url: "https://pub.dev"
source: hosted
version: "10.0.4"
version: "10.0.5"
leak_tracker_flutter_testing:
dependency: transitive
description:
name: leak_tracker_flutter_testing
sha256: "06e98f569d004c1315b991ded39924b21af84cf14cc94791b8aea337d25b57f8"
sha256: "932549fb305594d82d7183ecd9fa93463e9914e1b67cacc34bc40906594a1806"
url: "https://pub.dev"
source: hosted
version: "3.0.3"
version: "3.0.5"
leak_tracker_testing:
dependency: transitive
description:
Expand Down Expand Up @@ -175,18 +175,18 @@ packages:
dependency: transitive
description:
name: material_color_utilities
sha256: "0e0a020085b65b6083975e499759762399b4475f766c21668c4ecca34ea74e5a"
sha256: f7142bb1154231d7ea5f96bc7bde4bda2a0945d2806bb11670e30b850d56bdec
url: "https://pub.dev"
source: hosted
version: "0.8.0"
version: "0.11.1"
meta:
dependency: transitive
description:
name: meta
sha256: "7687075e408b093f36e6bbf6c91878cc0d4cd10f409506f7bc996f68220b9136"
sha256: bdb68674043280c3428e9ec998512fb681678676b3c54e773629ffe74419f8c7
url: "https://pub.dev"
source: hosted
version: "1.12.0"
version: "1.15.0"
path:
dependency: transitive
description:
Expand All @@ -207,10 +207,10 @@ packages:
dependency: transitive
description:
name: pointycastle
sha256: "43ac87de6e10afabc85c445745a7b799e04de84cebaa4fd7bf55a5e1e9604d29"
sha256: "4be0097fcf3fd3e8449e53730c631200ebc7b88016acecab2b0da2f0149222fe"
url: "https://pub.dev"
source: hosted
version: "3.7.4"
version: "3.9.1"
single_factor_auth_flutter:
dependency: "direct main"
description:
Expand Down Expand Up @@ -267,10 +267,10 @@ packages:
dependency: transitive
description:
name: test_api
sha256: "9955ae474176f7ac8ee4e989dadfb411a58c30415bcfb648fa04b2b8a03afa7f"
sha256: "5b8a98dafc4d5c4c9c72d8b31ab2b23fc13422348d2997120294d3bac86b4ddb"
url: "https://pub.dev"
source: hosted
version: "0.7.0"
version: "0.7.2"
typed_data:
dependency: transitive
description:
Expand All @@ -291,10 +291,10 @@ packages:
dependency: transitive
description:
name: vm_service
sha256: "3923c89304b715fb1eb6423f017651664a03bf5f4b29983627c4da791f74a4ec"
sha256: "5c5f338a667b4c644744b661f309fb8080bb94b18a7e91ef1dbd343bed00ed6d"
url: "https://pub.dev"
source: hosted
version: "14.2.1"
version: "14.2.5"
sdks:
dart: ">=3.3.0 <4.0.0"
dart: ">=3.4.0 <4.0.0"
flutter: ">=3.18.0-18.0.pre.54"
Loading

0 comments on commit a54169b

Please sign in to comment.