cmd: make types.go accessible in lnd package
What changed, and why it matters
This commit simply moves a set of helper types and functions used for formatting Bitcoin transaction data from one internal package to another. It does not change what the code does, only where it lives in the project. There is no user-facing change and no security impact.
No security action needed. Treat as a normal code refactor.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change relocates OutPoint, Utxo, FailedUpdate, and related conversion helpers from cmd/commands/types.go to a new top-level types.go in the lnd package. All call sites are updated to use the lnd package prefix. The implementation is identical; this is a pure refactor to make these types accessible outside the cmd/commands package.
Changed components
cmd/commands/cmd_open_channel.gocmd/commands/commands.gocmd/commands/walletrpc_active.gocmd/commands/walletrpc_types.gotypes.goInspect captured patch +145 / −136
diff --git a/cmd/commands/cmd_open_channel.go b/cmd/commands/cmd_open_channel.go
index b4fe83f..3b00862 100644
--- a/cmd/commands/cmd_open_channel.go
+++ b/cmd/commands/cmd_open_channel.go
@@ -16,6 +16,7 @@ import (
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
+ "github.com/lightningnetwork/lnd"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnwallet/chanfunding"
"github.com/urfave/cli"
@@ -406,7 +407,7 @@ func openChannel(ctx *cli.Context) error {
if ctx.IsSet("utxo") {
utxos := ctx.StringSlice("utxo")
- outpoints, err := UtxosToOutpoints(utxos)
+ outpoints, err := lnd.UtxosToOutpoints(utxos)
if err != nil {
return fmt.Errorf("unable to decode utxos: %w", err)
}
diff --git a/cmd/commands/commands.go b/cmd/commands/commands.go
index f0b4bf6..48633c7 100644
--- a/cmd/commands/commands.go
+++ b/cmd/commands/commands.go
@@ -607,7 +607,7 @@ func sendCoins(ctx *cli.Context) error {
if ctx.IsSet("utxo") {
utxos := ctx.StringSlice("utxo")
- outpoints, err = UtxosToOutpoints(utxos)
+ outpoints, err = lnd.UtxosToOutpoints(utxos)
if err != nil {
return fmt.Errorf("unable to decode utxos: %w", err)
}
@@ -784,12 +784,12 @@ func listUnspent(ctx *cli.Context) error {
// to stdout. At the moment, this filters out the raw txid bytes from
// each utxo's outpoint and only prints the txid string.
var listUnspentResp = struct {
- Utxos []*Utxo `json:"utxos"`
+ Utxos []*lnd.Utxo `json:"utxos"`
}{
- Utxos: make([]*Utxo, 0, len(resp.Utxos)),
+ Utxos: make([]*lnd.Utxo, 0, len(resp.Utxos)),
}
for _, protoUtxo := range resp.Utxos {
- utxo := NewUtxoFromProto(protoUtxo)
+ utxo := lnd.NewUtxoFromProto(protoUtxo)
listUnspentResp.Utxos = append(listUnspentResp.Utxos, utxo)
}
@@ -2789,12 +2789,14 @@ func updateChannelPolicy(ctx *cli.Context) error {
// to stdout. At the moment, this filters out the raw txid bytes from
// each failed update's outpoint and only prints the txid string.
var listFailedUpdateResp = struct {
- FailedUpdates []*FailedUpdate `json:"failed_updates"`
+ FailedUpdates []*lnd.FailedUpdate `json:"failed_updates"`
}{
- FailedUpdates: make([]*FailedUpdate, 0, len(resp.FailedUpdates)),
+ FailedUpdates: make(
+ []*lnd.FailedUpdate, 0, len(resp.FailedUpdates),
+ ),
}
for _, protoUpdate := range resp.FailedUpdates {
- failedUpdate := NewFailedUpdateFromProto(protoUpdate)
+ failedUpdate := lnd.NewFailedUpdateFromProto(protoUpdate)
listFailedUpdateResp.FailedUpdates = append(
listFailedUpdateResp.FailedUpdates, failedUpdate)
}
diff --git a/cmd/commands/types.go b/cmd/commands/types.go
deleted file mode 100644
index 2a82e71..0000000
--- a/cmd/commands/types.go
+++ /dev/null
@@ -1,106 +0,0 @@
-package commands
-
-import (
- "encoding/hex"
- "errors"
- "fmt"
- "strconv"
- "strings"
-
- "github.com/btcsuite/btcd/chaincfg/chainhash"
- "github.com/lightningnetwork/lnd/lnrpc"
-)
-
-// OutPoint displays an outpoint string in the form "<txid>:<output-index>".
-type OutPoint string
-
-// NewOutPointFromProto formats the lnrpc.OutPoint into an OutPoint for display.
-func NewOutPointFromProto(op *lnrpc.OutPoint) OutPoint {
- var hash chainhash.Hash
- copy(hash[:], op.TxidBytes)
- return OutPoint(fmt.Sprintf("%v:%d", hash, op.OutputIndex))
-}
-
-// NewProtoOutPoint parses an OutPoint into its corresponding lnrpc.OutPoint
-// type.
-func NewProtoOutPoint(op string) (*lnrpc.OutPoint, error) {
- parts := strings.Split(op, ":")
- if len(parts) != 2 {
- return nil, errors.New("outpoint should be of the form txid:index")
- }
- txid := parts[0]
- if hex.DecodedLen(len(txid)) != chainhash.HashSize {
- return nil, fmt.Errorf("invalid hex-encoded txid %v", txid)
- }
- outputIndex, err := strconv.Atoi(parts[1])
- if err != nil {
- return nil, fmt.Errorf("invalid output index: %w", err)
- }
- return &lnrpc.OutPoint{
- TxidStr: txid,
- OutputIndex: uint32(outputIndex),
- }, nil
-}
-
-// Utxo displays information about an unspent output, including its address,
-// amount, pkscript, and confirmations.
-type Utxo struct {
- Type lnrpc.AddressType `json:"address_type"`
- Address string `json:"address"`
- AmountSat int64 `json:"amount_sat"`
- PkScript string `json:"pk_script"`
- OutPoint OutPoint `json:"outpoint"`
- Confirmations int64 `json:"confirmations"`
-}
-
-// NewUtxoFromProto creates a display Utxo from the Utxo proto. This filters out
-// the raw txid bytes from the provided outpoint, which will otherwise be
-// printed in base64.
-func NewUtxoFromProto(utxo *lnrpc.Utxo) *Utxo {
- return &Utxo{
- Type: utxo.AddressType,
- Address: utxo.Address,
- AmountSat: utxo.AmountSat,
- PkScript: utxo.PkScript,
- OutPoint: NewOutPointFromProto(utxo.Outpoint),
- Confirmations: utxo.Confirmations,
- }
-}
-
-// FailedUpdate displays information about a failed update, including its
-// address, reason and update error.
-type FailedUpdate struct {
- OutPoint OutPoint `json:"outpoint"`
- Reason string `json:"reason"`
- UpdateError string `json:"update_error"`
-}
-
-// NewFailedUpdateFromProto creates a display from the FailedUpdate
-// proto. This filters out the raw txid bytes from the provided outpoint,
-// which will otherwise be printed in base64.
-func NewFailedUpdateFromProto(update *lnrpc.FailedUpdate) *FailedUpdate {
- return &FailedUpdate{
- OutPoint: NewOutPointFromProto(update.Outpoint),
- Reason: update.Reason.String(),
- UpdateError: update.UpdateError,
- }
-}
-
-// UtxosToOutpoints converts a slice of UTXO strings into a slice of OutPoint
-// protobuf objects. It returns an error if no UTXOs are specified or if any
-// UTXO string cannot be parsed into an OutPoint.
-func UtxosToOutpoints(utxos []string) ([]*lnrpc.OutPoint, error) {
- var outpoints []*lnrpc.OutPoint
- if len(utxos) == 0 {
- return nil, fmt.Errorf("no utxos specified")
- }
- for _, utxo := range utxos {
- outpoint, err := NewProtoOutPoint(utxo)
- if err != nil {
- return nil, err
- }
- outpoints = append(outpoints, outpoint)
- }
-
- return outpoints, nil
-}
diff --git a/cmd/commands/walletrpc_active.go b/cmd/commands/walletrpc_active.go
index 9f955bf..87c3f4e 100644
--- a/cmd/commands/walletrpc_active.go
+++ b/cmd/commands/walletrpc_active.go
@@ -20,6 +20,7 @@ import (
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
+ "github.com/lightningnetwork/lnd"
"github.com/lightningnetwork/lnd/lnrpc/walletrpc"
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
"github.com/lightningnetwork/lnd/lnwallet/chanfunding"
@@ -330,7 +331,7 @@ func bumpFee(ctx *cli.Context) error {
}
// Validate and parse the relevant arguments/flags.
- protoOutPoint, err := NewProtoOutPoint(ctx.Args().Get(0))
+ protoOutPoint, err := lnd.NewProtoOutPoint(ctx.Args().Get(0))
if err != nil {
return err
}
@@ -812,11 +813,11 @@ func removeTransaction(ctx *cli.Context) error {
// utxoLease contains JSON annotations for a lease on an unspent output.
type utxoLease struct {
- ID string `json:"id"`
- OutPoint OutPoint `json:"outpoint"`
- Expiration uint64 `json:"expiration"`
- PkScript []byte `json:"pk_script"`
- Value uint64 `json:"value"`
+ ID string `json:"id"`
+ OutPoint lnd.OutPoint `json:"outpoint"`
+ Expiration uint64 `json:"expiration"`
+ PkScript []byte `json:"pk_script"`
+ Value uint64 `json:"value"`
}
// fundPsbtResponse is a struct that contains JSON annotations for nice result
@@ -1358,7 +1359,7 @@ func fundPsbt(ctx *cli.Context) error {
}
for idx, input := range inputs {
- op, err := NewProtoOutPoint(input)
+ op, err := lnd.NewProtoOutPoint(input)
if err != nil {
return fmt.Errorf("error parsing "+
"UTXO outpoint %d: %v", idx,
@@ -1447,7 +1448,7 @@ func marshallLocks(lockedUtxos []*walletrpc.UtxoLease) []*utxoLease {
for idx, lock := range lockedUtxos {
jsonLocks[idx] = &utxoLease{
ID: hex.EncodeToString(lock.Id),
- OutPoint: NewOutPointFromProto(lock.Outpoint),
+ OutPoint: lnd.NewOutPointFromProto(lock.Outpoint),
Expiration: lock.Expiration,
PkScript: lock.PkScript,
Value: lock.Value,
@@ -1578,7 +1579,7 @@ func leaseOutput(ctx *cli.Context) error {
}
outpointStr := ctx.String("outpoint")
- outpoint, err := NewProtoOutPoint(outpointStr)
+ outpoint, err := lnd.NewProtoOutPoint(outpointStr)
if err != nil {
return fmt.Errorf("error parsing outpoint: %w", err)
}
@@ -1663,7 +1664,7 @@ func releaseOutput(ctx *cli.Context) error {
return fmt.Errorf("outpoint argument missing")
}
- outpoint, err := NewProtoOutPoint(outpointStr)
+ outpoint, err := lnd.NewProtoOutPoint(outpointStr)
if err != nil {
return fmt.Errorf("error parsing outpoint: %w", err)
}
diff --git a/cmd/commands/walletrpc_types.go b/cmd/commands/walletrpc_types.go
index 790114c..f3a025c 100644
--- a/cmd/commands/walletrpc_types.go
+++ b/cmd/commands/walletrpc_types.go
@@ -1,6 +1,9 @@
package commands
-import "github.com/lightningnetwork/lnd/lnrpc/walletrpc"
+import (
+ "github.com/lightningnetwork/lnd"
+ "github.com/lightningnetwork/lnd/lnrpc/walletrpc"
+)
// PendingSweep is a CLI-friendly type of the walletrpc.PendingSweep proto. We
// use this to show more useful string versions of byte slices and enums.
@@ -9,16 +12,16 @@ import "github.com/lightningnetwork/lnd/lnrpc/walletrpc"
// here. Instead, we should rely on the struct defined in the proto
// `PendingSweepsResponse` only.
type PendingSweep struct {
- OutPoint OutPoint `json:"outpoint"`
- WitnessType string `json:"witness_type"`
- AmountSat uint32 `json:"amount_sat"`
- SatPerVByte uint32 `json:"sat_per_vbyte"`
- BroadcastAttempts uint32 `json:"broadcast_attempts"`
- RequestedSatPerVByte uint32 `json:"requested_sat_per_vbyte"`
- Immediate bool `json:"immediate"`
- Budget uint64 `json:"budget"`
- DeadlineHeight uint32 `json:"deadline_height"`
- MaturityHeight uint32 `json:"maturity_height"`
+ OutPoint lnd.OutPoint `json:"outpoint"`
+ WitnessType string `json:"witness_type"`
+ AmountSat uint32 `json:"amount_sat"`
+ SatPerVByte uint32 `json:"sat_per_vbyte"`
+ BroadcastAttempts uint32 `json:"broadcast_attempts"`
+ RequestedSatPerVByte uint32 `json:"requested_sat_per_vbyte"`
+ Immediate bool `json:"immediate"`
+ Budget uint64 `json:"budget"`
+ DeadlineHeight uint32 `json:"deadline_height"`
+ MaturityHeight uint32 `json:"maturity_height"`
NextBroadcastHeight uint32 `json:"next_broadcast_height"`
RequestedConfTarget uint32 `json:"requested_conf_target"`
@@ -29,7 +32,9 @@ type PendingSweep struct {
// its corresponding CLI-friendly type.
func NewPendingSweepFromProto(pendingSweep *walletrpc.PendingSweep) *PendingSweep {
return &PendingSweep{
- OutPoint: NewOutPointFromProto(pendingSweep.Outpoint),
+ OutPoint: lnd.NewOutPointFromProto(
+ pendingSweep.Outpoint,
+ ),
WitnessType: pendingSweep.WitnessType.String(),
AmountSat: pendingSweep.AmountSat,
SatPerVByte: uint32(pendingSweep.SatPerVbyte),
diff --git a/types.go b/types.go
new file mode 100644
index 0000000..bb446de
--- /dev/null
+++ b/types.go
@@ -0,0 +1,106 @@
+package lnd
+
+import (
+ "encoding/hex"
+ "errors"
+ "fmt"
+ "strconv"
+ "strings"
+
+ "github.com/btcsuite/btcd/chaincfg/chainhash"
+ "github.com/lightningnetwork/lnd/lnrpc"
+)
+
+// OutPoint displays an outpoint string in the form "<txid>:<output-index>".
+type OutPoint string
+
+// NewOutPointFromProto formats the lnrpc.OutPoint into an OutPoint for display.
+func NewOutPointFromProto(op *lnrpc.OutPoint) OutPoint {
+ var hash chainhash.Hash
+ copy(hash[:], op.TxidBytes)
+ return OutPoint(fmt.Sprintf("%v:%d", hash, op.OutputIndex))
+}
+
+// NewProtoOutPoint parses an OutPoint into its corresponding lnrpc.OutPoint
+// type.
+func NewProtoOutPoint(op string) (*lnrpc.OutPoint, error) {
+ parts := strings.Split(op, ":")
+ if len(parts) != 2 {
+ return nil, errors.New("outpoint should be of the form txid:index")
+ }
+ txid := parts[0]
+ if hex.DecodedLen(len(txid)) != chainhash.HashSize {
+ return nil, fmt.Errorf("invalid hex-encoded txid %v", txid)
+ }
+ outputIndex, err := strconv.Atoi(parts[1])
+ if err != nil {
+ return nil, fmt.Errorf("invalid output index: %w", err)
+ }
+ return &lnrpc.OutPoint{
+ TxidStr: txid,
+ OutputIndex: uint32(outputIndex),
+ }, nil
+}
+
+// Utxo displays information about an unspent output, including its address,
+// amount, pkscript, and confirmations.
+type Utxo struct {
+ Type lnrpc.AddressType `json:"address_type"`
+ Address string `json:"address"`
+ AmountSat int64 `json:"amount_sat"`
+ PkScript string `json:"pk_script"`
+ OutPoint OutPoint `json:"outpoint"`
+ Confirmations int64 `json:"confirmations"`
+}
+
+// NewUtxoFromProto creates a display Utxo from the Utxo proto. This filters out
+// the raw txid bytes from the provided outpoint, which will otherwise be
+// printed in base64.
+func NewUtxoFromProto(utxo *lnrpc.Utxo) *Utxo {
+ return &Utxo{
+ Type: utxo.AddressType,
+ Address: utxo.Address,
+ AmountSat: utxo.AmountSat,
+ PkScript: utxo.PkScript,
+ OutPoint: NewOutPointFromProto(utxo.Outpoint),
+ Confirmations: utxo.Confirmations,
+ }
+}
+
+// FailedUpdate displays information about a failed update, including its
+// address, reason and update error.
+type FailedUpdate struct {
+ OutPoint OutPoint `json:"outpoint"`
+ Reason string `json:"reason"`
+ UpdateError string `json:"update_error"`
+}
+
+// NewFailedUpdateFromProto creates a display from the FailedUpdate
+// proto. This filters out the raw txid bytes from the provided outpoint,
+// which will otherwise be printed in base64.
+func NewFailedUpdateFromProto(update *lnrpc.FailedUpdate) *FailedUpdate {
+ return &FailedUpdate{
+ OutPoint: NewOutPointFromProto(update.Outpoint),
+ Reason: update.Reason.String(),
+ UpdateError: update.UpdateError,
+ }
+}
+
+// UtxosToOutpoints converts a slice of UTXO strings into a slice of OutPoint
+// protobuf objects. It returns an error if no UTXOs are specified or if any
+// UTXO string cannot be parsed into an OutPoint.
+func UtxosToOutpoints(utxos []string) ([]*lnrpc.OutPoint, error) {
+ var outpoints []*lnrpc.OutPoint
+ if len(utxos) == 0 {
+ return nil, fmt.Errorf("no utxos specified")
+ }
+ for _, utxo := range utxos {
+ outpoint, err := NewProtoOutPoint(utxo)
+ if err != nil {
+ return nil, err
+ }
+ outpoints = append(outpoints, outpoint)
+ }
+
+ return outpoints, nil
+}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.