From 82e0aba3e0d79b9229d02cf098e956212e04e6e4 Mon Sep 17 00:00:00 2001 From: KonradStaniec Date: Tue, 15 Oct 2024 17:22:37 +0200 Subject: [PATCH] Fix: do not send signed staking transaction to btc --- staker/stakerapp.go | 23 ++++++++++++++++++++--- stakerdb/trackedtransactionstore_test.go | 2 +- walletcontroller/interface.go | 2 +- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/staker/stakerapp.go b/staker/stakerapp.go index 5e4787e..d232079 100644 --- a/staker/stakerapp.go +++ b/staker/stakerapp.go @@ -1316,7 +1316,23 @@ func (app *StakerApp) handlePreApprovalCmd(cmd *stakingRequestCmd) error { func (app *StakerApp) handlePostApprovalCmd(cmd *stakingRequestCmd) error { bestBlockHeight := app.currentBestBlockHeight.Load() - _, err := app.wc.SendRawTransaction(cmd.stakingTx, true) + err := app.wc.UnlockWallet(defaultWalletUnlockTimeout) + + if err != nil { + return err + } + + tx, fullySignd, err := app.wc.SignRawTransaction(cmd.stakingTx) + + if err != nil { + return err + } + + if !fullySignd { + return fmt.Errorf("failed to fully sign transaction with hash %s", cmd.stakingTxHash) + } + + _, err = app.wc.SendRawTransaction(tx, true) if err != nil { return err @@ -1780,8 +1796,9 @@ func (app *StakerApp) StakeFunds( feeRate := app.feeEstimator.EstimateFeePerKb() - // Create unsigned transaction by wallet - tx, err := app.wc.CreateAndSignTx([]*wire.TxOut{stakingInfo.StakingOutput}, btcutil.Amount(feeRate), stakerAddress) + // Create unsigned transaction by wallet without signing. Signing will happen + // in next steps + tx, err := app.wc.CreateTransaction([]*wire.TxOut{stakingInfo.StakingOutput}, btcutil.Amount(feeRate), stakerAddress) if err != nil { return nil, err diff --git a/stakerdb/trackedtransactionstore_test.go b/stakerdb/trackedtransactionstore_test.go index a1b4016..a22544d 100644 --- a/stakerdb/trackedtransactionstore_test.go +++ b/stakerdb/trackedtransactionstore_test.go @@ -195,7 +195,7 @@ func TestStateTransitions(t *testing.T) { // Inital state storedTx, err := s.GetTransaction(&txHash) require.NoError(t, err) - require.Equal(t, proto.TransactionState_TRANSACTION_CREATED, storedTx.State) + require.Equal(t, proto.TransactionState_SENT_TO_BTC, storedTx.State) require.Equal(t, uint64(1), storedTx.StoredTransactionIdx) // Confirmed hash := datagen.GenRandomBtcdHash(r) diff --git a/walletcontroller/interface.go b/walletcontroller/interface.go index 8dd4f4c..2a776ba 100644 --- a/walletcontroller/interface.go +++ b/walletcontroller/interface.go @@ -49,7 +49,7 @@ type WalletController interface { SignRawTransaction(tx *wire.MsgTx) (*wire.MsgTx, bool, error) // requires wallet to be unlocked CreateAndSignTx( - output []*wire.TxOut, + outputs []*wire.TxOut, feeRatePerKb btcutil.Amount, changeAddress btcutil.Address, ) (*wire.MsgTx, error)