Skip to content

Commit 26a20dd

Browse files
committed
cln: add CLN node key derivation
1 parent 0e91d98 commit 26a20dd

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

cln/derivation.go

+13
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,24 @@ import (
99
)
1010

1111
var (
12+
InfoNodeID = []byte("nodeid")
1213
InfoPeerSeed = []byte("peer seed")
1314
InfoPerPeer = []byte("per-peer seed")
1415
InfoCLightning = []byte("c-lightning")
1516
)
1617

18+
// NodeKey derives a CLN node key from the given HSM secret.
19+
func NodeKey(hsmSecret [32]byte) (*btcec.PublicKey, error) {
20+
salt := make([]byte, 4)
21+
privKeyBytes, err := HkdfSha256(hsmSecret[:], salt, InfoNodeID)
22+
if err != nil {
23+
return nil, err
24+
}
25+
26+
_, pubKey := btcec.PrivKeyFromBytes(privKeyBytes[:])
27+
return pubKey, nil
28+
}
29+
1730
// FundingKey derives a CLN channel funding key for the given peer and channel
1831
// number (incrementing database index).
1932
func FundingKey(hsmSecret [32]byte, peerPubKey *btcec.PublicKey,

cln/derivation_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ var (
1515
0xbf, 0x72, 0xbe, 0xb4, 0x30, 0xe5, 0x9e, 0x71,
1616
0xb5, 0xac, 0x5a, 0x73, 0x58, 0x1a, 0x62, 0x70,
1717
}
18+
nodeKeyBytes, _ = hex.DecodeString(
19+
"035149629152c1bee83f1e148a51400b5f24bf3e2ca53384dd801418446e" +
20+
"1f53fe",
21+
)
22+
1823
peerPubKeyBytes, _ = hex.DecodeString(
1924
"02678187ca43e6a6f62f9185be98a933bf485313061e6a05578bbd83c54e" +
2025
"88d460",
@@ -27,6 +32,13 @@ var (
2732
)
2833
)
2934

35+
func TestNodeKey(t *testing.T) {
36+
nodeKey, err := NodeKey(hsmSecret)
37+
require.NoError(t, err)
38+
39+
require.Equal(t, nodeKeyBytes, nodeKey.SerializeCompressed())
40+
}
41+
3042
func TestFundingKey(t *testing.T) {
3143
fundingKey, err := FundingKey(hsmSecret, peerPubKey, 1)
3244
require.NoError(t, err)

0 commit comments

Comments
 (0)