Skip to content

Commit

Permalink
lnd/signer: add ECDH method
Browse files Browse the repository at this point in the history
It can now be used as keychain.ECDHRing.
  • Loading branch information
starius committed Jul 7, 2024
1 parent 139f73a commit d872f10
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lnd/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,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)
}

0 comments on commit d872f10

Please sign in to comment.