Skip to content

Commit

Permalink
add ability to verify signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Nov 21, 2024
1 parent b1648c9 commit 05ac562
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,80 @@ class ClientTest {
listOf(fixtures.boClient.inboxId, fixtures.caroClient.inboxId)
)
}
assertEquals(states.first().recoveryAddress.lowercase(), fixtures.bo.walletAddress.lowercase())
assertEquals(states.last().recoveryAddress.lowercase(), fixtures.caro.walletAddress.lowercase())
assertEquals(
states.first().recoveryAddress.lowercase(),
fixtures.bo.walletAddress.lowercase()
)
assertEquals(
states.last().recoveryAddress.lowercase(),
fixtures.caro.walletAddress.lowercase()
)
}

@Test
fun testsSignatures() {
val fixtures = fixtures()
val signature = fixtures.alixClient.signWithInstallationKey("Testing")
assertEquals(fixtures.alixClient.verifySignature("Testing", signature), true)
assertEquals(fixtures.alixClient.verifySignature("Not Testing", signature), false)

val alixInstallationId = fixtures.alixClient.installationId
assertEquals(
fixtures.alixClient.verifySignatureWithInstallationId(
"Testing",
signature,
alixInstallationId
), true
)
assertEquals(
fixtures.alixClient.verifySignatureWithInstallationId(
"Not Testing",
signature,
alixInstallationId
), false
)
assertEquals(
fixtures.alixClient.verifySignatureWithInstallationId(
"Testing",
signature,
fixtures.boClient.installationId
), false
)
assertEquals(
fixtures.boClient.verifySignatureWithInstallationId(
"Testing",
signature,
alixInstallationId
), true
)
fixtures.alixClient.deleteLocalDatabase()

val key = SecureRandom().generateSeed(32)
val context = InstrumentationRegistry.getInstrumentation().targetContext
val alixClient2 = runBlocking {
Client().create(
account = fixtures.alixAccount,
options = ClientOptions(
ClientOptions.Api(XMTPEnvironment.LOCAL, false),
appContext = context,
dbEncryptionKey = key
)
)
}

assertEquals(
alixClient2.verifySignatureWithInstallationId(
"Testing",
signature,
alixInstallationId
), true
)
assertEquals(
alixClient2.verifySignatureWithInstallationId(
"Testing2",
signature,
alixInstallationId
), false
)
}
}
18 changes: 18 additions & 0 deletions library/src/main/java/org/xmtp/android/library/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,24 @@ class Client() {
return ffiClient.signWithInstallationKey(message)
}

fun verifySignature(message: String, signature: ByteArray): Boolean {
return try {
ffiClient.verifySignedWithInstallationKey(message, signature)
true
} catch (e: Exception) {
false
}
}

fun verifySignatureWithInstallationId(message: String, signature: ByteArray, installationId: String): Boolean {
return try {
ffiClient.verifySignedWithPublicKey(message, signature, installationId.hexToByteArray())
true
} catch (e: Exception) {
false
}
}

fun findGroup(groupId: String): Group? {
return try {
Group(this, ffiClient.conversation(groupId.hexToByteArray()))
Expand Down

0 comments on commit 05ac562

Please sign in to comment.