Fix out of bounds issues with lnd mappers (#4425)

This commit is contained in:
benthecarman 2022-06-25 07:27:54 -05:00 committed by GitHub
parent 0b9190f968
commit 828d03c727
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 3 deletions

View File

@ -1,8 +1,9 @@
package org.bitcoins.lnd.rpc
import lnrpc.Invoice.InvoiceState
import lnrpc.{HopHint, Invoice, RouteHint}
import org.bitcoins.core.currency.Satoshis
import org.bitcoins.core.number.UInt32
import org.bitcoins.core.number._
import org.bitcoins.core.protocol.ln.LnInvoice
import org.bitcoins.core.protocol.ln.currency._
import org.bitcoins.core.protocol.script.TaprootScriptPubKey
@ -143,4 +144,27 @@ class LndRpcClientTest extends LndFixture {
leases <- lnd.listLeases()
} yield assert(leases.isEmpty)
}
it must "correctly handle UInt64 & UInt32 converters" in { lnd =>
val hopHint = HopHint(
nodeId =
"037c862ec724bc85462aaeb804dd0941cd77dc4521cabd05edf3d0f23ed6b01f09",
chanId = UInt64.max - UInt64.twentyTwo,
feeBaseMsat = UInt32.zero,
feeProportionalMillionths = UInt32.max,
cltvExpiryDelta = UInt32.zero
)
val invoice = Invoice("memo",
value = 100000,
expiry = 1000,
routeHints = Vector(RouteHint(Vector(hopHint))))
for {
res <- lnd.addInvoice(invoice)
lookup <- lnd.lookupInvoice(res.rHash)
} yield {
assert(lookup.routeHints == invoice.routeHints)
}
}
}

View File

@ -14,6 +14,8 @@ import scalapb.TypeMapper
import scodec.bits._
import signrpc.TxOut
import java.lang.Long.toUnsignedString
import java.math.BigInteger
import scala.language.implicitConversions
trait LndUtils {
@ -102,10 +104,15 @@ trait LndUtils {
}
implicit val uint64Mapper: TypeMapper[Long, UInt64] =
TypeMapper[Long, UInt64](UInt64.apply)(_.toBigInt.longValue)
TypeMapper[Long, UInt64] { l =>
val bigInt = new BigInteger(toUnsignedString(l))
UInt64(bigInt)
}(_.toBigInt.longValue)
implicit val uint32Mapper: TypeMapper[Int, UInt32] =
TypeMapper[Int, UInt32](UInt32.apply)(_.toBigInt.intValue)
TypeMapper[Int, UInt32] { i =>
UInt32(Integer.toUnsignedLong(i))
}(_.toBigInt.intValue)
}
object LndUtils extends LndUtils