Skip to content

Commit

Permalink
add submit signed psbt cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
keithsue committed Jun 19, 2024
1 parent 385b97e commit 5a077b1
Showing 1 changed file with 47 additions and 7 deletions.
54 changes: 47 additions & 7 deletions x/btcbridge/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"time"

"github.com/btcsuite/btcd/btcutil/psbt"
"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
)

Expand All @@ -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
Expand Down

0 comments on commit 5a077b1

Please sign in to comment.