graph/db: add FundingPKScript method on ChannelEdgeInfo
What changed, and why it matters
This commit is a routine code cleanup in LND's channel graph database. It moves the logic for generating a Bitcoin funding script into a dedicated method on a data structure, replacing a few direct helper function calls. There is no security fix or vulnerability here.
No security action needed. This is a refactoring commit. Reviewers may want to confirm the SQL-store TODO for V2 channels is tracked separately.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change adds a FundingPKScript() method on ChannelEdgeInfo that version-aware generates the funding output pkScript. For GossipVersion1 it builds a 2-of-2 multisig P2WSH from the two bitcoin keys using input.GenMultiSigScript and input.WitnessScriptHash. It replaces inline genMultiSigP2WSH calls in the KV store and tests. The SQL store path is left with a TODO for V2 support. No behavior change is introduced for V1 channels.
Changed components
graph/db/models/channel_edge_info.gograph/db/kv_store.gograph/db/graph_test.gograph/db/sql_store.goInspect captured patch +38 / −17
diff --git a/graph/db/graph_test.go b/graph/db/graph_test.go
index 5c9c030..db308fe 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 f5a1ec2..0854a50 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 aea69e9..3e4a065 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 281975e..e515b35 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,
)
Why this scored 13/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.