Skip to content

Commit 9abb895

Browse files
Merge pull request #80 from Web3Auth/feat/transaction_signing
Feat/transaction signing
2 parents 57434a5 + ae8178e commit 9abb895

File tree

9 files changed

+279
-13
lines changed

9 files changed

+279
-13
lines changed

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ dependencies {
5858

5959
// Encoding
6060
implementation 'com.google.code.gson:gson:2.9.0'
61+
implementation 'org.web3j:core:4.8.8-android'
6162

6263
// Web3Auth
6364
implementation project(":core")

app/src/main/java/com/web3auth/app/MainActivity.kt

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@ import android.text.method.ScrollingMovementMethod
77
import android.util.Log
88
import android.view.View
99
import android.widget.*
10+
import androidx.appcompat.app.AlertDialog
1011
import androidx.appcompat.app.AppCompatActivity
1112
import com.google.android.material.textfield.TextInputLayout
1213
import com.google.gson.Gson
14+
import com.google.gson.JsonArray
1315
import com.web3auth.core.Web3Auth
1416
import com.web3auth.core.isEmailValid
1517
import com.web3auth.core.types.*
1618
import org.json.JSONObject
19+
import org.web3j.crypto.Credentials
1720
import java.util.concurrent.CompletableFuture
1821
import java.util.concurrent.atomic.AtomicBoolean
1922

@@ -86,6 +89,7 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
8689
val signInButton = findViewById<Button>(R.id.signInButton)
8790
val signOutButton = findViewById<Button>(R.id.signOutButton)
8891
val launchWalletButton = findViewById<Button>(R.id.launchWalletButton)
92+
val signMsgButton = findViewById<Button>(R.id.signMsgButton)
8993
val btnSetUpMfa = findViewById<Button>(R.id.btnSetUpMfa)
9094
val spinner = findViewById<TextInputLayout>(R.id.verifierList)
9195
val hintEmailEditText = findViewById<EditText>(R.id.etEmailHint)
@@ -106,6 +110,7 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
106110
signInButton.visibility = View.GONE
107111
signOutButton.visibility = View.VISIBLE
108112
launchWalletButton.visibility = View.VISIBLE
113+
signMsgButton.visibility = View.VISIBLE
109114
btnSetUpMfa.visibility = View.VISIBLE
110115
spinner.visibility = View.GONE
111116
hintEmailEditText.visibility = View.GONE
@@ -116,6 +121,7 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
116121
signOutButton.visibility = View.GONE
117122
btnSetUpMfa.visibility = View.GONE
118123
launchWalletButton.visibility = View.GONE
124+
signMsgButton.visibility = View.GONE
119125
spinner.visibility = View.VISIBLE
120126
}
121127
}
@@ -202,6 +208,38 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
202208
}
203209
}
204210

211+
val signResultButton = findViewById<Button>(R.id.signResultButton)
212+
val signMsgButton = findViewById<Button>(R.id.signMsgButton)
213+
signMsgButton.setOnClickListener {
214+
val credentials: Credentials = Credentials.create(web3Auth.getPrivkey())
215+
val params = JsonArray().apply {
216+
add("Hello, World!")
217+
add(credentials.address)
218+
add("Android")
219+
}
220+
val signMsgCompletableFuture = web3Auth.request(
221+
loginParams = LoginParams(
222+
selectedLoginProvider,
223+
extraLoginOptions = null,
224+
mfaLevel = MFALevel.NONE,
225+
), "personal_sign", requestParams = params
226+
)
227+
signMsgCompletableFuture.whenComplete { _, error ->
228+
if (error == null) {
229+
Log.d("MainActivity_Web3Auth", "Message signed successfully")
230+
signResultButton.visibility = View.VISIBLE
231+
} else {
232+
Log.d("MainActivity_Web3Auth", error.message ?: "Something went wrong")
233+
signResultButton.visibility = View.GONE
234+
}
235+
}
236+
}
237+
238+
signResultButton.setOnClickListener {
239+
val signResult = Web3Auth.getSignResponse()
240+
showAlertDialog("Sign Result", signResult.toString())
241+
}
242+
205243
val btnSetUpMfa = findViewById<Button>(R.id.btnSetUpMfa)
206244
btnSetUpMfa.setOnClickListener {
207245
val setupMfaCf = web3Auth.enableMFA()
@@ -237,6 +275,9 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
237275
count = 0
238276
} else {
239277
if (count > 0) {
278+
if (Web3Auth.getSignResponse() != null) {
279+
return
280+
}
240281
Toast.makeText(this, "User closed the browser.", Toast.LENGTH_SHORT).show()
241282
web3Auth.setResultUrl(null)
242283
}
@@ -254,4 +295,14 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
254295
hintEmailEditText.visibility = View.GONE
255296
}
256297
}
298+
299+
private fun showAlertDialog(title: String, message: String) {
300+
val builder = AlertDialog.Builder(this@MainActivity)
301+
builder.setTitle(title)
302+
.setMessage(message)
303+
.setPositiveButton("OK") { dialog, _ ->
304+
dialog.dismiss()
305+
}
306+
.show()
307+
}
257308
}

app/src/main/res/layout/activity_main.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,30 @@
9191
app:layout_constraintStart_toStartOf="parent"
9292
app:layout_constraintTop_toBottomOf="@+id/signInButton" />
9393

94+
<Button
95+
android:id="@+id/signMsgButton"
96+
android:layout_width="wrap_content"
97+
android:layout_height="wrap_content"
98+
android:layout_marginEnd="10dp"
99+
android:text="@string/sign_msg"
100+
android:textAllCaps="false"
101+
android:visibility="gone"
102+
app:layout_constraintEnd_toEndOf="parent"
103+
app:layout_constraintStart_toStartOf="parent"
104+
app:layout_constraintTop_toBottomOf="@+id/launchWalletButton" />
105+
106+
<Button
107+
android:id="@+id/signResultButton"
108+
android:layout_width="wrap_content"
109+
android:layout_height="wrap_content"
110+
android:layout_marginEnd="10dp"
111+
android:text="@string/sign_result"
112+
android:textAllCaps="false"
113+
android:visibility="gone"
114+
app:layout_constraintEnd_toEndOf="parent"
115+
app:layout_constraintStart_toStartOf="parent"
116+
app:layout_constraintTop_toBottomOf="@+id/signMsgButton" />
117+
94118
<Button
95119
android:id="@+id/btnSetUpMfa"
96120
android:layout_width="wrap_content"

app/src/main/res/values/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,8 @@
66
<string name="sign_out">Sign out</string>
77
<string name="claim">Claim Flow</string>
88
<string name="launch_wallet_services">Launch Wallet Services</string>
9+
<string name="sign_msg">Sign Message</string>
10+
<string name="send_tx">Send Transaction</string>
11+
<string name="sign_result">Get Sign Result</string>
912
<string name="mfa">SetUp MFA</string>
1013
</resources>

core/src/main/AndroidManifest.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
android:allowBackup="true"
1616
android:supportsRtl="true">
1717

18-
<activity android:name="com.web3auth.core.WebViewActivity" />
18+
<activity
19+
android:name="com.web3auth.core.WebViewActivity"
20+
android:theme="@style/Theme.AppCompat" />
1921

2022
</application>
2123

0 commit comments

Comments
 (0)