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

Signature verification #334

Merged
merged 7 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -331,7 +331,86 @@ 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
)
}
}
4 changes: 2 additions & 2 deletions library/src/main/java/libxmtp-version.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Version: 8e8ff6a6
Version: cbff7296
Branch: main
Date: 2024-11-19 01:30:43 +0000
Date: 2024-11-21 17:51:27 +0000
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 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, I missed this when I approved. Can we rename this function to match with the ffi client?

Copy link
Contributor

@codabrink codabrink Nov 22, 2024

Choose a reason for hiding this comment

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

Or I can rename the fn on the client if it's confusing. I'm fine with going with the nomenclature you went with on ios.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it's confusing for integrators since it's not clear that the installationId is the public key. I think it's okay for the names between the SDKs and libxmtp to differ. This isn't the first time I've strayed from the ffi naming in benefits of clarity in the sdk.

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
Loading