-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathMainActivity.kt
58 lines (50 loc) · 2.11 KB
/
MainActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.example.xmtpv3_example
import android.os.Bundle
import android.util.Log
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.example.xmtpv3_example.R.id.selftest_output
import kotlinx.coroutines.runBlocking
import org.web3j.crypto.Credentials
import org.web3j.crypto.ECKeyPair
import org.web3j.crypto.Sign
import uniffi.xmtpv3.FfiInboxOwner
import uniffi.xmtpv3.FfiLogger
import java.nio.charset.StandardCharsets
import java.security.SecureRandom
const val EMULATOR_LOCALHOST_ADDRESS = "http://10.0.2.2:5556"
class Web3jInboxOwner(private val credentials: Credentials) : FfiInboxOwner {
override fun getAddress(): String {
return credentials.address
}
override fun sign(text: String): ByteArray {
val messageBytes: ByteArray = text.toByteArray(StandardCharsets.UTF_8)
val signature = Sign.signPrefixedMessage(messageBytes, credentials.ecKeyPair)
return signature.r + signature.s + signature.v
}
}
class AndroidFfiLogger : FfiLogger {
override fun log(level: UInt, levelLabel: String, message: String) {
Log.i("Rust", levelLabel + " - " + message)
}
}
// An example Android app testing the end-to-end flow through Rust
// Run setup_android_example.sh to set it up
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val textView: TextView = findViewById<TextView>(selftest_output)
val privateKey: ByteArray = SecureRandom().generateSeed(32)
val credentials: Credentials = Credentials.create(ECKeyPair.create(privateKey))
val inboxOwner = Web3jInboxOwner(credentials)
runBlocking {
try {
val client = uniffi.xmtpv3.createClient(AndroidFfiLogger(), inboxOwner, EMULATOR_LOCALHOST_ADDRESS, false);
textView.text = "Client constructed, wallet address: " + client.walletAddress();
} catch (e: Exception) {
textView.text = "Failed to construct client: " + e.message;
}
}
}
}