Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleaner code and added tests #107

Merged
merged 2 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)

}
}
Loading