Speeds up LNInvoice parsing
This commit is contained in:
@@ -6,7 +6,7 @@ import java.util.regex.Pattern
|
|||||||
|
|
||||||
/** based on litecoinj */
|
/** based on litecoinj */
|
||||||
object LnInvoiceUtil {
|
object LnInvoiceUtil {
|
||||||
private val invoicePattern = Pattern.compile("lnbc((?<amount>\\d+)(?<multiplier>[munp])?)?1[^1\\s]+", Pattern.CASE_INSENSITIVE)
|
private val invoicePattern = Pattern.compile("lnbc((\\d+)([munp])?)?1[^1\\s]+", Pattern.CASE_INSENSITIVE)
|
||||||
|
|
||||||
/** The Bech32 character set for encoding. */
|
/** The Bech32 character set for encoding. */
|
||||||
private const val CHARSET = "qpzry9x8gf2tvdw0s3jn54khce6mua7l"
|
private const val CHARSET = "qpzry9x8gf2tvdw0s3jn54khce6mua7l"
|
||||||
@@ -107,8 +107,8 @@ object LnInvoiceUtil {
|
|||||||
|
|
||||||
val matcher = invoicePattern.matcher(invoice)
|
val matcher = invoicePattern.matcher(invoice)
|
||||||
require(matcher.matches()) { "Failed to match HRP pattern" }
|
require(matcher.matches()) { "Failed to match HRP pattern" }
|
||||||
val amountGroup = matcher.group("amount")
|
val amountGroup = matcher.group(2)
|
||||||
val multiplierGroup = matcher.group("multiplier")
|
val multiplierGroup = matcher.group(3)
|
||||||
if (amountGroup == null) {
|
if (amountGroup == null) {
|
||||||
return BigDecimal.ZERO
|
return BigDecimal.ZERO
|
||||||
}
|
}
|
||||||
@@ -120,16 +120,22 @@ object LnInvoiceUtil {
|
|||||||
return amount.multiply(multiplier(multiplierGroup))
|
return amount.multiply(multiplier(multiplierGroup))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val OneHundredK = BigDecimal(100000000)
|
||||||
|
val OneMili = BigDecimal("0.001")
|
||||||
|
val OneMicro = BigDecimal("0.000001")
|
||||||
|
val OneNano = BigDecimal("0.000000001")
|
||||||
|
val OnePico = BigDecimal("0.000000000001")
|
||||||
|
|
||||||
fun getAmountInSats(invoice: String): BigDecimal {
|
fun getAmountInSats(invoice: String): BigDecimal {
|
||||||
return getAmount(invoice).multiply(BigDecimal(100000000))
|
return getAmount(invoice).multiply(OneHundredK)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun multiplier(multiplier: String): BigDecimal {
|
private fun multiplier(multiplier: String): BigDecimal {
|
||||||
return when (multiplier.lowercase()) {
|
return when (multiplier.lowercase()) {
|
||||||
"m" -> BigDecimal("0.001")
|
"m" -> OneMili
|
||||||
"u" -> BigDecimal("0.000001")
|
"u" -> OneMicro
|
||||||
"n" -> BigDecimal("0.000000001")
|
"n" -> OneNano
|
||||||
"p" -> BigDecimal("0.000000000001")
|
"p" -> OnePico
|
||||||
else -> throw IllegalArgumentException("Invalid multiplier: $multiplier")
|
else -> throw IllegalArgumentException("Invalid multiplier: $multiplier")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user