Skip to content

Commit

Permalink
taprpc+mutli: move marshal functions from priceoraclerpc and universerpc
Browse files Browse the repository at this point in the history
  • Loading branch information
bhandras committed Aug 16, 2024
1 parent c66ef87 commit 04e48ae
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 119 deletions.
3 changes: 2 additions & 1 deletion docs/examples/basic-price-oracle/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net"
"time"

"github.com/lightninglabs/taproot-assets/rpcutils"
oraclerpc "github.com/lightninglabs/taproot-assets/taprpc/priceoraclerpc"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
Expand Down Expand Up @@ -105,7 +106,7 @@ func (p *RpcPriceOracleServer) QueryRateTick(_ context.Context,

// Ensure that the payment asset is BTC. We only support BTC as the
// payment asset in this example.
if !oraclerpc.IsAssetBtc(req.PaymentAsset) {
if !rpcutils.IsAssetBtc(req.PaymentAsset) {
return &oraclerpc.QueryRateTickResponse{
Result: &oraclerpc.QueryRateTickResponse_Error{
Error: &oraclerpc.QueryRateTickErrResponse{
Expand Down
5 changes: 3 additions & 2 deletions itest/universe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/lightninglabs/taproot-assets/fn"
"github.com/lightninglabs/taproot-assets/internal/test"
"github.com/lightninglabs/taproot-assets/mssmt"
"github.com/lightninglabs/taproot-assets/rpcutils"
"github.com/lightninglabs/taproot-assets/taprpc"
"github.com/lightninglabs/taproot-assets/taprpc/mintrpc"
unirpc "github.com/lightninglabs/taproot-assets/taprpc/universerpc"
Expand Down Expand Up @@ -709,7 +710,7 @@ func testFederationSyncConfig(t *harnessTest) {
AssetID: assetID1,
ProofType: universe.ProofTypeIssuance,
}
uniIdRpc1 := unirpc.MarshalUniverseID(assetIDBytes1, nil)
uniIdRpc1 := rpcutils.MarshalUniverseID(assetIDBytes1, nil)
uniIdRpc1.ProofType = unirpc.ProofType_PROOF_TYPE_ISSUANCE

// Generate universe ID #2.
Expand All @@ -720,7 +721,7 @@ func testFederationSyncConfig(t *harnessTest) {
GroupKey: groupKey2,
ProofType: universe.ProofTypeTransfer,
}
uniIdRpc2 := unirpc.MarshalUniverseID(nil, groupKeyBytes2)
uniIdRpc2 := rpcutils.MarshalUniverseID(nil, groupKeyBytes2)
uniIdRpc2.ProofType = unirpc.ProofType_PROOF_TYPE_TRANSFER

// Set both the global and a universe specific federation sync configs.
Expand Down
8 changes: 4 additions & 4 deletions proof/courier.go
Original file line number Diff line number Diff line change
Expand Up @@ -1074,7 +1074,7 @@ func (c *UniverseRpcCourier) DeliverProof(ctx context.Context,

// Construct universe key.
outPoint := transitionProof.OutPoint()
assetKey := unirpc.MarshalAssetKey(
assetKey := rpcutils.MarshalAssetKey(
outPoint, proofAsset.ScriptKey.PubKey,
)
assetID := proofAsset.ID()
Expand All @@ -1088,7 +1088,7 @@ func (c *UniverseRpcCourier) DeliverProof(ctx context.Context,
groupPubKeyBytes = groupPubKey.SerializeCompressed()
}

universeID := unirpc.MarshalUniverseID(
universeID := rpcutils.MarshalUniverseID(
assetID[:], groupPubKeyBytes,
)
universeKey := unirpc.UniverseKey{
Expand Down Expand Up @@ -1150,10 +1150,10 @@ func (c *UniverseRpcCourier) ReceiveProof(ctx context.Context,
}

universeKey := unirpc.UniverseKey{
Id: unirpc.MarshalUniverseID(
Id: rpcutils.MarshalUniverseID(
loc.AssetID[:], groupKeyBytes,
),
LeafKey: unirpc.MarshalAssetKey(
LeafKey: rpcutils.MarshalAssetKey(
*loc.OutPoint, &loc.ScriptKey,
),
}
Expand Down
2 changes: 1 addition & 1 deletion rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -6160,7 +6160,7 @@ func MarshalAssetFedSyncCfg(
if uniID.GroupKey != nil {
groupKeyBytes = uniID.GroupKey.SerializeCompressed()
}
uniIdRPC := unirpc.MarshalUniverseID(assetIDBytes, groupKeyBytes)
uniIdRPC := rpcutils.MarshalUniverseID(assetIDBytes, groupKeyBytes)

// Marshal proof type.
proofTypeRpc, err := MarshalUniProofType(uniID.ProofType)
Expand Down
84 changes: 84 additions & 0 deletions rpcutils/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"fmt"

"github.com/btcsuite/btcd/blockchain"
Expand All @@ -17,7 +18,9 @@ import (
"github.com/lightninglabs/taproot-assets/fn"
"github.com/lightninglabs/taproot-assets/rfq"
"github.com/lightninglabs/taproot-assets/taprpc"
"github.com/lightninglabs/taproot-assets/taprpc/priceoraclerpc"
"github.com/lightninglabs/taproot-assets/taprpc/rfqrpc"
"github.com/lightninglabs/taproot-assets/taprpc/universerpc"
"github.com/lightningnetwork/lnd/keychain"
)

Expand Down Expand Up @@ -689,3 +692,84 @@ func NewAddAssetSellOrderResponse(
"type: %T", e)
}
}

// IsAssetBtc is a helper function that returns true if the given asset
// specifier represents BTC, and false otherwise.
func IsAssetBtc(assetSpecifier *priceoraclerpc.AssetSpecifier) bool {
// An unset asset specifier does not represent BTC.
if assetSpecifier == nil {
return false
}

// Verify that the asset specifier has a valid asset ID (either bytes or
// string). The asset ID must be all zeros for the asset specifier to
// represent BTC.
assetIdBytes := assetSpecifier.GetAssetId()
assetIdStr := assetSpecifier.GetAssetIdStr()

if len(assetIdBytes) != 32 && assetIdStr == "" {
return false
}

var assetId [32]byte
copy(assetId[:], assetIdBytes)

var zeroAssetId [32]byte
zeroAssetHexStr := hex.EncodeToString(zeroAssetId[:])

isAssetIdZero := bytes.Equal(assetId[:], zeroAssetId[:]) ||
assetIdStr == zeroAssetHexStr

// Ensure that the asset specifier does not have any group key related
// fields set. When specifying BTC, the group key fields must be unset.
groupKeySet := assetSpecifier.GetGroupKey() != nil ||
assetSpecifier.GetGroupKeyStr() != ""

return isAssetIdZero && !groupKeySet
}

// MarshalOutpoint marshals a wire.OutPoint into an RPC ready Outpoint.
func MarshalOutpoint(outPoint wire.OutPoint) *universerpc.Outpoint {
return &universerpc.Outpoint{
HashStr: outPoint.Hash.String(),
Index: int32(outPoint.Index),
}
}

// MarshalAssetKey returns an RPC ready AssetKey.
func MarshalAssetKey(outPoint wire.OutPoint,
scriptKeyPubKey *btcec.PublicKey) *universerpc.AssetKey {

scriptKeyBytes := scriptKeyPubKey.SerializeCompressed()

return &universerpc.AssetKey{
Outpoint: &universerpc.AssetKey_Op{
Op: MarshalOutpoint(outPoint),
},
ScriptKey: &universerpc.AssetKey_ScriptKeyBytes{
ScriptKeyBytes: scriptKeyBytes,
},
}
}

// MarshalUniverseID returns an RPC ready universe ID.
func MarshalUniverseID(assetIDBytes []byte,
groupKeyBytes []byte) *universerpc.ID {

// We will marshal either a group key ID or an asset ID. If group key
// bytes are given, we marshal a group key ID, otherwise we marshal an
// asset ID.
if groupKeyBytes != nil {
return &universerpc.ID{
Id: &universerpc.ID_GroupKey{
GroupKey: groupKeyBytes,
},
}
}

return &universerpc.ID{
Id: &universerpc.ID_AssetId{
AssetId: assetIDBytes,
},
}
}
35 changes: 18 additions & 17 deletions taprpc/priceoraclerpc/marshal_test.go → rpcutils/marshal_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package priceoraclerpc
package rpcutils

import (
"encoding/hex"
"testing"

"github.com/lightninglabs/taproot-assets/taprpc/priceoraclerpc"
"github.com/stretchr/testify/require"
)

// isAssetBtcTC is a test case for the IsAssetBtc function.
type isAssetBtcTC struct {
testName string

assetSpecifier *AssetSpecifier
assetSpecifier *priceoraclerpc.AssetSpecifier
expected bool
}

Expand All @@ -32,13 +33,13 @@ func TestIsAssetBtc(t *testing.T) {
},
{
testName: "empty asset specifier",
assetSpecifier: &AssetSpecifier{},
assetSpecifier: &priceoraclerpc.AssetSpecifier{},
expected: false,
},
{
testName: "asset specifier with zero asset ID bytes",
assetSpecifier: &AssetSpecifier{
Id: &AssetSpecifier_AssetId{
assetSpecifier: &priceoraclerpc.AssetSpecifier{
Id: &priceoraclerpc.AssetSpecifier_AssetId{
AssetId: zeroAssetId[:],
},
},
Expand All @@ -47,53 +48,53 @@ func TestIsAssetBtc(t *testing.T) {
{
testName: "asset specifier with incorrect length " +
"zero asset ID bytes",
assetSpecifier: &AssetSpecifier{
Id: &AssetSpecifier_AssetId{
assetSpecifier: &priceoraclerpc.AssetSpecifier{
Id: &priceoraclerpc.AssetSpecifier_AssetId{
AssetId: []byte{0, 0, 0},
},
},
expected: false,
},
{
testName: "asset specifier with empty asset ID bytes",
assetSpecifier: &AssetSpecifier{
Id: &AssetSpecifier_AssetId{
assetSpecifier: &priceoraclerpc.AssetSpecifier{
Id: &priceoraclerpc.AssetSpecifier_AssetId{
AssetId: []byte{},
},
},
expected: false,
},
{
testName: "asset specifier with zero asset ID string",
assetSpecifier: &AssetSpecifier{
Id: &AssetSpecifier_AssetIdStr{
assetSpecifier: &priceoraclerpc.AssetSpecifier{
Id: &priceoraclerpc.AssetSpecifier_AssetIdStr{
AssetIdStr: zeroAssetHexStr,
},
},
expected: true,
},
{
testName: "asset specifier with empty asset ID string",
assetSpecifier: &AssetSpecifier{
Id: &AssetSpecifier_AssetIdStr{
assetSpecifier: &priceoraclerpc.AssetSpecifier{
Id: &priceoraclerpc.AssetSpecifier_AssetIdStr{
AssetIdStr: "",
},
},
expected: false,
},
{
testName: "asset specifier with set group key bytes",
assetSpecifier: &AssetSpecifier{
Id: &AssetSpecifier_GroupKey{
assetSpecifier: &priceoraclerpc.AssetSpecifier{
Id: &priceoraclerpc.AssetSpecifier_GroupKey{
GroupKey: []byte{0, 0, 0},
},
},
expected: false,
},
{
testName: "asset specifier with set group key string",
assetSpecifier: &AssetSpecifier{
Id: &AssetSpecifier_GroupKeyStr{
assetSpecifier: &priceoraclerpc.AssetSpecifier{
Id: &priceoraclerpc.AssetSpecifier_GroupKeyStr{
GroupKeyStr: "test-group-key",
},
},
Expand Down
41 changes: 0 additions & 41 deletions taprpc/priceoraclerpc/marshal.go

This file was deleted.

53 changes: 0 additions & 53 deletions taprpc/universerpc/marshal.go

This file was deleted.

0 comments on commit 04e48ae

Please sign in to comment.