Skip to content

Commit

Permalink
Merge pull request #80 from Web3Auth/feat/transaction_signing
Browse files Browse the repository at this point in the history
Feat/transaction signing
  • Loading branch information
chaitanyapotti authored Mar 15, 2024
2 parents 57434a5 + ae8178e commit 9abb895
Show file tree
Hide file tree
Showing 9 changed files with 279 additions and 13 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ dependencies {

// Encoding
implementation 'com.google.code.gson:gson:2.9.0'
implementation 'org.web3j:core:4.8.8-android'

// Web3Auth
implementation project(":core")
Expand Down
51 changes: 51 additions & 0 deletions app/src/main/java/com/web3auth/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ import android.text.method.ScrollingMovementMethod
import android.util.Log
import android.view.View
import android.widget.*
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.textfield.TextInputLayout
import com.google.gson.Gson
import com.google.gson.JsonArray
import com.web3auth.core.Web3Auth
import com.web3auth.core.isEmailValid
import com.web3auth.core.types.*
import org.json.JSONObject
import org.web3j.crypto.Credentials
import java.util.concurrent.CompletableFuture
import java.util.concurrent.atomic.AtomicBoolean

Expand Down Expand Up @@ -86,6 +89,7 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
val signInButton = findViewById<Button>(R.id.signInButton)
val signOutButton = findViewById<Button>(R.id.signOutButton)
val launchWalletButton = findViewById<Button>(R.id.launchWalletButton)
val signMsgButton = findViewById<Button>(R.id.signMsgButton)
val btnSetUpMfa = findViewById<Button>(R.id.btnSetUpMfa)
val spinner = findViewById<TextInputLayout>(R.id.verifierList)
val hintEmailEditText = findViewById<EditText>(R.id.etEmailHint)
Expand All @@ -106,6 +110,7 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
signInButton.visibility = View.GONE
signOutButton.visibility = View.VISIBLE
launchWalletButton.visibility = View.VISIBLE
signMsgButton.visibility = View.VISIBLE
btnSetUpMfa.visibility = View.VISIBLE
spinner.visibility = View.GONE
hintEmailEditText.visibility = View.GONE
Expand All @@ -116,6 +121,7 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
signOutButton.visibility = View.GONE
btnSetUpMfa.visibility = View.GONE
launchWalletButton.visibility = View.GONE
signMsgButton.visibility = View.GONE
spinner.visibility = View.VISIBLE
}
}
Expand Down Expand Up @@ -202,6 +208,38 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
}
}

val signResultButton = findViewById<Button>(R.id.signResultButton)
val signMsgButton = findViewById<Button>(R.id.signMsgButton)
signMsgButton.setOnClickListener {
val credentials: Credentials = Credentials.create(web3Auth.getPrivkey())
val params = JsonArray().apply {
add("Hello, World!")
add(credentials.address)
add("Android")
}
val signMsgCompletableFuture = web3Auth.request(
loginParams = LoginParams(
selectedLoginProvider,
extraLoginOptions = null,
mfaLevel = MFALevel.NONE,
), "personal_sign", requestParams = params
)
signMsgCompletableFuture.whenComplete { _, error ->
if (error == null) {
Log.d("MainActivity_Web3Auth", "Message signed successfully")
signResultButton.visibility = View.VISIBLE
} else {
Log.d("MainActivity_Web3Auth", error.message ?: "Something went wrong")
signResultButton.visibility = View.GONE
}
}
}

signResultButton.setOnClickListener {
val signResult = Web3Auth.getSignResponse()
showAlertDialog("Sign Result", signResult.toString())
}

val btnSetUpMfa = findViewById<Button>(R.id.btnSetUpMfa)
btnSetUpMfa.setOnClickListener {
val setupMfaCf = web3Auth.enableMFA()
Expand Down Expand Up @@ -237,6 +275,9 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
count = 0
} else {
if (count > 0) {
if (Web3Auth.getSignResponse() != null) {
return
}
Toast.makeText(this, "User closed the browser.", Toast.LENGTH_SHORT).show()
web3Auth.setResultUrl(null)
}
Expand All @@ -254,4 +295,14 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
hintEmailEditText.visibility = View.GONE
}
}

private fun showAlertDialog(title: String, message: String) {
val builder = AlertDialog.Builder(this@MainActivity)
builder.setTitle(title)
.setMessage(message)
.setPositiveButton("OK") { dialog, _ ->
dialog.dismiss()
}
.show()
}
}
24 changes: 24 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,30 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/signInButton" />

<Button
android:id="@+id/signMsgButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:text="@string/sign_msg"
android:textAllCaps="false"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/launchWalletButton" />

<Button
android:id="@+id/signResultButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:text="@string/sign_result"
android:textAllCaps="false"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/signMsgButton" />

<Button
android:id="@+id/btnSetUpMfa"
android:layout_width="wrap_content"
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@
<string name="sign_out">Sign out</string>
<string name="claim">Claim Flow</string>
<string name="launch_wallet_services">Launch Wallet Services</string>
<string name="sign_msg">Sign Message</string>
<string name="send_tx">Send Transaction</string>
<string name="sign_result">Get Sign Result</string>
<string name="mfa">SetUp MFA</string>
</resources>
4 changes: 3 additions & 1 deletion core/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
android:allowBackup="true"
android:supportsRtl="true">

<activity android:name="com.web3auth.core.WebViewActivity" />
<activity
android:name="com.web3auth.core.WebViewActivity"
android:theme="@style/Theme.AppCompat" />

</application>

Expand Down
Loading

0 comments on commit 9abb895

Please sign in to comment.