Change Nip19 from class to object

This commit is contained in:
Chemaclass
2023-03-08 13:07:33 +01:00
parent 30fe861853
commit 49199fe892
9 changed files with 58 additions and 57 deletions

View File

@@ -20,8 +20,7 @@ data class ATag(val kind: Int, val pubKeyHex: String, val dTag: String, val rela
val dTag = dTag.toByteArray(Charsets.UTF_8)
val relay = relay?.toByteArray(Charsets.UTF_8)
var fullArray =
byteArrayOf(TlvTypes.SPECIAL.id, dTag.size.toByte()) + dTag
var fullArray = byteArrayOf(TlvTypes.SPECIAL.id, dTag.size.toByte()) + dTag
if (relay != null) {
fullArray = fullArray + byteArrayOf(TlvTypes.RELAY.id, relay.size.toByte()) + relay

View File

@@ -1,13 +1,9 @@
package com.vitorpamplona.amethyst.service
package com.vitorpamplona.amethyst.service.nip19
import com.vitorpamplona.amethyst.model.toHexKey
import com.vitorpamplona.amethyst.service.nip19.TlvTypes
import com.vitorpamplona.amethyst.service.nip19.parseTLV
import com.vitorpamplona.amethyst.service.nip19.toInt32
import nostr.postr.bechToBytes
class Nip19 {
object Nip19 {
enum class Type {
USER, NOTE, RELAY, ADDRESS
}

View File

@@ -16,7 +16,7 @@ import coil.decode.ImageDecoderDecoder
import coil.decode.SvgDecoder
import com.vitorpamplona.amethyst.LocalPreferences
import com.vitorpamplona.amethyst.ServiceManager
import com.vitorpamplona.amethyst.service.Nip19
import com.vitorpamplona.amethyst.service.nip19.Nip19
import com.vitorpamplona.amethyst.service.relays.Client
import com.vitorpamplona.amethyst.ui.screen.AccountScreen
import com.vitorpamplona.amethyst.ui.screen.AccountStateViewModel
@@ -26,7 +26,7 @@ class MainActivity : FragmentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val nip19 = Nip19().uriToRoute(intent?.data?.toString())
val nip19 = Nip19.uriToRoute(intent?.data?.toString())
val startingPage = when (nip19?.type) {
Nip19.Type.USER -> "User/${nip19.hex}"
Nip19.Type.NOTE -> "Note/${nip19.hex}"

View File

@@ -10,8 +10,8 @@ import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.text.AnnotatedString
import androidx.navigation.NavController
import com.vitorpamplona.amethyst.model.LocalCache
import com.vitorpamplona.amethyst.service.Nip19
import com.vitorpamplona.amethyst.service.model.ChannelCreateEvent
import com.vitorpamplona.amethyst.service.nip19.Nip19
@Composable
fun ClickableRoute(

View File

@@ -12,9 +12,7 @@ import androidx.compose.material.LocalTextStyle
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
@@ -34,8 +32,8 @@ import com.halilibo.richtext.ui.RichTextStyle
import com.halilibo.richtext.ui.material.MaterialRichText
import com.halilibo.richtext.ui.resolveDefaults
import com.vitorpamplona.amethyst.model.LocalCache
import com.vitorpamplona.amethyst.service.Nip19
import com.vitorpamplona.amethyst.service.lnurl.LnInvoiceUtil
import com.vitorpamplona.amethyst.service.nip19.Nip19
import com.vitorpamplona.amethyst.ui.note.NoteCompose
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
import java.net.MalformedURLException
@@ -210,7 +208,7 @@ fun BechLink(word: String, navController: NavController) {
}
val nip19Route = try {
Nip19().uriToRoute(uri)
Nip19.uriToRoute(uri)
} catch (e: Exception) {
null
}

View File

@@ -21,7 +21,7 @@ import com.google.mlkit.vision.barcode.BarcodeScannerOptions
import com.google.mlkit.vision.barcode.BarcodeScanning
import com.google.mlkit.vision.barcode.common.Barcode
import com.google.mlkit.vision.common.InputImage
import com.vitorpamplona.amethyst.service.Nip19
import com.vitorpamplona.amethyst.service.nip19.Nip19
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
@@ -52,7 +52,7 @@ fun QrCodeScanner(onScan: (String) -> Unit) {
val analyzer = QRCodeAnalyzer { result ->
result?.let {
try {
val nip19 = Nip19().uriToRoute(it)
val nip19 = Nip19.uriToRoute(it)
val startingPage = when (nip19?.type) {
Nip19.Type.USER -> "User/${nip19.hex}"
Nip19.Type.NOTE -> "Note/${nip19.hex}"

View File

@@ -1,26 +1,26 @@
package com.vitorpamplona.amethyst
import com.vitorpamplona.amethyst.service.Nip19
import com.vitorpamplona.amethyst.service.model.ATag
import com.vitorpamplona.amethyst.service.nip19.Nip19
import org.junit.Assert.assertEquals
import org.junit.Test
class NIP19ParserTest {
@Test
fun nAddrParser() {
val result = Nip19().uriToRoute("nostr:naddr1qqqqygzxpsj7dqha57pjk5k37gkn6g4nzakewtmqmnwryyhd3jfwlpgxtspsgqqqw4rs3xyxus")
val result = Nip19.uriToRoute("nostr:naddr1qqqqygzxpsj7dqha57pjk5k37gkn6g4nzakewtmqmnwryyhd3jfwlpgxtspsgqqqw4rs3xyxus")
assertEquals("30023:460c25e682fda7832b52d1f22d3d22b3176d972f60dcdc3212ed8c92ef85065c:", result?.hex)
}
@Test
fun nAddrParser2() {
val result = Nip19().uriToRoute("nostr:naddr1qq8kwatfv3jj6amfwfjkwatpwfjqygxsm6lelvfda7qlg0tud9pfhduysy4vrexj65azqtdk4tr75j6xdspsgqqqw4rsg32ag8")
val result = Nip19.uriToRoute("nostr:naddr1qq8kwatfv3jj6amfwfjkwatpwfjqygxsm6lelvfda7qlg0tud9pfhduysy4vrexj65azqtdk4tr75j6xdspsgqqqw4rsg32ag8")
assertEquals("30023:d0debf9fb12def81f43d7c69429bb784812ac1e4d2d53a202db6aac7ea4b466c:guide-wireguard", result?.hex)
}
@Test
fun nAddrParse3() {
val result = Nip19().uriToRoute("naddr1qqyrswtyv5mnjv3sqy28wumn8ghj7un9d3shjtnyv9kh2uewd9hsygx3uczxts4hwue9ayfn7ggq62anzstde2qs749pm9tx2csuthhpjvpsgqqqw4rs8pmj38")
val result = Nip19.uriToRoute("naddr1qqyrswtyv5mnjv3sqy28wumn8ghj7un9d3shjtnyv9kh2uewd9hsygx3uczxts4hwue9ayfn7ggq62anzstde2qs749pm9tx2csuthhpjvpsgqqqw4rs8pmj38")
assertEquals(Nip19.Type.ADDRESS, result?.type)
assertEquals("30023:d1e60465c2b777325e9133f2100d2bb31416dca810f54a1d95665621c5dee193:89de7920", result?.hex)
assertEquals("wss://relay.damus.io", result?.relay)

View File

@@ -1,46 +1,21 @@
package com.vitorpamplona.amethyst.service
import com.vitorpamplona.amethyst.service.nip19.Nip19
import org.junit.Assert
import org.junit.Ignore
import org.junit.Test
class Nip19Test {
private val nip19 = Nip19()
@Test(expected = IllegalArgumentException::class)
fun to_int_32_length_smaller_than_4() {
toInt32(byteArrayOfInts(1, 2, 3))
}
@Test(expected = IllegalArgumentException::class)
fun to_int_32_length_bigger_than_4() {
toInt32(byteArrayOfInts(1, 2, 3, 4, 5))
}
@Test()
fun to_int_32_length_4() {
val actual = toInt32(byteArrayOfInts(1, 2, 3, 4))
Assert.assertEquals(16909060, actual)
}
@Ignore("Test not implemented yet")
@Test()
fun parse_TLV() {
// TODO: I don't know how to test this (?)
}
@Test()
fun uri_to_route_null() {
val actual = nip19.uriToRoute(null)
val actual = Nip19.uriToRoute(null)
Assert.assertEquals(null, actual)
}
@Test()
fun uri_to_route_unknown() {
val actual = nip19.uriToRoute("nostr:unknown")
val actual = Nip19.uriToRoute("nostr:unknown")
Assert.assertEquals(null, actual)
}
@@ -48,7 +23,7 @@ class Nip19Test {
@Test()
fun uri_to_route_npub() {
val actual =
nip19.uriToRoute("nostr:npub1hv7k2s755n697sptva8vkh9jz40lzfzklnwj6ekewfmxp5crwdjs27007y")
Nip19.uriToRoute("nostr:npub1hv7k2s755n697sptva8vkh9jz40lzfzklnwj6ekewfmxp5crwdjs27007y")
Assert.assertEquals(Nip19.Type.USER, actual?.type)
Assert.assertEquals(
@@ -60,7 +35,7 @@ class Nip19Test {
@Test()
fun uri_to_route_note() {
val actual =
nip19.uriToRoute("nostr:note1stqea6wmwezg9x6yyr6qkukw95ewtdukyaztycws65l8wppjmtpscawevv")
Nip19.uriToRoute("nostr:note1stqea6wmwezg9x6yyr6qkukw95ewtdukyaztycws65l8wppjmtpscawevv")
Assert.assertEquals(Nip19.Type.NOTE, actual?.type)
Assert.assertEquals(
@@ -72,7 +47,7 @@ class Nip19Test {
@Ignore("Test not implemented yet")
@Test()
fun uri_to_route_nprofile() {
val actual = nip19.uriToRoute("nostr:nprofile")
val actual = Nip19.uriToRoute("nostr:nprofile")
Assert.assertEquals(Nip19.Type.USER, actual?.type)
Assert.assertEquals("*", actual?.hex)
@@ -81,7 +56,7 @@ class Nip19Test {
@Ignore("Test not implemented yet")
@Test()
fun uri_to_route_nevent() {
val actual = nip19.uriToRoute("nostr:nevent")
val actual = Nip19.uriToRoute("nostr:nevent")
Assert.assertEquals(Nip19.Type.USER, actual?.type)
Assert.assertEquals("*", actual?.hex)
@@ -90,7 +65,7 @@ class Nip19Test {
@Ignore("Test not implemented yet")
@Test()
fun uri_to_route_nrelay() {
val actual = nip19.uriToRoute("nostr:nrelay")
val actual = Nip19.uriToRoute("nostr:nrelay")
Assert.assertEquals(Nip19.Type.RELAY, actual?.type)
Assert.assertEquals("*", actual?.hex)
@@ -99,11 +74,9 @@ class Nip19Test {
@Ignore("Test not implemented yet")
@Test()
fun uri_to_route_naddr() {
val actual = nip19.uriToRoute("nostr:naddr")
val actual = Nip19.uriToRoute("nostr:naddr")
Assert.assertEquals(Nip19.Type.ADDRESS, actual?.type)
Assert.assertEquals("*", actual?.hex)
}
private fun byteArrayOfInts(vararg ints: Int) = ByteArray(ints.size) { pos -> ints[pos].toByte() }
}

View File

@@ -0,0 +1,35 @@
package com.vitorpamplona.amethyst.service
import com.vitorpamplona.amethyst.service.nip19.toInt32
import org.junit.Assert
import org.junit.Ignore
import org.junit.Test
class UtilsTest {
@Test(expected = IllegalArgumentException::class)
fun to_int_32_length_smaller_than_4() {
toInt32(byteArrayOfInts(1, 2, 3))
}
@Test(expected = IllegalArgumentException::class)
fun to_int_32_length_bigger_than_4() {
toInt32(byteArrayOfInts(1, 2, 3, 4, 5))
}
@Test()
fun to_int_32_length_4() {
val actual = toInt32(byteArrayOfInts(1, 2, 3, 4))
Assert.assertEquals(16909060, actual)
}
@Ignore("Test not implemented yet")
@Test()
fun parse_TLV() {
// TODO: I don't know how to test this (?)
}
private fun byteArrayOfInts(vararg ints: Int) =
ByteArray(ints.size) { pos -> ints[pos].toByte() }
}