Skip to content

Commit

Permalink
Static Can Message (#344)
Browse files Browse the repository at this point in the history
* add a static can message method to the client

* add static can message

* fix up linter
  • Loading branch information
nplasterer authored Dec 4, 2024
1 parent 1a2d6a6 commit 4449843
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,35 @@ class ClientTest {
assertEquals(inboxId, client.inboxId)
}

@Test
fun testStaticCanMessage() {
val context = InstrumentationRegistry.getInstrumentation().targetContext
val fixtures = fixtures()
val notOnNetwork = PrivateKeyBuilder()

val canMessageList = runBlocking {
Client.canMessage(
listOf(
fixtures.alix.walletAddress,
notOnNetwork.address,
fixtures.bo.walletAddress
),
context,
ClientOptions.Api(XMTPEnvironment.LOCAL, false)
)
}

val expectedResults = mapOf(
fixtures.alix.walletAddress.lowercase() to true,
notOnNetwork.address.lowercase() to false,
fixtures.bo.walletAddress.lowercase() to true
)

expectedResults.forEach { (address, expected) ->
assertEquals(expected, canMessageList[address.lowercase()])
}
}

@Test
fun testCanDeleteDatabase() {
val key = SecureRandom().generateSeed(32)
Expand Down
43 changes: 40 additions & 3 deletions library/src/main/java/org/xmtp/android/library/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,39 @@ class Client() {
fun register(codec: ContentCodec<*>) {
codecRegistry.register(codec = codec)
}

suspend fun canMessage(
accountAddresses: List<String>,
appContext: Context,
api: ClientOptions.Api,
): Map<String, Boolean> {
val accountAddress = "0x0000000000000000000000000000000000000000"
val inboxId = getOrCreateInboxId(api, accountAddress)
val alias = "xmtp-${api.env}-$inboxId"

val directoryFile = File(appContext.filesDir.absolutePath, "xmtp_db")
directoryFile.mkdir()
val dbPath = directoryFile.absolutePath + "/$alias.db3"

val ffiClient = createClient(
logger = XMTPLogger(),
host = api.env.getUrl(),
isSecure = api.isSecure,
db = dbPath,
encryptionKey = null,
accountAddress = accountAddress.lowercase(),
inboxId = inboxId,
nonce = 0.toULong(),
legacySignedPrivateKeyProto = null,
historySyncUrl = null
)

val result = ffiClient.canMessage(accountAddresses)
ffiClient.releaseDbConnection()
File(dbPath).delete()

return result
}
}

constructor(
Expand All @@ -101,7 +134,7 @@ class Client() {
address: String,
clientOptions: ClientOptions,
signingKey: SigningKey? = null,
inboxId: String? = null
inboxId: String? = null,
): Client {
val accountAddress = address.lowercase()
val recoveredInboxId = inboxId ?: getOrCreateInboxId(clientOptions.api, accountAddress)
Expand Down Expand Up @@ -140,7 +173,7 @@ class Client() {
suspend fun build(
address: String,
options: ClientOptions,
inboxId: String? = null
inboxId: String? = null,
): Client {
return try {
initializeV3Client(address, options, inboxId = inboxId)
Expand Down Expand Up @@ -245,7 +278,11 @@ class Client() {
}
}

fun verifySignatureWithInstallationId(message: String, signature: ByteArray, installationId: String): Boolean {
fun verifySignatureWithInstallationId(
message: String,
signature: ByteArray,
installationId: String,
): Boolean {
return try {
ffiClient.verifySignedWithPublicKey(message, signature, installationId.hexToByteArray())
true
Expand Down

0 comments on commit 4449843

Please sign in to comment.