From 2358796c5d7cb2dde0c60d02bea2ce01e281a8c4 Mon Sep 17 00:00:00 2001 From: Jared Tobin Date: Wed, 25 Nov 2026 13:52:35 +0400 Subject: [PATCH] rpcutils+rpcserver: reject unknown RPC asset type and sort values UnmarshalAssetType rejects values outside the defined asset types. MintAsset and UnmarshalGenesisInfo unmarshal the RPC asset type field through UnmarshalAssetType instead of a raw type conversion. unmarshalUniSyncStatsSort maps the RPC asset query sort field to its universe.SyncStatsSort counterpart and rejects unrecognized values. QueryAssetStats unmarshals its sort field through unmarshalUniSyncStatsSort instead of a raw type conversion. --- rpcserver/rpcserver.go | 49 ++++++++++++++++++++++++++++++++++++++-- rpcutils/marshal.go | 21 ++++++++++++++++- rpcutils/marshal_test.go | 48 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 115 insertions(+), 3 deletions(-) diff --git a/rpcserver/rpcserver.go b/rpcserver/rpcserver.go index ba6c9d1439..5ed76d00f0 100644 --- a/rpcserver/rpcserver.go +++ b/rpcserver/rpcserver.go @@ -801,9 +801,14 @@ func (r *RPCServer) MintAsset(ctx context.Context, "and group internal key descriptor") } + assetType, err := rpcutils.UnmarshalAssetType(req.Asset.AssetType) + if err != nil { + return nil, err + } + seedling := &tapgarden.Seedling{ AssetVersion: assetVersion, - AssetType: asset.Type(req.Asset.AssetType), + AssetType: assetType, AssetName: req.Asset.Name, Amount: req.Asset.Amount, EnableEmission: req.Asset.NewGroupedAsset, @@ -6888,6 +6893,41 @@ func unmarshalUniSortDirection( } } +// unmarshalUniSyncStatsSort parses the RPC asset query sort field into +// the native sync stats sort counterpart. +func unmarshalUniSyncStatsSort( + rpcSort unirpc.AssetQuerySort) (universe.SyncStatsSort, error) { + + switch rpcSort { + case unirpc.AssetQuerySort_SORT_BY_NONE: + return universe.SortByNone, nil + + case unirpc.AssetQuerySort_SORT_BY_ASSET_NAME: + return universe.SortByAssetName, nil + + case unirpc.AssetQuerySort_SORT_BY_ASSET_ID: + return universe.SortByAssetID, nil + + case unirpc.AssetQuerySort_SORT_BY_ASSET_TYPE: + return universe.SortByAssetType, nil + + case unirpc.AssetQuerySort_SORT_BY_TOTAL_SYNCS: + return universe.SortByTotalSyncs, nil + + case unirpc.AssetQuerySort_SORT_BY_TOTAL_PROOFS: + return universe.SortByTotalProofs, nil + + case unirpc.AssetQuerySort_SORT_BY_GENESIS_HEIGHT: + return universe.SortByGenesisHeight, nil + + case unirpc.AssetQuerySort_SORT_BY_TOTAL_SUPPLY: + return universe.SortByTotalSupply, nil + + default: + return 0, fmt.Errorf("unknown asset query sort: %v", rpcSort) + } +} + // validatePage validates the pagination parameters for universe // queries. func validatePage(offset, limit int32) error { @@ -9016,6 +9056,11 @@ func (r *RPCServer) QueryAssetStats(ctx context.Context, limit = universe.RequestPageSize } + sortBy, err := unmarshalUniSyncStatsSort(req.SortBy) + if err != nil { + return nil, err + } + assetStats, err := r.cfg.UniverseStats.QuerySyncStats( ctx, universe.SyncStatsQuery{ AssetNameFilter: req.AssetNameFilter, @@ -9034,7 +9079,7 @@ func (r *RPCServer) QueryAssetStats(ctx context.Context, AssetIDFilter: fn.ToArray[asset.ID]( req.AssetIdFilter, ), - SortBy: universe.SyncStatsSort(req.SortBy), + SortBy: sortBy, SortDirection: unmarshalUniSortDirection(req.Direction), Offset: int(req.Offset), Limit: int(limit + 1), diff --git a/rpcutils/marshal.go b/rpcutils/marshal.go index 7f8979b48a..a7057f77c4 100644 --- a/rpcutils/marshal.go +++ b/rpcutils/marshal.go @@ -229,6 +229,20 @@ func MarshalAssetVersion(version asset.Version) (taprpc.AssetVersion, error) { } } +// UnmarshalAssetType parses an asset type from the RPC variant. +func UnmarshalAssetType(rpcType taprpc.AssetType) (asset.Type, error) { + switch rpcType { + case taprpc.AssetType_NORMAL: + return asset.Normal, nil + + case taprpc.AssetType_COLLECTIBLE: + return asset.Collectible, nil + + default: + return 0, fmt.Errorf("unknown asset type: %v", rpcType) + } +} + // MarshalGenesisInfo marshals the native asset genesis into the RPC // counterpart. func MarshalGenesisInfo(gen *asset.Genesis, @@ -256,12 +270,17 @@ func UnmarshalGenesisInfo(rpcGen *taprpc.GenesisInfo) (*asset.Genesis, error) { sha256.Size) } + assetType, err := UnmarshalAssetType(rpcGen.AssetType) + if err != nil { + return nil, err + } + return &asset.Genesis{ FirstPrevOut: *firstPrevOut, Tag: rpcGen.Name, MetaHash: fn.ToArray[[32]byte](rpcGen.MetaHash), OutputIndex: rpcGen.OutputIndex, - Type: asset.Type(rpcGen.AssetType), + Type: assetType, }, nil } diff --git a/rpcutils/marshal_test.go b/rpcutils/marshal_test.go index 2aff46563b..eb6fa36735 100644 --- a/rpcutils/marshal_test.go +++ b/rpcutils/marshal_test.go @@ -3,6 +3,8 @@ package rpcutils import ( "testing" + "github.com/lightninglabs/taproot-assets/asset" + "github.com/lightninglabs/taproot-assets/taprpc" "github.com/stretchr/testify/require" ) @@ -83,3 +85,49 @@ func TestParseDerivationPath(t *testing.T) { }) } } + +func TestUnmarshalAssetType(t *testing.T) { + testCases := []struct { + name string + rpcType taprpc.AssetType + expectedErr string + expectedResult asset.Type + }{{ + name: "normal", + rpcType: taprpc.AssetType_NORMAL, + expectedResult: asset.Normal, + }, { + name: "collectible", + rpcType: taprpc.AssetType_COLLECTIBLE, + expectedResult: asset.Collectible, + }, { + name: "unknown positive value", + rpcType: taprpc.AssetType(2), + expectedErr: "unknown asset type", + }, { + name: "value 256", + rpcType: taprpc.AssetType(256), + expectedErr: "unknown asset type", + }, { + name: "negative value", + rpcType: taprpc.AssetType(-1), + expectedErr: "unknown asset type", + }} + + for _, tc := range testCases { + tc := tc + + t.Run(tc.name, func(t *testing.T) { + result, err := UnmarshalAssetType(tc.rpcType) + + if tc.expectedErr != "" { + require.Error(t, err) + require.Contains(t, err.Error(), tc.expectedErr) + return + } + + require.NoError(t, err) + require.Equal(t, tc.expectedResult, result) + }) + } +}