Skip to content

Commit a67a5e9

Browse files
committed
cmd/commands: add script key flag to commands
This commit adds a new --script_key_type flag to the asset related commands, where supported by the corresponding RPC. If nothing is specified, the previous "show all assets" behavior is kept.
1 parent e3104b0 commit a67a5e9

File tree

1 file changed

+88
-3
lines changed

1 file changed

+88
-3
lines changed

cmd/commands/assets.go

Lines changed: 88 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,48 @@ import (
1414
"github.com/lightninglabs/taproot-assets/taprpc/mintrpc"
1515
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
1616
"github.com/urfave/cli"
17+
"golang.org/x/exp/maps"
1718
)
1819

20+
var (
21+
// nolint:lll
22+
scriptKeyTypeMap = map[string]taprpc.ScriptKeyType{
23+
"unknown": taprpc.ScriptKeyType_SCRIPT_KEY_UNKNOWN,
24+
"bip86": taprpc.ScriptKeyType_SCRIPT_KEY_BIP86,
25+
"script-path": taprpc.ScriptKeyType_SCRIPT_KEY_SCRIPT_PATH_EXTERNAL,
26+
"burn": taprpc.ScriptKeyType_SCRIPT_KEY_BURN,
27+
"tombstone": taprpc.ScriptKeyType_SCRIPT_KEY_TOMBSTONE,
28+
"channel": taprpc.ScriptKeyType_SCRIPT_KEY_CHANNEL,
29+
}
30+
)
31+
32+
// parseScriptKeyType parses the script key type query from the command line
33+
// context. If the user didn't specify a script key type, the "show all" query
34+
// type is returned.
35+
func parseScriptKeyType(c *cli.Context) (*taprpc.ScriptKeyTypeQuery, error) {
36+
allScriptKeysQuery := &taprpc.ScriptKeyTypeQuery{
37+
Type: &taprpc.ScriptKeyTypeQuery_AllTypes{
38+
AllTypes: true,
39+
},
40+
}
41+
42+
if !c.IsSet(scriptKeyTypeName) || c.String(scriptKeyTypeName) == "" {
43+
return allScriptKeysQuery, nil
44+
}
45+
46+
scriptKeyType, ok := scriptKeyTypeMap[c.String(scriptKeyTypeName)]
47+
if !ok {
48+
return nil, fmt.Errorf("script key type '%v' is unknown",
49+
c.String(scriptKeyTypeName))
50+
}
51+
52+
return &taprpc.ScriptKeyTypeQuery{
53+
Type: &taprpc.ScriptKeyTypeQuery_ExplicitType{
54+
ExplicitType: scriptKeyType,
55+
},
56+
}, nil
57+
}
58+
1959
var assetsCommands = []cli.Command{
2060
{
2161
Name: "assets",
@@ -64,6 +104,7 @@ var (
64104
skipProofCourierPingCheckName = "skip-proof-courier-ping-check"
65105
assetAmountName = "amount"
66106
burnOverrideConfirmationName = "override_confirmation_destroy_assets"
107+
scriptKeyTypeName = "script_key_type"
67108
)
68109

69110
var mintAssetCommand = cli.Command{
@@ -666,6 +707,12 @@ var listAssetsCommand = cli.Command{
666707
Usage: "include freshly minted and not yet confirmed " +
667708
"assets in the list",
668709
},
710+
cli.StringFlag{
711+
Name: scriptKeyTypeName,
712+
Usage: "filter assets by the type of script key they " +
713+
"use; possible values are: " +
714+
strings.Join(maps.Keys(scriptKeyTypeMap), ", "),
715+
},
669716
},
670717
Action: listAssets,
671718
}
@@ -677,15 +724,22 @@ func listAssets(ctx *cli.Context) error {
677724

678725
// TODO(roasbeef): need to reverse txid
679726

727+
scriptKeyQuery, err := parseScriptKeyType(ctx)
728+
if err != nil {
729+
return fmt.Errorf("unable to parse script key type: %w", err)
730+
}
731+
680732
resp, err := client.ListAssets(ctxc, &taprpc.ListAssetRequest{
681733
WithWitness: ctx.Bool(assetShowWitnessName),
682734
IncludeSpent: ctx.Bool(assetShowSpentName),
683735
IncludeLeased: ctx.Bool(assetShowLeasedName),
684736
IncludeUnconfirmedMints: ctx.Bool(assetShowUnconfMintsName),
737+
ScriptKeyType: scriptKeyQuery,
685738
})
686739
if err != nil {
687740
return fmt.Errorf("unable to list assets: %w", err)
688741
}
742+
689743
printRespJSON(resp)
690744
return nil
691745
}
@@ -695,18 +749,39 @@ var listUtxosCommand = cli.Command{
695749
ShortName: "u",
696750
Usage: "list all utxos",
697751
Description: "list all utxos managing assets",
698-
Action: listUtxos,
752+
Flags: []cli.Flag{
753+
cli.BoolFlag{
754+
Name: assetShowLeasedName,
755+
Usage: "include leased assets in the list",
756+
},
757+
cli.StringFlag{
758+
Name: scriptKeyTypeName,
759+
Usage: "filter assets by the type of script key they " +
760+
"use; possible values are: " +
761+
strings.Join(maps.Keys(scriptKeyTypeMap), ", "),
762+
},
763+
},
764+
Action: listUtxos,
699765
}
700766

701767
func listUtxos(ctx *cli.Context) error {
702768
ctxc := getContext()
703769
client, cleanUp := getClient(ctx)
704770
defer cleanUp()
705771

706-
resp, err := client.ListUtxos(ctxc, &taprpc.ListUtxosRequest{})
772+
scriptKeyQuery, err := parseScriptKeyType(ctx)
773+
if err != nil {
774+
return fmt.Errorf("unable to parse script key type: %w", err)
775+
}
776+
777+
resp, err := client.ListUtxos(ctxc, &taprpc.ListUtxosRequest{
778+
IncludeLeased: ctx.Bool(assetShowLeasedName),
779+
ScriptKeyType: scriptKeyQuery,
780+
})
707781
if err != nil {
708782
return fmt.Errorf("unable to list utxos: %w", err)
709783
}
784+
710785
printRespJSON(resp)
711786
return nil
712787
}
@@ -758,6 +833,12 @@ var listAssetBalancesCommand = cli.Command{
758833
"balance query against. Must be used " +
759834
"together with --by_group",
760835
},
836+
cli.StringFlag{
837+
Name: scriptKeyTypeName,
838+
Usage: "filter assets by the type of script key they " +
839+
"use; possible values are: " +
840+
strings.Join(maps.Keys(scriptKeyTypeMap), ", "),
841+
},
761842
},
762843
}
763844

@@ -766,10 +847,14 @@ func listAssetBalances(ctx *cli.Context) error {
766847
client, cleanUp := getClient(ctx)
767848
defer cleanUp()
768849

769-
var err error
850+
scriptKeyQuery, err := parseScriptKeyType(ctx)
851+
if err != nil {
852+
return fmt.Errorf("unable to parse script key type: %w", err)
853+
}
770854

771855
req := &taprpc.ListBalancesRequest{
772856
IncludeLeased: ctx.Bool(assetIncludeLeasedName),
857+
ScriptKeyType: scriptKeyQuery,
773858
}
774859

775860
if !ctx.Bool(groupByGroupName) {

0 commit comments

Comments
 (0)