Skip to content

Commit 1ed9ae9

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 2f69999 commit 1ed9ae9

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
@@ -1985,14 +1985,9 @@ func TestGraphPruning(t *testing.T) {
19851985
t.Fatalf("unable to add node: %v", err)
19861986
}
19871987

1988-
btcKey1 := edgeInfo.BitcoinKey1Bytes.UnwrapOr(route.Vertex{})
1989-
btcKey2 := edgeInfo.BitcoinKey2Bytes.UnwrapOr(route.Vertex{})
1990-
pkScript, err := genMultiSigP2WSH(
1991-
btcKey1[:], btcKey2[:],
1992-
)
1993-
if err != nil {
1994-
t.Fatalf("unable to gen multi-sig p2wsh: %v", err)
1995-
}
1988+
pkScript, err := edgeInfo.FundingPKScript()
1989+
require.NoError(t, err)
1990+
19961991
edgePoints = append(edgePoints, EdgePoint{
19971992
FundingPkScript: pkScript,
19981993
OutPoint: op,

graph/db/kv_store.go

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

4113-
btcKey1 := edgeInfo.BitcoinKey1Bytes.UnwrapOr(
4114-
route.Vertex{},
4115-
)
4116-
btcKey2 := edgeInfo.BitcoinKey2Bytes.UnwrapOr(
4117-
route.Vertex{},
4118-
)
4119-
pkScript, err := genMultiSigP2WSH(
4120-
btcKey1[:], btcKey2[:],
4121-
)
4113+
pkScript, err := edgeInfo.FundingPKScript()
41224114
if err != nil {
41234115
return err
41244116
}

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
)
@@ -228,6 +229,38 @@ func (c *ChannelEdgeInfo) OtherNodeKeyBytes(thisNodeKey []byte) (
228229
}
229230
}
230231

232+
// FundingPKScript returns the funding output's pkScript for the channel.
233+
func (c *ChannelEdgeInfo) FundingPKScript() ([]byte, error) {
234+
switch c.Version {
235+
case lnwire.GossipVersion1:
236+
btc1Key, err := c.BitcoinKey1Bytes.UnwrapOrErr(
237+
fmt.Errorf("missing bitcoin key 1"),
238+
)
239+
if err != nil {
240+
return nil, err
241+
}
242+
btc2Key, err := c.BitcoinKey2Bytes.UnwrapOrErr(
243+
fmt.Errorf("missing bitcoin key 2"),
244+
)
245+
if err != nil {
246+
return nil, err
247+
}
248+
249+
witnessScript, err := input.GenMultiSigScript(
250+
btc1Key[:], btc2Key[:],
251+
)
252+
if err != nil {
253+
return nil, err
254+
}
255+
256+
return input.WitnessScriptHash(witnessScript)
257+
258+
default:
259+
return nil, fmt.Errorf("unsupported channel version: %d",
260+
c.Version)
261+
}
262+
}
263+
231264
// ToChannelAnnouncement converts the ChannelEdgeInfo to a
232265
// lnwire.ChannelAnnouncement1 message. Returns an error if AuthProof is nil
233266
// 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
@@ -2752,6 +2752,7 @@ func (s *SQLStore) ChannelView() ([]EdgePoint, error) {
27522752
handleChannel := func(_ context.Context,
27532753
channel sqlc.ListChannelsPaginatedRow) error {
27542754

2755+
// TODO(elle): update to handle V2 channels.
27552756
pkScript, err := genMultiSigP2WSH(
27562757
channel.BitcoinKey1, channel.BitcoinKey2,
27572758
)

0 commit comments

Comments
 (0)