Solving all problems with Quartz tests

This commit is contained in:
Vitor Pamplona
2026-03-16 19:22:28 -04:00
parent 962da3280b
commit 56cce46858
5 changed files with 258 additions and 50 deletions
+12 -1
View File
@@ -274,8 +274,10 @@ kotlin {
}
getByName("androidHostTest") {
dependsOn(jvmAndroidTest)
dependencies {
implementation(libs.kotlin.test)
implementation(libs.kotlinx.coroutines.test)
// Bitcoin secp256k1 bindings
implementation(libs.secp256k1.kmp.jni.jvm)
@@ -291,7 +293,16 @@ kotlin {
implementation(libs.androidx.core)
implementation(libs.androidx.junit)
implementation(libs.androidx.espresso.core)
implementation(libs.kotlin.test)
implementation(libs.kotlinx.coroutines.test)
// Bitcoin secp256k1 bindings to Android
api(libs.secp256k1.kmp.jni.android)
// LibSodium for ChaCha encryption (NIP-44)
implementation("com.goterl:lazysodium-android:5.2.0@aar")
implementation("net.java.dev.jna:jna:5.18.1@aar")
}
}
@@ -0,0 +1,84 @@
/*
* Copyright (c) 2025 Vitor Pamplona
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.vitorpamplona.quartz.nip47WalletConnect
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.vitorpamplona.quartz.nip01Core.core.HexKey
import com.vitorpamplona.quartz.nip01Core.core.toHexKey
import com.vitorpamplona.quartz.nip01Core.crypto.KeyPair
import com.vitorpamplona.quartz.nip01Core.signers.NostrSignerInternal
import kotlinx.coroutines.test.runTest
import org.junit.runner.RunWith
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertIs
@RunWith(AndroidJUnit4::class)
class LnZapPaymentRequestNip44EventTest {
@Test
fun testCreateRequestWithNip44() =
runTest {
val clientKeyPair = KeyPair()
val walletKeyPair = KeyPair()
val clientSigner = NostrSignerInternal(clientKeyPair)
val walletServicePubkey: HexKey =
walletKeyPair.pubKey.toHexKey()
val request = GetBalanceMethod.create()
val event =
LnZapPaymentRequestEvent.createRequest(
request = request,
walletServicePubkey = walletServicePubkey,
signer = clientSigner,
createdAt = 1000L,
useNip44 = true,
)
assertEquals(23194, event.kind)
assertEquals("nip44_v2", event.encryptionScheme())
}
@Test
fun testDecryptNip44Request() =
runTest {
val clientKeyPair = KeyPair()
val walletKeyPair = KeyPair()
val clientSigner = NostrSignerInternal(clientKeyPair)
val walletSigner = NostrSignerInternal(walletKeyPair)
val walletServicePubkey: HexKey =
walletKeyPair.pubKey.toHexKey()
val request = GetInfoMethod.create()
val event =
LnZapPaymentRequestEvent.createRequest(
request = request,
walletServicePubkey = walletServicePubkey,
signer = clientSigner,
useNip44 = true,
)
assertEquals("nip44_v2", event.encryptionScheme())
// Wallet service should be able to decrypt NIP-44 encrypted request
val decrypted = event.decryptRequest(walletSigner)
assertIs<GetInfoMethod>(decrypted)
}
}
@@ -86,29 +86,6 @@ class LnZapPaymentRequestEventTest {
assertNull(event.encryptionScheme())
}
@Test
fun testCreateRequestWithNip44() =
runTest {
val clientKeyPair = KeyPair()
val walletKeyPair = KeyPair()
val clientSigner = NostrSignerInternal(clientKeyPair)
val walletServicePubkey: HexKey =
walletKeyPair.pubKey.toHexKey()
val request = GetBalanceMethod.create()
val event =
LnZapPaymentRequestEvent.createRequest(
request = request,
walletServicePubkey = walletServicePubkey,
signer = clientSigner,
createdAt = 1000L,
useNip44 = true,
)
assertEquals(23194, event.kind)
assertEquals("nip44_v2", event.encryptionScheme())
}
@Test
fun testDecryptPayInvoiceRequest() =
runTest {
@@ -156,32 +133,6 @@ class LnZapPaymentRequestEventTest {
assertEquals("test payment", decrypted.params?.description)
}
@Test
fun testDecryptNip44Request() =
runTest {
val clientKeyPair = KeyPair()
val walletKeyPair = KeyPair()
val clientSigner = NostrSignerInternal(clientKeyPair)
val walletSigner = NostrSignerInternal(walletKeyPair)
val walletServicePubkey: HexKey =
walletKeyPair.pubKey.toHexKey()
val request = GetInfoMethod.create()
val event =
LnZapPaymentRequestEvent.createRequest(
request = request,
walletServicePubkey = walletServicePubkey,
signer = clientSigner,
useNip44 = true,
)
assertEquals("nip44_v2", event.encryptionScheme())
// Wallet service should be able to decrypt NIP-44 encrypted request
val decrypted = event.decryptRequest(walletSigner)
assertIs<GetInfoMethod>(decrypted)
}
@Test
fun testCanDecrypt() =
runTest {
@@ -0,0 +1,81 @@
/*
* Copyright (c) 2025 Vitor Pamplona
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.vitorpamplona.quartz.nip47WalletConnect
import com.vitorpamplona.quartz.nip01Core.core.HexKey
import com.vitorpamplona.quartz.nip01Core.core.toHexKey
import com.vitorpamplona.quartz.nip01Core.crypto.KeyPair
import com.vitorpamplona.quartz.nip01Core.signers.NostrSignerInternal
import kotlinx.coroutines.test.runTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertIs
class LnZapPaymentRequestNip44EventTest {
@Test
fun testCreateRequestWithNip44() =
runTest {
val clientKeyPair = KeyPair()
val walletKeyPair = KeyPair()
val clientSigner = NostrSignerInternal(clientKeyPair)
val walletServicePubkey: HexKey =
walletKeyPair.pubKey.toHexKey()
val request = GetBalanceMethod.create()
val event =
LnZapPaymentRequestEvent.createRequest(
request = request,
walletServicePubkey = walletServicePubkey,
signer = clientSigner,
createdAt = 1000L,
useNip44 = true,
)
assertEquals(23194, event.kind)
assertEquals("nip44_v2", event.encryptionScheme())
}
@Test
fun testDecryptNip44Request() =
runTest {
val clientKeyPair = KeyPair()
val walletKeyPair = KeyPair()
val clientSigner = NostrSignerInternal(clientKeyPair)
val walletSigner = NostrSignerInternal(walletKeyPair)
val walletServicePubkey: HexKey =
walletKeyPair.pubKey.toHexKey()
val request = GetInfoMethod.create()
val event =
LnZapPaymentRequestEvent.createRequest(
request = request,
walletServicePubkey = walletServicePubkey,
signer = clientSigner,
useNip44 = true,
)
assertEquals("nip44_v2", event.encryptionScheme())
// Wallet service should be able to decrypt NIP-44 encrypted request
val decrypted = event.decryptRequest(walletSigner)
assertIs<GetInfoMethod>(decrypted)
}
}
@@ -0,0 +1,81 @@
/*
* Copyright (c) 2025 Vitor Pamplona
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.vitorpamplona.quartz.nip47WalletConnect
import com.vitorpamplona.quartz.nip01Core.core.HexKey
import com.vitorpamplona.quartz.nip01Core.core.toHexKey
import com.vitorpamplona.quartz.nip01Core.crypto.KeyPair
import com.vitorpamplona.quartz.nip01Core.signers.NostrSignerInternal
import kotlinx.coroutines.test.runTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertIs
class LnZapPaymentRequestNip44EventTest {
@Test
fun testCreateRequestWithNip44() =
runTest {
val clientKeyPair = KeyPair()
val walletKeyPair = KeyPair()
val clientSigner = NostrSignerInternal(clientKeyPair)
val walletServicePubkey: HexKey =
walletKeyPair.pubKey.toHexKey()
val request = GetBalanceMethod.create()
val event =
LnZapPaymentRequestEvent.createRequest(
request = request,
walletServicePubkey = walletServicePubkey,
signer = clientSigner,
createdAt = 1000L,
useNip44 = true,
)
assertEquals(23194, event.kind)
assertEquals("nip44_v2", event.encryptionScheme())
}
@Test
fun testDecryptNip44Request() =
runTest {
val clientKeyPair = KeyPair()
val walletKeyPair = KeyPair()
val clientSigner = NostrSignerInternal(clientKeyPair)
val walletSigner = NostrSignerInternal(walletKeyPair)
val walletServicePubkey: HexKey =
walletKeyPair.pubKey.toHexKey()
val request = GetInfoMethod.create()
val event =
LnZapPaymentRequestEvent.createRequest(
request = request,
walletServicePubkey = walletServicePubkey,
signer = clientSigner,
useNip44 = true,
)
assertEquals("nip44_v2", event.encryptionScheme())
// Wallet service should be able to decrypt NIP-44 encrypted request
val decrypted = event.decryptRequest(walletSigner)
assertIs<GetInfoMethod>(decrypted)
}
}