@@ -3,6 +3,7 @@ package lndclient
3
3
import (
4
4
"context"
5
5
6
+ "github.com/btcsuite/btcd/btcec"
6
7
"github.com/btcsuite/btcd/wire"
7
8
"github.com/lightninglabs/loop/swap"
8
9
"github.com/lightningnetwork/lnd/input"
@@ -26,6 +27,19 @@ type SignerClient interface {
26
27
// encoded.
27
28
VerifyMessage (ctx context.Context , msg , sig []byte , pubkey [33 ]byte ) (
28
29
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 )
29
43
}
30
44
31
45
type signerClient struct {
@@ -152,3 +166,37 @@ func (s *signerClient) VerifyMessage(ctx context.Context, msg, sig []byte,
152
166
}
153
167
return resp .Valid , nil
154
168
}
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