From f02e26921e5b3b721e9492ef7f72900355753e0f Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Fri, 31 May 2024 13:07:33 +0200 Subject: [PATCH] tapcfg+tapchannel: register/declare funding script key 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. --- tapcfg/server.go | 1 + tapchannel/aux_funding_controller.go | 26 +++++++++++++++++++++++++- tapdb/assets_store.go | 13 ++++++++++++- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/tapcfg/server.go b/tapcfg/server.go index 1c629f492..f8eadf171 100644 --- a/tapcfg/server.go +++ b/tapcfg/server.go @@ -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, diff --git a/tapchannel/aux_funding_controller.go b/tapchannel/aux_funding_controller.go index 1dbd8532d..89ff9859a 100644 --- a/tapchannel/aux_funding_controller.go +++ b/tapchannel/aux_funding_controller.go @@ -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" @@ -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 @@ -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 diff --git a/tapdb/assets_store.go b/tapdb/assets_store.go index 4a493b08b..0836efb5c 100644 --- a/tapdb/assets_store.go +++ b/tapdb/assets_store.go @@ -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 @@ -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 }