lncli: add lncli command for publishtransaction

This commit is contained in:
ErikEk 2021-07-01 20:43:42 +02:00
parent 44971f0c46
commit ace615b6e8
2 changed files with 68 additions and 0 deletions

View file

@ -3,6 +3,7 @@
package main package main
import ( import (
"bytes"
"encoding/base64" "encoding/base64"
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
@ -11,6 +12,7 @@ import (
"sort" "sort"
"github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/lightningnetwork/lnd/lnrpc" "github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnrpc/walletrpc" "github.com/lightningnetwork/lnd/lnrpc/walletrpc"
"github.com/urfave/cli" "github.com/urfave/cli"
@ -57,6 +59,7 @@ func walletCommands() []cli.Command {
bumpCloseFeeCommand, bumpCloseFeeCommand,
listSweepsCommand, listSweepsCommand,
labelTxCommand, labelTxCommand,
publishTxCommand,
releaseOutputCommand, releaseOutputCommand,
listLeasesCommand, listLeasesCommand,
psbtCommand, psbtCommand,
@ -477,6 +480,68 @@ func labelTransaction(ctx *cli.Context) error {
return nil 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. // utxoLease contains JSON annotations for a lease on an unspent output.
type utxoLease struct { type utxoLease struct {
ID string `json:"id"` ID string `json:"id"`

View file

@ -23,6 +23,9 @@ If you use a strange system or changed group membership of the group running LND
you may want to check your system to see if it introduces additional risk for you may want to check your system to see if it introduces additional risk for
you. you.
* [Makes publishtransaction, in the wallet sub-server, reachable through
lncli](https://github.com/lightningnetwork/lnd/pull/5460).
# Build System # Build System
* [A new pre-submit check has been * [A new pre-submit check has been