Skip to content

Commit

Permalink
Clean LnInvoiceUtils and add tests (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
davotoula authored Mar 11, 2024
1 parent ef7efa5 commit 908c5cc
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ object LnInvoiceUtils {
private fun decodeUnlimitedLength(invoice: String): Boolean {
var lower = false
var upper = false
for (i in 0 until invoice.length) {
for (i in invoice.indices) {
val c = invoice[i]
if (c.code < 33 || c.code > 126) {
throw AddressFormatException(
Expand All @@ -95,12 +95,12 @@ object LnInvoiceUtils {
val values = ByteArray(dataPartLength)
for (i in 0 until dataPartLength) {
val c = invoice[i + pos + 1]
if (CHARSET_REV.get(c.code).toInt() == -1) {
if (CHARSET_REV[c.code].toInt() == -1) {
throw AddressFormatException(
"Invalid character: " + c + ", pos: " + (i + pos + 1),
)
}
values[i] = CHARSET_REV.get(c.code)
values[i] = CHARSET_REV[c.code]
}
val hrp = invoice.substring(0, pos).lowercase(Locale.ROOT)
if (!verifyChecksum(hrp, values)) throw AddressFormatException("Invalid Checksum")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package net.primal.android.wallet.utils

import io.kotest.matchers.shouldBe
import org.junit.Test

class LnInvoiceUtilsTest {

private val validLnInvoice =
"lnbc123450n1pj7welppp53umfyxp6jn9uvkt463hydtq2zpfvz78hxhpfv9wqx6v4uwdw2rnqdzqg3hkuct5v56zu3n4dcsxgmmwv96x" +
"jmmwyp6x7gzqgpc8ymmrv4h8ghmrwfuhqar0cqzpgxqrrsssp5tntqjpngx6l8y9va9tzd7fmtemtyp5vvsqphw8f8yqjjrr26x5qs9" +
"qyyssqyyv7tqp5kpsmv6s5825kcq8fxsn4ag2h5uj2j6lnsnclyyq6844khayzqrl7yue46nwlukfr4uftqcwzxzh8krqg9rqsg9tg6x" +
"ggszcp0gyjcd"

private val invoiceInvalidCharacter =
"lnbc123450n1pj7welpp 53umfyxp6jn9uvkt463hydtq2zpfvz78hxhpfv9wqx6v4uwdw2rnqdzqg3hkuct5v56zu3n4dcsxgmmwv96x" +
"jmmwyp6x7gzqgpc8ymmrv4h8ghmrwfuhqar0cqzpgxqrrsssp5tntqjpngx6l8y9va9tzd7fmtemtyp5vvsqphw8f8yqjjrr26x5qs9" +
"qyyssqyyv7tqp5kpsmv6s5825kcq8fxsn4ag2h5uj2j6lnsnclyyq6844khayzqrl7yue46nwlukfr4uftqcwzxzh8krqg9rqsg9tg6" +
"xggszcp0gyjcd"

private val invalidLnInvoice =
"lnbc123450n1pj7welppp53umfyxp6jn9uvkt463hydtq2zpfvz78hxhpfv9wqx6v4uwdw2rnqdzqg3hkuct5v56zu3n4dcsxgmmwv96x" +
"jmmwyp6x7gzqgpc8ymmrv4h8ghmrwXuhqar0cqzpgxqrrsssp5tntqjpngx6l8y9va9tzd7fmtemtyp5vvsqphw8f8yqjjrr26x5qs9" +
"qyyssqyyv7tqp5kpsmv6s5825kcq8fxsn4ag2h5uj2j6lnsnclyyq6844khayzqrl7yue46nwlukfr4uftqcwzxzh8krqg9rqsg9tg6x" +
"ggszcp0gyjcd"

private val validLnInvoiceNoAmount =
"lnbc1pvjluezpp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqdpl2pkx2ctnv5sxxmmwwd5kgetjypeh2ursdae8g" +
"6twvus8g6rfwvs8qun0dfjkxaq8rkx3yf5tcsyz3d73gafnh3cax9rn449d9p5uxz9ezhhypd0elx87sjle52x86fux2ypatgddc6k6" +
"3n7erqz25le42c4u4ecky03ylcqca784w"

@Test
fun getAmountInSatsValidInvoice() {
val expectedValue = 12345

val amount = LnInvoiceUtils.getAmountInSats(validLnInvoice)

amount.toInt() shouldBe expectedValue

}

@Test
fun getAmountInSatsValidInvoiceNoAmount() {
val expectedValue = 0

val amount = LnInvoiceUtils.getAmountInSats(validLnInvoiceNoAmount)

amount.toInt() shouldBe expectedValue

}

@Test(expected = IllegalArgumentException::class)
fun getAmountInSatsInvalidInvoice() {

LnInvoiceUtils.getAmountInSats(invalidLnInvoice)

}

@Test(expected = IllegalArgumentException::class)
fun getAmountInSatsInvalidCharacter() {

LnInvoiceUtils.getAmountInSats(invoiceInvalidCharacter)

}
}

0 comments on commit 908c5cc

Please sign in to comment.