Fix lnurlAuthPath by switching to bitcoin-kmp's int32 getter
@@ -2,6 +2,7 @@ package fr.acinq.phoenix.data
import fr.acinq.bitcoin.*
import fr.acinq.bitcoin.crypto.Digest
+import fr.acinq.bitcoin.crypto.Pack
import fr.acinq.bitcoin.crypto.hmac
import fr.acinq.secp256k1.Hex
import io.ktor.utils.io.bits.*
@@ -67,6 +68,7 @@ data class Wallet(val seed: ByteVector64, val chain: Chain) {
*
* Test vectors exist for path derivation.
*/
+ @ExperimentalUnsignedTypes
internal fun lnurlAuthPath(domain: String, hashingKey: ByteArray): KeyPath {
val fullHash = Digest.sha256().hmac(
key = hashingKey,
@@ -74,25 +76,13 @@ data class Wallet(val seed: ByteVector64, val chain: Chain) {
blockSize = 64
)
require(fullHash.size >= 16) { "domain hash must be at least 16 bytes" }
- val path1 = fullHash.sliceArray(IntRange(0, 3)).readUInt(0, ByteOrder.BIG_ENDIAN)
- val path2 = fullHash.sliceArray(IntRange(4, 7)).readUInt(0, ByteOrder.BIG_ENDIAN)
- val path3 = fullHash.sliceArray(IntRange(8, 11)).readUInt(0, ByteOrder.BIG_ENDIAN)
- val path4 = fullHash.sliceArray(IntRange(12, 15)).readUInt(0, ByteOrder.BIG_ENDIAN)
+ val path1 = fullHash.sliceArray(IntRange(0, 3)).let { Pack.int32BE(it, 0) }.toUInt()
+ val path2 = fullHash.sliceArray(IntRange(4, 7)).let { Pack.int32BE(it, 0) }.toUInt()
+ val path3 = fullHash.sliceArray(IntRange(8, 11)).let { Pack.int32BE(it, 0) }.toUInt()
+ val path4 = fullHash.sliceArray(IntRange(12, 15)).let { Pack.int32BE(it, 0) }.toUInt()
- return KeyPath("m/138'/${path1}/${path2}/${path3}/${path4}")
+ return KeyPath("m/138'/$path1/$path2/$path3/$path4")
}
override fun toString(): String = "Wallet"
}
-
-@OptIn(ExperimentalUnsignedTypes::class)
-fun ByteArray.readUInt(index: Int, order: ByteOrder): UInt {
- // According to the docs for `ByteArray.getXAt(index: Int)`:
- // > [These operations] extract primitive values out of the [ByteArray] byte buffers.
- // > Data is treated as if it was in Least-Significant-Byte first (little-endian) byte order.
- val littleEndian = this.get(index) as UInt // as getUIntAt(index)
- return when (order) {
- ByteOrder.LITTLE_ENDIAN -> littleEndian
- ByteOrder.BIG_ENDIAN -> littleEndian.reverseByteOrder()
- }
-}
