Skip to content

Commit 10fd65d

Browse files
committed
cmd/tapcli: update mint parsing to validate new meta json type
1 parent 9166543 commit 10fd65d

File tree

1 file changed

+49
-5
lines changed

1 file changed

+49
-5
lines changed

cmd/tapcli/assets.go

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package main
22

33
import (
44
"encoding/hex"
5+
"encoding/json"
56
"fmt"
67
"math"
78
"os"
9+
"strconv"
810

911
taprootassets "github.com/lightninglabs/taproot-assets"
1012
"github.com/lightninglabs/taproot-assets/tapcfg"
@@ -88,9 +90,11 @@ var mintAssetCommand = cli.Command{
8890
Usage: "a path to a file on disk that should be read " +
8991
"and used as the asset meta",
9092
},
91-
cli.IntFlag{
92-
Name: assetMetaTypeName,
93-
Usage: "the type of the meta data for the asset",
93+
cli.StringFlag{
94+
Name: assetMetaTypeName,
95+
Usage: "the type of the meta data for the asset, must " +
96+
"be either: opaque or json",
97+
Value: "opaque",
9498
},
9599
cli.BoolFlag{
96100
Name: assetNewGroupedAssetName,
@@ -142,6 +146,36 @@ func parseAssetType(ctx *cli.Context) (taprpc.AssetType, error) {
142146
}
143147
}
144148

149+
func parseMetaType(metaType string,
150+
metaBytes []byte) (taprpc.AssetMetaType, error) {
151+
152+
switch metaType {
153+
case "opaque":
154+
fallthrough
155+
case "blob":
156+
return taprpc.AssetMetaType_META_TYPE_OPAQUE, nil
157+
158+
case "json":
159+
// If JSON is selected, the bytes must be valid.
160+
if !json.Valid(metaBytes) {
161+
return 0, fmt.Errorf("invalid JSON for meta: %s",
162+
metaBytes)
163+
}
164+
165+
return taprpc.AssetMetaType_META_TYPE_JSON, nil
166+
167+
// Otherwise, this is a custom meta type, we may not understand it, but
168+
// we want to support specifying arbitrary meta types.
169+
default:
170+
intType, err := strconv.Atoi(metaType)
171+
if err != nil {
172+
return 0, fmt.Errorf("invalid meta type: %s", metaType)
173+
}
174+
175+
return taprpc.AssetMetaType(intType), nil
176+
}
177+
}
178+
145179
func mintAsset(ctx *cli.Context) error {
146180
switch {
147181
case ctx.String(assetTagName) == "":
@@ -163,6 +197,8 @@ func mintAsset(ctx *cli.Context) error {
163197
}
164198
}
165199

200+
metaTypeStr := ctx.String(assetMetaTypeName)
201+
166202
// Both the meta bytes and the meta path can be set.
167203
var assetMeta *taprpc.AssetMeta
168204
switch {
@@ -174,7 +210,11 @@ func mintAsset(ctx *cli.Context) error {
174210
case ctx.String(assetMetaBytesName) != "":
175211
assetMeta = &taprpc.AssetMeta{
176212
Data: []byte(ctx.String(assetMetaBytesName)),
177-
Type: taprpc.AssetMetaType(ctx.Int(assetMetaTypeName)),
213+
}
214+
215+
assetMeta.Type, err = parseMetaType(metaTypeStr, assetMeta.Data)
216+
if err != nil {
217+
return fmt.Errorf("unable to parse meta type: %w", err)
178218
}
179219

180220
case ctx.String(assetMetaFilePathName) != "":
@@ -188,7 +228,11 @@ func mintAsset(ctx *cli.Context) error {
188228

189229
assetMeta = &taprpc.AssetMeta{
190230
Data: metaFileBytes,
191-
Type: taprpc.AssetMetaType(ctx.Int(assetMetaTypeName)),
231+
}
232+
233+
assetMeta.Type, err = parseMetaType(metaTypeStr, assetMeta.Data)
234+
if err != nil {
235+
return fmt.Errorf("unable to parse meta type: %w", err)
192236
}
193237
}
194238

0 commit comments

Comments
 (0)