-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(android) Add swap-in input signing tool
This tool is for debugging purposes. It lets users sign a swap-in input locked in the potentiam scheme, and unlock it in cooperation with the peer.
- Loading branch information
Showing
13 changed files
with
353 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
168 changes: 168 additions & 0 deletions
168
...-android/src/main/kotlin/fr/acinq/phoenix/android/settings/walletinfo/SwapInSignerView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
/* | ||
* Copyright 2024 ACINQ SAS | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package fr.acinq.phoenix.android.settings.walletinfo | ||
|
||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.PaddingValues | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.Spacer | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.height | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.layout.width | ||
import androidx.compose.material.MaterialTheme | ||
import androidx.compose.material.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.collectAsState | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.ExperimentalComposeUiApi | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.platform.LocalSoftwareKeyboardController | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.unit.dp | ||
import androidx.lifecycle.viewmodel.compose.viewModel | ||
import fr.acinq.bitcoin.Satoshi | ||
import fr.acinq.lightning.MilliSatoshi | ||
import fr.acinq.phoenix.android.LocalBitcoinUnit | ||
import fr.acinq.phoenix.android.business | ||
import fr.acinq.phoenix.android.components.DefaultScreenHeader | ||
import fr.acinq.phoenix.android.components.DefaultScreenLayout | ||
import fr.acinq.phoenix.android.R | ||
import fr.acinq.phoenix.android.components.AmountInput | ||
import fr.acinq.phoenix.android.components.BorderButton | ||
import fr.acinq.phoenix.android.components.Button | ||
import fr.acinq.phoenix.android.components.Card | ||
import fr.acinq.phoenix.android.components.InlineSatoshiInput | ||
import fr.acinq.phoenix.android.components.ProgressView | ||
import fr.acinq.phoenix.android.components.TextInput | ||
import fr.acinq.phoenix.android.components.feedback.ErrorMessage | ||
import fr.acinq.phoenix.android.settings.channels.ImportChannelsDataViewModel | ||
import fr.acinq.phoenix.android.utils.copyToClipboard | ||
import fr.acinq.phoenix.data.BitcoinUnit | ||
|
||
|
||
@OptIn(ExperimentalComposeUiApi::class) | ||
@Composable | ||
fun SwapInSignerView( | ||
onBackClick: () -> Unit, | ||
) { | ||
val context = LocalContext.current | ||
val vm = viewModel<SwapInSignerViewModel>(factory = SwapInSignerViewModel.Factory(business.walletManager)) | ||
|
||
var amountInput by remember { mutableStateOf<MilliSatoshi?>(null) } | ||
var txInput by remember { mutableStateOf("") } | ||
|
||
DefaultScreenLayout(isScrollable = true) { | ||
DefaultScreenHeader( | ||
onBackClick = onBackClick, | ||
title = stringResource(id = R.string.swapin_signer_title), | ||
) | ||
|
||
Card(internalPadding = PaddingValues(horizontal = 16.dp, vertical = 12.dp)) { | ||
val state = vm.state.value | ||
Text(text = stringResource(id = R.string.swapin_signer_instructions)) | ||
Spacer(modifier = Modifier.height(16.dp)) | ||
AmountInput( | ||
amount = amountInput, | ||
onAmountChange = { | ||
amountInput = it?.amount | ||
if (state != SwapInSignerState.Init) vm.state.value = SwapInSignerState.Init | ||
}, | ||
staticLabel = stringResource(id = R.string.swapin_signer_amount), | ||
enabled = state !is SwapInSignerState.Signing, | ||
modifier = Modifier.fillMaxWidth(), | ||
forceUnit = BitcoinUnit.Sat, | ||
) | ||
Spacer(modifier = Modifier.height(8.dp)) | ||
TextInput( | ||
text = txInput, | ||
onTextChange = { | ||
txInput = it | ||
if (state != SwapInSignerState.Init) vm.state.value = SwapInSignerState.Init | ||
}, | ||
staticLabel = stringResource(id = R.string.swapin_signer_tx), | ||
maxLines = 4, | ||
enabled = state !is SwapInSignerState.Signing, | ||
errorMessage = if (txInput.isBlank()) stringResource(id = R.string.validation_empty) else null | ||
) | ||
} | ||
|
||
val keyboardManager = LocalSoftwareKeyboardController.current | ||
Card(horizontalAlignment = Alignment.CenterHorizontally) { | ||
when (val state = vm.state.value) { | ||
is SwapInSignerState.Init -> { | ||
Button( | ||
text = stringResource(id = R.string.swapin_signer_sign), | ||
icon = R.drawable.ic_check, | ||
onClick = { | ||
if (amountInput != null && txInput.isNotBlank()) { | ||
vm.sign(unsignedTx = txInput, amount = amountInput!!.truncateToSatoshi()) | ||
keyboardManager?.hide() | ||
} | ||
}, | ||
modifier = Modifier.fillMaxWidth() | ||
) | ||
} | ||
is SwapInSignerState.Signing -> { | ||
ProgressView(text = stringResource(id = R.string.swapin_signer_signing)) | ||
} | ||
is SwapInSignerState.Signed -> { | ||
Column(modifier = Modifier.padding(PaddingValues(horizontal = 16.dp, vertical = 12.dp))) { | ||
Row(horizontalArrangement = Arrangement.spacedBy(6.dp)) { | ||
Text(text = stringResource(id = R.string.swapin_signer_signed_sig), style = MaterialTheme.typography.body2, modifier = Modifier.width(100.dp)) | ||
Text(text = state.userSig) | ||
} | ||
Spacer(modifier = Modifier.height(16.dp)) | ||
BorderButton( | ||
text = stringResource(id = R.string.btn_copy), | ||
icon = R.drawable.ic_copy, | ||
onClick = { | ||
copyToClipboard( | ||
context = context, | ||
data = """ | ||
user_sig=${state.userSig} | ||
""".trimIndent(), | ||
dataLabel = "swap input signature" | ||
) | ||
} | ||
) | ||
} | ||
} | ||
is SwapInSignerState.Failed.Error -> { | ||
ErrorMessage( | ||
header = stringResource(id = R.string.swapin_signer_error_header), | ||
details = state.cause.message ?: state.cause::class.java.simpleName, | ||
modifier = Modifier.fillMaxWidth(), | ||
) | ||
} | ||
is SwapInSignerState.Failed.InvalidTxInput -> { | ||
ErrorMessage( | ||
header = stringResource(id = R.string.swapin_signer_invalid_tx_header), | ||
details = stringResource(id = R.string.swapin_signer_invalid_tx_details), | ||
modifier = Modifier.fillMaxWidth(), | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
...oid/src/main/kotlin/fr/acinq/phoenix/android/settings/walletinfo/SwapInSignerViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* Copyright 2024 ACINQ SAS | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package fr.acinq.phoenix.android.settings.walletinfo | ||
|
||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.ViewModelProvider | ||
import androidx.lifecycle.viewModelScope | ||
import fr.acinq.bitcoin.ByteVector | ||
import fr.acinq.bitcoin.Satoshi | ||
import fr.acinq.bitcoin.Transaction | ||
import fr.acinq.bitcoin.TxOut | ||
import fr.acinq.lightning.MilliSatoshi | ||
import fr.acinq.lightning.crypto.KeyManager | ||
import fr.acinq.lightning.crypto.LocalKeyManager | ||
import fr.acinq.lightning.transactions.Transactions | ||
import fr.acinq.phoenix.managers.NodeParamsManager | ||
import fr.acinq.phoenix.managers.PeerManager | ||
import fr.acinq.phoenix.managers.WalletManager | ||
import kotlinx.coroutines.CoroutineExceptionHandler | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.filterNotNull | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.launch | ||
import org.slf4j.LoggerFactory | ||
|
||
sealed class SwapInSignerState { | ||
object Init : SwapInSignerState() | ||
object Signing : SwapInSignerState() | ||
data class Signed( | ||
val amount: Satoshi, | ||
val txId: String, | ||
val userSig: String, | ||
) : SwapInSignerState() | ||
sealed class Failed : SwapInSignerState() { | ||
data class InvalidTxInput(val cause: Throwable) : Failed() | ||
data class Error(val cause: Throwable) : Failed() | ||
} | ||
} | ||
|
||
class SwapInSignerViewModel( | ||
val walletManager: WalletManager, | ||
) : ViewModel() { | ||
|
||
val log = LoggerFactory.getLogger(this::class.java) | ||
val state = mutableStateOf<SwapInSignerState>(SwapInSignerState.Init) | ||
|
||
fun sign( | ||
unsignedTx: String, | ||
amount: Satoshi, | ||
) { | ||
if (state.value == SwapInSignerState.Signing) return | ||
state.value = SwapInSignerState.Signing | ||
|
||
viewModelScope.launch(Dispatchers.Default + CoroutineExceptionHandler { _, e -> | ||
log.error("failed to sign tx=$unsignedTx for amount=$amount : ", e) | ||
state.value = SwapInSignerState.Failed.Error(e) | ||
}) { | ||
log.debug("signing tx=$unsignedTx amount=$amount") | ||
val tx = try { | ||
Transaction.read(unsignedTx) | ||
} catch (e: Exception) { | ||
log.error("invalid transaction input: ", e) | ||
state.value = SwapInSignerState.Failed.InvalidTxInput(e) | ||
return@launch | ||
} | ||
val keyManager = walletManager.keyManager.filterNotNull().first() | ||
val userSig = Transactions.signSwapInputUser( | ||
fundingTx = tx, | ||
index = 0, | ||
parentTxOut = TxOut(amount, ByteVector.empty), | ||
userKey = keyManager.swapInOnChainWallet.userPrivateKey, | ||
serverKey = keyManager.swapInOnChainWallet.remoteServerPublicKey, | ||
refundDelay = 144 * 30 * 6 | ||
) | ||
state.value = SwapInSignerState.Signed( | ||
amount = amount, | ||
txId = tx.txid.toString(), | ||
userSig = userSig.toString() | ||
) | ||
} | ||
} | ||
|
||
class Factory( | ||
private val walletManager: WalletManager, | ||
) : ViewModelProvider.Factory { | ||
override fun <T : ViewModel> create(modelClass: Class<T>): T { | ||
@Suppress("UNCHECKED_CAST") | ||
return SwapInSignerViewModel(walletManager) as T | ||
} | ||
} | ||
} |
Oops, something went wrong.