From ace615b6e8c4571ccf5f1d96ca3eef0c77f003b5 Mon Sep 17 00:00:00 2001 From: ErikEk Date: Thu, 1 Jul 2021 20:43:42 +0200 Subject: [PATCH] lncli: add lncli command for publishtransaction --- cmd/lncli/walletrpc_active.go | 65 ++++++++++++++++++++++ docs/release-notes/release-notes-0.14.0.md | 3 + 2 files changed, 68 insertions(+) diff --git a/cmd/lncli/walletrpc_active.go b/cmd/lncli/walletrpc_active.go index 179ae6940..7710b3a3a 100644 --- a/cmd/lncli/walletrpc_active.go +++ b/cmd/lncli/walletrpc_active.go @@ -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"` diff --git a/docs/release-notes/release-notes-0.14.0.md b/docs/release-notes/release-notes-0.14.0.md index 3cf1f09c7..1dc60d04e 100644 --- a/docs/release-notes/release-notes-0.14.0.md +++ b/docs/release-notes/release-notes-0.14.0.md @@ -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. +* [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