From 5a077b10c0f7dedac99f8912011cba4c9b04c5b1 Mon Sep 17 00:00:00 2001 From: sufay Date: Wed, 19 Jun 2024 17:25:45 +0800 Subject: [PATCH] add submit signed psbt cmd --- x/btcbridge/client/cli/tx.go | 54 +++++++++++++++++++++++++++++++----- 1 file changed, 47 insertions(+), 7 deletions(-) diff --git a/x/btcbridge/client/cli/tx.go b/x/btcbridge/client/cli/tx.go index 0d4cee2..0b6c85e 100644 --- a/x/btcbridge/client/cli/tx.go +++ b/x/btcbridge/client/cli/tx.go @@ -8,6 +8,7 @@ import ( "strings" "time" + "github.com/btcsuite/btcd/btcutil/psbt" "github.com/spf13/cobra" "github.com/cosmos/cosmos-sdk/client" @@ -38,9 +39,8 @@ func GetTxCmd() *cobra.Command { cmd.AddCommand(CmdSubmitBlocks()) cmd.AddCommand(CmdUpdateSenders()) - // this line is used by starport scaffolding # 1 - cmd.AddCommand(CmdWithdrawBitcoin()) + cmd.AddCommand(CmdSubmitWithdrawSignatures()) return cmd } @@ -116,28 +116,28 @@ func CmdUpdateSenders() *cobra.Command { // Withdraw Bitcoin func CmdWithdrawBitcoin() *cobra.Command { cmd := &cobra.Command{ - Use: "withdraw-bitcoin [sender] [amount] [fee-rate]", + Use: "withdraw [amount] [fee-rate]", Short: "Withdraw bitcoin to the given sender", - Args: cobra.ExactArgs(3), + Args: cobra.ExactArgs(2), RunE: func(cmd *cobra.Command, args []string) (err error) { clientCtx, err := client.GetClientTxContext(cmd) if err != nil { return err } - _, err = sdk.ParseCoinsNormalized(args[1]) + _, err = sdk.ParseCoinsNormalized(args[0]) if err != nil { return fmt.Errorf("invalid amount") } - feeRate, err := strconv.ParseInt(args[2], 10, 64) + feeRate, err := strconv.ParseInt(args[1], 10, 64) if err != nil { return fmt.Errorf("invalid fee rate") } msg := types.NewMsgWithdrawBitcoinRequest( + clientCtx.GetFromAddress().String(), args[0], - args[1], feeRate, ) @@ -154,6 +154,46 @@ func CmdWithdrawBitcoin() *cobra.Command { return cmd } +func CmdSubmitWithdrawSignatures() *cobra.Command { + cmd := &cobra.Command{ + Use: "submit-signature [psbt]", + Short: "Submit signed withdrawal psbt", + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) (err error) { + clientCtx, err := client.GetClientTxContext(cmd) + if err != nil { + return err + } + + p, err := psbt.NewFromRawBytes(strings.NewReader(args[0]), true) + if err != nil { + return fmt.Errorf("invalid psbt") + } + + signedTx, err := psbt.Extract(p) + if err != nil { + return fmt.Errorf("failed to extract tx from psbt") + } + + msg := types.NewMsgSubmitWithdrawSignaturesRequest( + clientCtx.GetFromAddress().String(), + signedTx.TxHash().String(), + args[0], + ) + + if err := msg.ValidateBasic(); err != nil { + return err + } + + return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg) + }, + } + + flags.AddTxFlagsToCmd(cmd) + + return cmd +} + // readBlockHeadersFromFile reads the block headers from the file func readBlockHeadersFromFile(filePath string) ([]*types.BlockHeader, error) { // read the file