lnd/signer: add ECDH method

It can now be used as keychain.ECDHRing.
This commit is contained in:
Boris Nagaev 2024-07-07 01:03:15 -03:00
parent 4a31d4c208
commit 22cff1a33b
No known key found for this signature in database

View file

@ -308,3 +308,26 @@ func ECDH(privKey *btcec.PrivateKey, pub *btcec.PublicKey) ([32]byte, error) {
sPubKey := btcec.NewPublicKey(&s.X, &s.Y)
return sha256.Sum256(sPubKey.SerializeCompressed()), nil
}
// ECDH performs a scalar multiplication (ECDH-like operation) between
// the target key descriptor and remote public key. The output
// returned will be the sha256 of the resulting shared point serialized
// in compressed format. If k is our private key, and P is the public
// key, we perform the following operation:
//
// sx := k*P
// s := sha256(sx.SerializeCompressed())
//
// NOTE: This is part of the keychain.ECDHRing interface.
func (s *Signer) ECDH(keyDesc keychain.KeyDescriptor, pubKey *btcec.PublicKey) (
[32]byte, error) {
// First, derive the private key.
privKey, err := s.FetchPrivateKey(&keyDesc)
if err != nil {
return [32]byte{}, fmt.Errorf("failed to derive the private "+
"key: %w", err)
}
return ECDH(privKey, pubKey)
}