@@ -16,6 +16,7 @@ import (
1616 "github.com/btcsuite/btcd/txscript"
1717 "github.com/btcsuite/btcd/wire"
1818 "github.com/davecgh/go-spew/spew"
19+ "github.com/lightninglabs/taproot-assets/address"
1920 "github.com/lightninglabs/taproot-assets/asset"
2021 "github.com/lightninglabs/taproot-assets/fn"
2122 "github.com/lightninglabs/taproot-assets/proof"
@@ -1457,9 +1458,41 @@ func (p *ChainPorter) stateStep(currentPkg sendPackage) (*sendPackage, error) {
14571458 // (e.g. a change output) or an outbound transfer. A key being
14581459 // local means the lnd node connected to this daemon knows how
14591460 // to derive the key.
1460- isLocalKey := func (key asset.ScriptKey ) bool {
1461+ isLocalKey := func (key asset.ScriptKey ) (bool , error ) {
1462+ // To make sure we have the correct internal key with
1463+ // the family and index set, we attempt to fetch it
1464+ // from the database. If it exists, then we know we
1465+ // stored it with the correct information.
1466+ dbKey , err := p .cfg .AssetWallet .FetchScriptKey (
1467+ ctx , key .PubKey ,
1468+ )
1469+ switch {
1470+ // If this isn't an output that goes to us, we won't
1471+ // find it, which is okay. Only for other database
1472+ // errors do we return the error.
1473+ case err != nil &&
1474+ ! errors .Is (err , address .ErrScriptKeyNotFound ):
1475+
1476+ return false , fmt .Errorf ("error fetching " +
1477+ "script key: %w" , err )
1478+
1479+ // We did find the key, so we can check if it's a local
1480+ // key with the key ring.
1481+ case err == nil :
1482+ return p .cfg .KeyRing .IsLocalKey (
1483+ ctx , dbKey .RawKey ,
1484+ ), nil
1485+ }
1486+
1487+ // As a fallback, in case only the internal key was
1488+ // declared, we can check if the key is local by
1489+ // using the info we have, with a potential for a false
1490+ // negative if the key family and index isn't set at
1491+ // this point. But if it isn't set, then we didn't
1492+ // import/declare the key before, so it's very likely
1493+ // not ours anyway.
14611494 return key .TweakedScriptKey != nil &&
1462- p .cfg .KeyRing .IsLocalKey (ctx , key .RawKey )
1495+ p .cfg .KeyRing .IsLocalKey (ctx , key .RawKey ), nil
14631496 }
14641497
14651498 // We need to prepare the parcel for storage.
0 commit comments