Skip to content

Commit

Permalink
Merge pull request #7 from Web3Auth/sapphire-network
Browse files Browse the repository at this point in the history
Sapphire network changes
  • Loading branch information
chaitanyapotti authored Sep 18, 2024
2 parents 71300de + e52809b commit a6ba193
Show file tree
Hide file tree
Showing 29 changed files with 699 additions and 479 deletions.
7 changes: 1 addition & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
local.properties
.idea/
1 change: 1 addition & 0 deletions .idea/gradle.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 @@ -9,7 +9,7 @@ android {

defaultConfig {
applicationId "com.web3auth.sfaexample"
minSdk 24
minSdk 26
targetSdk 34
versionCode 1
versionName "1.0"
Expand Down Expand Up @@ -42,7 +42,7 @@ dependencies {
// SFA
implementation project(":singlefactorauth")
implementation 'com.auth0:java-jwt:3.19.2'
implementation 'org.web3j:crypto:4.9.5'
implementation 'org.web3j:crypto:4.8.8-android'
implementation 'org.torusresearch:fetch-node-details-java:5.0.0'
api 'io.jsonwebtoken:jjwt-api:0.11.5'
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.11.5'
Expand Down
46 changes: 21 additions & 25 deletions app/src/main/java/com/web3auth/sfaexample/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,32 +1,24 @@
package com.web3auth.sfaexample

import android.os.Build
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import com.auth0.jwt.algorithms.Algorithm
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.SFAParams
import org.torusresearch.fetchnodedetails.types.Web3AuthNetwork
import java.util.concurrent.CompletableFuture

class MainActivity : AppCompatActivity() {

private lateinit var btnTorusKey: Button
private lateinit var tv: TextView
lateinit var singleFactorAuth: SingleFactorAuth
private lateinit var singleFactorAuthArgs: SingleFactorAuthArgs
private lateinit var sfaParams: SFAParams
lateinit var loginParams: LoginParams
lateinit var algorithmRs: Algorithm
var TEST_VERIFIER = "torus-test-health"
var TORUS_TEST_EMAIL = "hello@tor.us"

@RequiresApi(Build.VERSION_CODES.O)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Expand All @@ -35,27 +27,31 @@ class MainActivity : AppCompatActivity() {
tv = findViewById(R.id.tv)

btnTorusKey.setOnClickListener {
getTorusKey()
getSFAKey()
}
val idToken = JwtUtils.generateIdToken(TORUS_TEST_EMAIL)
sfaParams =
SFAParams(Web3AuthNetwork.SAPPHIRE_MAINNET, "YOUR_CLIENT_ID", 86400, null, 0)
singleFactorAuth = SingleFactorAuth(sfaParams, this)
loginParams = LoginParams(TEST_VERIFIER, TORUS_TEST_EMAIL, idToken)

singleFactorAuthArgs = SingleFactorAuthArgs(Web3AuthNetwork.AQUA, "YOUR_CLIENT_ID")
singleFactorAuth = SingleFactorAuth(singleFactorAuthArgs)
val sessionResponse: CompletableFuture<TorusKey> = singleFactorAuth.initialize(this.applicationContext)
sessionResponse.whenComplete { torusKey, error ->
if (error == null) {
tv.text = "Private Key: ${torusKey.privateKey}"
} else {
Log.d("MainActivity_SFA", error.message ?: "Something went wrong")
}
if (singleFactorAuth.isSessionIdExists()) {
val sfakey = singleFactorAuth.initialize(this.applicationContext)
val text =
"Public Address: ${sfakey.getPublicAddress()} , Private Key: ${sfakey.getPrivateKey()}"
tv.text = text
}
}

@RequiresApi(Build.VERSION_CODES.O)
private fun getTorusKey() {
private fun getSFAKey() {
val idToken = JwtUtils.generateIdToken(TORUS_TEST_EMAIL)
loginParams = LoginParams(TEST_VERIFIER, TORUS_TEST_EMAIL, idToken)
val torusKey: TorusKey =
singleFactorAuth.getKey(loginParams, this.applicationContext, 86400).get()
tv.text = "Private Key: ${torusKey.privateKey}"
val sfakey =
singleFactorAuth.connect(loginParams, this.applicationContext)
if (sfakey != null) {
val text =
"Public Address: ${sfakey.getPublicAddress()} , Private Key: ${sfakey.getPrivateKey()}"
tv.text = text
}
}
}
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
2 changes: 2 additions & 0 deletions jitpack.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
jdk:
- openjdk11
20 changes: 13 additions & 7 deletions singlefactorauth/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ android {
compileSdk 34

defaultConfig {
minSdk 24
minSdk 26
targetSdk 34

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand All @@ -22,10 +22,12 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

kotlinOptions {
jvmTarget = '1.8'
}
Expand All @@ -36,20 +38,24 @@ dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.6.1'

implementation 'org.web3j:crypto:4.8.9-android'
implementation 'org.torusresearch:torus-utils-java:3.4.0'
implementation 'org.web3j:crypto:4.8.8-android'
implementation 'org.torusresearch:torus-utils-java:4.0.2'
implementation 'org.torusresearch:fetch-node-details-java:5.0.0'
implementation 'com.google.code.gson:gson:2.9.1'
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:1.3.0'
implementation 'com.github.Web3Auth:session-manager-android:2.4.0'

testImplementation 'junit:junit:4.13.2'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
testImplementation 'com.auth0:java-jwt:3.19.2'
testImplementation 'net.andreinc:mockneat:0.4.8'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'

androidTestImplementation 'androidx.test.ext:junit:1.2.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'
androidTestImplementation'junit:junit:4.13.2'
androidTestImplementation 'com.auth0:java-jwt:3.19.2'
androidTestImplementation 'net.andreinc:mockneat:0.4.8'
}

project.afterEvaluate {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package com.web3auth.singlefactorauth

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.SingleFactorAuthArgs
import com.web3auth.singlefactorauth.types.TorusKey
import com.web3auth.singlefactorauth.types.SFAParams
import com.web3auth.singlefactorauth.types.TorusSubVerifierInfo
import com.web3auth.singlefactorauth.utils.JwtUtils
import com.web3auth.singlefactorauth.utils.PemUtils
import junit.framework.Assert.assertEquals
import com.web3auth.singlefactorauth.utils.JwtUtils.generateIdToken
import com.web3auth.singlefactorauth.utils.PemUtils.readPrivateKeyFromReader
import com.web3auth.singlefactorauth.utils.WellKnownSecret
import junit.framework.TestCase.fail
import org.junit.Test
import org.junit.jupiter.api.DisplayName
import org.junit.runner.RunWith
import org.torusresearch.fetchnodedetails.types.Web3AuthNetwork
import java.math.BigInteger
import java.security.KeyFactory
Expand All @@ -18,24 +20,26 @@ import java.security.interfaces.ECPublicKey
import java.security.spec.ECPublicKeySpec
import java.util.concurrent.ExecutionException

@RunWith(AndroidJUnit4::class)
class AquaTest {

lateinit var singleFactorAuth: SingleFactorAuth
private lateinit var singleFactorAuthArgs: SingleFactorAuthArgs
private lateinit var sfaParams: SFAParams
lateinit var loginParams: LoginParams
lateinit var algorithmRs: Algorithm
var TEST_VERIFIER = "torus-test-health"
var TEST_AGGREGRATE_VERIFIER = "torus-test-health-aggregate"
var TORUS_TEST_EMAIL = "hello@tor.us"

@DisplayName("Test getTorusKey")
@Test
@Throws(ExecutionException::class, InterruptedException::class)
fun shouldGetTorusKey() {
singleFactorAuthArgs = SingleFactorAuthArgs(Web3AuthNetwork.AQUA, "YOUR_CLIENT_ID")
singleFactorAuth = SingleFactorAuth(singleFactorAuthArgs)
val privateKey = PemUtils.readPrivateKeyFromFile(
"src/test/java/com/web3Auth/singlefactorauth/keys/key.pem",

val context = getInstrumentation().context
sfaParams = SFAParams(Web3AuthNetwork.AQUA, "YOUR_CLIENT_ID", 86400, null, 0)
singleFactorAuth = SingleFactorAuth(sfaParams, context)
val privateKey = readPrivateKeyFromReader(
WellKnownSecret.pem(),
"EC"
) as ECPrivateKey
val publicKey = KeyFactory.getInstance("EC").generatePublic(
Expand All @@ -45,23 +49,28 @@ class AquaTest {
)
) as ECPublicKey
algorithmRs = Algorithm.ECDSA256(publicKey, privateKey)
val idToken: String = JwtUtils.generateIdToken(TORUS_TEST_EMAIL, algorithmRs)
val idToken: String = generateIdToken(TORUS_TEST_EMAIL, algorithmRs)
loginParams = LoginParams(TEST_VERIFIER, TORUS_TEST_EMAIL, idToken)
val torusKey: TorusKey = singleFactorAuth.getKey(loginParams).get()
val sfaKey = singleFactorAuth.connect(loginParams, context)
singleFactorAuth.initialize(context)
val requiredPrivateKey =
BigInteger("d8204e9f8c270647294c54acd8d49ee208789f981a7503158e122527d38626d8", 16)
assert(requiredPrivateKey == torusKey.privateKey)
assertEquals("0x8b32926cD9224fec3B296aA7250B049029434807", torusKey.publicAddress)
if (sfaKey != null) {
assert(requiredPrivateKey.toString(16) == sfaKey.getPrivateKey())
assert("0x8b32926cD9224fec3B296aA7250B049029434807" == sfaKey.getPublicAddress())
} else {
fail()
}
}

@DisplayName("Test Aggregate getTorusKey")
@Test
@Throws(ExecutionException::class, InterruptedException::class)
fun shouldAggregrateGetTorusKey() {
singleFactorAuthArgs = SingleFactorAuthArgs(Web3AuthNetwork.AQUA, "YOUR_CLIENT_ID")
singleFactorAuth = SingleFactorAuth(singleFactorAuthArgs)
val privateKey = PemUtils.readPrivateKeyFromFile(
"src/test/java/com/web3Auth/singlefactorauth/keys/key.pem",
sfaParams = SFAParams(Web3AuthNetwork.AQUA, "YOUR_CLIENT_ID")
val context = getInstrumentation().context
singleFactorAuth = SingleFactorAuth(sfaParams, context)
val privateKey = readPrivateKeyFromReader(
WellKnownSecret.pem(),
"EC"
) as ECPrivateKey
val publicKey = KeyFactory.getInstance("EC").generatePublic(
Expand All @@ -71,18 +80,23 @@ class AquaTest {
)
) as ECPublicKey
algorithmRs = Algorithm.ECDSA256(publicKey, privateKey)
val idToken: String = JwtUtils.generateIdToken(TORUS_TEST_EMAIL, algorithmRs)
val idToken: String = generateIdToken(TORUS_TEST_EMAIL, algorithmRs)
loginParams = LoginParams(
TEST_AGGREGRATE_VERIFIER, TORUS_TEST_EMAIL, idToken, arrayOf(
TorusSubVerifierInfo(
TEST_VERIFIER, idToken
)
)
)
val torusKey: TorusKey = singleFactorAuth.getKey(loginParams).get()
val sfaKey = singleFactorAuth.connect(loginParams, context)
singleFactorAuth.initialize(context)
val requiredPrivateKey =
BigInteger("6f8b884f19975fb0d138ed21b22a6a7e1b79e37f611d0a29f1442b34efc6bacd", 16)
assert(requiredPrivateKey == torusKey.privateKey)
assertEquals("0x62BaCa60f48C2b2b7e3074f7B7b4795EeF2afD2e", torusKey.publicAddress)
if (sfaKey != null) {
assert(requiredPrivateKey.toString(16) == sfaKey.getPrivateKey())
assert("0x62BaCa60f48C2b2b7e3074f7B7b4795EeF2afD2e" == sfaKey.getPublicAddress())
} else {
fail()
}
}
}
Loading

0 comments on commit a6ba193

Please sign in to comment.