Skip to content

Commit

Permalink
tapcfg+tapchannel: register/declare funding script key
Browse files Browse the repository at this point in the history
To recognize the funding output as belonging to the wallet, we now also
insert it as a declared key into the wallet.
And to make sure the asset is created in the asset DB, we also don't
skip any transfer outputs that are marked as declared keys.
  • Loading branch information
guggero committed May 31, 2024
1 parent 3a0b179 commit f02e269
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
1 change: 1 addition & 0 deletions tapcfg/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ func genServerConfig(cfg *Config, cfgLogger btclog.Logger,
ErrReporter: msgTransportClient,
AssetWallet: assetWallet,
CoinSelector: coinSelect,
AddrBook: tapdbAddrBook,
ChainParams: tapChainParams,
GroupKeyIndex: tapdbAddrBook,
PeerMessenger: msgTransportClient,
Expand Down
26 changes: 25 additions & 1 deletion tapchannel/aux_funding_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/lightninglabs/taproot-assets/proof"
"github.com/lightninglabs/taproot-assets/rfq"
cmsg "github.com/lightninglabs/taproot-assets/tapchannelmsg"
"github.com/lightninglabs/taproot-assets/tapdb"
"github.com/lightninglabs/taproot-assets/tapfreighter"
"github.com/lightninglabs/taproot-assets/tapgarden"
"github.com/lightninglabs/taproot-assets/tappsbt"
Expand Down Expand Up @@ -135,8 +136,12 @@ type FundingControllerCfg struct {
// specific steps of the funding process.
AssetWallet tapfreighter.Wallet

// CoinSelector is used to select assets for funding.
CoinSelector tapfreighter.CoinSelector

// AddrBook is used to manage script keys and addresses.
AddrBook *tapdb.TapAddressBook

// ChainParams is the chain params of the chain we operate on.
ChainParams address.ChainParams

Expand Down Expand Up @@ -591,7 +596,26 @@ func (f *FundingController) fundVirtualPacket(ctx context.Context,
// Our funding script key will be the OP_TRUE addr that we'll use as
// the funding script on the asset level.
fundingScriptTree := NewFundingScriptTree()
fundingScriptKey := asset.NewScriptKey(fundingScriptTree.TaprootKey)
fundingTaprootKey, _ := schnorr.ParsePubKey(
schnorr.SerializePubKey(fundingScriptTree.TaprootKey),
)
fundingScriptKey := asset.ScriptKey{
PubKey: fundingTaprootKey,
TweakedScriptKey: &asset.TweakedScriptKey{
RawKey: keychain.KeyDescriptor{
PubKey: fundingScriptTree.InternalKey,
},
Tweak: fundingScriptTree.TapscriptRoot,
},
}

// We'll also need to import the funding script key into the wallet so
// the asset will be materialized in the asset table and show up in the
// balance correctly.
err := f.cfg.AddrBook.InsertScriptKey(ctx, fundingScriptKey, true)
if err != nil {
return nil, fmt.Errorf("unable to insert script key: %w", err)
}

// Next, we'll use the asset wallet to fund a new vPSBT which'll be
// used as the asset level funding output for this transaction. In this
Expand Down
13 changes: 12 additions & 1 deletion tapdb/assets_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2668,6 +2668,7 @@ func (a *AssetStore) ConfirmParcelDelivery(ctx context.Context,
out.OutputType == int16(tappsbt.TypeSplitRoot)
isBurn := !isNumsKey && len(witnessData) > 0 &&
asset.IsBurnKey(scriptPubKey, witnessData[0])
scriptKeyDeclared := out.ScriptKeyDeclared.Valid

// If this is an outbound transfer (meaning that our
// node doesn't control the script key, and it isn't a
Expand All @@ -2676,7 +2677,17 @@ func (a *AssetStore) ConfirmParcelDelivery(ctx context.Context,
// leaving the node. The same goes for outputs that are
// only used to anchor passive assets, which are handled
// separately.
if !isTombstone && !isBurn && !out.ScriptKeyLocal {
if !isTombstone && !isBurn && !out.ScriptKeyLocal &&
!scriptKeyDeclared {

log.Tracef("Skipping asset creation for "+
"output %d with script key %x, "+
"isTombstone=%v, isBurn=%v, "+
"scriptKeyLocal=%v, "+
"scriptKeyDeclared=%v", idx,
scriptPubKey.SerializeCompressed(),
isTombstone, isBurn, out.ScriptKeyLocal,
scriptKeyDeclared)
continue
}

Expand Down

0 comments on commit f02e269

Please sign in to comment.