diff --git a/README.md b/README.md index dc5c204..ee2c290 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ You can include the library from either Maven Central or Jitpack. You can include the library in the common source set like this: ```kotlin dependencies { - implementation("io.github.kotlingeekdev:rhodium:1.0-beta-13") + implementation("io.github.kotlingeekdev:rhodium:1.0-beta-14") } ``` @@ -74,7 +74,7 @@ then, in your module's `build.gradle(.kts)`, you need to add: // build.gradle.kts dependencies { //... - implementation("com.github.KotlinGeekDev.Rhodium:rhodium:1.0-beta-13") + implementation("com.github.KotlinGeekDev.Rhodium:rhodium:1.0-beta-14") } @@ -85,7 +85,7 @@ If you're including it in an Android app, you can just add: // app/build.gradle.kts dependencies { //... - implementation("com.github.KotlinGeekDev.Rhodium:rhodium-android:1.0-beta-13") + implementation("com.github.KotlinGeekDev.Rhodium:rhodium-android:1.0-beta-14") } ``` diff --git a/build.gradle.kts b/build.gradle.kts index 9269e06..c7e0273 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -31,7 +31,7 @@ allprojects { val isJitpack = System.getenv("JITPACK") == "true" group = "io.github.kotlingeekdev" - version = "1.0-beta-13" + version = "1.0-beta-14" // val javadocJar = tasks.register("javadocJar") { diff --git a/rhodium-core/src/commonMain/kotlin/rhodium/net/NostrService.kt b/rhodium-core/src/commonMain/kotlin/rhodium/net/NostrService.kt index 4f8f93b..c564016 100644 --- a/rhodium-core/src/commonMain/kotlin/rhodium/net/NostrService.kt +++ b/rhodium-core/src/commonMain/kotlin/rhodium/net/NostrService.kt @@ -14,6 +14,7 @@ import rhodium.logging.serviceLogger import rhodium.nostr.* import rhodium.nostr.client.ClientMessage import rhodium.nostr.client.RequestMessage +import rhodium.nostr.events.MetadataEvent import rhodium.nostr.relay.* import kotlin.coroutines.CoroutineContext @@ -198,6 +199,7 @@ class NostrService( is RelayNotice -> { serviceLogger.i("Received a relay notice: $message") + if (message.message.contains("ERROR")) break } } @@ -229,7 +231,7 @@ class NostrService( return results } - suspend fun getMetadataFor(profileHex: String, preferredRelays: List): Event { + suspend fun getMetadataFor(profileHex: String, preferredRelays: List): MetadataEvent { val profileRequest = RequestMessage.singleFilterRequest( filter = NostrFilter.newFilter() .kinds(EventKind.METADATA.kind) @@ -241,6 +243,7 @@ class NostrService( requestWithResult(profileRequest) else requestWithResult(profileRequest, preferredRelays.map { Relay(it) }) return potentialResults.maxBy { it.creationDate } + .let { MetadataEvent(it.id, it.pubkey, it.creationDate, it.tags, it.content, it.eventSignature) } } suspend fun fetchRelayListFor(profileHex: String, fetchRelays: List): List { diff --git a/rhodium-core/src/commonMain/kotlin/rhodium/nostr/Event.kt b/rhodium-core/src/commonMain/kotlin/rhodium/nostr/Event.kt index 2244ecd..9ebe7fc 100644 --- a/rhodium-core/src/commonMain/kotlin/rhodium/nostr/Event.kt +++ b/rhodium-core/src/commonMain/kotlin/rhodium/nostr/Event.kt @@ -1,13 +1,13 @@ package rhodium.nostr -import rhodium.crypto.CryptoUtils -import rhodium.crypto.toHexString import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable import kotlinx.serialization.builtins.ListSerializer import kotlinx.serialization.json.add import kotlinx.serialization.json.buildJsonArray +import rhodium.crypto.CryptoUtils +import rhodium.crypto.toHexString /** * The Event class representing the Nostr Event. @@ -22,7 +22,7 @@ import kotlinx.serialization.json.buildJsonArray * @param eventSignature The event's signature, as a 64-byte string */ @Serializable -data class Event( +open class Event( val id: String, val pubkey: String, @SerialName("created_at") val creationDate: Long, @@ -30,7 +30,10 @@ data class Event( val tags: List, val content: String, @SerialName("sig") val eventSignature: String -) +){ + override fun toString(): String = + "Event(id=$id, pubkey=$pubkey, creationDate=$creationDate, eventKind=$eventKind, tags=$tags, content=$content, eventSignature=$eventSignature)" +} diff --git a/rhodium-core/src/commonMain/kotlin/rhodium/nostr/EventExt.kt b/rhodium-core/src/commonMain/kotlin/rhodium/nostr/EventExt.kt index d0e57d3..6263782 100644 --- a/rhodium-core/src/commonMain/kotlin/rhodium/nostr/EventExt.kt +++ b/rhodium-core/src/commonMain/kotlin/rhodium/nostr/EventExt.kt @@ -12,7 +12,7 @@ import kotlinx.serialization.encoding.Encoder import kotlinx.serialization.json.Json import rhodium.crypto.CryptoUtils -internal val eventMapper = Json +internal val eventMapper = Json { ignoreUnknownKeys = true } @OptIn(ExperimentalSerializationApi::class) internal val arraySerializer: KSerializer> = ArraySerializer(String.serializer()) diff --git a/rhodium-core/src/commonMain/kotlin/rhodium/nostr/client/ClientMessage.kt b/rhodium-core/src/commonMain/kotlin/rhodium/nostr/client/ClientMessage.kt index 27a4913..98e2e80 100644 --- a/rhodium-core/src/commonMain/kotlin/rhodium/nostr/client/ClientMessage.kt +++ b/rhodium-core/src/commonMain/kotlin/rhodium/nostr/client/ClientMessage.kt @@ -12,7 +12,6 @@ import kotlinx.serialization.descriptors.SerialDescriptor import kotlinx.serialization.encoding.Decoder import kotlinx.serialization.encoding.Encoder import kotlinx.serialization.json.* -import rhodium.crypto.toHexString import rhodium.nostr.Event import rhodium.nostr.NostrFilter @@ -115,7 +114,7 @@ open class RequestMessage( companion object { fun singleFilterRequest( - subscriptionId: String = uuid4().bytes.toHexString().substring(0, 5), + subscriptionId: String = uuid4().bytes.decodeToString().substring(0, 5), filter: NostrFilter ): RequestMessage { return RequestMessage(messageType = "REQ", subscriptionId, listOf(filter)) diff --git a/rhodium-core/src/commonMain/kotlin/rhodium/nostr/events/MetadataEvent.kt b/rhodium-core/src/commonMain/kotlin/rhodium/nostr/events/MetadataEvent.kt new file mode 100644 index 0000000..b632888 --- /dev/null +++ b/rhodium-core/src/commonMain/kotlin/rhodium/nostr/events/MetadataEvent.kt @@ -0,0 +1,33 @@ +package rhodium.nostr.events + +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable +import rhodium.nostr.Event +import rhodium.nostr.EventKind +import rhodium.nostr.Tag +import rhodium.nostr.eventMapper + + +class MetadataEvent( + id: String, + pubkey: String, + creationDate: Long, + tags: List, + content: String, + signature: String +): Event(id, pubkey, creationDate, eventKind = EventKind.METADATA.kind, tags, content, signature) { + + fun userInfo(): UserInfo = eventMapper.decodeFromString(content) + +} + +@Serializable() +data class UserInfo( + val name: String, + @SerialName("display_name") val displayName: String? = null, + val about: String?, + val picture: String? = null, + val banner: String? = null, + @SerialName("nip05") val address: String? = null, + val website: String? = null, +) \ No newline at end of file