Skip to content

Commit 92990a1

Browse files
committed
cmd/commands: add --qr flag to addinvoice for terminal QR display
Add a `--qr` flag to the `addinvoice` CLI command that renders the payment request as a QR code directly in the terminal using the qrterminal library. This makes it convenient to scan invoices from a mobile wallet without needing to copy-paste the payment request string.
1 parent d344984 commit 92990a1

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

cmd/commands/cmd_invoice.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package commands
33
import (
44
"encoding/hex"
55
"fmt"
6+
"os"
67
"strconv"
78
"strings"
89

910
"github.com/lightningnetwork/lnd/lnrpc"
11+
qrterminal "github.com/mdp/qrterminal/v3"
1012
"github.com/urfave/cli"
1113
"google.golang.org/protobuf/proto"
1214
)
@@ -124,6 +126,11 @@ var AddInvoiceCommand = cli.Command{
124126
"id (separated by commas), starting from a " +
125127
"channel which points to the self node.",
126128
},
129+
cli.BoolFlag{
130+
Name: "qr",
131+
Usage: "display a QR code of the payment request " +
132+
"in the terminal",
133+
},
127134
},
128135
Action: actionDecorator(addInvoice),
129136
}
@@ -202,6 +209,15 @@ func addInvoice(ctx *cli.Context) error {
202209

203210
printRespJSON(resp)
204211

212+
if ctx.Bool("qr") {
213+
// If the user requested it, we'll also print a QR code that
214+
// encodes the payment request for easy scanning.
215+
fmt.Println()
216+
qrterminal.GenerateHalfBlock(
217+
resp.PaymentRequest, qrterminal.L, os.Stdout,
218+
)
219+
}
220+
205221
return nil
206222
}
207223

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ require (
130130
github.com/mattn/go-isatty v0.0.20 // indirect
131131
github.com/mattn/go-runewidth v0.0.13 // indirect
132132
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
133+
github.com/mdp/qrterminal/v3 v3.2.1
133134
github.com/moby/docker-image-spec v1.3.1 // indirect
134135
github.com/moby/term v0.5.0 // indirect
135136
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
@@ -199,6 +200,7 @@ require (
199200
modernc.org/sqlite v1.29.10 // indirect
200201
modernc.org/strutil v1.2.0 // indirect
201202
modernc.org/token v1.1.0 // indirect
203+
rsc.io/qr v0.2.0 // indirect
202204
sigs.k8s.io/yaml v1.2.0 // indirect
203205
)
204206

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,8 @@ github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4
405405
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
406406
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
407407
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
408+
github.com/mdp/qrterminal/v3 v3.2.1 h1:6+yQjiiOsSuXT5n9/m60E54vdgFsw0zhADHhHLrFet4=
409+
github.com/mdp/qrterminal/v3 v3.2.1/go.mod h1:jOTmXvnBsMy5xqLniO0R++Jmjs2sTm9dFSuQ5kpz/SU=
408410
github.com/miekg/dns v1.1.43 h1:JKfpVSCB84vrAmHzyrsxB5NAr5kLoMXZArPSw7Qlgyg=
409411
github.com/miekg/dns v1.1.43/go.mod h1:+evo5L0630/F6ca/Z9+GAqzhjGyn8/c+TBaOyfEl0V4=
410412
github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0=
@@ -852,5 +854,7 @@ modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y=
852854
modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=
853855
pgregory.net/rapid v1.2.0 h1:keKAYRcjm+e1F0oAuU5F5+YPAWcyxNNRK2wud503Gnk=
854856
pgregory.net/rapid v1.2.0/go.mod h1:PY5XlDGj0+V1FCq0o192FdRhpKHGTRIWBgqjDBTrq04=
857+
rsc.io/qr v0.2.0 h1:6vBLea5/NRMVTz8V66gipeLycZMl/+UlFmk8DvqQ6WY=
858+
rsc.io/qr v0.2.0/go.mod h1:IF+uZjkb9fqyeF/4tlBoynqmQxUoPfWEKh921coOuXs=
855859
sigs.k8s.io/yaml v1.2.0 h1:kr/MCeFWJWTwyaHoR9c8EjH9OumOmoF9YGiZd7lFm/Q=
856860
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=

0 commit comments

Comments
 (0)