mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-09-21 21:51:44 +02:00
Testing that both 02 and 03 compressed keys are valid Nostr keys and can encrypt and decrypt to each other.
This commit is contained in:
@@ -44,6 +44,24 @@ class Nip01Test {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testGetPublicCompressedWith2() {
|
||||||
|
val key = "e6159851715b4aa6190c22b899b0c792847de0a4435ac5b678f35738351c43b0".hexToByteArray()
|
||||||
|
assertEquals(
|
||||||
|
"029fa4ce8c87ca546b196e6518db80a6780e1bd5552b61f9f17bafee5d4e34e09b",
|
||||||
|
nip01.compressedPubkeyCreate(key).toHexKey(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testGetPublicCompressedWith3() {
|
||||||
|
val key = "65f039136f8da8d3e87b4818746b53318d5481e24b2673f162815144223a0b5a".hexToByteArray()
|
||||||
|
assertEquals(
|
||||||
|
"033dcef7585efbdb68747d919152bd481e21f5e952aaaef5a19604fbd096a93dd5",
|
||||||
|
nip01.compressedPubkeyCreate(key).toHexKey(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testDeterministicSign() {
|
fun testDeterministicSign() {
|
||||||
assertEquals(
|
assertEquals(
|
||||||
|
@@ -25,6 +25,7 @@ import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation
|
|||||||
import com.fasterxml.jackson.annotation.JsonProperty
|
import com.fasterxml.jackson.annotation.JsonProperty
|
||||||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
||||||
import com.vitorpamplona.quartz.crypto.KeyPair
|
import com.vitorpamplona.quartz.crypto.KeyPair
|
||||||
|
import com.vitorpamplona.quartz.crypto.nip01.Nip01
|
||||||
import com.vitorpamplona.quartz.encoders.hexToByteArray
|
import com.vitorpamplona.quartz.encoders.hexToByteArray
|
||||||
import com.vitorpamplona.quartz.encoders.toHexKey
|
import com.vitorpamplona.quartz.encoders.toHexKey
|
||||||
import fr.acinq.secp256k1.Secp256k1
|
import fr.acinq.secp256k1.Secp256k1
|
||||||
@@ -48,6 +49,7 @@ class NIP44v2Test {
|
|||||||
|
|
||||||
private val random = SecureRandom()
|
private val random = SecureRandom()
|
||||||
private val nip44v2 = Nip44v2(Secp256k1.get(), random)
|
private val nip44v2 = Nip44v2(Secp256k1.get(), random)
|
||||||
|
private val nip01 = Nip01(Secp256k1.get(), random)
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun conversationKeyTest() {
|
fun conversationKeyTest() {
|
||||||
@@ -67,6 +69,34 @@ class NIP44v2Test {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testCompressedWith02Keys() {
|
||||||
|
val privateKeyA = "f410f88bcec6cbfda04d6a273c7b1dd8bba144cd45b71e87109cfa11dd7ed561".hexToByteArray()
|
||||||
|
val privateKeyB = "65f039136f8da8d3e87b4818746b53318d5481e24b2673f162815144223a0b5a".hexToByteArray()
|
||||||
|
|
||||||
|
val publicKeyA = nip01.pubkeyCreate(privateKeyA)
|
||||||
|
val publicKeyB = nip01.pubkeyCreate(privateKeyB)
|
||||||
|
|
||||||
|
assertEquals(
|
||||||
|
nip44v2.getConversationKey(privateKeyA, publicKeyB).toHexKey(),
|
||||||
|
nip44v2.getConversationKey(privateKeyB, publicKeyA).toHexKey(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testCompressedWith03Keys() {
|
||||||
|
val privateKeyA = "f410f88bcec6cbfda04d6a273c7b1dd8bba144cd45b71e87109cfa11dd7ed561".hexToByteArray()
|
||||||
|
val privateKeyB = "e6159851715b4aa6190c22b899b0c792847de0a4435ac5b678f35738351c43b0".hexToByteArray()
|
||||||
|
|
||||||
|
val publicKeyA = nip01.pubkeyCreate(privateKeyA)
|
||||||
|
val publicKeyB = nip01.pubkeyCreate(privateKeyB)
|
||||||
|
|
||||||
|
assertEquals(
|
||||||
|
nip44v2.getConversationKey(privateKeyA, publicKeyB).toHexKey(),
|
||||||
|
nip44v2.getConversationKey(privateKeyB, publicKeyA).toHexKey(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun encryptDecryptTest() {
|
fun encryptDecryptTest() {
|
||||||
for (v in vectors.v2?.valid?.encryptDecrypt!!) {
|
for (v in vectors.v2?.valid?.encryptDecrypt!!) {
|
||||||
|
@@ -33,7 +33,9 @@ class Nip01(
|
|||||||
/** Provides a 32B "private key" aka random number */
|
/** Provides a 32B "private key" aka random number */
|
||||||
fun privkeyCreate() = random(32)
|
fun privkeyCreate() = random(32)
|
||||||
|
|
||||||
fun pubkeyCreate(privKey: ByteArray) = secp256k1.pubKeyCompress(secp256k1.pubkeyCreate(privKey)).copyOfRange(1, 33)
|
fun compressedPubkeyCreate(privKey: ByteArray) = secp256k1.pubKeyCompress(secp256k1.pubkeyCreate(privKey))
|
||||||
|
|
||||||
|
fun pubkeyCreate(privKey: ByteArray) = compressedPubkeyCreate(privKey).copyOfRange(1, 33)
|
||||||
|
|
||||||
fun sign(
|
fun sign(
|
||||||
data: ByteArray,
|
data: ByteArray,
|
||||||
|
Reference in New Issue
Block a user