Skip to content

Adds receiving payments #34

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

Merged
merged 3 commits into from
Feb 21, 2023
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 @@ -4,7 +4,6 @@ import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.example.umlandowallet.data.OnchainWallet
import com.example.umlandowallet.utils.LDKTAG
import java.io.File

Expand Down
17 changes: 15 additions & 2 deletions app/src/main/java/com/example/umlandowallet/HandleEvent.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.example.umlandowallet

import android.util.Log
import com.example.umlandowallet.data.OnchainWallet
import com.example.umlandowallet.Global.channelManager
import com.example.umlandowallet.utils.LDKTAG
import com.example.umlandowallet.utils.storeEvent
import com.example.umlandowallet.utils.toHex
import org.ldk.structs.*
import org.ldk.util.UInt128
import kotlin.random.Random
Expand Down Expand Up @@ -50,7 +52,7 @@ fun handleEvent(event: Event) {
storeEvent("${Global.homeDir}/events_open_channel_request", params)
Global.eventsFundingGenerationReady = Global.eventsFundingGenerationReady.plus(params.toString())

Global.channelManager!!.accept_inbound_channel(
channelManager!!.accept_inbound_channel(
event.temporary_channel_id,
event.counterparty_node_id,
userChannelId
Expand Down Expand Up @@ -98,4 +100,15 @@ fun handleEvent(event: Event) {
if(event is Event.PaymentFailed) {
Log.i(LDKTAG, "Payment Failed")
}

if(event is Event.PaymentClaimable) {
Log.i(LDKTAG, "Event.PaymentClaimable")
if (event.payment_hash != null) {
channelManager?.claim_funds(event.payment_hash)
}
}

if(event is Event.PaymentClaimed) {
Log.i(LDKTAG, "Claimed Payment: ${event.payment_hash}")
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.example.umlandowallet.data
package com.example.umlandowallet

import android.util.Log
import com.example.umlandowallet.Global
import com.example.umlandowallet.toHex
import com.example.umlandowallet.utils.LDKTAG
import com.example.umlandowallet.utils.toHex
import org.bitcoindevkit.*
import java.io.File

Expand Down
54 changes: 33 additions & 21 deletions app/src/main/java/com/example/umlandowallet/Start.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package com.example.umlandowallet

import android.util.Log
import com.example.umlandowallet.data.WatchedTransaction
import com.example.umlandowallet.data.remote.AccessImpl
import com.example.umlandowallet.data.remote.Service
import com.example.umlandowallet.utils.LDKTAG
import com.example.umlandowallet.ui.settings.ListItem
import com.example.umlandowallet.utils.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -80,7 +82,6 @@ fun start(

val channelHandshakeLimits = ChannelHandshakeLimits.with_default()
channelHandshakeLimits._max_minimum_depth = 1
channelHandshakeLimits._force_announced_channel_preference = false
userConfig._channel_handshake_limits = channelHandshakeLimits

val params = ProbabilisticScoringParameters.with_default()
Expand All @@ -99,7 +100,7 @@ fun start(
try {
if (serializedChannelManager != null) {
// loading from disk (restarting)
Global.channelManagerConstructor = ChannelManagerConstructor(
val channelManagerConstructor = ChannelManagerConstructor(
serializedChannelManager,
serializedChannelMonitors,
userConfig,
Expand All @@ -112,15 +113,29 @@ fun start(
logger
)

Global.channelManager = Global.channelManagerConstructor!!.channel_manager
Global.peerManager = Global.channelManagerConstructor!!.peer_manager
Global.nioPeerHandler = Global.channelManagerConstructor!!.nio_peer_handler
Global.router = Global.channelManagerConstructor!!.net_graph
Global.invoicePayer = Global.channelManagerConstructor!!.payer

Global.channelManagerConstructor = channelManagerConstructor
Global.channelManager = channelManagerConstructor.channel_manager
Global.nioPeerHandler = channelManagerConstructor.nio_peer_handler
Global.peerManager = channelManagerConstructor.peer_manager
Global.router = channelManagerConstructor.net_graph
Global.invoicePayer = channelManagerConstructor.payer
Global.scorer = scorer

channelManagerConstructor.chain_sync_completed(
ChannelManagerEventHandler,
scorer
)

// If you want to communicate from your computer to your emulator,
// the IP address to use is 127.0.0.1 and you need to do some port forwarding
// using ADB in command line e.g adb forward tcp:9777 tcp:9777
// If you want to do the reverse use 10.0.2.2 instead of localhost

channelManagerConstructor.nio_peer_handler.bind_listener(InetSocketAddress("127.0.0.1", 9777))
} else {
// fresh start
Global.channelManagerConstructor = ChannelManagerConstructor(
val channelManagerConstructor = ChannelManagerConstructor(
Network.LDKNetwork_Regtest,
userConfig,
latestBlockHash.toByteArray(),
Expand All @@ -133,23 +148,20 @@ fun start(
logger
)

Global.channelManager = Global.channelManagerConstructor!!.channel_manager
Global.peerManager = Global.channelManagerConstructor!!.peer_manager
Global.nioPeerHandler = Global.channelManagerConstructor!!.nio_peer_handler
Global.router = Global.channelManagerConstructor!!.net_graph
Global.channelManagerConstructor = channelManagerConstructor
Global.channelManager = channelManagerConstructor.channel_manager
Global.peerManager = channelManagerConstructor.peer_manager
Global.nioPeerHandler = channelManagerConstructor.nio_peer_handler
Global.router = channelManagerConstructor.net_graph
Global.scorer = scorer
Global.invoicePayer = Global.channelManagerConstructor!!.payer
Global.channelManagerConstructor!!.chain_sync_completed(
Global.invoicePayer = channelManagerConstructor.payer
channelManagerConstructor.chain_sync_completed(
ChannelManagerEventHandler,
scorer
)
}

// If you want to communicate from your computer to your emulator,
// the IP address to use is 127.0.0.1 and you need to do some port forwarding
// using ADB in command line e.g adb forward tcp:9777 tcp:9777
// If you want to do the reverse use 10.0.2.2 instead of localhost
Global.nioPeerHandler!!.bind_listener(InetSocketAddress("127.0.0.1", 9777))
channelManagerConstructor.nio_peer_handler.bind_listener(InetSocketAddress("127.0.0.1", 9777))
}
} catch (e: Exception) {
Log.i(LDKTAG, "LDK: can't start, ${e.message}")
}
Expand Down
19 changes: 6 additions & 13 deletions app/src/main/java/com/example/umlandowallet/data/remote/Access.kt
Original file line number Diff line number Diff line change
@@ -1,36 +1,29 @@
package com.example.umlandowallet.data.remote

import com.example.umlandowallet.data.OnchainWallet
import org.bitcoindevkit.*
import com.example.umlandowallet.OnchainWallet
import org.ldk.structs.ChainMonitor
import org.ldk.structs.ChannelManager
import org.ldk.structs.TwoTuple_TxidBlockHashZ

interface Access {
suspend fun sync(): Unit
suspend fun sync()

suspend fun syncWallet(onchainWallet: OnchainWallet): Unit
suspend fun syncWallet(onchainWallet: OnchainWallet)

suspend fun syncBestBlockConnected(
channelManager: ChannelManager,
chainMonitor: ChainMonitor
): Unit
)

suspend fun syncTransactionConfirmed(
relevantTxIds: Array<TwoTuple_TxidBlockHashZ>,
channelManager: ChannelManager,
chainMonitor: ChainMonitor
): Unit
)

suspend fun syncTransactionsUnconfirmed(
relevantTxIds: Array<TwoTuple_TxidBlockHashZ>,
channelManager: ChannelManager,
chainMonitor: ChainMonitor
): Unit

companion object {
fun create(): Access {
return AccessImpl()
}
}
)
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.example.umlandowallet.data.remote

import com.example.umlandowallet.ChannelManagerEventHandler
import com.example.umlandowallet.Global
import com.example.umlandowallet.*
import com.example.umlandowallet.data.*
import com.example.umlandowallet.toByteArray
import com.example.umlandowallet.toHex
import com.example.umlandowallet.utils.toByteArray
import com.example.umlandowallet.utils.toHex
import org.ldk.structs.ChainMonitor
import org.ldk.structs.ChannelManager
import org.ldk.structs.TwoTuple_TxidBlockHashZ
Expand Down Expand Up @@ -86,7 +85,7 @@ class AccessImpl: Access {
cTx.block_header.toByteArray(),
arrayOf<TwoTuple_usizeTransactionZ>(
TwoTuple_usizeTransactionZ.of(
cTx.block_height.toLong(),
cTx.merkle_proof_pos.toLong(),
cTx.tx
)
),
Expand All @@ -97,7 +96,7 @@ class AccessImpl: Access {
cTx.block_header.toByteArray(),
arrayOf<TwoTuple_usizeTransactionZ>(
TwoTuple_usizeTransactionZ.of(
cTx.block_height.toLong(),
cTx.merkle_proof_pos.toLong(),
cTx.tx
)
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ interface Service {

suspend fun getTxHex(txid: String): String

// suspend fun getTxStatus(txid: String) : TxStatus

suspend fun getHeader(hash: String) : String

suspend fun getMerkleProof(txid: String) : MerkleProof
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import android.util.Log
import com.example.umlandowallet.Global
import com.example.umlandowallet.data.MerkleProof
import com.example.umlandowallet.data.Tx
import com.example.umlandowallet.data.TxStatus
import com.example.umlandowallet.toByteArray
import com.example.umlandowallet.toHex
import com.example.umlandowallet.utils.toByteArray
import com.example.umlandowallet.utils.toHex
import com.example.umlandowallet.utils.LDKTAG
import io.ktor.client.*
import io.ktor.client.call.*
Expand Down
17 changes: 17 additions & 0 deletions app/src/main/java/com/example/umlandowallet/ui/Navigation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,23 @@ fun Navigation(
}
) { SendPaymentScreen() }

// Receive Payment
composable(
route = Screen.ReceivePaymentScreen.route,
enterTransition = {
slideIntoContainer(AnimatedContentScope.SlideDirection.Up, animationSpec = tween(animationDuration))
},
popEnterTransition = {
slideIntoContainer(AnimatedContentScope.SlideDirection.Up, animationSpec = tween(animationDuration))
},
exitTransition = {
slideOutOfContainer(AnimatedContentScope.SlideDirection.Down, animationSpec = tween(animationDuration))
},
popExitTransition = {
slideOutOfContainer(AnimatedContentScope.SlideDirection.Down, animationSpec = tween(animationDuration))
}
) { ReceivePaymentScreen() }

// Recovery phrase
composable(
route = Screen.RecoveryPhraseScreen.route,
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/example/umlandowallet/ui/Screen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ sealed class Screen(val route: String) {
object OpenChannelScreen : Screen("open_channel")
object ListChannelsScreen : Screen("list_channels")
object SendPaymentScreen : Screen("send_payment")
object ReceivePaymentScreen: Screen("receive_payment")
object RecoveryPhraseScreen : Screen("recovery_phrase")
}
15 changes: 14 additions & 1 deletion app/src/main/java/com/example/umlandowallet/ui/SettingsScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import com.example.umlandowallet.R
import com.example.umlandowallet.data.OnchainWallet
import com.example.umlandowallet.OnchainWallet
import com.example.umlandowallet.utils.LDKTAG

@Composable
Expand Down Expand Up @@ -128,6 +128,19 @@ fun SettingsScreen(navController: NavController) {
}
)

// Receive payment
SettingButton(
label = "Receive payment",
onClick = {
navController.navigate(Screen.ReceivePaymentScreen.route) {
navController.graph.startDestinationRoute?.let { route ->
popUpTo(route)
}
launchSingleTop = true
}
}
)

Text(
text = "Onchain Wallet",
fontSize = 18.sp,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
package com.example.umlandowallet.ui.settings

import androidx.compose.foundation.background
import android.util.Log
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.umlandowallet.Global
import com.example.umlandowallet.toByteArray
import com.example.umlandowallet.toHex
import com.example.umlandowallet.ui.Screen
import com.example.umlandowallet.utils.LDKTAG
import com.example.umlandowallet.utils.toHex
import org.ldk.structs.*
import java.time.temporal.TemporalAmount

@OptIn(ExperimentalMaterial3Api::class)
@Composable
Expand Down Expand Up @@ -55,20 +49,16 @@ fun ListChannelsScreen() {
usableChannels.forEach { channel ->
val status = channel._is_channel_ready

var balance = 0L
balance = if (channel._outbound_capacity_msat == 0L) {
0L
} else {
channel._outbound_capacity_msat / channel._balance_msat
}
val nodeId = channel._counterparty._node_id.toHex()
val sendAmount = channel._outbound_capacity_msat / 1000
val receiveAmount = channel._inbound_capacity_msat / 1000
val progress: Double = receiveAmount.toDouble() / (sendAmount.toDouble() + receiveAmount.toDouble())
Log.i(LDKTAG, "Progress update: $progress with $sendAmount and $receiveAmount")

ListItem(
status,
nodeId,
balance.toFloat(),
progress.toFloat(),
sendAmount.toString(),
receiveAmount.toString()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.umlandowallet.Global
import com.example.umlandowallet.toHex
import com.example.umlandowallet.Global.peerManager
import com.example.umlandowallet.utils.toHex

@Composable
fun ListPeersScreen() {
Expand All @@ -27,7 +27,7 @@ fun ListPeersScreen() {
.padding(start = 24.dp, end = 24.dp, bottom = 24.dp)
)

val peers = Global.peerManager!!.get_peer_node_ids()
val peers = peerManager!!._peer_node_ids
val peersList: MutableList<String> = mutableListOf()
peers.forEach {
peersList.add(it.toHex())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.umlandowallet.Global
import com.example.umlandowallet.toHex
import com.example.umlandowallet.utils.toHex

@Composable
fun NodeIdScreen() {
Expand Down
Loading