mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-09-20 14:01:22 +02:00
Extract from Nip19 global funcs into another script
This commit is contained in:
@@ -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
|
|
||||||
}
|
|
||||||
|
@@ -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
|
||||||
|
@@ -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
|
||||||
|
}
|
Reference in New Issue
Block a user