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

Switch to a full 64bit nonce #156

Merged
merged 1 commit into from
Feb 21, 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 @@ -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
Loading