feat(lightning): the group re-keys its Lightning wallet when members change (Phase 14)

Iceberg cannot reshare, so a member who leaves keeps a share of the key
and the node's seed until the group opens a new wallet without them.

- A ceremony is now over the onchain key's holders who are still in the
  room (D1, narrowed), and a statement verifies when every participant
  holds the onchain key, so the wallets before and after a removal both
  stand.
- A room can have several wallets on a network. The highest-numbered is
  current, and a new statement takes the next number, under a key no
  wallet uses yet.
- The wallet screen names who has left and offers the new key or the new
  wallet. It lists earlier wallets with their balance, one decision to
  close every channel they still have, their sweeps, and a way to run
  their node when nobody does.
- `GroupLightningNodes` runs one node per wallet a device operates.
- Removing a member who holds a share says what the removal leaves.

A five-device test removes a member, refuses the leaver's own ceremony,
re-keys over the four who remain, opens wallet 1 beside wallet 0 with an
operator each, and closes wallet 0's channels in one session.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kgothatso NgakoandClaude Opus 5.5 committed 2026-10-23 19:09:13 +02:00
1 parent b770b07472
commit 6140aef33a
23 files changed
+459 -65

No files matched your search

@@ -134,6 +134,7 @@
<string name="group_disbanded">This group was disbanded. Nothing more can be sent into it, and nobody can rejoin.</string>
<string name="disband_group_explainer">Ends the group for everyone. Nobody can rejoin; a new conversation is a new group.</string>
<string name="member_removal_requested">Removal sent; it takes effect once a relay accepts it</string>
<string name="member_removal_requested_lightning">Removal sent; it takes effect once a relay accepts it. They still hold a share of the Lightning wallet's key and its node's seed until the group opens a new Lightning wallet without them.</string>
<string name="library">Library</string>
<string name="lightning_invoice">Lightning invoice</string>
<string name="live">LIVE</string>
@@ -766,4 +767,11 @@
<string name="s_sats_wait_in_closed_channels">%1$s sats wait in closed channels</string>
<string name="a_closed_channel_s_outputs_go_to_the_onchain_wallet">They are the group's alone, under its own keys. Sweeping asks the group to sign moving them to the onchain wallet; the sweep is sent once the channel's delay is over.</string>
<string name="sweep_into_the_onchain_wallet">Sweep into the onchain wallet</string>
<string name="s_people_who_left_still_hold_a_share">%1$s people who have left the group still hold a share of this wallet's key and its node's seed. Make a new Lightning wallet without them, then close this one's channels.</string>
<string name="open_a_new_lightning_wallet">Open a new Lightning wallet</string>
<string name="earlier_wallet_s">Earlier wallet %1$s</string>
<string name="s_sats_in_s_channels">%1$s sats in %2$s channels</string>
<string name="close_its_channels">Close its channels</string>
<string name="sweep_s_sats">Sweep %1$s sats</string>
<string name="nobody_runs_this_wallet_s_node">Nobody runs its node, and its channels close only through it.</string>
</resources>
@@ -1,6 +1,7 @@
package com.machankura.bennu.compose.database.repository
import fr.acinq.bitcoin.Chain
import com.machankura.bennu.compose.managers.GroupLightningWalletManager
import com.machankura.bennu.compose.managers.GroupWalletManager
import com.machankura.bennu.compose.managers.GroupWalletDerivation
import com.machankura.bennu.compose.nostr.wallet.GroupWallet
@@ -187,6 +188,9 @@ class DatabaseChatRepository(
emptyList()
}
override suspend fun lightningParticipants(chatRoomId: String, chain: Chain): Set<HexKey> =
runCatching { GroupLightningWalletManager.current(database, chatRoomId, chain)?.statement?.participants?.toSet() }.getOrNull().orEmpty()
override suspend fun groupWallet(chatRoomId: String, chain: Chain): GroupWallet? = try {
GroupWalletManager.walletFor(database, chatRoomId, chain)
} catch (e: Throwable) {
@@ -31,10 +31,19 @@ class DatabaseGroupLightningRepository(private val database: BennuDatabase) : Gr
override suspend fun completedCeremony(chatRoomId: String): IcebergKeySession? = IcebergRitualManager.completedKey(database, chatRoomId)
override suspend fun lightningWallet(chatRoomId: String, chain: Chain): GroupLightningWalletManager.Wallet? =
GroupLightningWalletManager.walletFor(database, chatRoomId, chain)
GroupLightningWalletManager.current(database, chatRoomId, chain)
override suspend fun operator(chatRoomId: String, chain: Chain): GroupLightningOperatorManager.Operator? =
GroupLightningOperatorManager.current(database, chatRoomId, chain)
override suspend fun earlierWallets(chatRoomId: String, chain: Chain): List<GroupLightningWalletManager.Wallet> =
GroupLightningWalletManager.wallets(database, chatRoomId, chain).dropLast(1)
override suspend fun departed(wallet: GroupLightningWalletManager.Wallet): List<HexKey> =
GroupLightningWalletManager.departed(database, wallet)
override suspend fun hasUnusedKey(chatRoomId: String, chain: Chain): Boolean =
GroupLightningWalletManager.unusedKey(database, chatRoomId, chain) != null
override suspend fun operator(chatRoomId: String, chain: Chain, wallet: Long): GroupLightningOperatorManager.Operator? =
GroupLightningOperatorManager.current(database, chatRoomId, chain, wallet)
override fun lspNodeId(chain: Chain): HexKey? =
runCatching { LSP.from(chain).walletParams.trampolineNode.id.toString() }.getOrNull()
@@ -42,25 +51,25 @@ class DatabaseGroupLightningRepository(private val database: BennuDatabase) : Gr
override suspend fun proposeWallet(localChatRoom: LocalChatRoom, userPublicKey: HexKey, nostrPrivateKey: ByteArray, chain: Chain): FrostSigningSession =
GroupLightningWalletManager.proposeWallet(database, localChatRoom, userPublicKey, nostrPrivateKey, chain, requireNotNull(lspNodeId(chain)) { "No LSP is known for ${chain.name}" })
override suspend fun nominateSelf(localChatRoom: LocalChatRoom, userPublicKey: HexKey, chain: Chain): FrostSigningSession =
GroupLightningOperatorManager.nominateSelf(database, localChatRoom, userPublicKey, chain)
override suspend fun nominateSelf(localChatRoom: LocalChatRoom, userPublicKey: HexKey, chain: Chain, wallet: Long): FrostSigningSession =
GroupLightningOperatorManager.nominateSelf(database, localChatRoom, userPublicKey, chain, wallet)
override suspend fun stepDown(localChatRoom: LocalChatRoom, userPublicKey: HexKey, chain: Chain): FrostSigningSession =
GroupLightningOperatorManager.stepDown(database, localChatRoom, userPublicKey, chain)
override suspend fun stepDown(localChatRoom: LocalChatRoom, userPublicKey: HexKey, chain: Chain, wallet: Long): FrostSigningSession =
GroupLightningOperatorManager.stepDown(database, localChatRoom, userPublicKey, chain, wallet)
override fun observeReport(chatRoomId: String, chain: Chain): Flow<GroupLightningReports.Report?> {
val key = GroupLightningCoSigner.walletKey(chatRoomId, chain, 0L)
override fun observeReport(chatRoomId: String, chain: Chain, wallet: Long): Flow<GroupLightningReports.Report?> {
val key = GroupLightningCoSigner.walletKey(chatRoomId, chain, wallet)
return database.groupLightningRecordsDao().observeSnapshot(key).map { GroupLightningReports.latest(database, key) }
}
override fun observePayments(chatRoomId: String, chain: Chain): Flow<List<GroupLightningPaymentRow>> =
database.groupLightningRecordsDao().observePayments(GroupLightningCoSigner.walletKey(chatRoomId, chain, 0L))
override fun observePayments(chatRoomId: String, chain: Chain, wallet: Long): Flow<List<GroupLightningPaymentRow>> =
database.groupLightningRecordsDao().observePayments(GroupLightningCoSigner.walletKey(chatRoomId, chain, wallet))
override suspend fun requestInvoice(localChatRoom: LocalChatRoom, userPublicKey: HexKey, wallet: GroupLightningWalletManager.Wallet, amountMsat: Long?, description: String): String =
GroupLightningReports.requestInvoice(database, localChatRoom, userPublicKey, wallet, amountMsat, description)
override fun observeInvoice(chatRoomId: String, chain: Chain, requestId: String): Flow<GroupLightningInvoiceRow?> =
database.groupLightningRecordsDao().observeInvoice(GroupLightningCoSigner.walletKey(chatRoomId, chain, 0L), requestId)
override fun observeInvoice(chatRoomId: String, chain: Chain, wallet: Long, requestId: String): Flow<GroupLightningInvoiceRow?> =
database.groupLightningRecordsDao().observeInvoice(GroupLightningCoSigner.walletKey(chatRoomId, chain, wallet), requestId)
override suspend fun proposePayment(localChatRoom: LocalChatRoom, userPublicKey: HexKey, pay: GroupLightningAuthorisationEvent.Pay): FrostSigningSession =
GroupLightningAuthorisations.propose(database, localChatRoom, userPublicKey, pay)
@@ -68,6 +77,9 @@ class DatabaseGroupLightningRepository(private val database: BennuDatabase) : Gr
override suspend fun proposeClose(localChatRoom: LocalChatRoom, userPublicKey: HexKey, wallet: GroupLightningWalletManager.Wallet, channelId: String, force: Boolean): FrostSigningSession =
GroupLightningAuthorisations.proposeChannelAction(database, localChatRoom, userPublicKey, wallet, channelId, force)
override suspend fun proposeCloseAll(localChatRoom: LocalChatRoom, userPublicKey: HexKey, wallet: GroupLightningWalletManager.Wallet, channelIds: List<String>): FrostSigningSession =
GroupLightningAuthorisations.proposeCloseAll(database, localChatRoom, userPublicKey, wallet, channelIds)
override suspend fun unswept(chatRoomId: String, report: GroupLightningReports.Report?): List<GroupSweep.Input> =
GroupLightningSweeps.unswept(database, chatRoomId, report)
@@ -87,10 +99,10 @@ class DatabaseGroupLightningRepository(private val database: BennuDatabase) : Gr
IcebergRitualManager.pendingApproval(database, session)
override suspend fun ceremonyThresholds(chatRoomId: String): List<Int> =
FrostSigningManager.completedKey(database, chatRoomId)?.let(IcebergRitualManager::thresholdsFor).orEmpty()
IcebergRitualManager.thresholds(database, chatRoomId)
override suspend fun keyHolderCount(chatRoomId: String): Int =
FrostSigningManager.completedKey(database, chatRoomId)?.participantPublicKeySet()?.size ?: 0
FrostSigningManager.completedKey(database, chatRoomId)?.let { IcebergRitualManager.holdersInRoom(database, chatRoomId, it).size } ?: 0
override suspend fun hasOnchainWallet(chatRoomId: String, chain: Chain): Boolean =
GroupWalletManager.walletFor(database, chatRoomId, chain) != null
@@ -111,6 +111,16 @@ object GroupLightningAuthorisations {
suspend fun forceCloseAuthorised(database: BennuDatabase, chatRoomId: String, statement: GroupLightningWalletStatementEvent.Statement, channelId: ByteVector32): Boolean =
channelActions(database, chatRoomId, statement).any { it.action == GroupLightningAuthorisationEvent.FORCE_CLOSE && it.channelId == channelId.toHex() }
/** Proposes closing every one of [channelIds] of [wallet] to the onchain wallet, as one signing session. */
suspend fun proposeCloseAll(database: BennuDatabase, localChatRoom: LocalChatRoom, userPublicKey: HexKey, wallet: GroupLightningWalletManager.Wallet, channelIds: List<String>): FrostSigningSession {
require(channelIds.isNotEmpty()) { "There are no channels to close" }
val expires = (Clock.System.now() + CHANNEL_ACTION_LIFETIME).epochSeconds
val templates = channelIds.map { channelId ->
GroupLightningAuthorisationEvent.template(GroupLightningAuthorisationEvent.ChannelAction(GroupLightningAuthorisationEvent.CLOSE, wallet.statement.chain, wallet.statement.wallet, channelId, expires))
}
return FrostSigningManager.proposeSigningBatch(database, localChatRoom, userPublicKey, templates)
}
/** Proposes that the operator's node close [channelId] -- mutually, or by force when [force]. */
suspend fun proposeChannelAction(database: BennuDatabase, localChatRoom: LocalChatRoom, userPublicKey: HexKey, wallet: GroupLightningWalletManager.Wallet, channelId: String, force: Boolean): FrostSigningSession {
val action = GroupLightningAuthorisationEvent.ChannelAction(
@@ -60,6 +60,38 @@ object GroupLightningWalletManager {
fun nodeIdOf(seed: ByteVector, chain: Chain): PublicKey =
DeterministicWallet.generate(seed).derivePrivateKey(LocalKeyManager.nodeKeyBasePath(chain)).publicKey
/**
* Every Lightning wallet [chatRoomId] has opened on [chain] that this device believes, the newest
* statement for each wallet number, lowest number first. The last is the current one; the others are
* wallets a re-key left behind, whose channels are still to be closed (Phase 14).
*/
suspend fun wallets(database: BennuDatabase, chatRoomId: String, chain: Chain): List<Wallet> =
database.groupSignedEventDao().getByChatRoomIdAndKind(chatRoomId, GroupLightningWalletStatementEvent.KIND)
.sortedWith(compareByDescending<GroupSignedEvent> { it.createdAt }.thenBy { it.id })
.mapNotNull { verify(database, chatRoomId, it)?.takeIf { wallet -> wallet.statement.chain == chain } }
.distinctBy { it.statement.wallet }
.sortedBy { it.statement.wallet }
/** The wallet a room uses on [chain] now: the highest-numbered one it has opened, or null. */
suspend fun current(database: BennuDatabase, chatRoomId: String, chain: Chain): Wallet? = wallets(database, chatRoomId, chain).lastOrNull()
/** The participants of [wallet] who are no longer in its room: each still holds a share and the seed. */
suspend fun departed(database: BennuDatabase, wallet: Wallet): List<HexKey> {
val inRoom = database.participantDao().findParticipantsByChatRoomId(wallet.signedEvent.chatRoomId).filter { it.deletedAt == null }.map { it.participantPublicKey.lowercase() }.toSet()
return wallet.statement.participants.filter { it !in inRoom }
}
/**
* A completed Iceberg key in [chatRoomId] that this device holds a share of and that no wallet the
* room opened uses yet: what a new wallet -- the first, or the one a re-key makes -- is opened under.
*/
suspend fun unusedKey(database: BennuDatabase, chatRoomId: String, chain: Chain): IcebergKeySession? {
val used = database.groupSignedEventDao().getByChatRoomIdAndKind(chatRoomId, GroupLightningWalletStatementEvent.KIND)
.mapNotNull { GroupLightningWalletStatementEvent.read(it.tags)?.ceremonyId }.toSet()
return database.icebergKeySessionDao().getSessionsForChatRoom(chatRoomId)
.firstOrNull { it.stage == IcebergRitualStage.COMPLETE && it.share != null && it.id !in used }
}
/** The newest statement for [chain] and [wallet] in [chatRoomId] that this device believes, or null. */
suspend fun walletFor(database: BennuDatabase, chatRoomId: String, chain: Chain, wallet: Long = 0L): Wallet? =
database.groupSignedEventDao().getByChatRoomIdAndKind(chatRoomId, GroupLightningWalletStatementEvent.KIND)
@@ -82,9 +114,11 @@ object GroupLightningWalletManager {
logger.w("Lightning wallet ${signed.id} closes to an onchain wallet this room has not opened")
return null
}
// D1 as Phase 14 narrows it: every participant holds the onchain key. A wallet made after a member
// left names fewer of them; the one before still names everyone, and both stay believable.
FrostSigningManager.completedKey(database, chatRoomId)?.participantPublicKeySet()?.let { holders ->
if (holders.map { it.lowercase() }.sorted() != statement.participants) {
logger.w("Lightning wallet ${signed.id} names participants who are not the onchain key's holders")
if (!holders.map { it.lowercase() }.containsAll(statement.participants)) {
logger.w("Lightning wallet ${signed.id} names participants who do not hold the onchain key")
return null
}
}
@@ -119,14 +153,16 @@ object GroupLightningWalletManager {
nostrPrivateKey: ByteArray,
chain: Chain,
lspNodeId: HexKey,
wallet: Long = 0L,
wallet: Long? = null,
onchainAccount: Long = 0L,
policy: GroupLightningWalletStatementEvent.Policy = GroupLightningWalletStatementEvent.Policy.DEFAULT,
): FrostSigningSession {
require(GroupLightningRollout.allows(chain)) { "A Lightning wallet cannot be opened on ${chain.name}" }
val roomId = localChatRoom.chatRoom.id
val ceremony = IcebergRitualManager.completedKey(database, roomId)
?: throw IllegalStateException("This group has no Lightning wallet key yet")
val ceremony = unusedKey(database, roomId, chain)
?: throw IllegalStateException("This group has no Lightning wallet key that no wallet uses yet")
// The next number: a re-key opens a new wallet beside the old one, whose channels still need closing.
val wallet = wallet ?: ((wallets(database, roomId, chain).maxOfOrNull { it.statement.wallet } ?: -1L) + 1)
requireNotNull(GroupWalletManager.walletFor(database, roomId, chain, onchainAccount)) {
"Open a group onchain wallet on ${chain.name} first: it is where the Lightning wallet closes to"
}
@@ -87,6 +87,26 @@ object IcebergRitualManager {
fun thresholdsFor(onchainKey: DkgSession): List<Int> =
onchainKey.participantPublicKeySet()?.let { IcebergDkg.deployableThresholds(it.size) }.orEmpty()
/** The thresholds a ceremony in [chatRoomId] could use now, over [holdersInRoom]; empty if none. */
suspend fun thresholds(database: BennuDatabase, chatRoomId: String): List<Int> {
val onchainKey = FrostSigningManager.completedKey(database, chatRoomId) ?: return emptyList()
return IcebergDkg.deployableThresholds(holdersInRoom(database, chatRoomId, onchainKey).size)
}
/**
* Who a ceremony in [chatRoomId] is over (decision D1, as Phase 14 narrows it): the holders of the
* onchain key [onchainKey] who are still in the room, as this device's MLS state has it. A member
* who was removed keeps their onchain share, but a new Lightning wallet is made without them.
*
* The removal commit is an earlier epoch than any proposal sent after it, so a device reading a
* proposal has already applied it, and every device computes the same set.
*/
suspend fun holdersInRoom(database: BennuDatabase, chatRoomId: String, onchainKey: DkgSession): Set<HexKey> {
val holders = onchainKey.participantPublicKeySet()?.map { it.lowercase() }?.toSet().orEmpty()
val inRoom = database.participantDao().findParticipantsByChatRoomId(chatRoomId).filter { it.deletedAt == null }.map { it.participantPublicKey.lowercase() }.toSet()
return holders intersect inRoom
}
/** A participant's nostr key as the compressed point [IcebergDkg.Params] orders and binds. */
private fun asPoint(nostrKey: HexKey): PublicKey = PublicKey(ByteVector("02$nostrKey"))
@@ -123,8 +143,8 @@ object IcebergRitualManager {
requireNotNull(GroupWalletManager.walletFor(database, roomId, chain)) {
"Open a group onchain wallet on ${chain.name} before a Lightning wallet: it is where the Lightning wallet closes to"
}
val members = requireNotNull(onchainKey.participantPublicKeySet()) { "The onchain key does not record who holds it" }
require(userPublicKey in members) { "This device does not hold the onchain key" }
val members = holdersInRoom(database, roomId, onchainKey)
require(userPublicKey.lowercase() in members) { "This device does not hold the onchain key" }
require(IcebergDkg.isDeployable(members.size, threshold)) { "$threshold-of-${members.size} is not a deployable Iceberg group" }
val session = IcebergKeySession(
@@ -194,9 +214,9 @@ object IcebergRitualManager {
return null
}
// D1, checked rather than trusted: the people the proposal names must be exactly the people
// who hold the onchain key it names, as this device's own record of that ceremony says.
// who hold the onchain key it names and are still in the room, as this device's own records say.
val onchainKey = onchainId?.let { database.dkgSessionDao().getSessionById(it) }
if (onchainKey?.participantPublicKeySet()?.map { it.lowercase() }?.toSet() != members) {
if (onchainKey == null || holdersInRoom(database, localChatRoom.chatRoom.id, onchainKey) != members) {
logger.w("Iceberg proposal $sessionId names people who are not the holders of onchain key $onchainId; dropping")
return null
}
@@ -113,6 +113,12 @@ interface ChatRepository {
*/
suspend fun groupWallet(chatRoomId: String, chain: Chain): GroupWallet?
/**
* Who holds a share of the key of the Lightning wallet this group uses on [chain], and its node's
* seed: the people a removal leaves able to grief it until a new wallet replaces it (Phase 14).
*/
suspend fun lightningParticipants(chatRoomId: String, chain: Chain): Set<HexKey> = emptySet()
/**
* The account this group *would* open on [chain], whether or not it has, or
* null for a room with no key: what a statement proposing the wallet carries.
@@ -46,31 +46,40 @@ interface GroupLightningRepository {
/** The newest completed Lightning wallet key this device holds a share of, or null. */
suspend fun completedCeremony(chatRoomId: String): IcebergKeySession?
/** The wallet the room agreed on [chain], as this device can check it, or null. */
/** The wallet the room uses on [chain] now -- the highest-numbered it agreed -- as this device can check it, or null. */
suspend fun lightningWallet(chatRoomId: String, chain: Chain): GroupLightningWalletManager.Wallet?
suspend fun operator(chatRoomId: String, chain: Chain): GroupLightningOperatorManager.Operator?
/** The wallets a re-key left behind on [chain], lowest number first: their channels are still to close. */
suspend fun earlierWallets(chatRoomId: String, chain: Chain): List<GroupLightningWalletManager.Wallet>
/** The participants of [wallet] who have left its room, and still hold a share of its key and its seed. */
suspend fun departed(wallet: GroupLightningWalletManager.Wallet): List<HexKey>
/** Whether the room has a completed Lightning wallet key no wallet uses yet, for a new wallet to open under. */
suspend fun hasUnusedKey(chatRoomId: String, chain: Chain): Boolean
suspend fun operator(chatRoomId: String, chain: Chain, wallet: Long): GroupLightningOperatorManager.Operator?
/** The LSP a Lightning wallet on [chain] opens channels with, or null for a network with none. */
fun lspNodeId(chain: Chain): HexKey?
suspend fun proposeWallet(localChatRoom: LocalChatRoom, userPublicKey: HexKey, nostrPrivateKey: ByteArray, chain: Chain): FrostSigningSession
suspend fun nominateSelf(localChatRoom: LocalChatRoom, userPublicKey: HexKey, chain: Chain): FrostSigningSession
suspend fun nominateSelf(localChatRoom: LocalChatRoom, userPublicKey: HexKey, chain: Chain, wallet: Long): FrostSigningSession
suspend fun stepDown(localChatRoom: LocalChatRoom, userPublicKey: HexKey, chain: Chain): FrostSigningSession
suspend fun stepDown(localChatRoom: LocalChatRoom, userPublicKey: HexKey, chain: Chain, wallet: Long): FrostSigningSession
/** The operator's newest snapshot of the wallet on [chain], each channel checked against this device's ledger. */
fun observeReport(chatRoomId: String, chain: Chain): Flow<GroupLightningReports.Report?>
fun observeReport(chatRoomId: String, chain: Chain, wallet: Long): Flow<GroupLightningReports.Report?>
/** Every payment any snapshot has reported, newest first. */
fun observePayments(chatRoomId: String, chain: Chain): Flow<List<GroupLightningPaymentRow>>
fun observePayments(chatRoomId: String, chain: Chain, wallet: Long): Flow<List<GroupLightningPaymentRow>>
/** Asks the operator's node for an invoice; returns the request id its answer will carry. */
suspend fun requestInvoice(localChatRoom: LocalChatRoom, userPublicKey: HexKey, wallet: GroupLightningWalletManager.Wallet, amountMsat: Long?, description: String): String
/** The operator's invoice for [requestId], once it has arrived and been checked. */
fun observeInvoice(chatRoomId: String, chain: Chain, requestId: String): Flow<GroupLightningInvoiceRow?>
fun observeInvoice(chatRoomId: String, chain: Chain, wallet: Long, requestId: String): Flow<GroupLightningInvoiceRow?>
/** Proposes [pay] to the group; the operator's node pays it once a quorum signs. */
suspend fun proposePayment(localChatRoom: LocalChatRoom, userPublicKey: HexKey, pay: GroupLightningAuthorisationEvent.Pay): FrostSigningSession
@@ -78,6 +87,9 @@ interface GroupLightningRepository {
/** Proposes closing [channelId] to the onchain wallet, or force-closing it when [force]. */
suspend fun proposeClose(localChatRoom: LocalChatRoom, userPublicKey: HexKey, wallet: GroupLightningWalletManager.Wallet, channelId: String, force: Boolean): FrostSigningSession
/** Proposes closing every one of [channelIds] to the onchain wallet, in one signing session. */
suspend fun proposeCloseAll(localChatRoom: LocalChatRoom, userPublicKey: HexKey, wallet: GroupLightningWalletManager.Wallet, channelIds: List<String>): FrostSigningSession
/** What closed channels left the group that no sweep spends yet, from [report]. */
suspend fun unswept(chatRoomId: String, report: GroupLightningReports.Report?): List<GroupSweep.Input>
@@ -2,6 +2,7 @@ package com.machankura.bennu.compose.ui.composable
import androidx.compose.material.icons.filled.Bolt
import bennu.composeapp.generated.resources.lightning_wallet
import bennu.composeapp.generated.resources.member_removal_requested_lightning
import com.machankura.bennu.compose.ui.composable.navigation.routes.GroupLightningWalletRoute
import bennu.composeapp.generated.resources.group_wallet_on_s
import fr.acinq.phoenix.managers.NodeParamsManager
@@ -199,6 +200,7 @@ fun ChatRoomDetailScreen(
// composable, and a lambda passed to onClick is not.
val notify = rememberNotifier(rememberCoroutineScope())
val removalRequested = stringResource(Res.string.member_removal_requested)
val removalLeavesLightningShare = stringResource(Res.string.member_removal_requested_lightning)
ScreenStateTransition(chatRoomDetailViewModel.chatRoomDetailUIState) { uiState ->
when (val chatRoomDetailUIState = uiState) {
@@ -791,7 +793,12 @@ fun ChatRoomDetailScreen(
chatRoomDetailViewModel.removeMember(
localChatRoom = chatRoomDetailUIState.localChatRoom,
memberPublicKey = memberPublicKey,
) { failure -> notify(failure ?: removalRequested) }
) { failure ->
// They keep a share of the Lightning wallet's key and its seed: say so,
// since only a new wallet takes it from them (Phase 14).
val holdsShare = memberPublicKey.lowercase() in chatRoomDetailUIState.lightningParticipants
notify(failure ?: if (holdsShare) removalLeavesLightningShare else removalRequested)
}
}
) {
Icon(
@@ -53,9 +53,11 @@ import bennu.composeapp.generated.resources.at_most_s_sats_in_flight
import bennu.composeapp.generated.resources.balance
import bennu.composeapp.generated.resources.channel_actions
import bennu.composeapp.generated.resources.channels
import bennu.composeapp.generated.resources.close_its_channels
import bennu.composeapp.generated.resources.close_to_the_onchain_wallet
import bennu.composeapp.generated.resources.couldn_t_propose_this
import bennu.composeapp.generated.resources.couldn_t_send_s_sats
import bennu.composeapp.generated.resources.earlier_wallet_s
import bennu.composeapp.generated.resources.force_close
import bennu.composeapp.generated.resources.group_wallet_on_s
import bennu.composeapp.generated.resources.lightning_wallet
@@ -64,8 +66,10 @@ import bennu.composeapp.generated.resources.make_a_key
import bennu.composeapp.generated.resources.matches_what_your_device_signed
import bennu.composeapp.generated.resources.no_payments_yet
import bennu.composeapp.generated.resources.nobody_runs_this_wallet
import bennu.composeapp.generated.resources.nobody_runs_this_wallet_s_node
import bennu.composeapp.generated.resources.node_s
import bennu.composeapp.generated.resources.open_a_lightning_wallet
import bennu.composeapp.generated.resources.open_a_new_lightning_wallet
import bennu.composeapp.generated.resources.open_an_onchain_wallet_on_s_first
import bennu.composeapp.generated.resources.opening_a_lightning_wallet_asks
import bennu.composeapp.generated.resources.payments
@@ -75,8 +79,10 @@ import bennu.composeapp.generated.resources.reported_by_the_operator_in_term_s
import bennu.composeapp.generated.resources.run_by_s_term_s
import bennu.composeapp.generated.resources.run_this_wallet_on_my_device
import bennu.composeapp.generated.resources.s_of_s_sats_are_the_group_s
import bennu.composeapp.generated.resources.s_people_who_left_still_hold_a_share
import bennu.composeapp.generated.resources.s_sats
import bennu.composeapp.generated.resources.s_sats_can_be_received
import bennu.composeapp.generated.resources.s_sats_in_s_channels
import bennu.composeapp.generated.resources.s_sats_wait_in_closed_channels
import bennu.composeapp.generated.resources.see_the_key_ceremony
import bennu.composeapp.generated.resources.send
@@ -84,6 +90,7 @@ import bennu.composeapp.generated.resources.sent_s_sats_with_a_fee_of_s_sats
import bennu.composeapp.generated.resources.signers_needed_s_of_s
import bennu.composeapp.generated.resources.step_down
import bennu.composeapp.generated.resources.sweep_into_the_onchain_wallet
import bennu.composeapp.generated.resources.sweep_s_sats
import bennu.composeapp.generated.resources.the_group_is_making_its_lightning_wallet_key
import bennu.composeapp.generated.resources.the_operator_hasn_t_reported_the_wallet_yet
import bennu.composeapp.generated.resources.the_operator_s_device_runs_the_node
@@ -225,6 +232,12 @@ fun GroupLightningWalletScreen(
onSend = { onNavigateToRoute(GroupLightningSendRoute(activeUserPublicKey = activeUserPublicKey, chatRoomId = chatRoomId)) },
onClose = { channelId, force -> viewModel.close(channelId, force, onSession = toSession, onFailure = { notify(couldNotPropose) }) },
onSweep = { viewModel.sweep(onSession = toSession, onFailure = { notify(couldNotPropose) }) },
onRekey = {
if (state.canOpenNew) viewModel.openWallet(onSession = toSession, onFailure = { notify(couldNotPropose) }) else toCeremony(null)
},
onCloseEarlier = { viewModel.closeEarlier(it, onSession = toSession, onFailure = { notify(couldNotPropose) }) },
onSweepEarlier = { viewModel.sweepEarlier(it, onSession = toSession, onFailure = { notify(couldNotPropose) }) },
onRunEarlier = { viewModel.nominateSelf(onSession = toSession, onFailure = { notify(couldNotPropose) }, wallet = it.wallet.statement.wallet) },
isPending = viewModel.isActionPending.value,
onNominate = { viewModel.nominateSelf(onSession = toSession, onFailure = { notify(couldNotPropose) }) },
onStepDown = { viewModel.stepDown(onSession = toSession, onFailure = { notify(couldNotPropose) }) },
@@ -235,7 +248,12 @@ fun GroupLightningWalletScreen(
}
@Composable
private fun OpenedWallet(modifier: Modifier, state: GroupLightningWalletUIState.Opened, onReceive: () -> Unit, onSend: () -> Unit, onClose: (channelId: String, force: Boolean) -> Unit, onSweep: () -> Unit, isPending: Boolean, onNominate: () -> Unit, onStepDown: () -> Unit) {
private fun OpenedWallet(modifier: Modifier, state: GroupLightningWalletUIState.Opened, onReceive: () -> Unit, onSend: () -> Unit, onClose: (channelId: String, force: Boolean) -> Unit, onSweep: () -> Unit,
onRekey: () -> Unit,
onCloseEarlier: (GroupLightningWalletUIState.Earlier) -> Unit,
onSweepEarlier: (GroupLightningWalletUIState.Earlier) -> Unit,
onRunEarlier: (GroupLightningWalletUIState.Earlier) -> Unit,
isPending: Boolean, onNominate: () -> Unit, onStepDown: () -> Unit) {
val statement = state.wallet.statement
val operatorName = state.operator?.publicKey?.let { key ->
if (state.isOperator) stringResource(Res.string.you)
@@ -245,6 +263,19 @@ private fun OpenedWallet(modifier: Modifier, state: GroupLightningWalletUIState.
modifier = modifier.readableContent().fillMaxSize().padding(MaterialTheme.spacing.screenMargin),
verticalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.itemGap),
) {
// A member left: until a new wallet replaces this one, they can still grief it with the seed.
if (state.departed.isNotEmpty()) {
item {
Card(modifier = Modifier.fillMaxWidth()) {
Column(modifier = Modifier.padding(MaterialTheme.spacing.containerPadding), verticalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.relatedGap)) {
Text(stringResource(Res.string.s_people_who_left_still_hold_a_share, state.departed.size.toString()), style = MaterialTheme.typography.bodyMedium, color = MaterialTheme.colorScheme.error)
Button(onClick = onRekey, enabled = !isPending) {
Text(stringResource(if (state.canOpenNew) Res.string.open_a_new_lightning_wallet else Res.string.make_a_key))
}
}
}
}
}
item { Balance(state.report, onReceive = onReceive.takeIf { state.operator?.publicKey != null }, onSend = onSend.takeIf { state.operator?.publicKey != null }) }
if (state.unswept.isNotEmpty()) {
item {
@@ -283,6 +314,16 @@ private fun OpenedWallet(modifier: Modifier, state: GroupLightningWalletUIState.
}
}
}
items(state.earlier, key = { "earlier:${it.wallet.statement.wallet}" }) { earlier ->
EarlierWallet(
earlier = earlier,
canRun = earlier.operator?.publicKey == null && state.localChatRoom.chatRoom.userPublicKey.lowercase() in earlier.wallet.statement.participants,
isPending = isPending,
onClose = { onCloseEarlier(earlier) },
onSweep = { onSweepEarlier(earlier) },
onRun = { onRunEarlier(earlier) },
)
}
item {
ListItem(
leadingContent = { Icon(Icons.Default.Key, contentDescription = Decorative) },
@@ -395,3 +436,27 @@ private fun PaymentRow(payment: GroupLightningPaymentRow) {
supportingContent = payment.description?.let { { Text(it, maxLines = 2, overflow = TextOverflow.Ellipsis) } },
)
}
/** A wallet a re-key left behind: what is still in it, and the two ways it reaches the onchain wallet. */
@Composable
private fun EarlierWallet(earlier: GroupLightningWalletUIState.Earlier, canRun: Boolean, isPending: Boolean, onClose: () -> Unit, onSweep: () -> Unit, onRun: () -> Unit) {
Card(modifier = Modifier.fillMaxWidth()) {
Column(modifier = Modifier.padding(MaterialTheme.spacing.containerPadding), verticalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.relatedGap)) {
Text(stringResource(Res.string.earlier_wallet_s, earlier.wallet.statement.wallet.toString()), style = MaterialTheme.typography.titleSmall)
earlier.report?.let { report ->
Text(stringResource(Res.string.s_sats_in_s_channels, Satoshi(report.snapshot.spendableMsat / 1000).grouped(), report.checks.size.toString()), style = MaterialTheme.typography.bodyMedium)
}
if (earlier.operator?.publicKey == null && earlier.openChannels.isNotEmpty()) {
Text(stringResource(Res.string.nobody_runs_this_wallet_s_node), style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant)
}
Row(horizontalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.itemGap)) {
if (earlier.openChannels.isNotEmpty()) OutlinedButton(onClick = onClose, enabled = !isPending) { Text(stringResource(Res.string.close_its_channels)) }
if (earlier.unswept.isNotEmpty()) {
OutlinedButton(onClick = onSweep, enabled = !isPending) { Text(stringResource(Res.string.sweep_s_sats, Satoshi(earlier.unswept.sumOf { it.amountSat }).grouped())) }
}
}
if (canRun) OutlinedButton(onClick = onRun, enabled = !isPending) { Text(stringResource(Res.string.run_this_wallet_on_my_device)) }
}
}
}
@@ -102,6 +102,7 @@ class ChatRoomDetailViewModel(
testNetworkWallet = if (GroupLightningRollout.ENABLED && GroupLightningRollout.NETWORK != NodeParamsManager.chain) {
chatRepository.groupWallet(chatRoomId, GroupLightningRollout.NETWORK)
} else null,
lightningParticipants = if (GroupLightningRollout.ENABLED) chatRepository.lightningParticipants(chatRoomId, GroupLightningRollout.NETWORK) else emptySet(),
canEditNostrProfile = canSign,
canAddSubgroup = canSign,
canGovern = canGovern,
@@ -50,7 +50,7 @@ class GroupLightningReceiveViewModel(
val localChatRoom = chatRepository.getChatRoomByIdentifier(chatRoomId)
?: return@launch run { uiState = GroupLightningReceiveUIState.Error("Couldn't find the group") }
val wallet = lightningRepository.lightningWallet(chatRoomId, chain)
uiState = if (wallet == null || lightningRepository.operator(chatRoomId, chain)?.publicKey == null) GroupLightningReceiveUIState.Unavailable
uiState = if (wallet == null || lightningRepository.operator(chatRoomId, chain, wallet.statement.wallet)?.publicKey == null) GroupLightningReceiveUIState.Unavailable
else GroupLightningReceiveUIState.Form(localChatRoom, wallet)
}
}
@@ -74,7 +74,7 @@ class GroupLightningReceiveViewModel(
isActionPending.value = false
if (requestId == null) return@launch withContext(Dispatchers.Main) { onFailure() }
uiState = GroupLightningReceiveUIState.Waiting(requestId)
val invoice = lightningRepository.observeInvoice(chatRoomId, chain, requestId).filterNotNull().first()
val invoice = lightningRepository.observeInvoice(chatRoomId, chain, form.wallet.statement.wallet, requestId).filterNotNull().first()
uiState = GroupLightningReceiveUIState.Invoice(invoice.bolt11)
}
}
@@ -53,7 +53,7 @@ class GroupLightningSendViewModel(
val localChatRoom = chatRepository.getChatRoomByIdentifier(chatRoomId)
?: return@launch run { uiState = GroupLightningSendUIState.Error("Couldn't find the group") }
val wallet = lightningRepository.lightningWallet(chatRoomId, chain)
uiState = if (wallet == null || lightningRepository.operator(chatRoomId, chain)?.publicKey == null) GroupLightningSendUIState.Unavailable
uiState = if (wallet == null || lightningRepository.operator(chatRoomId, chain, wallet.statement.wallet)?.publicKey == null) GroupLightningSendUIState.Unavailable
else GroupLightningSendUIState.Form(localChatRoom, wallet)
}
}
@@ -60,19 +60,30 @@ class GroupLightningWalletViewModel(
val key = lightningRepository.completedCeremony(chatRoomId)
when {
wallet != null -> {
val operator = lightningRepository.operator(chatRoomId, chain)
val operator = lightningRepository.operator(chatRoomId, chain, wallet.statement.wallet)
GroupLightningWalletUIState.Opened(
localChatRoom = localChatRoom,
wallet = wallet,
operator = operator,
isParticipant = activeUserPublicKey.lowercase() in wallet.statement.participants && wallet.ceremony?.share != null,
isOperator = operator?.publicKey == activeUserPublicKey,
departed = lightningRepository.departed(wallet),
canOpenNew = lightningRepository.hasUnusedKey(chatRoomId, chain) && canPropose(),
earlier = lightningRepository.earlierWallets(chatRoomId, chain).map { earlier ->
val report = lightningRepository.observeReport(chatRoomId, chain, earlier.statement.wallet).first()
GroupLightningWalletUIState.Earlier(
wallet = earlier,
operator = lightningRepository.operator(chatRoomId, chain, earlier.statement.wallet),
report = report,
unswept = lightningRepository.unswept(chatRoomId, report),
)
},
)
}
key != null -> GroupLightningWalletUIState.NotOpened(
localChatRoom = localChatRoom,
key = key,
canPropose = lightningRepository.lspNodeId(chain) != null && activeIdentityStateFlow.value?.nostrPrivateKey != null,
canPropose = canPropose(),
)
else -> {
val running = lightningRepository.observeCeremonies(chatRoomId).first().firstOrNull { it.stage != IcebergRitualStage.FAILED }
@@ -85,13 +96,17 @@ class GroupLightningWalletViewModel(
}
}
/** Whether this device can propose a wallet statement: an LSP to name, and a key to send the seed with. */
private fun canPropose(): Boolean = lightningRepository.lspNodeId(chain) != null && activeIdentityStateFlow.value?.nostrPrivateKey != null
private var watching: Job? = null
/** Keeps an opened wallet's snapshot and payments current as the operator's reports arrive. */
private fun watchReports() {
watching?.cancel()
watching = viewModelScope.launch(Dispatchers.IO) {
combine(lightningRepository.observeReport(chatRoomId, chain), lightningRepository.observePayments(chatRoomId, chain)) { report, payments -> report to payments }
val wallet = (uiState as? GroupLightningWalletUIState.Opened)?.wallet?.statement?.wallet ?: return@launch
combine(lightningRepository.observeReport(chatRoomId, chain, wallet), lightningRepository.observePayments(chatRoomId, chain, wallet)) { report, payments -> report to payments }
.collect { (report, payments) ->
val unswept = lightningRepository.unswept(chatRoomId, report)
(uiState as? GroupLightningWalletUIState.Opened)?.let { uiState = it.copy(report = report, payments = payments, unswept = unswept) }
@@ -101,19 +116,37 @@ class GroupLightningWalletViewModel(
/** Proposes the wallet's statement; the wallet exists once a quorum signs, on the session it returns. */
fun openWallet(onSession: (sessionId: String) -> Unit, onFailure: () -> Unit) {
val state = uiState as? GroupLightningWalletUIState.NotOpened ?: return
// The first wallet, or the one a re-key opens beside the current one under a new key.
val localChatRoom = (uiState as? GroupLightningWalletUIState.NotOpened)?.localChatRoom
?: (uiState as? GroupLightningWalletUIState.Opened)?.takeIf { it.canOpenNew }?.localChatRoom
?: return
val privateKey = activeIdentityStateFlow.value?.nostrPrivateKey?.value?.toByteArray() ?: return onFailure()
act(onSession, onFailure) { lightningRepository.proposeWallet(state.localChatRoom, activeUserPublicKey, privateKey, chain) }
act(onSession, onFailure) { lightningRepository.proposeWallet(localChatRoom, activeUserPublicKey, privateKey, chain) }
}
fun nominateSelf(onSession: (sessionId: String) -> Unit, onFailure: () -> Unit) {
/** Nominates this device to run [wallet]'s node: the current wallet's when null. */
fun nominateSelf(onSession: (sessionId: String) -> Unit, onFailure: () -> Unit, wallet: Long? = null) {
val state = uiState as? GroupLightningWalletUIState.Opened ?: return
act(onSession, onFailure) { lightningRepository.nominateSelf(state.localChatRoom, activeUserPublicKey, chain) }
act(onSession, onFailure) { lightningRepository.nominateSelf(state.localChatRoom, activeUserPublicKey, chain, wallet ?: state.wallet.statement.wallet) }
}
fun stepDown(onSession: (sessionId: String) -> Unit, onFailure: () -> Unit) {
val state = uiState as? GroupLightningWalletUIState.Opened ?: return
act(onSession, onFailure) { lightningRepository.stepDown(state.localChatRoom, activeUserPublicKey, chain) }
act(onSession, onFailure) { lightningRepository.stepDown(state.localChatRoom, activeUserPublicKey, chain, state.wallet.statement.wallet) }
}
/** Proposes closing every channel an earlier wallet still has, so its money reaches the onchain wallet. */
fun closeEarlier(earlier: GroupLightningWalletUIState.Earlier, onSession: (sessionId: String) -> Unit, onFailure: () -> Unit) {
val state = uiState as? GroupLightningWalletUIState.Opened ?: return
if (earlier.openChannels.isEmpty()) return
act(onSession, onFailure) { lightningRepository.proposeCloseAll(state.localChatRoom, activeUserPublicKey, earlier.wallet, earlier.openChannels) }
}
/** Proposes sweeping what an earlier wallet's closed channels left the group. */
fun sweepEarlier(earlier: GroupLightningWalletUIState.Earlier, onSession: (sessionId: String) -> Unit, onFailure: () -> Unit) {
val state = uiState as? GroupLightningWalletUIState.Opened ?: return
if (earlier.unswept.isEmpty()) return
act(onSession, onFailure) { lightningRepository.proposeSweep(state.localChatRoom, activeUserPublicKey, earlier.wallet, earlier.unswept) }
}
/** Proposes closing [channelId] to the onchain wallet, or by force when the LSP is gone. */
@@ -98,6 +98,8 @@ sealed interface ChatRoomDetailUIState {
* Lightning wallet is not offered on this build (`GroupLightningRollout`).
*/
val testNetworkWallet: GroupWallet? = null,
/** Who holds a share of the group's Lightning wallet key, so removing one of them can say what it leaves. */
val lightningParticipants: Set<HexKey> = emptySet(),
/**
* Whether this device could sign an event for the group, which on this
* screen gates the paste at the end of the identity block. Each of the
@@ -1,6 +1,7 @@
package com.machankura.bennu.compose.ui.view.state
import com.machankura.bennu.compose.database.model.IcebergKeySession
import com.vitorpamplona.quartz.nip01Core.core.HexKey
import com.machankura.bennu.compose.database.model.intermdiate.LocalChatRoom
import com.machankura.bennu.compose.database.model.GroupLightningPaymentRow
import com.machankura.bennu.compose.managers.GroupLightningOperatorManager
@@ -38,5 +39,21 @@ sealed class GroupLightningWalletUIState {
val payments: List<GroupLightningPaymentRow> = emptyList(),
/** What closed channels left the group that no sweep spends yet. */
val unswept: List<GroupSweep.Input> = emptyList(),
/** Participants who have left the room, and still hold a share of the key and the node's seed. */
val departed: List<HexKey> = emptyList(),
/** Whether a new wallet can be opened now: a key no wallet uses, and this device can propose. */
val canOpenNew: Boolean = false,
/** The wallets a re-key left behind, whose channels are still to be closed and swept. */
val earlier: List<Earlier> = emptyList(),
) : GroupLightningWalletUIState()
/** A wallet a re-key left behind, with what its operator last said of it. */
data class Earlier(
val wallet: GroupLightningWalletManager.Wallet,
val operator: GroupLightningOperatorManager.Operator?,
val report: GroupLightningReports.Report?,
val unswept: List<GroupSweep.Input>,
) {
val openChannels: List<String> get() = report?.checks?.map { it.channel.channelId }.orEmpty()
}
}
@@ -41,25 +41,35 @@ class GroupLightningNodes(
private val nodes = mutableMapOf<String, GroupLightningNode>()
private var job: Job? = null
fun nodeFor(chatRoomId: String): GroupLightningNode? = nodes[chatRoomId]
/** The node of [chatRoomId]'s wallet [wallet] -- its current one when null -- if this device runs it. */
fun nodeFor(chatRoomId: String, wallet: Long? = null): GroupLightningNode? =
if (wallet != null) nodes[key(chatRoomId, wallet)]
else nodes.filterKeys { it.startsWith("$chatRoomId:") }.maxByOrNull { it.value.wallet.statement.wallet }?.value
/** Starts every node this device may run, and forgets the ones that stopped. */
private fun key(chatRoomId: String, wallet: Long) = "$chatRoomId:$wallet"
/**
* Starts every node this device may run, and forgets the ones that stopped. After a re-key a room has
* two wallets, each with its own operator: the old one runs until its channels are closed (Phase 14).
*/
suspend fun refresh() = mutex.withLock {
if (!GroupLightningRollout.ENABLED && chain != Chain.Regtest) return@withLock
val rooms = database.icebergKeySessionDao().getCompletedSessions().filter { it.userPublicKey == userPublicKey }.map { it.chatRoomId }.toSet()
for (roomId in rooms) {
val localChatRoom = database.chatRoomDao().findChatRoomById(roomId) ?: continue
val wallet = GroupLightningWalletManager.walletFor(database, roomId, chain) ?: continue
val operator = GroupLightningOperatorManager.current(database, roomId, chain, wallet.statement.wallet)
val existing = nodes[roomId]
if (existing?.state?.value is GroupLightningNode.State.Running) continue
if (operator?.publicKey != userPublicKey) {
nodes.remove(roomId)
continue
for (wallet in GroupLightningWalletManager.wallets(database, roomId, chain)) {
val key = key(roomId, wallet.statement.wallet)
val operator = GroupLightningOperatorManager.current(database, roomId, chain, wallet.statement.wallet)
val existing = nodes[key]
if (existing?.state?.value is GroupLightningNode.State.Running) continue
if (operator?.publicKey != userPublicKey) {
nodes.remove(key)
continue
}
val node = existing ?: GroupLightningNode(database, localChatRoom, userPublicKey, wallet, scope, peerFactory).also { nodes[key] = it }
val state = runCatching { node.start() }.onFailure { logger.e("Could not start the node for $key", it) }.getOrNull()
if (state is GroupLightningNode.State.Refused) logger.i("Not running the node for $key: ${state.reason}")
}
val node = existing ?: GroupLightningNode(database, localChatRoom, userPublicKey, wallet, scope, peerFactory).also { nodes[roomId] = it }
val state = runCatching { node.start() }.onFailure { logger.e("Could not start the node for $roomId", it) }.getOrNull()
if (state is GroupLightningNode.State.Refused) logger.i("Not running the node for $roomId: ${state.reason}")
}
}