@@ -20,16 +20,8 @@ import androidx.compose.runtime.mutableStateOf
20
20
import androidx.lifecycle.ViewModel
21
21
import androidx.lifecycle.ViewModelProvider
22
22
import androidx.lifecycle.viewModelScope
23
- import fr.acinq.bitcoin.ByteVector
24
- import fr.acinq.bitcoin.Satoshi
25
23
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
33
25
import fr.acinq.phoenix.managers.WalletManager
34
26
import kotlinx.coroutines.CoroutineExceptionHandler
35
27
import kotlinx.coroutines.Dispatchers
@@ -42,7 +34,6 @@ sealed class SwapInSignerState {
42
34
object Init : SwapInSignerState()
43
35
object Signing : SwapInSignerState()
44
36
data class Signed (
45
- val amount : Satoshi ,
46
37
val txId : String ,
47
38
val userSig : String ,
48
39
) : SwapInSignerState()
@@ -53,39 +44,40 @@ sealed class SwapInSignerState {
53
44
}
54
45
55
46
class SwapInSignerViewModel (
56
- val walletManager : WalletManager ,
47
+ private val walletManager : WalletManager ,
48
+ private val electrumClient : ElectrumClient ,
57
49
) : ViewModel() {
58
50
59
- val log = LoggerFactory .getLogger(this ::class .java)
51
+ private val log = LoggerFactory .getLogger(this ::class .java)
60
52
val state = mutableStateOf<SwapInSignerState >(SwapInSignerState .Init )
61
53
62
54
fun sign (
63
55
unsignedTx : String ,
64
- amount : Satoshi ,
65
56
) {
66
57
if (state.value == SwapInSignerState .Signing ) return
67
58
state.value = SwapInSignerState .Signing
68
59
69
60
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)
71
62
state.value = SwapInSignerState .Failed .Error (e)
72
63
}) {
73
- log.debug(" signing tx=$unsignedTx amount= $amount " )
64
+ log.debug(" signing tx=$unsignedTx " )
74
65
val tx = try {
75
66
Transaction .read(unsignedTx)
76
67
} catch (e: Exception ) {
77
68
log.error(" invalid transaction input: " , e)
78
69
state.value = SwapInSignerState .Failed .InvalidTxInput (e)
79
70
return @launch
80
71
}
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" )
81
74
val keyManager = walletManager.keyManager.filterNotNull().first()
82
75
val userSig = keyManager.swapInOnChainWallet.signSwapInputUserLegacy(
83
76
fundingTx = tx,
84
- index = 0 ,
85
- parentTxOuts = listOf ( TxOut (amount, ByteVector .empty)) ,
77
+ index = input.outPoint.index.toInt() ,
78
+ parentTxOuts = prevTx.txOut ,
86
79
)
87
80
state.value = SwapInSignerState .Signed (
88
- amount = amount,
89
81
txId = tx.txid.toString(),
90
82
userSig = userSig.toString(),
91
83
)
@@ -94,10 +86,11 @@ class SwapInSignerViewModel(
94
86
95
87
class Factory (
96
88
private val walletManager : WalletManager ,
89
+ private val electrumClient : ElectrumClient ,
97
90
) : ViewModelProvider.Factory {
98
91
override fun <T : ViewModel > create (modelClass : Class <T >): T {
99
92
@Suppress(" UNCHECKED_CAST" )
100
- return SwapInSignerViewModel (walletManager) as T
93
+ return SwapInSignerViewModel (walletManager, electrumClient ) as T
101
94
}
102
95
}
103
96
}
0 commit comments