Skip to content

Commit

Permalink
(android) Provide set of trusted swap-in txs instead of a boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
dpad85 committed Jun 30, 2023
1 parent 57eefbe commit 9fca7e4
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 18 deletions.
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
object Versions {
const val lightningKmp = "1.5.0"
const val lightningKmp = "1.5.1-SNAPSHOT"
const val secp256k1 = "0.7.0"
const val torMobile = "0.2.0"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.google.android.gms.tasks.OnCompleteListener
import com.google.firebase.messaging.FirebaseMessaging
import fr.acinq.bitcoin.ByteVector32
import fr.acinq.lightning.LiquidityEvents
import fr.acinq.lightning.MilliSatoshi
import fr.acinq.lightning.io.PaymentReceived
Expand Down Expand Up @@ -220,7 +221,7 @@ class NodeService : Service() {
val electrumServer = UserPrefs.getElectrumServer(applicationContext).first()
val isTorEnabled = UserPrefs.getIsTorEnabled(applicationContext).first()
val liquidityPolicy = UserPrefs.getLiquidityPolicy(applicationContext).first()
val isMigrationFromAndroidLegacyApp = PrefsDatastore.getLegacyMigrationPeerFlag(applicationContext).first() ?: false
val trustedSwapInTxs = PrefsDatastore.getMigrationTrustedSwapInTxs(applicationContext).first()
val preferredFiatCurrency = UserPrefs.getFiatCurrency(applicationContext).first()
val seed = business.walletManager.mnemonicsToSeed(EncryptedSeed.toMnemonics(decryptedPayload))

Expand All @@ -234,7 +235,7 @@ class NodeService : Service() {
requestCheckLegacyChannels = requestCheckLegacyChannels,
isTorEnabled = isTorEnabled,
liquidityPolicy = liquidityPolicy,
isMigrationFromAndroidLegacyApp = isMigrationFromAndroidLegacyApp
trustedSwapInTxs = trustedSwapInTxs.map { ByteVector32.fromValidHex(it) }.toSet()
)
)
business.appConfigurationManager.updateElectrumConfig(electrumServer)
Expand All @@ -251,10 +252,6 @@ class NodeService : Service() {
}
}
ChannelsWatcher.schedule(applicationContext)
// update migration flag to false, if necessary - only used once.
if (isMigrationFromAndroidLegacyApp) {
PrefsDatastore.saveLegacyMigrationPeerFlag(applicationContext, false)
}
return WalletState.Started.Kmm(business)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import kotlinx.coroutines.launch
import org.bouncycastle.util.encoders.Hex
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import scala.collection.JavaConverters
import scala.util.Either
import scala.util.Left
import scodec.bits.ByteVector
Expand Down Expand Up @@ -237,6 +238,7 @@ class MigrationDialogViewModel : ViewModel() {
// wait for the closing transaction to be published for each channel
// the verification is done every 3 seconds
val channelsPublicationStatusMap = channelsClosed.associateWith { false }.toMutableMap()
val mutualClosePublishedTxs = mutableSetOf<String>()
while (channelsPublicationStatusMap.any { !it.value }) {
val notClosedChannels = channelsPublicationStatusMap.filter { !it.value }.keys
log.info("(migration) checking mutual-close-publish status for ${notClosedChannels.size} channels...")
Expand All @@ -248,6 +250,10 @@ class MigrationDialogViewModel : ViewModel() {
log.debug("(migration) could not get publication status for channel=$it")
}
data is DATA_CLOSING && !data.mutualClosePublished().isEmpty -> {
val mutualCloseTxs = JavaConverters.asJavaCollectionConverter(data.mutualClosePublished()).asJavaCollection().map {
it.txid().toString()
}
mutualClosePublishedTxs.addAll(mutualCloseTxs)
log.info("(migration) mutual-close published for channel=$it")
channelsPublicationStatusMap[it] = true
state.value = MigrationScreenState.Processing.MonitoringChannelsPublication(swapInAddress, channelsClosed, channelsPublicationStatusMap.filter { it.value }.keys)
Expand All @@ -262,7 +268,7 @@ class MigrationDialogViewModel : ViewModel() {
}

// migration is successful, update state
log.info("(migration) ${channelsPublicationStatusMap.size} channels have been closed to $swapInAddress")
log.info("(migration) ${channelsPublicationStatusMap.size} channels have been closed to $swapInAddress, txs=${mutualClosePublishedTxs}")
state.value = MigrationScreenState.Complete(swapInAddress, channelsPublicationStatusMap.keys)

// pause then update preferences to switch to the new app
Expand All @@ -275,7 +281,7 @@ class MigrationDialogViewModel : ViewModel() {
address = swapInAddress
)
)
PrefsDatastore.saveLegacyMigrationPeerFlag(context, true)
PrefsDatastore.saveMigrationTrustedSwapInTxs(context, mutualClosePublishedTxs)
PrefsDatastore.saveStartLegacyApp(context, LegacyAppStatus.NotRequired)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ object PrefsDatastore {
it[DATA_MIGRATION_EXPECTED] = isExpected
}

/** Used to know whether the peer has already been started with the `isMigrationFromLegacyApp` flag. */
private val LEGACY_MIGRATION_PEER_FLAG = booleanPreferencesKey("LEGACY_MIGRATION_PEER_FLAG")
fun getLegacyMigrationPeerFlag(context: Context): Flow<Boolean?> = prefs(context).map { it[LEGACY_MIGRATION_PEER_FLAG] }
suspend fun saveLegacyMigrationPeerFlag(context: Context, flag: Boolean) = context.internalData.edit {
it[LEGACY_MIGRATION_PEER_FLAG] = flag
/** List of transaction ids that can be used for swap-in, even if zero conf. Used for migration. */
private val MIGRATION_TRUSTED_SWAP_IN_TXS = stringSetPreferencesKey("MIGRATION_TRUSTED_SWAP_IN_TXS")
fun getMigrationTrustedSwapInTxs(context: Context): Flow<Set<String>> = prefs(context).map { it[MIGRATION_TRUSTED_SWAP_IN_TXS] ?: emptySet() }
suspend fun saveMigrationTrustedSwapInTxs(context: Context, txs: Set<String>) = context.internalData.edit {
it[MIGRATION_TRUSTED_SWAP_IN_TXS] = txs
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fr.acinq.phoenix.data

import fr.acinq.bitcoin.ByteVector32
import fr.acinq.bitcoin.Satoshi
import fr.acinq.lightning.payment.LiquidityPolicy
import fr.acinq.lightning.utils.ServerAddress
Expand Down Expand Up @@ -201,7 +202,7 @@ sealed class ElectrumConfig {
return when (this) {
is Custom -> {
when (other) {
is Custom -> this == other // custom =?= custom
is Custom -> this === other // custom =?= custom
is Random -> false // custom != random
}
}
Expand All @@ -222,8 +223,8 @@ data class StartupParams(
val isTorEnabled: Boolean,
/** The liquidity policy must be injected into the node params manager. */
val liquidityPolicy: LiquidityPolicy,
/** Should always be false for iOS. If true, the peer is initialized with a special flag used in the swap-in process. */
val isMigrationFromAndroidLegacyApp: Boolean = false,
/** List of transaction ids that can be used for swap-in even if they are zero-conf. */
val trustedSwapInTxs: Set<ByteVector32>,
// TODO: add custom electrum address, fiat currencies, ...
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class PeerManager(
walletParams = walletParams,
watcher = electrumWatcher,
db = databaseManager.databases.filterNotNull().first(),
isMigrationFromLegacyApp = startupParams.isMigrationFromAndroidLegacyApp,
trustedSwapInTxs = startupParams.trustedSwapInTxs,
socketBuilder = null,
scope = MainScope()
)
Expand Down

0 comments on commit 9fca7e4

Please sign in to comment.