Skip to content

Commit

Permalink
replace String length with indices
Browse files Browse the repository at this point in the history
use indexing operator instead of get call
added tests
  • Loading branch information
davotoula committed Mar 8, 2024
1 parent 037f947 commit 67c6d88
Show file tree
Hide file tree
Showing 2 changed files with 63 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,60 @@
package net.primal.android.wallet.utils

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

class LnInvoiceUtilsTest {

private val validLnInvoice = "lnbc123450n1pj7welppp53umfyxp6jn9uvkt463hydtq2zpfvz78hxhpfv9wqx6v4uwdw2rnqdzqg3hkuct5v56zu" +
"3n4dcsxgmmwv96xjmmwyp6x7gzqgpc8ymmrv4h8ghmrwfuhqar0cqzpgxqrrsssp5tntqjpngx6l8y9va9tzd7fmtemtyp5vvsqphw8f8yqjjr" +
"r26x5qs9qyyssqyyv7tqp5kpsmv6s5825kcq8fxsn4ag2h5uj2j6lnsnclyyq6844khayzqrl7yue46nwlukfr4uftqcwzxzh8krqg9rqsg9tg6x" +
"ggszcp0gyjcd"

private val invoiceInvalidCharacter = "lnbc123450n1pj7welpp 53umfyxp6jn9uvkt463hydtq2zpfvz78hxhpfv9wqx6v4uwdw2rnqdzqg3hkuct5v56zu" +
"3n4dcsxgmmwv96xjmmwyp6x7gzqgpc8ymmrv4h8ghmrwfuhqar0cqzpgxqrrsssp5tntqjpngx6l8y9va9tzd7fmtemtyp5vvsqphw8f8yqjjr" +
"r26x5qs9qyyssqyyv7tqp5kpsmv6s5825kcq8fxsn4ag2h5uj2j6lnsnclyyq6844khayzqrl7yue46nwlukfr4uftqcwzxzh8krqg9rqsg9tg6x" +
"ggszcp0gyjcd"

private val invalidLnInvoice = "lnbc123450n1pj7welppp53umfyxp6jn9uvkt463hydtq2zpfvz78hxhpfv9wqx6v4uwdw2rnqdzqg3hkuct5v56zu" +
"3n4dcsxgmmwv96xjmmwyp6x7gzqgpc8ymmrv4h8ghmrwXuhqar0cqzpgxqrrsssp5tntqjpngx6l8y9va9tzd7fmtemtyp5vvsqphw8f8yqjjr" +
"r26x5qs9qyyssqyyv7tqp5kpsmv6s5825kcq8fxsn4ag2h5uj2j6lnsnclyyq6844khayzqrl7yue46nwlukfr4uftqcwzxzh8krqg9rqsg9tg6x" +
"ggszcp0gyjcd"

private val validLnInvoiceNoAmount = "lnbc1pvjluezpp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqy" +
"pqdpl2pkx2ctnv5sxxmmwwd5kgetjypeh2ursdae8g6twvus8g6rfwvs8qun0dfjkxaq8rkx3yf5tcsyz3d73gafn" +
"h3cax9rn449d9p5uxz9ezhhypd0elx87sjle52x86fux2ypatgddc6k63n7erqz25le42c4u4ecky03ylcqca784w"

@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 67c6d88

Please sign in to comment.