Skip to content

Commit

Permalink
rpcserver: simplify other unmarshal funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
jharveyb committed Aug 16, 2024
1 parent 428506f commit 3c85836
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 59 deletions.
127 changes: 68 additions & 59 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -6196,64 +6198,71 @@ 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 {
return nil, nil, fmt.Errorf("error decoding group "+
"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.
return nil, nil, fmt.Errorf("either asset ID or asset group " +
"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
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# 2024/08/15 22:37:52.574252 [TestUnmarshalAssetLeaf] [rapid] draw Leaf: (*universerpc.AssetLeaf)(nil)
#
v0.4.8#10744725395972853327
0x0

0 comments on commit 3c85836

Please sign in to comment.