Skip to content

Commit

Permalink
Signature verification (#334)
Browse files Browse the repository at this point in the history
* bump bindings

* bump bindings

* add ability to verify signatures

* fix linter issues

* latest release again

* fix linter
  • Loading branch information
nplasterer authored Nov 22, 2024
1 parent 8c4a882 commit 3416280
Show file tree
Hide file tree
Showing 8 changed files with 645 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,79 @@ class ClientTest {
)
}

@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
)
}

@Test
fun testAddAccounts() {
val fixtures = fixtures()
Expand Down
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 @@ -236,6 +236,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
Loading

0 comments on commit 3416280

Please sign in to comment.