Skip to content

Commit 70b0fa2

Browse files
committed
lnd/signer: add ECDH method
It can now be used as keychain.ECDHRing.
1 parent f1fc4c1 commit 70b0fa2

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

lnd/signer.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,3 +308,26 @@ func ECDH(privKey *btcec.PrivateKey, pub *btcec.PublicKey) ([32]byte, error) {
308308
sPubKey := btcec.NewPublicKey(&s.X, &s.Y)
309309
return sha256.Sum256(sPubKey.SerializeCompressed()), nil
310310
}
311+
312+
// ECDH performs a scalar multiplication (ECDH-like operation) between
313+
// the target key descriptor and remote public key. The output
314+
// returned will be the sha256 of the resulting shared point serialized
315+
// in compressed format. If k is our private key, and P is the public
316+
// key, we perform the following operation:
317+
//
318+
// sx := k*P
319+
// s := sha256(sx.SerializeCompressed())
320+
//
321+
// NOTE: This is part of the keychain.ECDHRing interface.
322+
func (s *Signer) ECDH(keyDesc keychain.KeyDescriptor, pubKey *btcec.PublicKey) (
323+
[32]byte, error) {
324+
325+
// First, derive the private key.
326+
privKey, err := s.FetchPrivateKey(&keyDesc)
327+
if err != nil {
328+
return [32]byte{}, fmt.Errorf("failed to derive the private "+
329+
"key: %w", err)
330+
}
331+
332+
return ECDH(privKey, pubKey)
333+
}

0 commit comments

Comments
 (0)