Merge pull request #5460 from ErikEk/lncli-add-command-for-publishtransaction

lncli: add command for publishtransaction
This commit is contained in:
Olaoluwa Osuntokun authored and GitHub committed 2021-08-06 14:42:49 -07:00
commit 90db8de6fe
2 files changed
+68

No files matched your search

+65
View File
@@ -3,6 +3,7 @@
package main
import (
"bytes"
"encoding/base64"
"encoding/hex"
"encoding/json"
@@ -11,6 +12,7 @@ import (
"sort"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnrpc/walletrpc"
"github.com/urfave/cli"
@@ -57,6 +59,7 @@ func walletCommands() []cli.Command {
bumpCloseFeeCommand,
listSweepsCommand,
labelTxCommand,
publishTxCommand,
releaseOutputCommand,
listLeasesCommand,
psbtCommand,
@@ -477,6 +480,68 @@ func labelTransaction(ctx *cli.Context) error {
return nil
}
var publishTxCommand = cli.Command{
Name: "publishtx",
Usage: "Attempts to publish the passed transaction to the network.",
ArgsUsage: "tx_hex",
Description: `
Publish a hex-encoded raw transaction to the on-chain network. The
wallet will continually attempt to re-broadcast the transaction on start up, until it
enters the chain. The label parameter is optional and limited to 500 characters. Note
that multi word labels must be contained in quotation marks ("").
`,
Flags: []cli.Flag{
cli.StringFlag{
Name: "label",
Usage: "(optional) transaction label",
},
},
Action: actionDecorator(publishTransaction),
}
func publishTransaction(ctx *cli.Context) error {
ctxc := getContext()
// Display the command's help message if we do not have the expected
// number of arguments/flags.
if ctx.NArg() != 1 || ctx.NumFlags() > 1 {
return cli.ShowCommandHelp(ctx, "publishtx")
}
walletClient, cleanUp := getWalletClient(ctx)
defer cleanUp()
tx, err := hex.DecodeString(ctx.Args().First())
if err != nil {
return err
}
// Deserialize the transaction to get the transaction hash.
msgTx := &wire.MsgTx{}
txReader := bytes.NewReader(tx)
if err := msgTx.Deserialize(txReader); err != nil {
return err
}
req := &walletrpc.Transaction{
TxHex: tx,
Label: ctx.String("label"),
}
_, err = walletClient.PublishTransaction(ctxc, req)
if err != nil {
return err
}
printJSON(&struct {
TXID string `json:"txid"`
}{
TXID: msgTx.TxHash().String(),
})
return nil
}
// utxoLease contains JSON annotations for a lease on an unspent output.
type utxoLease struct {
ID string `json:"id"`