From e83cfe4f0725721e6f67624d37c73b0ae8bd24ea Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Mon, 24 Jul 2023 11:47:25 -0400 Subject: [PATCH] Removes dirty word parser from HexUtils and puts into the New Message Processor. --- .../com/vitorpamplona/amethyst/model/Hex.kt | 118 ------------------ .../vitorpamplona/amethyst/model/HexUtils.kt | 52 ++++++++ .../amethyst/ui/actions/NewMessageTagger.kt | 55 +++++++- 3 files changed, 106 insertions(+), 119 deletions(-) delete mode 100644 app/src/main/java/com/vitorpamplona/amethyst/model/Hex.kt create mode 100644 app/src/main/java/com/vitorpamplona/amethyst/model/HexUtils.kt diff --git a/app/src/main/java/com/vitorpamplona/amethyst/model/Hex.kt b/app/src/main/java/com/vitorpamplona/amethyst/model/Hex.kt deleted file mode 100644 index 0e96daa80..000000000 --- a/app/src/main/java/com/vitorpamplona/amethyst/model/Hex.kt +++ /dev/null @@ -1,118 +0,0 @@ -package com.vitorpamplona.amethyst.model - -import com.vitorpamplona.amethyst.service.Bech32 -import com.vitorpamplona.amethyst.service.KeyPair -import com.vitorpamplona.amethyst.service.bechToBytes -import com.vitorpamplona.amethyst.service.nip19.Nip19 -import com.vitorpamplona.amethyst.service.toNpub -import com.vitorpamplona.amethyst.ui.note.toShortenHex -import fr.acinq.secp256k1.Hex - -/** Makes the distinction between String and Hex **/ -typealias HexKey = String - -typealias NPubKey = String -typealias NoteId = String - -fun NPubKey.toDisplayKey(): String { - return this.toShortenHex() -} - -fun NoteId.toDisplayId(): String { - return this.toShortenHex() -} - -fun ByteArray.toNote() = Bech32.encodeBytes(hrp = "note", this, Bech32.Encoding.Bech32) - -fun ByteArray.toHexKey(): HexKey { - return Hex.encode(this) -} - -fun HexKey.hexToByteArray(): ByteArray { - return Hex.decode(this) -} - -fun HexKey.toDisplayHexKey(): String { - return this.toShortenHex() -} - -fun decodePublicKey(key: String): ByteArray { - val parsed = Nip19.uriToRoute(key) - val pubKeyParsed = parsed?.hex?.hexToByteArray() - - return if (key.startsWith("nsec")) { - KeyPair(privKey = key.bechToBytes()).pubKey - } else if (pubKeyParsed != null) { - pubKeyParsed - } else { - Hex.decode(key) - } -} - -fun decodePublicKeyAsHexOrNull(key: String): HexKey? { - return try { - val parsed = Nip19.uriToRoute(key) - val pubKeyParsed = parsed?.hex - - if (key.startsWith("nsec")) { - KeyPair(privKey = key.bechToBytes()).pubKey.toHexKey() - } else if (pubKeyParsed != null) { - pubKeyParsed - } else { - Hex.decode(key).toHexKey() - } - } catch (e: Exception) { - null - } -} - -data class DirtyKeyInfo(val key: Nip19.Return, val restOfWord: String) - -fun parseDirtyWordForKey(mightBeAKey: String): DirtyKeyInfo? { - var key = mightBeAKey - if (key.startsWith("nostr:", true)) { - key = key.substring("nostr:".length) - } - - key = key.removePrefix("@") - - if (key.length < 63) { - return null - } - - try { - val keyB32 = key.substring(0, 63) - val restOfWord = key.substring(63) - - if (key.startsWith("nsec1", true)) { - // Converts to npub - val pubkey = Nip19.uriToRoute(KeyPair(privKey = keyB32.bechToBytes()).pubKey.toNpub()) ?: return null - - return DirtyKeyInfo(pubkey, restOfWord) - } else if (key.startsWith("npub1", true)) { - val pubkey = Nip19.uriToRoute(keyB32) ?: return null - - return DirtyKeyInfo(pubkey, restOfWord) - } else if (key.startsWith("note1", true)) { - val noteId = Nip19.uriToRoute(keyB32) ?: return null - - return DirtyKeyInfo(noteId, restOfWord) - } else if (key.startsWith("nprofile", true)) { - val pubkeyRelay = Nip19.uriToRoute(keyB32 + restOfWord) ?: return null - - return DirtyKeyInfo(pubkeyRelay, pubkeyRelay.additionalChars) - } else if (key.startsWith("nevent1", true)) { - val noteRelayId = Nip19.uriToRoute(keyB32 + restOfWord) ?: return null - - return DirtyKeyInfo(noteRelayId, noteRelayId.additionalChars) - } else if (key.startsWith("naddr1", true)) { - val address = Nip19.uriToRoute(keyB32 + restOfWord) ?: return null - - return DirtyKeyInfo(address, address.additionalChars) // no way to know when they address ends and dirt begins - } - } catch (e: Exception) { - e.printStackTrace() - } - - return null -} diff --git a/app/src/main/java/com/vitorpamplona/amethyst/model/HexUtils.kt b/app/src/main/java/com/vitorpamplona/amethyst/model/HexUtils.kt new file mode 100644 index 000000000..0f5a13027 --- /dev/null +++ b/app/src/main/java/com/vitorpamplona/amethyst/model/HexUtils.kt @@ -0,0 +1,52 @@ +package com.vitorpamplona.amethyst.model + +import com.vitorpamplona.amethyst.service.KeyPair +import com.vitorpamplona.amethyst.service.bechToBytes +import com.vitorpamplona.amethyst.service.nip19.Nip19 +import com.vitorpamplona.amethyst.ui.note.toShortenHex +import fr.acinq.secp256k1.Hex + +/** Makes the distinction between String and Hex **/ +typealias HexKey = String + +fun ByteArray.toHexKey(): HexKey { + return Hex.encode(this) +} + +fun HexKey.hexToByteArray(): ByteArray { + return Hex.decode(this) +} + +fun HexKey.toDisplayHexKey(): String { + return this.toShortenHex() +} + +fun decodePublicKey(key: String): ByteArray { + val parsed = Nip19.uriToRoute(key) + val pubKeyParsed = parsed?.hex?.hexToByteArray() + + return if (key.startsWith("nsec")) { + KeyPair(privKey = key.bechToBytes()).pubKey + } else if (pubKeyParsed != null) { + pubKeyParsed + } else { + Hex.decode(key) + } +} + +fun decodePublicKeyAsHexOrNull(key: String): HexKey? { + return try { + val parsed = Nip19.uriToRoute(key) + val pubKeyParsed = parsed?.hex + + if (key.startsWith("nsec")) { + KeyPair(privKey = key.bechToBytes()).pubKey.toHexKey() + } else if (pubKeyParsed != null) { + pubKeyParsed + } else { + Hex.decode(key).toHexKey() + } + } catch (e: Exception) { + null + } +} diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewMessageTagger.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewMessageTagger.kt index b298169ef..775ea94d1 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewMessageTagger.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewMessageTagger.kt @@ -4,8 +4,10 @@ import com.vitorpamplona.amethyst.model.HexKey import com.vitorpamplona.amethyst.model.LocalCache import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.User -import com.vitorpamplona.amethyst.model.parseDirtyWordForKey +import com.vitorpamplona.amethyst.service.KeyPair +import com.vitorpamplona.amethyst.service.bechToBytes import com.vitorpamplona.amethyst.service.nip19.Nip19 +import com.vitorpamplona.amethyst.service.toNpub class NewMessageTagger(var channelHex: String?, var mentions: List?, var replyTos: List?, var message: String) { @@ -83,4 +85,55 @@ class NewMessageTagger(var channelHex: String?, var mentions: List?, var r }.joinToString(" ") }.joinToString("\n") } + + data class DirtyKeyInfo(val key: Nip19.Return, val restOfWord: String) + + fun parseDirtyWordForKey(mightBeAKey: String): DirtyKeyInfo? { + var key = mightBeAKey + if (key.startsWith("nostr:", true)) { + key = key.substring("nostr:".length) + } + + key = key.removePrefix("@") + + if (key.length < 63) { + return null + } + + try { + val keyB32 = key.substring(0, 63) + val restOfWord = key.substring(63) + + if (key.startsWith("nsec1", true)) { + // Converts to npub + val pubkey = Nip19.uriToRoute(KeyPair(privKey = keyB32.bechToBytes()).pubKey.toNpub()) ?: return null + + return DirtyKeyInfo(pubkey, restOfWord) + } else if (key.startsWith("npub1", true)) { + val pubkey = Nip19.uriToRoute(keyB32) ?: return null + + return DirtyKeyInfo(pubkey, restOfWord) + } else if (key.startsWith("note1", true)) { + val noteId = Nip19.uriToRoute(keyB32) ?: return null + + return DirtyKeyInfo(noteId, restOfWord) + } else if (key.startsWith("nprofile", true)) { + val pubkeyRelay = Nip19.uriToRoute(keyB32 + restOfWord) ?: return null + + return DirtyKeyInfo(pubkeyRelay, pubkeyRelay.additionalChars) + } else if (key.startsWith("nevent1", true)) { + val noteRelayId = Nip19.uriToRoute(keyB32 + restOfWord) ?: return null + + return DirtyKeyInfo(noteRelayId, noteRelayId.additionalChars) + } else if (key.startsWith("naddr1", true)) { + val address = Nip19.uriToRoute(keyB32 + restOfWord) ?: return null + + return DirtyKeyInfo(address, address.additionalChars) // no way to know when they address ends and dirt begins + } + } catch (e: Exception) { + e.printStackTrace() + } + + return null + } }