Skip to content

Commit 87d3f11

Browse files
committed
tapdb+tapfreighter: correctly identify pedersen keys
1 parent 10da0b4 commit 87d3f11

File tree

3 files changed

+56
-11
lines changed

3 files changed

+56
-11
lines changed

tapdb/assets_store.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3167,17 +3167,22 @@ func (a *AssetStore) LogAnchorTxConfirm(ctx context.Context,
31673167
isBurn := !isNumsKey && len(witnessData) > 0 &&
31683168
asset.IsBurnKey(scriptPubKey, witnessData[0])
31693169
isKnown := fullScriptKey.Type != asset.ScriptKeyUnknown
3170+
isRemotePedersen := fullScriptKey.Type ==
3171+
asset.ScriptKeyUniquePedersen &&
3172+
!out.ScriptKeyLocal
31703173
skipAssetCreation := !isTombstone && !isBurn &&
3171-
!out.ScriptKeyLocal && !isKnown
3174+
!out.ScriptKeyLocal &&
3175+
(!isKnown || isRemotePedersen)
31723176

31733177
log.Tracef("Skip asset creation for "+
31743178
"output %d?: %v, position=%v, scriptKey=%x, "+
31753179
"isTombstone=%v, isBurn=%v, "+
3176-
"scriptKeyLocal=%v, scriptKeyKnown=%v",
3177-
idx, skipAssetCreation, out.Position,
3178-
scriptPubKey.SerializeCompressed(),
3179-
isTombstone, isBurn, out.ScriptKeyLocal,
3180-
isKnown)
3180+
"scriptKeyLocal=%v, scriptKeyKnown=%v, "+
3181+
"isRemotePedersen=%v", idx, skipAssetCreation,
3182+
out.Position,
3183+
scriptPubKey.SerializeCompressed(), isTombstone,
3184+
isBurn, out.ScriptKeyLocal, isKnown,
3185+
isRemotePedersen)
31813186

31823187
// If this is an outbound transfer (meaning that our
31833188
// node doesn't control the script key, and it isn't a

tapfreighter/chain_porter.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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.

tapfreighter/parcel.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ type sendPackage struct {
538538
// they were already committed at.
539539
func ConvertToTransfer(currentHeight uint32, activeTransfers []*tappsbt.VPacket,
540540
anchorTx *tapsend.AnchorTransaction, passiveAssets []*tappsbt.VPacket,
541-
isLocalKey func(asset.ScriptKey) bool, label string,
541+
isLocalKey func(asset.ScriptKey) (bool, error), label string,
542542
skipAnchorTxBroadcast bool) (*OutboundParcel, error) {
543543

544544
var passiveAssetAnchor *Anchor
@@ -650,7 +650,8 @@ func transferInput(vIn *tappsbt.VInput) (*TransferInput, error) {
650650
// packet.
651651
func transferOutput(vPkt *tappsbt.VPacket, vOutIdx int, position uint64,
652652
anchorTx *tapsend.AnchorTransaction, passiveAssets []*tappsbt.VPacket,
653-
isLocalKey func(asset.ScriptKey) bool) (*TransferOutput, error) {
653+
isLocalKey func(asset.ScriptKey) (bool, error)) (*TransferOutput,
654+
error) {
654655

655656
vOut := vPkt.Outputs[vOutIdx]
656657

@@ -680,6 +681,12 @@ func transferOutput(vPkt *tappsbt.VPacket, vOutIdx int, position uint64,
680681
return nil, fmt.Errorf("unable to create anchor: %w", err)
681682
}
682683

684+
scriptKeyLocal, err := isLocalKey(vOut.ScriptKey)
685+
if err != nil {
686+
return nil, fmt.Errorf("unable to determine if script key "+
687+
"is local: %w", err)
688+
}
689+
683690
out := TransferOutput{
684691
Anchor: *anchor,
685692
Type: vOut.Type,
@@ -692,7 +699,7 @@ func transferOutput(vPkt *tappsbt.VPacket, vOutIdx int, position uint64,
692699
SplitCommitmentRoot: vOut.Asset.SplitCommitmentRoot,
693700
ProofSuffix: proofSuffixBytes,
694701
ProofCourierAddr: proofCourierAddrBytes,
695-
ScriptKeyLocal: isLocalKey(vOut.ScriptKey),
702+
ScriptKeyLocal: scriptKeyLocal,
696703
Position: position,
697704
}
698705

0 commit comments

Comments
 (0)