Skip to content

Commit

Permalink
add a test for this case
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Feb 11, 2025
1 parent 0d0251e commit 1613070
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import kotlinx.coroutines.runBlocking
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertThrows
import org.junit.Assert.fail
import org.junit.Test
Expand Down Expand Up @@ -517,6 +518,7 @@ class ClientTest {
)
}

@OptIn(DelicateApi::class)
@Test
fun testAddAccounts() {
val fixtures = fixtures()
Expand All @@ -539,6 +541,29 @@ class ClientTest {
)
}

@OptIn(DelicateApi::class)
@Test
fun testAddAccountsWithExistingInboxIds() {
val fixtures = fixtures()

assertThrows(
"This wallet is already associated with inbox ${fixtures.boClient.inboxId}",
XMTPException::class.java
) {
runBlocking { fixtures.alixClient.addAccount(fixtures.boAccount) }
}

assert(fixtures.boClient.inboxId != fixtures.alixClient.inboxId)
runBlocking { fixtures.alixClient.addAccount(fixtures.boAccount, true) }

val state = runBlocking { fixtures.alixClient.inboxState(true) }
assertEquals(state.addresses.size, 2)

val inboxId = runBlocking { fixtures.alixClient.inboxIdFromAddress(fixtures.boClient.address) }
assertEquals(inboxId, fixtures.alixClient.inboxId)
}

@OptIn(DelicateApi::class)
@Test
fun testRemovingAccounts() {
val fixtures = fixtures()
Expand Down
7 changes: 4 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 @@ -259,10 +259,11 @@ class Client() {
}

@DelicateApi("This function is delicate and should be used with caution. Adding a wallet already associated with an inboxId will cause the wallet to loose access to that inbox. See: inboxIdFromAddress(address)")
suspend fun addAccount(newAccount: SigningKey, changeInboxId: Boolean = false) {
val inboxId: String? = if (!changeInboxId) inboxIdFromAddress(newAccount.address) else null
suspend fun addAccount(newAccount: SigningKey, allowReassignInboxId: Boolean = false) {
val inboxId: String? =
if (!allowReassignInboxId) inboxIdFromAddress(newAccount.address) else null

if (changeInboxId || inboxId.isNullOrBlank()) {
if (allowReassignInboxId || inboxId.isNullOrBlank()) {
val signatureRequest = ffiClient.addWallet(newAccount.address.lowercase())
handleSignature(signatureRequest, newAccount)
ffiClient.applySignatureRequest(signatureRequest)
Expand Down

0 comments on commit 1613070

Please sign in to comment.