Skip to content

Commit

Permalink
rpcserver: unmarshal Asset.ExternalGroupKey in MintAsset endpoint
Browse files Browse the repository at this point in the history
Add unmarshalling support for the new `Asset.ExternalGroupKey` field
in the `MintAsset` RPC endpoint. Also populate the external key
in the `Seedling` instance.
  • Loading branch information
ffranr committed Jan 9, 2025
1 parent 74decd6 commit a993070
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
32 changes: 32 additions & 0 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,13 @@ func (r *rpcServer) MintAsset(ctx context.Context,
groupTapscriptRoot = bytes.Clone(req.Asset.GroupTapscriptRoot)
}

if req.Asset.ExternalGroupKey != nil &&
req.Asset.GroupInternalKey != nil {

return nil, fmt.Errorf("cannot set both external group key " +
"and group internal key descriptor")
}

seedling := &tapgarden.Seedling{
AssetVersion: assetVersion,
AssetType: asset.Type(req.Asset.AssetType),
Expand All @@ -606,6 +613,31 @@ func (r *rpcServer) MintAsset(ctx context.Context,
seedling.GroupTapscriptRoot = groupTapscriptRoot
}

if req.Asset.ExternalGroupKey != nil {
externalKey, err := taprpc.UnmarshalExternalKey(
req.Asset.ExternalGroupKey,
)
if err != nil {
return nil, fmt.Errorf("unable to parse external key: "+
"%w", err)
}

if err := externalKey.Validate(); err != nil {
return nil, fmt.Errorf("invalid external key: %w", err)
}

internalKey, err := externalKey.PubKey()
if err != nil {
return nil, fmt.Errorf("unable to derive internal "+
"group key from xpub: %w", err)
}

seedling.ExternalKey = fn.Some(externalKey)
seedling.GroupInternalKey = &keychain.KeyDescriptor{
PubKey: &internalKey,
}
}

switch {
// If a group key is provided, parse the provided group public key
// before creating the asset seedling.
Expand Down
48 changes: 48 additions & 0 deletions taprpc/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import (
"bytes"
"context"
"crypto/sha256"
"encoding/binary"
"fmt"

"github.com/btcsuite/btcd/blockchain"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/schnorr"
"github.com/btcsuite/btcd/btcutil/hdkeychain"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/lightninglabs/taproot-assets/asset"
Expand All @@ -19,6 +21,7 @@ import (
"github.com/lightninglabs/taproot-assets/rfqmsg"
"github.com/lightninglabs/taproot-assets/taprpc/rfqrpc"
"github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/lntest"
)

// Shorthand for the asset transfer output proof delivery status enum.
Expand Down Expand Up @@ -326,6 +329,51 @@ func MarshalGroupKeyRequest(req *asset.GroupKeyRequest) (*GroupKeyRequest,
}, nil
}

// UnmarshalExternalKey parses an external key from the RPC variant.
func UnmarshalExternalKey(rpcKey *ExternalKey) (asset.ExternalKey, error) {
if rpcKey == nil {
return asset.ExternalKey{}, fmt.Errorf("unexpected nil RPC " +
"external key")
}

// Parse xpub.
xpub, err := hdkeychain.NewKeyFromString(rpcKey.Xpub)
if err != nil {
return asset.ExternalKey{}, err
}

// Parse derivation path.
path, err := lntest.ParseDerivationPath(rpcKey.DerivationPath)
if err != nil {
return asset.ExternalKey{}, err
}

// We assume the first three elements of the derivation path are
// hardened, so we need to add the hardened key offset.
for i := 0; i < 3; i++ {
path[i] += hdkeychain.HardenedKeyStart
}

// Parse master fingerprint.
var masterFingerprint uint32
if len(rpcKey.MasterFingerprint) > 0 {
if len(rpcKey.MasterFingerprint) != 4 {
return asset.ExternalKey{}, fmt.Errorf("master " +
"fingerprint must be 4 bytes")
}

masterFingerprint = binary.LittleEndian.Uint32(
rpcKey.MasterFingerprint,
)
}

return asset.ExternalKey{
XPub: *xpub,
MasterFingerprint: masterFingerprint,
DerivationPath: path,
}, nil
}

// MarshalGroupVirtualTx marshals the native asset group virtual transaction
// into the RPC counterpart.
func MarshalGroupVirtualTx(genTx *asset.GroupVirtualTx) (*GroupVirtualTx,
Expand Down

0 comments on commit a993070

Please sign in to comment.