Extract from Nip19 global funcs into another script

This commit is contained in:
Chemaclass
2023-03-08 12:58:28 +01:00
parent 1998711e60
commit 30fe861853
3 changed files with 31 additions and 27 deletions

View File

@@ -2,9 +2,9 @@ package com.vitorpamplona.amethyst.service
import com.vitorpamplona.amethyst.model.toHexKey import com.vitorpamplona.amethyst.model.toHexKey
import com.vitorpamplona.amethyst.service.nip19.TlvTypes 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 import nostr.postr.bechToBytes
import java.nio.ByteBuffer
import java.nio.ByteOrder
class Nip19 { class Nip19 {
@@ -106,26 +106,3 @@ class Nip19 {
return Return(Type.ADDRESS, "$kind:$author:$d", relay) return Return(Type.ADDRESS, "$kind:$author:$d", relay)
} }
} }
fun toInt32(bytes: ByteArray): Int {
require(bytes.size == 4) { "length must be 4, got: ${bytes.size}" }
return ByteBuffer.wrap(bytes, 0, 4).order(ByteOrder.BIG_ENDIAN).int
}
fun parseTLV(data: ByteArray): Map<Byte, List<ByteArray>> {
val result = mutableMapOf<Byte, MutableList<ByteArray>>()
var rest = data
while (rest.isNotEmpty()) {
val t = rest[0]
val l = rest[1]
val v = rest.sliceArray(IntRange(2, (2 + l) - 1))
rest = rest.sliceArray(IntRange(2 + l, rest.size - 1))
if (v.size < l) continue
if (!result.containsKey(t)) {
result[t] = mutableListOf()
}
result[t]?.add(v)
}
return result
}

View File

@@ -4,8 +4,8 @@ import android.util.Log
import com.vitorpamplona.amethyst.model.toByteArray import com.vitorpamplona.amethyst.model.toByteArray
import com.vitorpamplona.amethyst.model.toHexKey import com.vitorpamplona.amethyst.model.toHexKey
import com.vitorpamplona.amethyst.service.nip19.TlvTypes import com.vitorpamplona.amethyst.service.nip19.TlvTypes
import com.vitorpamplona.amethyst.service.parseTLV import com.vitorpamplona.amethyst.service.nip19.parseTLV
import com.vitorpamplona.amethyst.service.toInt32 import com.vitorpamplona.amethyst.service.nip19.toInt32
import fr.acinq.secp256k1.Hex import fr.acinq.secp256k1.Hex
import nostr.postr.Bech32 import nostr.postr.Bech32
import nostr.postr.bechToBytes import nostr.postr.bechToBytes

View File

@@ -0,0 +1,27 @@
package com.vitorpamplona.amethyst.service.nip19
import java.nio.ByteBuffer
import java.nio.ByteOrder
fun toInt32(bytes: ByteArray): Int {
require(bytes.size == 4) { "length must be 4, got: ${bytes.size}" }
return ByteBuffer.wrap(bytes, 0, 4).order(ByteOrder.BIG_ENDIAN).int
}
fun parseTLV(data: ByteArray): Map<Byte, List<ByteArray>> {
val result = mutableMapOf<Byte, MutableList<ByteArray>>()
var rest = data
while (rest.isNotEmpty()) {
val t = rest[0]
val l = rest[1]
val v = rest.sliceArray(IntRange(2, (2 + l) - 1))
rest = rest.sliceArray(IntRange(2 + l, rest.size - 1))
if (v.size < l) continue
if (!result.containsKey(t)) {
result[t] = mutableListOf()
}
result[t]?.add(v)
}
return result
}