From 06a669bc89273d9e89a83c84b1e656a7d4946d28 Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Wed, 19 Nov 2025 13:15:19 +0200 Subject: [PATCH] graph/db: add FundingPKScript method on ChannelEdgeInfo Add FundingPKScript() method that returns the funding output's pkScript for the channel. The implementation is version-aware: - V1: generates a 2-of-2 multisig P2WSH script from the two bitcoin keys - V2: will use taproot script (to be implemented) This encapsulates the script generation logic and makes it clear which bitcoin keys are being used. Replaces direct calls to genMultiSigP2WSH with the cleaner method call. --- graph/db/graph_test.go | 11 +++------- graph/db/kv_store.go | 10 +-------- graph/db/models/channel_edge_info.go | 33 ++++++++++++++++++++++++++++ graph/db/sql_store.go | 1 + 4 files changed, 38 insertions(+), 17 deletions(-) diff --git a/graph/db/graph_test.go b/graph/db/graph_test.go index 5c9c0308d..db308fe0b 100644 --- a/graph/db/graph_test.go +++ b/graph/db/graph_test.go @@ -2044,14 +2044,9 @@ func TestGraphPruning(t *testing.T) { t.Fatalf("unable to add node: %v", err) } - btcKey1 := edgeInfo.BitcoinKey1Bytes.UnwrapOr(route.Vertex{}) - btcKey2 := edgeInfo.BitcoinKey2Bytes.UnwrapOr(route.Vertex{}) - pkScript, err := genMultiSigP2WSH( - btcKey1[:], btcKey2[:], - ) - if err != nil { - t.Fatalf("unable to gen multi-sig p2wsh: %v", err) - } + pkScript, err := edgeInfo.FundingPKScript() + require.NoError(t, err) + edgePoints = append(edgePoints, EdgePoint{ FundingPkScript: pkScript, OutPoint: op, diff --git a/graph/db/kv_store.go b/graph/db/kv_store.go index f5a1ec219..0854a506f 100644 --- a/graph/db/kv_store.go +++ b/graph/db/kv_store.go @@ -4112,15 +4112,7 @@ func (c *KVStore) ChannelView() ([]EdgePoint, error) { return err } - btcKey1 := edgeInfo.BitcoinKey1Bytes.UnwrapOr( - route.Vertex{}, - ) - btcKey2 := edgeInfo.BitcoinKey2Bytes.UnwrapOr( - route.Vertex{}, - ) - pkScript, err := genMultiSigP2WSH( - btcKey1[:], btcKey2[:], - ) + pkScript, err := edgeInfo.FundingPKScript() if err != nil { return err } diff --git a/graph/db/models/channel_edge_info.go b/graph/db/models/channel_edge_info.go index aea69e919..3e4a06587 100644 --- a/graph/db/models/channel_edge_info.go +++ b/graph/db/models/channel_edge_info.go @@ -9,6 +9,7 @@ import ( "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/wire" "github.com/lightningnetwork/lnd/fn/v2" + "github.com/lightningnetwork/lnd/input" "github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/routing/route" ) @@ -200,6 +201,38 @@ func (c *ChannelEdgeInfo) OtherNodeKeyBytes(thisNodeKey []byte) ( } } +// FundingPKScript returns the funding output's pkScript for the channel. +func (c *ChannelEdgeInfo) FundingPKScript() ([]byte, error) { + switch c.Version { + case lnwire.GossipVersion1: + btc1Key, err := c.BitcoinKey1Bytes.UnwrapOrErr( + fmt.Errorf("missing bitcoin key 1"), + ) + if err != nil { + return nil, err + } + btc2Key, err := c.BitcoinKey2Bytes.UnwrapOrErr( + fmt.Errorf("missing bitcoin key 2"), + ) + if err != nil { + return nil, err + } + + witnessScript, err := input.GenMultiSigScript( + btc1Key[:], btc2Key[:], + ) + if err != nil { + return nil, err + } + + return input.WitnessScriptHash(witnessScript) + + default: + return nil, fmt.Errorf("unsupported channel version: %d", + c.Version) + } +} + // ToChannelAnnouncement converts the ChannelEdgeInfo to a // lnwire.ChannelAnnouncement1 message. Returns an error if AuthProof is nil // or if the version is not v1. diff --git a/graph/db/sql_store.go b/graph/db/sql_store.go index 281975e0c..e515b35c8 100644 --- a/graph/db/sql_store.go +++ b/graph/db/sql_store.go @@ -2768,6 +2768,7 @@ func (s *SQLStore) ChannelView() ([]EdgePoint, error) { handleChannel := func(_ context.Context, channel sqlc.ListChannelsPaginatedRow) error { + // TODO(elle): update to handle V2 channels. pkScript, err := genMultiSigP2WSH( channel.BitcoinKey1, channel.BitcoinKey2, )