Merge branch 'main' into claude/remove-libsodium-dependency-DjtWa

This commit is contained in:
Vitor Pamplona
2026-03-25 11:24:55 -04:00
committed by GitHub
170 changed files with 6727 additions and 1054 deletions
@@ -33,9 +33,7 @@ import kotlin.test.Test
import kotlin.test.assertEquals
class QueryAssemblerTest : BaseDBTest() {
val hasher =
_root_ide_package_.com.vitorpamplona.quartz.nip01Core.store.sqlite
.TagNameValueHasher(0)
val hasher = TagNameValueHasher(0)
val key1 = "7c5eb72a4584fdaaeaa145b25c92ea9917704224951219dbd43acef9e91fb88d"
val key2 = "f3ac434d61bc0f491a814782ccfdf9c439dae1f0bde9097ad4a245f4c495cd14"
val key3 = "12ae0fd81c85e1e7d9ed096397dc3129849425fe6f8afce7213ebf38ddfc6ca9"
@@ -21,7 +21,6 @@
package com.vitorpamplona.quartz.nip10Notes.urls
import com.vitorpamplona.quartz.nip10Notes.content.findURLs
import com.vitorpamplona.quartz.utils.fastFindURLs
import kotlin.test.Test
import kotlin.test.assertContains
import kotlin.test.assertEquals
@@ -31,7 +30,7 @@ class UrlsDetectorTest {
@Test
fun detectUrlNumber() {
val detectedLinks = fastFindURLs(testSentence)
val detectedLinks = findURLs(testSentence)
assertEquals(2, detectedLinks.size)
}
@@ -41,4 +40,14 @@ class UrlsDetectorTest {
assertContains(detectedLinks, "https://mysite.xyz")
assertContains(detectedLinks, "https://myblog.xyz")
}
/**
* Regression test for PR #1907: the Japanese phrase "今北産業" must not crash the URL
* detector with a StringIndexOutOfBoundsException and must return no URLs.
*/
@Test
fun doesNotCrashOnJapaneseText() {
val detectedLinks = findURLs("今北産業")
assertEquals(0, detectedLinks.size)
}
}
@@ -0,0 +1,103 @@
/*
* 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.nip44Encryption
import com.vitorpamplona.quartz.nip01Core.core.hexToByteArray
import com.vitorpamplona.quartz.nip01Core.core.toHexKey
import com.vitorpamplona.quartz.nip01Core.crypto.KeyPair
import kotlin.test.Test
import kotlin.test.assertEquals
/**
* NIP-44v2 tests verifying conversation key derivation, ECDH symmetry,
* and encrypt/decrypt round-trips on JVM Desktop.
*/
class Nip44v2Test {
@Test
fun conversationKeyFromSpecVector1() {
val nip44v2 = Nip44v2()
val convKey =
nip44v2.getConversationKey(
"315e59ff51cb9209768cf7da80791ddcaae56ac9775eb25b6dee1234bc5d2268".hexToByteArray(),
"c2f9d9948dc8c7c38321e4b85c8558872eafa0641cd269db76848a6073e69133".hexToByteArray(),
)
assertEquals(
"3dfef0ce2a4d80a25e7a328accf73448ef67096f65f79588e358d9a0eb9013f1",
convKey.toHexKey(),
)
}
@Test
fun conversationKeyFromSpecVector2() {
val nip44v2 = Nip44v2()
val convKey =
nip44v2.getConversationKey(
"a1e37752c9fdc1273be53f68c5f74be7c8905728e8de75800b94262f9497c86e".hexToByteArray(),
"03bb7947065dde12ba991ea045132581d0954f042c84e06d8c00066e23c1a800".hexToByteArray(),
)
assertEquals(
"4d14f36e81b8452128da64fe6f1eae873baae2f444b02c950b90e43553f2178b",
convKey.toHexKey(),
)
}
@Test
fun conversationKeyIsSymmetric() {
val nip44v2 = Nip44v2()
val privA = "f410f88bcec6cbfda04d6a273c7b1dd8bba144cd45b71e87109cfa11dd7ed561".hexToByteArray()
val privB = "65f039136f8da8d3e87b4818746b53318d5481e24b2673f162815144223a0b5a".hexToByteArray()
val pubA = KeyPair(privA).pubKey
val pubB = KeyPair(privB).pubKey
val convAB = nip44v2.getConversationKey(privA, pubB)
val convBA = nip44v2.getConversationKey(privB, pubA)
assertEquals(convAB.toHexKey(), convBA.toHexKey())
}
@Test
fun encryptDecryptRoundTrip() {
val nip44v2 = Nip44v2()
val privA = "0000000000000000000000000000000000000000000000000000000000000001".hexToByteArray()
val privB = "0000000000000000000000000000000000000000000000000000000000000002".hexToByteArray()
val pubA = KeyPair(privA).pubKey
val pubB = KeyPair(privB).pubKey
val encrypted = nip44v2.encrypt("hello world", privA, pubB)
val decrypted = nip44v2.decrypt(encrypted, privB, pubA)
assertEquals("hello world", decrypted)
}
@Test
fun crossKeyPairDecrypt() {
val nip44v2 = Nip44v2()
val ephemeralPriv = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".hexToByteArray()
val signerPriv = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb".hexToByteArray()
val ephemeralPub = KeyPair(ephemeralPriv).pubKey
val signerPub = KeyPair(signerPriv).pubKey
val request = """{"id":"test","method":"connect","params":["deadbeef"]}"""
val encryptedRequest = nip44v2.encrypt(request, ephemeralPriv, signerPub)
val decryptedRequest = nip44v2.decrypt(encryptedRequest, signerPriv, ephemeralPub)
assertEquals(request, decryptedRequest)
}
}
@@ -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 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 com.vitorpamplona.quartz.nip47WalletConnect.events.LnZapPaymentRequestEvent
import com.vitorpamplona.quartz.nip47WalletConnect.rpc.GetBalanceMethod
import com.vitorpamplona.quartz.nip47WalletConnect.rpc.GetInfoMethod
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.Companion.create()
val event =
LnZapPaymentRequestEvent.Companion.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.Companion.create()
val event =
LnZapPaymentRequestEvent.Companion.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,64 @@
/*
* 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.utils.urldetector
import com.vitorpamplona.quartz.utils.urldetector.detection.UrlDetector
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
/**
* Regression tests for PR #1907: StringIndexOutOfBoundsException in [Url.getPart] when
* processing the Japanese text "今北産業".
*/
class UrlTest {
/**
* Regression: detecting URLs in "今北産業" must not throw and must return no URLs.
*/
@Test
fun detectingImakitaSangyoDoesNotThrow() {
val urls = UrlDetector("今北産業").detect()
assertEquals(0, urls.size)
}
/**
* Regression: constructing a Url with "今北産業" as the original string and a HOST
* marker at 0 with PORT at the string length (simulating a trimmed trailing character)
* must not throw StringIndexOutOfBoundsException when accessing any property.
*
* "今北産業" has length 4. PORT at 4 == length triggers the startIndex >= length guard
* added to getPart() in PR #1907.
*/
@Test
fun urlPropertiesDoNotThrowForImakitaSangyoWithOutOfRangeMarker() {
val marker = UrlMarker()
marker.setIndex(UrlPart.HOST, 0)
marker.setIndex(UrlPart.PORT, 4) // == "今北産業".length
val url = marker.createUrl("今北産業")
assertNotNull(url.scheme)
assertNotNull(url.host)
assertNotNull(url.path)
assertNotNull(url.query)
assertNotNull(url.fragment)
assertEquals(443, url.port) // getPart(PORT) returns null → -1
}
}
@@ -739,6 +739,37 @@ class UriDetectionTest {
runTest("blossom:9584b6d64e43747364b10276f4b821e5df09f46477b3b8c60cced3e8c647fbef.jpg?xs=blossom.primal.net", "blossom:9584b6d64e43747364b10276f4b821e5df09f46477b3b8c60cced3e8c647fbef.jpg?xs=blossom.primal.net")
}
@Test
fun testBrokenCaseInProduction() {
runTest("今北産業")
runTest("http://test.com今北産業", "http://test.com")
runTest("ftp://test.com今北産業", "ftp://test.com")
runTest("test.com今北産業", "test.com")
runTest("wss://test.com今北産業", "wss://test.com")
runTest("blossom:test.com今北産業", "blossom:test.com")
runTest("nostr:test.com今北産業", "nostr:test.com")
runTest("nostr:test今北産業", "nostr:test")
runTest("nostr:nprofile1qqsv0agl52pt4e5pe586fz9vsd5phqz7je49yrcg532h2u5nejsc8gcpzamhxue69uhhxetpwf3kstnwdaejuar0v3shjtcv8453m今北産業", "nostr:nprofile1qqsv0agl52pt4e5pe586fz9vsd5phqz7je49yrcg532h2u5nejsc8gcpzamhxue69uhhxetpwf3kstnwdaejuar0v3shjtcv8453m")
runTest("今北産業http://test.com", "http://test.com")
runTest("今北産業ftp://test.com", "ftp://test.com")
runTest("今北産業test.com", "test.com")
runTest("今北産業wss://test.com", "wss://test.com")
runTest("今北産業blossom:test.com", "blossom:test.com")
runTest("今北産業nostr:test.com", "nostr:test.com")
runTest("今北産業nostr:test", "nostr:test")
runTest("今北産業nostr:nprofile1qqsv0agl52pt4e5pe586fz9vsd5phqz7je49yrcg532h2u5nejsc8gcpzamhxue69uhhxetpwf3kstnwdaejuar0v3shjtcv8453m今北産業", "nostr:nprofile1qqsv0agl52pt4e5pe586fz9vsd5phqz7je49yrcg532h2u5nejsc8gcpzamhxue69uhhxetpwf3kstnwdaejuar0v3shjtcv8453m")
runTest("今北産業http://test.com今北産業", "http://test.com")
runTest("今北産業ftp://test.com今北産業", "ftp://test.com")
runTest("今北産業test.com今北産業", "test.com")
runTest("今北産業wss://test.com今北産業", "wss://test.com")
runTest("今北産業blossom:test.com今北産業", "blossom:test.com")
runTest("今北産業nostr:test.com今北産業", "nostr:test.com")
runTest("今北産業nostr:test今北産業", "nostr:test")
runTest("今北産業nostr:nprofile1qqsv0agl52pt4e5pe586fz9vsd5phqz7je49yrcg532h2u5nejsc8gcpzamhxue69uhhxetpwf3kstnwdaejuar0v3shjtcv8453m今北産業", "nostr:nprofile1qqsv0agl52pt4e5pe586fz9vsd5phqz7je49yrcg532h2u5nejsc8gcpzamhxue69uhhxetpwf3kstnwdaejuar0v3shjtcv8453m")
}
@Test
fun testFullText() {
val text =