Skip to content

Commit 006e4c5

Browse files
committed
lnd/signer: add ECDH method
It can now be used as keychain.ECDHRing.
1 parent 7bc8765 commit 006e4c5

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
@@ -265,3 +265,26 @@ func ECDH(privKey *btcec.PrivateKey, pub *btcec.PublicKey) ([32]byte, error) {
265265
sPubKey := btcec.NewPublicKey(&s.X, &s.Y)
266266
return sha256.Sum256(sPubKey.SerializeCompressed()), nil
267267
}
268+
269+
// ECDH performs a scalar multiplication (ECDH-like operation) between
270+
// the target key descriptor and remote public key. The output
271+
// returned will be the sha256 of the resulting shared point serialized
272+
// in compressed format. If k is our private key, and P is the public
273+
// key, we perform the following operation:
274+
//
275+
// sx := k*P
276+
// s := sha256(sx.SerializeCompressed())
277+
//
278+
// NOTE: This is part of the keychain.ECDHRing interface.
279+
func (s *Signer) ECDH(keyDesc keychain.KeyDescriptor, pubKey *btcec.PublicKey) (
280+
[32]byte, error) {
281+
282+
// First, derive the private key.
283+
privKey, err := s.FetchPrivateKey(&keyDesc)
284+
if err != nil {
285+
return [32]byte{}, fmt.Errorf("failed to derive the private "+
286+
"key: %w", err)
287+
}
288+
289+
return ECDH(privKey, pubKey)
290+
}

0 commit comments

Comments
 (0)