From ab35b8637ab8810e51a1511faf83eb2548ac0a09 Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Wed, 25 Oct 2023 13:36:20 -0700 Subject: [PATCH] update the way the client is created --- dev/local/docker-compose.yml | 10 +- .../org/xmtp/android/library/ContactsTest.kt | 4 +- .../java/org/xmtp/android/library/Contacts.kt | 134 ++++++++---------- .../org/xmtp/android/library/Conversations.kt | 1 + 4 files changed, 74 insertions(+), 75 deletions(-) rename library/src/{test => androidTest}/java/org/xmtp/android/library/ContactsTest.kt (94%) diff --git a/dev/local/docker-compose.yml b/dev/local/docker-compose.yml index 197ba1d63..5e3261b02 100644 --- a/dev/local/docker-compose.yml +++ b/dev/local/docker-compose.yml @@ -5,9 +5,13 @@ services: environment: - GOWAKU-NODEKEY=8a30dcb604b0b53627a5adc054dbf434b446628d4bd1eccc681d223f0550ce67 command: - - --store.enable - - --store.db-connection-string=postgres://postgres:xmtp@db:5432/postgres?sslmode=disable - - --store.reader-db-connection-string=postgres://postgres:xmtp@db:5432/postgres?sslmode=disable + - --ws + - --store + - --message-db-connection-string=postgres://postgres:xmtp@db:5432/postgres?sslmode=disable + - --message-db-reader-connection-string=postgres://postgres:xmtp@db:5432/postgres?sslmode=disable + - --lightpush + - --filter + - --ws-port=9001 - --wait-for-db=30s - --api.authn.enable ports: diff --git a/library/src/test/java/org/xmtp/android/library/ContactsTest.kt b/library/src/androidTest/java/org/xmtp/android/library/ContactsTest.kt similarity index 94% rename from library/src/test/java/org/xmtp/android/library/ContactsTest.kt rename to library/src/androidTest/java/org/xmtp/android/library/ContactsTest.kt index d9cbf7036..3ccf68486 100644 --- a/library/src/test/java/org/xmtp/android/library/ContactsTest.kt +++ b/library/src/androidTest/java/org/xmtp/android/library/ContactsTest.kt @@ -1,9 +1,11 @@ package org.xmtp.android.library +import androidx.test.ext.junit.runners.AndroidJUnit4 import org.junit.Assert.assertEquals import org.junit.Test +import org.junit.runner.RunWith import org.xmtp.android.library.messages.walletAddress - +@RunWith(AndroidJUnit4::class) class ContactsTest { @Test diff --git a/library/src/main/java/org/xmtp/android/library/Contacts.kt b/library/src/main/java/org/xmtp/android/library/Contacts.kt index 41691cd8f..457781842 100644 --- a/library/src/main/java/org/xmtp/android/library/Contacts.kt +++ b/library/src/main/java/org/xmtp/android/library/Contacts.kt @@ -33,84 +33,76 @@ data class AllowListEntry( get() = "${entryType.name}-$value" } -class AllowList { +class AllowList(val client: Client) { private val entries: MutableMap = mutableMapOf() - - companion object { - @OptIn(ExperimentalUnsignedTypes::class) - suspend fun load(client: Client): AllowList { - val publicKey = - client.privateKeyBundleV1.identityKey.publicKey.secp256K1Uncompressed.bytes - val privateKey = client.privateKeyBundleV1.identityKey.secp256K1.bytes - - val identifier = uniffi.xmtp_dh.generatePrivatePreferencesTopicIdentifier( - privateKey.toByteArray().toUByteArray().toList() + private val publicKey = + client.privateKeyBundleV1.identityKey.publicKey.secp256K1Uncompressed.bytes + private val privateKey = client.privateKeyBundleV1.identityKey.secp256K1.bytes + + @OptIn(ExperimentalUnsignedTypes::class) + private val identifier: String = uniffi.xmtp_dh.generatePrivatePreferencesTopicIdentifier( + privateKey.toByteArray().toUByteArray().toList() + ) + + @OptIn(ExperimentalUnsignedTypes::class) + suspend fun load(): AllowList { + val envelopes = client.query(Topic.allowList(identifier)) + val allowList = AllowList(client) + val preferences: MutableList = mutableListOf() + + for (envelope in envelopes.envelopesList) { + val payload = uniffi.xmtp_dh.eciesDecryptK256Sha3256( + publicKey.toByteArray().toUByteArray().toList(), + privateKey.toByteArray().toUByteArray().toList(), + envelope.message.toByteArray().toUByteArray().toList() ) - val envelopes = client.query(Topic.allowList(identifier)) - val allowList = AllowList() - val preferences: MutableList = mutableListOf() - - for (envelope in envelopes.envelopesList) { - val payload = uniffi.xmtp_dh.eciesDecryptK256Sha3256( - publicKey.toByteArray().toUByteArray().toList(), - privateKey.toByteArray().toUByteArray().toList(), - envelope.message.toByteArray().toUByteArray().toList() - ) - preferences.add( - PrivatePreferencesAction.parseFrom( - payload.toUByteArray().toByteArray() - ) + preferences.add( + PrivatePreferencesAction.parseFrom( + payload.toUByteArray().toByteArray() ) - } + ) + } - preferences.forEach { preference -> - preference.allow?.walletAddressesList?.forEach { address -> - allowList.allow(address) - } - preference.block?.walletAddressesList?.forEach { address -> - allowList.block(address) - } + preferences.forEach { preference -> + preference.allow?.walletAddressesList?.forEach { address -> + allowList.allow(address) + } + preference.block?.walletAddressesList?.forEach { address -> + allowList.block(address) } - return allowList } + return allowList + } - @OptIn(ExperimentalUnsignedTypes::class) - fun publish(entry: AllowListEntry, client: Client) { - val payload = PrivatePreferencesAction.newBuilder().also { - when (entry.permissionType) { - PrivatePreferencesAction.MessageTypeCase.ALLOW -> it.setAllow( - PrivatePreferencesAction.Allow.newBuilder().addWalletAddresses(entry.value) - ) - - PrivatePreferencesAction.MessageTypeCase.BLOCK -> it.setBlock( - PrivatePreferencesAction.Block.newBuilder().addWalletAddresses(entry.value) - ) - - PrivatePreferencesAction.MessageTypeCase.MESSAGETYPE_NOT_SET -> it.clearMessageType() - } - }.build() - - val publicKey = - client.privateKeyBundleV1.identityKey.publicKey.secp256K1Uncompressed.bytes - val privateKey = client.privateKeyBundleV1.identityKey.secp256K1.bytes - val identifier = uniffi.xmtp_dh.generatePrivatePreferencesTopicIdentifier( - privateKey.toByteArray().toUByteArray().toList() - ) + @OptIn(ExperimentalUnsignedTypes::class) + fun publish(entry: AllowListEntry) { + val payload = PrivatePreferencesAction.newBuilder().also { + when (entry.permissionType) { + PrivatePreferencesAction.MessageTypeCase.ALLOW -> it.setAllow( + PrivatePreferencesAction.Allow.newBuilder().addWalletAddresses(entry.value) + ) - val message = uniffi.xmtp_dh.eciesDecryptK256Sha3256( - publicKey.toByteArray().toUByteArray().toList(), - privateKey.toByteArray().toUByteArray().toList(), - payload.toByteArray().toUByteArray().toList() - ) + PrivatePreferencesAction.MessageTypeCase.BLOCK -> it.setBlock( + PrivatePreferencesAction.Block.newBuilder().addWalletAddresses(entry.value) + ) - val envelope = EnvelopeBuilder.buildFromTopic( - Topic.allowList(identifier), - Date(), - ByteArray(message.size) { message[it].toByte() }) + PrivatePreferencesAction.MessageTypeCase.MESSAGETYPE_NOT_SET -> it.clearMessageType() + } + }.build() - client.publish(listOf(envelope)) - } + val message = uniffi.xmtp_dh.eciesDecryptK256Sha3256( + publicKey.toByteArray().toUByteArray().toList(), + privateKey.toByteArray().toUByteArray().toList(), + payload.toByteArray().toUByteArray().toList() + ) + + val envelope = EnvelopeBuilder.buildFromTopic( + Topic.allowList(identifier), + Date(), + ByteArray(message.size) { message[it].toByte() }) + + client.publish(listOf(envelope)) } fun allow(address: String): AllowListEntry { @@ -138,11 +130,11 @@ data class Contacts( val hasIntroduced: MutableMap = mutableMapOf(), ) { - var allowList: AllowList = AllowList() + var allowList: AllowList = AllowList(client) fun refreshAllowList() { runBlocking { - allowList = AllowList.load(client) + allowList = AllowList(client).load() } } @@ -156,13 +148,13 @@ data class Contacts( fun allow(addresses: List) { for (address in addresses) { - AllowList.publish(allowList.allow(address), client) + AllowList(client).publish(allowList.allow(address)) } } fun block(addresses: List) { for (address in addresses) { - AllowList.publish(allowList.block(address), client) + AllowList(client).publish(allowList.block(address)) } } diff --git a/library/src/main/java/org/xmtp/android/library/Conversations.kt b/library/src/main/java/org/xmtp/android/library/Conversations.kt index 84fbb88d8..1cea017e1 100644 --- a/library/src/main/java/org/xmtp/android/library/Conversations.kt +++ b/library/src/main/java/org/xmtp/android/library/Conversations.kt @@ -142,6 +142,7 @@ data class Conversations( invitation = invitation, header = sealedInvitation.v1.header ) + client.contacts.allow(addresses = listOf(peerAddress)) val conversation = Conversation.V2(conversationV2) conversationsByTopic[conversation.topic] = conversation return conversation