Skip to content

Commit

Permalink
rpc: move MarshalAsset function to new file taprpc/marshal.go
Browse files Browse the repository at this point in the history
This refactor allows us to avoid an import cycle when the RPC proof
courier marshals an asset.
  • Loading branch information
ffranr committed Aug 29, 2023
1 parent cf762a7 commit b875569
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 94 deletions.
3 changes: 2 additions & 1 deletion cmd/tapcli/universe.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

tap "github.com/lightninglabs/taproot-assets"
"github.com/lightninglabs/taproot-assets/proof"
"github.com/lightninglabs/taproot-assets/taprpc"
"github.com/lightninglabs/taproot-assets/taprpc/universerpc"
unirpc "github.com/lightninglabs/taproot-assets/taprpc/universerpc"
"github.com/lightningnetwork/lnd/lncfg"
Expand Down Expand Up @@ -417,7 +418,7 @@ func universeProofInsert(ctx *cli.Context) error {
if err != nil {
return err
}
rpcAsset, err := tap.MarshalAsset(
rpcAsset, err := taprpc.MarshalAsset(
ctxc, &assetProof.Asset, false, true, nil,
)
if err != nil {
Expand Down
96 changes: 3 additions & 93 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ func (r *rpcServer) fetchRpcAssets(ctx context.Context, withWitness,
func (r *rpcServer) marshalChainAsset(ctx context.Context, a *tapdb.ChainAsset,
withWitness bool) (*taprpc.Asset, error) {

rpcAsset, err := MarshalAsset(
rpcAsset, err := taprpc.MarshalAsset(
ctx, a.Asset, a.IsSpent, withWitness, r.cfg.AddrBook,
)
if err != nil {
Expand Down Expand Up @@ -616,96 +616,6 @@ func (r *rpcServer) marshalChainAsset(ctx context.Context, a *tapdb.ChainAsset,
return rpcAsset, nil
}

// KeyLookup is used to determine whether a key is under the control of the
// local wallet.
type KeyLookup interface {
// IsLocalKey returns true if the key is under the control of the
// wallet and can be derived by it.
IsLocalKey(ctx context.Context, desc keychain.KeyDescriptor) bool
}

func MarshalAsset(ctx context.Context, a *asset.Asset,
isSpent, withWitness bool,
keyRing KeyLookup) (*taprpc.Asset, error) {

assetID := a.Genesis.ID()
scriptKeyIsLocal := false
if a.ScriptKey.TweakedScriptKey != nil && keyRing != nil {
scriptKeyIsLocal = keyRing.IsLocalKey(
ctx, a.ScriptKey.RawKey,
)
}

rpcAsset := &taprpc.Asset{
Version: int32(a.Version),
AssetGenesis: &taprpc.GenesisInfo{
GenesisPoint: a.Genesis.FirstPrevOut.String(),
Name: a.Genesis.Tag,
MetaHash: a.Genesis.MetaHash[:],
AssetId: assetID[:],
OutputIndex: a.Genesis.OutputIndex,
},
AssetType: taprpc.AssetType(a.Type),
Amount: a.Amount,
LockTime: int32(a.LockTime),
RelativeLockTime: int32(a.RelativeLockTime),
ScriptVersion: int32(a.ScriptVersion),
ScriptKey: a.ScriptKey.PubKey.SerializeCompressed(),
ScriptKeyIsLocal: scriptKeyIsLocal,
IsSpent: isSpent,
}

if a.GroupKey != nil {
var rawKey []byte
if a.GroupKey.RawKey.PubKey != nil {
rawKey = a.GroupKey.RawKey.PubKey.SerializeCompressed()
}
rpcAsset.AssetGroup = &taprpc.AssetGroup{
RawGroupKey: rawKey,
TweakedGroupKey: a.GroupKey.GroupPubKey.SerializeCompressed(),
AssetIdSig: a.GroupKey.Sig.Serialize(),
}
}

if withWitness {
for idx := range a.PrevWitnesses {
witness := a.PrevWitnesses[idx]

prevID := witness.PrevID
rpcPrevID := &taprpc.PrevInputAsset{
AnchorPoint: prevID.OutPoint.String(),
AssetId: prevID.ID[:],
ScriptKey: prevID.ScriptKey[:],
}

var rpcSplitCommitment *taprpc.SplitCommitment
if witness.SplitCommitment != nil {
rootAsset, err := MarshalAsset(
ctx, &witness.SplitCommitment.RootAsset,
false, true, nil,
)
if err != nil {
return nil, err
}

rpcSplitCommitment = &taprpc.SplitCommitment{
RootAsset: rootAsset,
}
}

rpcAsset.PrevWitnesses = append(
rpcAsset.PrevWitnesses, &taprpc.PrevWitness{
PrevId: rpcPrevID,
TxWitness: witness.TxWitness,
SplitCommitment: rpcSplitCommitment,
},
)
}
}

return rpcAsset, nil
}

func (r *rpcServer) listBalancesByAsset(ctx context.Context,
assetID *asset.ID) (*taprpc.ListBalancesResponse, error) {

Expand Down Expand Up @@ -2586,7 +2496,7 @@ func (r *rpcServer) AssetLeafKeys(ctx context.Context,
return resp, nil
}

func marshalAssetLeaf(ctx context.Context, keys KeyLookup,
func marshalAssetLeaf(ctx context.Context, keys taprpc.KeyLookup,
assetLeaf *universe.MintingLeaf) (*unirpc.AssetLeaf, error) {

// In order to display the full asset, we'll parse the genesis
Expand All @@ -2598,7 +2508,7 @@ func marshalAssetLeaf(ctx context.Context, keys KeyLookup,
return nil, err
}

rpcAsset, err := MarshalAsset(
rpcAsset, err := taprpc.MarshalAsset(
ctx, &assetProof.Asset, false, true, keys,
)
if err != nil {
Expand Down
99 changes: 99 additions & 0 deletions taprpc/marshal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package taprpc

import (
"context"

"github.com/lightninglabs/taproot-assets/asset"
"github.com/lightningnetwork/lnd/keychain"
)

// KeyLookup is used to determine whether a key is under the control of the
// local wallet.
type KeyLookup interface {
// IsLocalKey returns true if the key is under the control of the
// wallet and can be derived by it.
IsLocalKey(ctx context.Context, desc keychain.KeyDescriptor) bool
}

// MarshalAsset converts an asset to its rpc representation.
func MarshalAsset(ctx context.Context, a *asset.Asset,
isSpent, withWitness bool,
keyRing KeyLookup) (*Asset, error) {

assetID := a.Genesis.ID()
scriptKeyIsLocal := false
if a.ScriptKey.TweakedScriptKey != nil && keyRing != nil {
scriptKeyIsLocal = keyRing.IsLocalKey(
ctx, a.ScriptKey.RawKey,
)
}

rpcAsset := &Asset{
Version: int32(a.Version),
AssetGenesis: &GenesisInfo{
GenesisPoint: a.Genesis.FirstPrevOut.String(),
Name: a.Genesis.Tag,
MetaHash: a.Genesis.MetaHash[:],
AssetId: assetID[:],
OutputIndex: a.Genesis.OutputIndex,
},
AssetType: AssetType(a.Type),
Amount: a.Amount,
LockTime: int32(a.LockTime),
RelativeLockTime: int32(a.RelativeLockTime),
ScriptVersion: int32(a.ScriptVersion),
ScriptKey: a.ScriptKey.PubKey.SerializeCompressed(),
ScriptKeyIsLocal: scriptKeyIsLocal,
IsSpent: isSpent,
}

if a.GroupKey != nil {
var rawKey []byte
if a.GroupKey.RawKey.PubKey != nil {
rawKey = a.GroupKey.RawKey.PubKey.SerializeCompressed()
}
rpcAsset.AssetGroup = &AssetGroup{
RawGroupKey: rawKey,
TweakedGroupKey: a.GroupKey.GroupPubKey.SerializeCompressed(),
AssetIdSig: a.GroupKey.Sig.Serialize(),
}
}

if withWitness {
for idx := range a.PrevWitnesses {
witness := a.PrevWitnesses[idx]

prevID := witness.PrevID
rpcPrevID := &PrevInputAsset{
AnchorPoint: prevID.OutPoint.String(),
AssetId: prevID.ID[:],
ScriptKey: prevID.ScriptKey[:],
}

var rpcSplitCommitment *SplitCommitment
if witness.SplitCommitment != nil {
rootAsset, err := MarshalAsset(
ctx, &witness.SplitCommitment.RootAsset,
false, true, nil,
)
if err != nil {
return nil, err
}

rpcSplitCommitment = &SplitCommitment{
RootAsset: rootAsset,
}
}

rpcAsset.PrevWitnesses = append(
rpcAsset.PrevWitnesses, &PrevWitness{
PrevId: rpcPrevID,
TxWitness: witness.TxWitness,
SplitCommitment: rpcSplitCommitment,
},
)
}
}

return rpcAsset, nil
}

0 comments on commit b875569

Please sign in to comment.