Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static Can Message #344

Merged
merged 3 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Comment on lines +107 to +108
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rygine I think we will want to delete the db that is created with this temp client? Maybe something to add to the js sdk.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep! i recently added the ability to create a browser/node clients without a DB or encryption key for this purpose. just need to update the SDKs. great reminder, thanks!


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
Loading