diff --git a/rpcserver.go b/rpcserver.go index 54cf1a975..9f077e1fc 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -4668,63 +4668,57 @@ func UnmarshalUniID(rpcID *unirpc.ID) (universe.Identifier, error) { return universe.Identifier{}, fmt.Errorf("unable to unmarshal "+ "proof type: %w", err) } + + var ( + assetIDBytes []byte + groupKeyBytes []byte + ) + switch { case rpcID.GetAssetId() != nil: - rpcAssetID := rpcID.GetAssetId() - if len(rpcAssetID) != sha256.Size { + assetIDBytes = rpcID.GetAssetId() + if len(assetIDBytes) != sha256.Size { return universe.Identifier{}, fmt.Errorf("asset ID " + "must be 32 bytes") } - var assetID asset.ID - copy(assetID[:], rpcAssetID) - - return universe.Identifier{ - AssetID: assetID, - ProofType: proofType, - }, nil - case rpcID.GetAssetIdStr() != "": rpcAssetIDStr := rpcID.GetAssetIdStr() if len(rpcAssetIDStr) != sha256.Size*2 { - return universe.Identifier{}, fmt.Errorf("asset ID string " + - "must be 64 bytes") + return universe.Identifier{}, fmt.Errorf("asset ID " + + "string must be 64 chars") } - assetIDBytes, err := hex.DecodeString(rpcAssetIDStr) + assetIDBytes, err = hex.DecodeString(rpcAssetIDStr) if err != nil { return universe.Identifier{}, err } - // TODO(roasbeef): reuse with above - - var assetID asset.ID - copy(assetID[:], assetIDBytes) - - return universe.Identifier{ - AssetID: assetID, - ProofType: proofType, - }, nil - case rpcID.GetGroupKey() != nil: - groupKey, err := parseUserKey(rpcID.GetGroupKey()) + groupKeyBytes = rpcID.GetGroupKey() + + case rpcID.GetGroupKeyStr() != "": + rpcGroupKeyStr := rpcID.GetGroupKeyStr() + groupKeyBytes, err = hex.DecodeString(rpcGroupKeyStr) if err != nil { return universe.Identifier{}, err } + default: + return universe.Identifier{}, fmt.Errorf("no id set") + } + + switch { + case len(assetIDBytes) != 0: + var assetID asset.ID + copy(assetID[:], assetIDBytes) + return universe.Identifier{ - GroupKey: groupKey, + AssetID: assetID, ProofType: proofType, }, nil - case rpcID.GetGroupKeyStr() != "": - groupKeyBytes, err := hex.DecodeString(rpcID.GetGroupKeyStr()) - if err != nil { - return universe.Identifier{}, err - } - - // TODO(roasbeef): reuse with above - + case len(groupKeyBytes) != 0: groupKey, err := parseUserKey(groupKeyBytes) if err != nil { return universe.Identifier{}, err @@ -4736,7 +4730,7 @@ func UnmarshalUniID(rpcID *unirpc.ID) (universe.Identifier, error) { }, nil default: - return universe.Identifier{}, fmt.Errorf("no id set") + return universe.Identifier{}, fmt.Errorf("malformed id") } } @@ -5341,6 +5335,10 @@ func unmarshalUniverseKey(key *unirpc.UniverseKey) (universe.Identifier, // unmarshalAssetLeaf unmarshals an asset leaf from the RPC form. func unmarshalAssetLeaf(leaf *unirpc.AssetLeaf) (*universe.Leaf, error) { + if leaf == nil { + return nil, fmt.Errorf("missing asset leaf") + } + // We'll just pull the asset details from the serialized issuance proof // itself. var proofAsset asset.Asset @@ -5371,6 +5369,10 @@ func unmarshalAssetLeaf(leaf *unirpc.AssetLeaf) (*universe.Leaf, error) { func (r *rpcServer) InsertProof(ctx context.Context, req *unirpc.AssetProof) (*unirpc.AssetProofResponse, error) { + if req == nil { + return nil, fmt.Errorf("missing proof and universe key") + } + universeID, leafKey, err := unmarshalUniverseKey(req.Key) if err != nil { return nil, err @@ -6196,44 +6198,40 @@ func unmarshalAssetSpecifier(req *rfqrpc.AssetSpecifier) (*asset.ID, // give precedence to the asset ID due to its higher level of // specificity. var ( - assetID *asset.ID - + assetIDBytes []byte + assetID *asset.ID groupKeyBytes []byte groupKey *btcec.PublicKey - - err error + err error ) switch { // Parse the asset ID if it's set. case len(req.GetAssetId()) > 0: - var assetIdBytes [32]byte - copy(assetIdBytes[:], req.GetAssetId()) - id := asset.ID(assetIdBytes) - assetID = &id + assetIDBytes = req.GetAssetId() + if len(assetIDBytes) != sha256.Size { + return nil, nil, fmt.Errorf("asset ID must be 32 bytes") + } case len(req.GetAssetIdStr()) > 0: - assetIDBytes, err := hex.DecodeString(req.GetAssetIdStr()) + reqAssetIDStr := req.GetAssetIdStr() + if len(reqAssetIDStr) != sha256.Size*2 { + return nil, nil, fmt.Errorf("asset ID string must be " + + "64 chars") + } + + assetIDBytes, err = hex.DecodeString(reqAssetIDStr) if err != nil { return nil, nil, fmt.Errorf("error decoding asset "+ "ID: %w", err) } - var id asset.ID - copy(id[:], assetIDBytes) - assetID = &id - // Parse the group key if it's set. case len(req.GetGroupKey()) > 0: groupKeyBytes = req.GetGroupKey() - groupKey, err = btcec.ParsePubKey(groupKeyBytes) - if err != nil { - return nil, nil, fmt.Errorf("error parsing group "+ - "key: %w", err) - } case len(req.GetGroupKeyStr()) > 0: - groupKeyBytes, err := hex.DecodeString( + groupKeyBytes, err = hex.DecodeString( req.GetGroupKeyStr(), ) if err != nil { @@ -6241,12 +6239,6 @@ func unmarshalAssetSpecifier(req *rfqrpc.AssetSpecifier) (*asset.ID, "key: %w", err) } - groupKey, err = btcec.ParsePubKey(groupKeyBytes) - if err != nil { - return nil, nil, fmt.Errorf("error parsing group "+ - "key: %w", err) - } - default: // At this point, we know that neither the asset ID nor the // group key are specified. Return an error. @@ -6254,6 +6246,23 @@ func unmarshalAssetSpecifier(req *rfqrpc.AssetSpecifier) (*asset.ID, "key must be specified") } + switch { + case len(assetIDBytes) != 0: + var id asset.ID + copy(id[:], assetIDBytes) + assetID = &id + + case len(groupKeyBytes) != 0: + groupKey, err = parseUserKey(groupKeyBytes) + if err != nil { + return nil, nil, fmt.Errorf("error parsing group "+ + "key: group key: %w", err) + } + + default: + return nil, nil, fmt.Errorf("malformed asset specifier") + } + return assetID, groupKey, nil } diff --git a/testdata/rapid/TestUnmarshalAssetLeaf/TestUnmarshalAssetLeaf-20240815223752-3701568.fail b/testdata/rapid/TestUnmarshalAssetLeaf/TestUnmarshalAssetLeaf-20240815223752-3701568.fail new file mode 100644 index 000000000..c4df2a851 --- /dev/null +++ b/testdata/rapid/TestUnmarshalAssetLeaf/TestUnmarshalAssetLeaf-20240815223752-3701568.fail @@ -0,0 +1,4 @@ +# 2024/08/15 22:37:52.574252 [TestUnmarshalAssetLeaf] [rapid] draw Leaf: (*universerpc.AssetLeaf)(nil) +# +v0.4.8#10744725395972853327 +0x0 \ No newline at end of file