Skip to content

Commit

Permalink
first pass at client creation
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed May 22, 2024
1 parent 9e06a7c commit adca07b
Show file tree
Hide file tree
Showing 6 changed files with 691 additions and 110 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.xmtp.android.library

import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import kotlinx.coroutines.runBlocking
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.xmtp.android.library.messages.PrivateKey
import org.xmtp.android.library.messages.PrivateKeyBuilder
import org.xmtp.android.library.messages.walletAddress

@RunWith(AndroidJUnit4::class)
class IdentityTest {
private lateinit var alixWallet: PrivateKeyBuilder
private lateinit var boWallet: PrivateKeyBuilder
private lateinit var alix: PrivateKey
private lateinit var alixClient: Client
private lateinit var bo: PrivateKey
private lateinit var boClient: Client
private lateinit var caroWallet: PrivateKeyBuilder
private lateinit var caro: PrivateKey
private lateinit var caroClient: Client
private lateinit var fixtures: Fixtures

@Before
fun setUp() {
val context = InstrumentationRegistry.getInstrumentation().targetContext
fixtures =
fixtures(
clientOptions = ClientOptions(
ClientOptions.Api(XMTPEnvironment.LOCAL, false),
enableAlphaMls = true,
appContext = context
)
)
alixWallet = fixtures.aliceAccount
alix = fixtures.alice
boWallet = fixtures.bobAccount
bo = fixtures.bob
caroWallet = fixtures.caroAccount
caro = fixtures.caro

alixClient = fixtures.aliceClient
boClient = fixtures.bobClient
caroClient = fixtures.caroClient
}
}
6 changes: 3 additions & 3 deletions library/src/main/java/libxmtp-version.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Version: c974c9b
Branch: main
Date: 2024-05-03 16:18:08 +0000
Version: c38495d
Branch: identity-release
Date: 2024-05-21 23:25:22 +0000
57 changes: 41 additions & 16 deletions library/src/main/java/org/xmtp/android/library/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,23 @@ import org.xmtp.android.library.messages.walletAddress
import org.xmtp.proto.message.api.v1.MessageApiOuterClass
import org.xmtp.proto.message.api.v1.MessageApiOuterClass.BatchQueryResponse
import org.xmtp.proto.message.api.v1.MessageApiOuterClass.QueryRequest
import org.xmtp.proto.message.contents.signature
import uniffi.xmtpv3.FfiXmtpClient
import uniffi.xmtpv3.LegacyIdentitySource
import uniffi.xmtpv3.createClient
import uniffi.xmtpv3.generateInboxId
import uniffi.xmtpv3.getInboxIdForAddress
import uniffi.xmtpv3.getVersionInfo
import java.io.File
import java.nio.charset.StandardCharsets
import java.security.KeyStore
import java.security.SecureRandom
import java.text.SimpleDateFormat
import java.time.Instant
import java.util.Date
import java.util.Locale
import java.util.TimeZone
import java.util.UUID
import javax.crypto.KeyGenerator
import javax.crypto.SecretKey

Expand Down Expand Up @@ -89,6 +94,7 @@ class Client() {
var installationId: String = ""
private var libXMTPClient: FfiXmtpClient? = null
private var dbPath: String = ""
var inboxId: String = ""

companion object {
private const val TAG = "Client"
Expand Down Expand Up @@ -166,6 +172,7 @@ class Client() {
libXMTPClient: FfiXmtpClient? = null,
dbPath: String = "",
installationId: String = "",
inboxId: String = "",
) : this() {
this.address = address
this.privateKeyBundleV1 = privateKeyBundleV1
Expand All @@ -176,6 +183,7 @@ class Client() {
Conversations(client = this, libXMTPConversations = libXMTPClient?.conversations())
this.dbPath = dbPath
this.installationId = installationId
this.inboxId = inboxId
}

fun buildFrom(
Expand Down Expand Up @@ -233,7 +241,8 @@ class Client() {
apiClient,
libXMTPClient,
dbPath,
libXMTPClient?.installationId()?.toHex() ?: ""
libXMTPClient?.installationId()?.toHex() ?: "",
libXMTPClient?.inboxId() ?: ""
)
client.ensureUserContactPublished()
client
Expand Down Expand Up @@ -281,7 +290,8 @@ class Client() {
apiClient = apiClient,
libXMTPClient = v3Client,
dbPath = dbPath,
installationId = v3Client?.installationId()?.toHex() ?: ""
installationId = v3Client?.installationId()?.toHex() ?: "",
inboxId = v3Client?.inboxId() ?: ""
)
}

Expand All @@ -300,7 +310,16 @@ class Client() {
var dbPath = ""
val v3Client: FfiXmtpClient? =
if (isAlphaMlsEnabled(options)) {
val alias = "xmtp-${options!!.api.env}-${accountAddress.lowercase()}"
var inboxId = getInboxIdForAddress(
logger = logger,
host = options!!.api.env.getUrl(),
isSecure = options.api.isSecure,
accountAddress = accountAddress
)
if (inboxId.isNullOrBlank()) {
inboxId = generateInboxId(accountAddress, SecureRandom().nextLong().toULong())
}
val alias = "xmtp-${options.api.env}-${inboxId.lowercase()}"

dbPath = if (options.dbPath == null) {
val dbDir = File(appContext?.filesDir?.absolutePath, "xmtp_db")
Expand Down Expand Up @@ -357,14 +376,15 @@ class Client() {
}

if (v3Client != null) {
if (v3Client.textToSign() == null) {
v3Client.registerIdentity(null)
} else if (account != null) {
v3Client.textToSign()?.let {
v3Client.registerIdentity(account.sign(it)?.rawData)
v3Client.signatureRequest()?.let { signatureRequest ->
if (account != null) {
account.sign(signatureRequest.signatureText())?.let {
signatureRequest.addEcdsaSignature(it.rawData)
}
v3Client.registerIdentity(signatureRequest)
} else {
throw XMTPException("No signer passed but signer was required.")
}
} else {
throw XMTPException("No signer passed but signer was required.")
}
}
Log.i(TAG, "LibXMTP $libXMTPVersion")
Expand Down Expand Up @@ -415,7 +435,8 @@ class Client() {
val encryptedBundles = authCheck(apiClient, account.address)
for (encryptedBundle in encryptedBundles) {
try {
val bundle = encryptedBundle.decrypted(account, options?.preEnableIdentityCallback)
val bundle =
encryptedBundle.decrypted(account, options?.preEnableIdentityCallback)
return bundle.v1
} catch (e: Throwable) {
print("Error decoding encrypted private key bundle: $e")
Expand Down Expand Up @@ -448,10 +469,11 @@ class Client() {
}.build()
it.v2 = it.v2.toBuilder().also { v2Builder ->
v2Builder.keyBundle = v2Builder.keyBundle.toBuilder().also { keyBuilder ->
keyBuilder.identityKey = keyBuilder.identityKey.toBuilder().also { idBuilder ->
idBuilder.signature =
it.v2.keyBundle.identityKey.signature.ensureWalletSignature()
}.build()
keyBuilder.identityKey =
keyBuilder.identityKey.toBuilder().also { idBuilder ->
idBuilder.signature =
it.v2.keyBundle.identityKey.signature.ensureWalletSignature()
}.build()
}.build()
}.build()
}.build()
Expand Down Expand Up @@ -484,7 +506,10 @@ class Client() {
return apiClient.subscribe(request = request)
}

suspend fun fetchConversation(topic: String?, includeGroups: Boolean = false): Conversation? {
suspend fun fetchConversation(
topic: String?,
includeGroups: Boolean = false,
): Conversation? {
if (topic.isNullOrBlank()) return null
return conversations.list(includeGroups = includeGroups).firstOrNull {
it.topic == topic
Expand Down
21 changes: 11 additions & 10 deletions library/src/main/java/org/xmtp/android/library/Group.kt
Original file line number Diff line number Diff line change
Expand Up @@ -153,20 +153,21 @@ class Group(val client: Client, private val libXMTPGroup: FfiGroup) {
return libXMTPGroup.isActive()
}

fun addedByAddress(): String {
return libXMTPGroup.addedByAddress()
fun addedByInboxId(): String {
return libXMTPGroup.addedByInboxId()
}

fun permissionLevel(): GroupPermissions {
return metadata.policyType()
TODO()
// return metadata.policyType()
}

fun isAdmin(): Boolean {
return metadata.creatorAccountAddress().lowercase() == client.address.lowercase()
return metadata.creatorInboxId().lowercase() == client.inboxId.lowercase()
}

fun adminAddress(): String {
return metadata.creatorAccountAddress()
fun adminInboxId(): String {
return metadata.creatorInboxId()
}

suspend fun addMembers(addresses: List<String>) {
Expand All @@ -185,13 +186,13 @@ class Group(val client: Client, private val libXMTPGroup: FfiGroup) {
}
}

fun memberAddresses(): List<String> {
return libXMTPGroup.listMembers().map { it.accountAddress }
fun memberInboxIds(): List<String> {
return libXMTPGroup.listMembers().map { it.inboxId }
}

fun peerAddresses(): List<String> {
val addresses = memberAddresses().map { it.lowercase() }.toMutableList()
addresses.remove(client.address.lowercase())
val addresses = memberInboxIds().map { it.lowercase() }.toMutableList()
addresses.remove(client.inboxId.lowercase())
return addresses
}

Expand Down
Loading

0 comments on commit adca07b

Please sign in to comment.