Skip to content

Commit fe0b0f4

Browse files
committed
(android) Use unsigned tx to infer amount/index in swap-in signer
Index may not be 0. We can also remove the amount field in the UI.
1 parent 017868d commit fe0b0f4

File tree

3 files changed

+14
-34
lines changed

3 files changed

+14
-34
lines changed

phoenix-android/src/main/kotlin/fr/acinq/phoenix/android/settings/walletinfo/SwapInSignerView.kt

+2-14
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ fun SwapInSignerView(
6767
onBackClick: () -> Unit,
6868
) {
6969
val context = LocalContext.current
70-
val vm = viewModel<SwapInSignerViewModel>(factory = SwapInSignerViewModel.Factory(business.walletManager))
70+
val vm = viewModel<SwapInSignerViewModel>(factory = SwapInSignerViewModel.Factory(business.walletManager, business.electrumClient))
7171

7272
var amountInput by remember { mutableStateOf<MilliSatoshi?>(null) }
7373
var txInput by remember { mutableStateOf("") }
@@ -82,18 +82,6 @@ fun SwapInSignerView(
8282
val state = vm.state.value
8383
Text(text = stringResource(id = R.string.swapin_signer_instructions))
8484
Spacer(modifier = Modifier.height(16.dp))
85-
AmountInput(
86-
amount = amountInput,
87-
onAmountChange = {
88-
amountInput = it?.amount
89-
if (state != SwapInSignerState.Init) vm.state.value = SwapInSignerState.Init
90-
},
91-
staticLabel = stringResource(id = R.string.swapin_signer_amount),
92-
enabled = state !is SwapInSignerState.Signing,
93-
modifier = Modifier.fillMaxWidth(),
94-
forceUnit = BitcoinUnit.Sat,
95-
)
96-
Spacer(modifier = Modifier.height(8.dp))
9785
TextInput(
9886
text = txInput,
9987
onTextChange = {
@@ -116,7 +104,7 @@ fun SwapInSignerView(
116104
icon = R.drawable.ic_check,
117105
onClick = {
118106
if (amountInput != null && txInput.isNotBlank()) {
119-
vm.sign(unsignedTx = txInput, amount = amountInput!!.truncateToSatoshi())
107+
vm.sign(unsignedTx = txInput)
120108
keyboardManager?.hide()
121109
}
122110
},

phoenix-android/src/main/kotlin/fr/acinq/phoenix/android/settings/walletinfo/SwapInSignerViewModel.kt

+12-19
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,8 @@ import androidx.compose.runtime.mutableStateOf
2020
import androidx.lifecycle.ViewModel
2121
import androidx.lifecycle.ViewModelProvider
2222
import androidx.lifecycle.viewModelScope
23-
import fr.acinq.bitcoin.ByteVector
24-
import fr.acinq.bitcoin.Satoshi
2523
import fr.acinq.bitcoin.Transaction
26-
import fr.acinq.bitcoin.TxOut
27-
import fr.acinq.lightning.MilliSatoshi
28-
import fr.acinq.lightning.crypto.KeyManager
29-
import fr.acinq.lightning.crypto.LocalKeyManager
30-
import fr.acinq.lightning.transactions.Transactions
31-
import fr.acinq.phoenix.managers.NodeParamsManager
32-
import fr.acinq.phoenix.managers.PeerManager
24+
import fr.acinq.lightning.blockchain.electrum.ElectrumClient
3325
import fr.acinq.phoenix.managers.WalletManager
3426
import kotlinx.coroutines.CoroutineExceptionHandler
3527
import kotlinx.coroutines.Dispatchers
@@ -42,7 +34,6 @@ sealed class SwapInSignerState {
4234
object Init : SwapInSignerState()
4335
object Signing : SwapInSignerState()
4436
data class Signed(
45-
val amount: Satoshi,
4637
val txId: String,
4738
val userSig: String,
4839
) : SwapInSignerState()
@@ -53,39 +44,40 @@ sealed class SwapInSignerState {
5344
}
5445

5546
class SwapInSignerViewModel(
56-
val walletManager: WalletManager,
47+
private val walletManager: WalletManager,
48+
private val electrumClient: ElectrumClient,
5749
) : ViewModel() {
5850

59-
val log = LoggerFactory.getLogger(this::class.java)
51+
private val log = LoggerFactory.getLogger(this::class.java)
6052
val state = mutableStateOf<SwapInSignerState>(SwapInSignerState.Init)
6153

6254
fun sign(
6355
unsignedTx: String,
64-
amount: Satoshi,
6556
) {
6657
if (state.value == SwapInSignerState.Signing) return
6758
state.value = SwapInSignerState.Signing
6859

6960
viewModelScope.launch(Dispatchers.Default + CoroutineExceptionHandler { _, e ->
70-
log.error("failed to sign tx=$unsignedTx for amount=$amount : ", e)
61+
log.error("failed to sign tx=$unsignedTx: ", e)
7162
state.value = SwapInSignerState.Failed.Error(e)
7263
}) {
73-
log.debug("signing tx=$unsignedTx amount=$amount")
64+
log.debug("signing tx=$unsignedTx")
7465
val tx = try {
7566
Transaction.read(unsignedTx)
7667
} catch (e: Exception) {
7768
log.error("invalid transaction input: ", e)
7869
state.value = SwapInSignerState.Failed.InvalidTxInput(e)
7970
return@launch
8071
}
72+
val input = if (tx.txIn.size == 1) tx.txIn.first() else throw RuntimeException("tx has ${tx.txIn.size} inputs")
73+
val prevTx = electrumClient.getTx(input.outPoint.txid) ?: throw RuntimeException("parent tx not found by electrum")
8174
val keyManager = walletManager.keyManager.filterNotNull().first()
8275
val userSig = keyManager.swapInOnChainWallet.signSwapInputUserLegacy(
8376
fundingTx = tx,
84-
index = 0,
85-
parentTxOuts = listOf(TxOut(amount, ByteVector.empty)),
77+
index = input.outPoint.index.toInt(),
78+
parentTxOuts = prevTx.txOut,
8679
)
8780
state.value = SwapInSignerState.Signed(
88-
amount = amount,
8981
txId = tx.txid.toString(),
9082
userSig = userSig.toString(),
9183
)
@@ -94,10 +86,11 @@ class SwapInSignerViewModel(
9486

9587
class Factory(
9688
private val walletManager: WalletManager,
89+
private val electrumClient: ElectrumClient,
9790
) : ViewModelProvider.Factory {
9891
override fun <T : ViewModel> create(modelClass: Class<T>): T {
9992
@Suppress("UNCHECKED_CAST")
100-
return SwapInSignerViewModel(walletManager) as T
93+
return SwapInSignerViewModel(walletManager, electrumClient) as T
10194
}
10295
}
10396
}

phoenix-android/src/main/res/values/strings.xml

-1
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,6 @@
775775

776776
<string name="swapin_signer_title">Swap-in signer</string>
777777
<string name="swapin_signer_instructions">This debugging tool lets you sign swap-in inputs. Only use if you understand what it does.</string>
778-
<string name="swapin_signer_amount">Amount</string>
779778
<string name="swapin_signer_tx">Unsigned tx</string>
780779
<string name="swapin_signer_sign">Sign</string>
781780
<string name="swapin_signer_signing">Signing…</string>

0 commit comments

Comments
 (0)