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

lncli: add command for publishtransaction
This commit is contained in:
Olaoluwa Osuntokun 2021-08-06 14:42:49 -07:00 committed by GitHub
commit 90db8de6fe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 68 additions and 0 deletions

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"`

View file

@ -38,6 +38,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.
* [Makes publishtransaction, in the wallet sub-server, reachable through
lncli](https://github.com/lightningnetwork/lnd/pull/5460).
# Build System
* [A new pre-submit check has been