Skip to content

Commit 3b0ddab

Browse files
committed
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.
1 parent 72828a3 commit 3b0ddab

File tree

4 files changed

+38
-17
lines changed

4 files changed

+38
-17
lines changed

graph/db/graph_test.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2005,14 +2005,9 @@ func TestGraphPruning(t *testing.T) {
20052005
t.Fatalf("unable to add node: %v", err)
20062006
}
20072007

2008-
btcKey1 := edgeInfo.BitcoinKey1Bytes.UnwrapOr(route.Vertex{})
2009-
btcKey2 := edgeInfo.BitcoinKey2Bytes.UnwrapOr(route.Vertex{})
2010-
pkScript, err := genMultiSigP2WSH(
2011-
btcKey1[:], btcKey2[:],
2012-
)
2013-
if err != nil {
2014-
t.Fatalf("unable to gen multi-sig p2wsh: %v", err)
2015-
}
2008+
pkScript, err := edgeInfo.FundingPKScript()
2009+
require.NoError(t, err)
2010+
20162011
edgePoints = append(edgePoints, EdgePoint{
20172012
FundingPkScript: pkScript,
20182013
OutPoint: op,

graph/db/kv_store.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4113,15 +4113,7 @@ func (c *KVStore) ChannelView() ([]EdgePoint, error) {
41134113
return err
41144114
}
41154115

4116-
btcKey1 := edgeInfo.BitcoinKey1Bytes.UnwrapOr(
4117-
route.Vertex{},
4118-
)
4119-
btcKey2 := edgeInfo.BitcoinKey2Bytes.UnwrapOr(
4120-
route.Vertex{},
4121-
)
4122-
pkScript, err := genMultiSigP2WSH(
4123-
btcKey1[:], btcKey2[:],
4124-
)
4116+
pkScript, err := edgeInfo.FundingPKScript()
41254117
if err != nil {
41264118
return err
41274119
}

graph/db/models/channel_edge_info.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/btcsuite/btcd/chaincfg/chainhash"
1010
"github.com/btcsuite/btcd/wire"
1111
"github.com/lightningnetwork/lnd/fn/v2"
12+
"github.com/lightningnetwork/lnd/input"
1213
"github.com/lightningnetwork/lnd/lnwire"
1314
"github.com/lightningnetwork/lnd/routing/route"
1415
)
@@ -221,6 +222,38 @@ func (c *ChannelEdgeInfo) OtherNodeKeyBytes(thisNodeKey []byte) (
221222
}
222223
}
223224

225+
// FundingPKScript returns the funding output's pkScript for the channel.
226+
func (c *ChannelEdgeInfo) FundingPKScript() ([]byte, error) {
227+
switch c.Version {
228+
case lnwire.GossipVersion1:
229+
btc1Key, err := c.BitcoinKey1Bytes.UnwrapOrErr(
230+
fmt.Errorf("missing bitcoin key 1"),
231+
)
232+
if err != nil {
233+
return nil, err
234+
}
235+
btc2Key, err := c.BitcoinKey2Bytes.UnwrapOrErr(
236+
fmt.Errorf("missing bitcoin key 2"),
237+
)
238+
if err != nil {
239+
return nil, err
240+
}
241+
242+
witnessScript, err := input.GenMultiSigScript(
243+
btc1Key[:], btc2Key[:],
244+
)
245+
if err != nil {
246+
return nil, err
247+
}
248+
249+
return input.WitnessScriptHash(witnessScript)
250+
251+
default:
252+
return nil, fmt.Errorf("unsupported channel version: %d",
253+
c.Version)
254+
}
255+
}
256+
224257
// ToChannelAnnouncement converts the ChannelEdgeInfo to a
225258
// lnwire.ChannelAnnouncement1 message. Returns an error if AuthProof is nil
226259
// or if the version is not v1.

graph/db/sql_store.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2755,6 +2755,7 @@ func (s *SQLStore) ChannelView() ([]EdgePoint, error) {
27552755
handleChannel := func(_ context.Context,
27562756
channel sqlc.ListChannelsPaginatedRow) error {
27572757

2758+
// TODO(elle): update to handle V2 channels.
27582759
pkScript, err := genMultiSigP2WSH(
27592760
channel.BitcoinKey1, channel.BitcoinKey2,
27602761
)

0 commit comments

Comments
 (0)