Skip to content

Commit

Permalink
update the way the client is created
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Oct 25, 2023
1 parent c06ffea commit ab35b86
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 75 deletions.
10 changes: 7 additions & 3 deletions dev/local/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
134 changes: 63 additions & 71 deletions library/src/main/java/org/xmtp/android/library/Contacts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,84 +33,76 @@ data class AllowListEntry(
get() = "${entryType.name}-$value"
}

class AllowList {
class AllowList(val client: Client) {
private val entries: MutableMap<String, MessageType> = 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<PrivatePreferencesAction> = 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<PrivatePreferencesAction> = 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 {
Expand Down Expand Up @@ -138,11 +130,11 @@ data class Contacts(
val hasIntroduced: MutableMap<String, Boolean> = mutableMapOf(),
) {

var allowList: AllowList = AllowList()
var allowList: AllowList = AllowList(client)

fun refreshAllowList() {
runBlocking {
allowList = AllowList.load(client)
allowList = AllowList(client).load()
}
}

Expand All @@ -156,13 +148,13 @@ data class Contacts(

fun allow(addresses: List<String>) {
for (address in addresses) {
AllowList.publish(allowList.allow(address), client)
AllowList(client).publish(allowList.allow(address))
}
}

fun block(addresses: List<String>) {
for (address in addresses) {
AllowList.publish(allowList.block(address), client)
AllowList(client).publish(allowList.block(address))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ab35b86

Please sign in to comment.