Skip to content

Commit f5f48dc

Browse files
committed
tapfreighter: set type for burn/tombstone keys correctly
In order for burn/tombstone keys to be stored with the correct type, we need to detect them before storing the transfer outputs to the database. We can't set the script key type before, because we're carrying around the script key in a virtual packet, where that information isn't available.
1 parent 6888d9a commit f5f48dc

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

tapfreighter/chain_porter.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,6 +1326,12 @@ func (p *ChainPorter) stateStep(currentPkg sendPackage) (*sendPackage, error) {
13261326
"%w", err)
13271327
}
13281328

1329+
// Burn and tombstone keys are the only keys that we don't
1330+
// explicitly store in the DB before this point. But we'll want
1331+
// them to have the correct type when creating the transfer, so
1332+
// we'll set that now.
1333+
detectUnSpendableKeys(currentPkg.VirtualPackets)
1334+
13291335
// We now need to find out if this is a transfer to ourselves
13301336
// (e.g. a change output) or an outbound transfer. A key being
13311337
// local means the lnd node connected to this daemon knows how
@@ -1597,6 +1603,53 @@ func (p *ChainPorter) publishSubscriberEvent(event fn.Event) {
15971603
}
15981604
}
15991605

1606+
// detectUnSpendableKeys checks if any of the outputs in the virtual packets are
1607+
// burn or tombstone keys and sets the appropriate type on the output script
1608+
// key.
1609+
func detectUnSpendableKeys(activeTransfers []*tappsbt.VPacket) {
1610+
setScriptKeyType := func(vOut *tappsbt.VOutput,
1611+
scriptKeyType asset.ScriptKeyType) {
1612+
1613+
if vOut.Asset.ScriptKey.TweakedScriptKey == nil {
1614+
vOut.Asset.ScriptKey.TweakedScriptKey = new(
1615+
asset.TweakedScriptKey,
1616+
)
1617+
vOut.Asset.ScriptKey.RawKey.PubKey =
1618+
vOut.Asset.ScriptKey.PubKey
1619+
}
1620+
if vOut.ScriptKey.TweakedScriptKey == nil {
1621+
vOut.ScriptKey.TweakedScriptKey = new(
1622+
asset.TweakedScriptKey,
1623+
)
1624+
vOut.ScriptKey.RawKey.PubKey = vOut.ScriptKey.PubKey
1625+
}
1626+
1627+
vOut.Asset.ScriptKey.Type = scriptKeyType
1628+
vOut.ScriptKey.Type = scriptKeyType
1629+
}
1630+
1631+
for _, vPkt := range activeTransfers {
1632+
for _, vOut := range vPkt.Outputs {
1633+
if vOut.Asset == nil {
1634+
continue
1635+
}
1636+
1637+
witness := vOut.Asset.PrevWitnesses
1638+
if len(witness) > 0 && asset.IsBurnKey(
1639+
vOut.ScriptKey.PubKey, witness[0],
1640+
) {
1641+
1642+
setScriptKeyType(vOut, asset.ScriptKeyBurn)
1643+
}
1644+
1645+
unSpendable, _ := vOut.ScriptKey.IsUnSpendable()
1646+
if unSpendable {
1647+
setScriptKeyType(vOut, asset.ScriptKeyTombstone)
1648+
}
1649+
}
1650+
}
1651+
}
1652+
16001653
// A compile-time assertion to make sure ChainPorter satisfies the
16011654
// fn.EventPublisher interface.
16021655
var _ fn.EventPublisher[fn.Event, bool] = (*ChainPorter)(nil)

0 commit comments

Comments
 (0)