Skip to content

Commit

Permalink
Merge pull request #156 from lightsparkdev/fix/64bitnonce
Browse files Browse the repository at this point in the history
Switch to a full 64bit nonce
  • Loading branch information
jklein24 authored Feb 21, 2024
2 parents d44c831 + e88f6fe commit 99d182f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package com.lightspark.sdk.core.crypto

import java.security.SecureRandom

internal actual fun nextInt(): Int {
return SecureRandom().nextInt()
internal actual fun nextLong(): Long {
return SecureRandom().nextLong()
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

package com.lightspark.sdk.core.crypto

internal expect fun nextInt(): Int
internal expect fun nextLong(): Long
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.lightspark.sdk.core.LightsparkException
import com.lightspark.sdk.core.auth.AuthProvider
import com.lightspark.sdk.core.crypto.MissingKeyException
import com.lightspark.sdk.core.crypto.NodeKeyCache
import com.lightspark.sdk.core.crypto.nextInt
import com.lightspark.sdk.core.crypto.nextLong
import com.lightspark.sdk.core.util.getPlatform
import io.ktor.client.HttpClient
import io.ktor.client.plugins.websocket.WebSockets
Expand All @@ -21,6 +21,7 @@ import kotlin.collections.component1
import kotlin.collections.component2
import kotlin.collections.set
import kotlin.coroutines.cancellation.CancellationException
import kotlin.math.absoluteValue
import kotlin.time.Duration.Companion.hours
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
Expand Down Expand Up @@ -203,8 +204,8 @@ class Requester constructor(

val newBodyData = bodyData.toMutableMap().apply {
// Note: The nonce is a 64-bit unsigned integer, but the Kotlin random number generator wants to
// spit out a signed int, which the backend can't decode.
put("nonce", JsonPrimitive(nextInt().toUInt().toLong()))
// spit out a signed int, which the backend can't decode, so we take the absolute value.
put("nonce", JsonPrimitive(nextLong().absoluteValue))
put("expires_at", JsonPrimitive(anHourFromNowISOString()))
}.let { JsonObject(it) }
val newBodyString = Json.encodeToString(newBodyData)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ class ClientIntegrationTests {

@Test
fun `create an LNURL invoice`() = runTest {
val node = getFirstNode()
val node = getFirstOskNode()
val metadata = "[[\\\"text/plain\\\",\\\"Pay to domain.org user ktfan98\\\"],[\\\"text/identifier\\\",\\\"ktfan98@domain.org\\\"]]"
val paymentRequest = client.createLnurlInvoice(node.id, 1000, metadata)

Expand All @@ -193,7 +193,7 @@ class ClientIntegrationTests {

@Test
fun `create and cancel an invoice`() = runTest {
val node = getFirstNode()
val node = getFirstOskNode()
val invoice = client.createInvoice(node.id, 1000)

println("encoded invoice: $invoice.data.encodedPaymentRequest}")
Expand All @@ -205,7 +205,7 @@ class ClientIntegrationTests {

@Test
fun `send a payment for an invoice`() = runTest {
val node = getFirstNode()
val node = getFirstOskNode()
client.loadNodeSigningKey(node.id, PasswordRecoverySigningKeyLoader(node.id, NODE_PASSWORD))

// Just paying a pre-existing AMP invoice.
Expand All @@ -220,7 +220,7 @@ class ClientIntegrationTests {

@Test
fun `get node channels`() = runTest {
val node = getFirstNode()
val node = getFirstOskNode()
val channels = node.getChannelsQuery().execute(client)
channels.shouldNotBeNull()
channels.entities.shouldNotBeEmpty()
Expand All @@ -230,7 +230,7 @@ class ClientIntegrationTests {

@Test
fun `test paying a test mode invoice`() = runTest {
val node = getFirstNode()
val node = getFirstOskNode()
client.loadNodeSigningKey(node.id, PasswordRecoverySigningKeyLoader(node.id, NODE_PASSWORD))
val invoice = client.createTestModeInvoice(node.id, 100_000, "test invoice")
var outgoingPayment: OutgoingPayment? = client.payInvoice(node.id, invoice, maxFeesMsats = 100_000)
Expand All @@ -245,7 +245,7 @@ class ClientIntegrationTests {

@Test
fun `test creating a test mode payment`() = runTest {
val node = getFirstNode()
val node = getFirstOskNode()
client.loadNodeSigningKey(node.id, PasswordRecoverySigningKeyLoader(node.id, NODE_PASSWORD))
val invoice = client.createInvoice(node.id, 100_000, "test invoice")
var payment: IncomingPayment? = client.createTestModePayment(node.id, invoice.data.encodedPaymentRequest)
Expand All @@ -260,16 +260,16 @@ class ClientIntegrationTests {

// TODO: Add tests for withdrawals and deposits.

private suspend fun getFirstNode(): LightsparkNode {
private suspend fun getFirstOskNode(): LightsparkNode {
val account = getCurrentAccount()
val nodes = account.getNodesQuery().execute(client)
nodes.shouldNotBeNull()
nodes.entities.shouldNotBeEmpty()
return nodes.entities.first()
return nodes.entities.first { it.id.contains("OSK")}
}

private suspend fun getNodeId(): String {
return getFirstNode().id
return getFirstOskNode().id
}

private suspend fun getCurrentAccount(): Account {
Expand Down

0 comments on commit 99d182f

Please sign in to comment.