@@ -3,6 +3,7 @@ package lndclient
33import (
44 "context"
55
6+ "github.com/btcsuite/btcd/btcec"
67 "github.com/btcsuite/btcd/wire"
78 "github.com/lightninglabs/loop/swap"
89 "github.com/lightningnetwork/lnd/input"
@@ -26,6 +27,19 @@ type SignerClient interface {
2627 // encoded.
2728 VerifyMessage (ctx context.Context , msg , sig []byte , pubkey [33 ]byte ) (
2829 bool , error )
30+
31+ // DeriveSharedKey returns a shared secret key by performing
32+ // Diffie-Hellman key derivation between the ephemeral public key and
33+ // the key specified by the key locator (or the node's identity private
34+ // key if no key locator is specified):
35+ //
36+ // P_shared = privKeyNode * ephemeralPubkey
37+ //
38+ // The resulting shared public key is serialized in the compressed
39+ // format and hashed with SHA256, resulting in a final key length of 256
40+ // bits.
41+ DeriveSharedKey (ctx context.Context , ephemeralPubKey * btcec.PublicKey ,
42+ keyLocator * keychain.KeyLocator ) ([32 ]byte , error )
2943}
3044
3145type signerClient struct {
@@ -152,3 +166,37 @@ func (s *signerClient) VerifyMessage(ctx context.Context, msg, sig []byte,
152166 }
153167 return resp .Valid , nil
154168}
169+
170+ // DeriveSharedKey returns a shared secret key by performing Diffie-Hellman key
171+ // derivation between the ephemeral public key and the key specified by the key
172+ // locator (or the node's identity private key if no key locator is specified):
173+ //
174+ // P_shared = privKeyNode * ephemeralPubkey
175+ //
176+ // The resulting shared public key is serialized in the compressed format and
177+ // hashed with SHA256, resulting in a final key length of 256 bits.
178+ func (s * signerClient ) DeriveSharedKey (ctx context.Context ,
179+ ephemeralPubKey * btcec.PublicKey ,
180+ keyLocator * keychain.KeyLocator ) ([32 ]byte , error ) {
181+
182+ rpcCtx , cancel := context .WithTimeout (ctx , rpcTimeout )
183+ defer cancel ()
184+
185+ rpcIn := & signrpc.SharedKeyRequest {
186+ EphemeralPubkey : ephemeralPubKey .SerializeCompressed (),
187+ KeyLoc : & signrpc.KeyLocator {
188+ KeyFamily : int32 (keyLocator .Family ),
189+ KeyIndex : int32 (keyLocator .Index ),
190+ },
191+ }
192+
193+ rpcCtx = s .signerMac .WithMacaroonAuth (rpcCtx )
194+ resp , err := s .client .DeriveSharedKey (rpcCtx , rpcIn )
195+ if err != nil {
196+ return [32 ]byte {}, err
197+ }
198+
199+ var sharedKey [32 ]byte
200+ copy (sharedKey [:], resp .SharedKey )
201+ return sharedKey , nil
202+ }
0 commit comments