Skip to content

Commit

Permalink
taprpc+rpcserver: fix hex encoding issue with REST GET call
Browse files Browse the repository at this point in the history
  • Loading branch information
guggero committed Aug 9, 2023
1 parent a9755de commit f632208
Show file tree
Hide file tree
Showing 6 changed files with 443 additions and 120 deletions.
38 changes: 38 additions & 0 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2253,6 +2253,25 @@ func (r *rpcServer) FetchAssetMeta(ctx context.Context,
ctx, assetID,
)

case in.GetAssetIdStr() != "":
if len(in.GetAssetIdStr()) != hex.EncodedLen(sha256.Size) {
return nil, fmt.Errorf("asset ID must be 32 bytes")
}

var assetIDBytes []byte
assetIDBytes, err = hex.DecodeString(in.GetAssetIdStr())
if err != nil {
return nil, fmt.Errorf("error hex decoding asset ID: "+
"%w", err)
}

var assetID asset.ID
copy(assetID[:], assetIDBytes)

assetMeta, err = r.cfg.AssetStore.FetchAssetMetaForAsset(
ctx, assetID,
)

case in.GetMetaHash() != nil:
if len(in.GetMetaHash()) != sha256.Size {
return nil, fmt.Errorf("meta hash must be 32 bytes")
Expand All @@ -2265,6 +2284,25 @@ func (r *rpcServer) FetchAssetMeta(ctx context.Context,
ctx, metaHash,
)

case in.GetMetaHashStr() != "":
if len(in.GetMetaHashStr()) != hex.EncodedLen(sha256.Size) {
return nil, fmt.Errorf("meta hash must be 32 bytes")
}

var metaHashBytes []byte
metaHashBytes, err = hex.DecodeString(in.GetMetaHashStr())
if err != nil {
return nil, fmt.Errorf("error hex decoding meta hash: "+
"%w", err)
}

var metaHash [asset.MetaHashLen]byte
copy(metaHash[:], metaHashBytes)

assetMeta, err = r.cfg.AssetStore.FetchAssetMetaByHash(
ctx, metaHash,
)

default:
return nil, fmt.Errorf("either asset ID or meta hash must " +
"be set")
Expand Down
Loading

0 comments on commit f632208

Please sign in to comment.