-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rpc: move MarshalAsset function to new file taprpc/marshal.go
This refactor allows us to avoid an import cycle when the RPC proof courier marshals an asset.
- Loading branch information
Showing
3 changed files
with
104 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |