2017-04-14 21:08:56 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2018-06-05 04:34:16 +03:00
|
|
|
"github.com/btcsuite/btcd/btcec"
|
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
2018-07-31 10:17:17 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet"
|
2017-04-14 21:08:56 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// nodeSigner is an implementation of the MessageSigner interface backed by the
|
|
|
|
// identity private key of running lnd node.
|
|
|
|
type nodeSigner struct {
|
|
|
|
privKey *btcec.PrivateKey
|
|
|
|
}
|
|
|
|
|
|
|
|
// newNodeSigner creates a new instance of the nodeSigner backed by the target
|
|
|
|
// private key.
|
|
|
|
func newNodeSigner(key *btcec.PrivateKey) *nodeSigner {
|
2017-08-05 04:27:13 +03:00
|
|
|
priv := &btcec.PrivateKey{}
|
|
|
|
priv.Curve = btcec.S256()
|
|
|
|
priv.PublicKey.X = key.X
|
|
|
|
priv.PublicKey.Y = key.Y
|
|
|
|
priv.D = key.D
|
2017-04-14 21:08:56 +03:00
|
|
|
return &nodeSigner{
|
2017-08-05 04:27:13 +03:00
|
|
|
privKey: priv,
|
2017-04-14 21:08:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SignMessage signs a double-sha256 digest of the passed msg under the
|
2017-04-20 05:28:10 +03:00
|
|
|
// resident node's private key. If the target public key is _not_ the node's
|
2017-04-14 21:08:56 +03:00
|
|
|
// private key, then an error will be returned.
|
|
|
|
func (n *nodeSigner) SignMessage(pubKey *btcec.PublicKey,
|
|
|
|
msg []byte) (*btcec.Signature, error) {
|
|
|
|
|
|
|
|
// If this isn't our identity public key, then we'll exit early with an
|
|
|
|
// error as we can't sign with this key.
|
|
|
|
if !pubKey.IsEqual(n.privKey.PubKey()) {
|
|
|
|
return nil, fmt.Errorf("unknown public key")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we'll sign the dsha256 of the target message.
|
|
|
|
digest := chainhash.DoubleHashB(msg)
|
|
|
|
sign, err := n.privKey.Sign(digest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't sign the message: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return sign, nil
|
|
|
|
}
|
|
|
|
|
2017-04-29 14:44:29 +03:00
|
|
|
// SignCompact signs a double-sha256 digest of the msg parameter under the
|
|
|
|
// resident node's private key. The returned signature is a pubkey-recoverable
|
|
|
|
// signature.
|
|
|
|
func (n *nodeSigner) SignCompact(msg []byte) ([]byte, error) {
|
2017-09-05 18:56:36 +03:00
|
|
|
// We'll sign the dsha256 of the target message.
|
2017-04-20 05:28:10 +03:00
|
|
|
digest := chainhash.DoubleHashB(msg)
|
2017-04-29 14:44:29 +03:00
|
|
|
|
2017-09-05 18:56:36 +03:00
|
|
|
return n.SignDigestCompact(digest)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SignDigestCompact signs the provided message digest under the resident
|
|
|
|
// node's private key. The returned signature is a pubkey-recoverable signature.
|
|
|
|
func (n *nodeSigner) SignDigestCompact(hash []byte) ([]byte, error) {
|
|
|
|
|
2017-04-20 05:28:10 +03:00
|
|
|
// Should the signature reference a compressed public key or not.
|
2017-05-07 15:02:51 +03:00
|
|
|
isCompressedKey := true
|
2017-04-29 14:44:29 +03:00
|
|
|
|
2017-04-20 05:28:10 +03:00
|
|
|
// btcec.SignCompact returns a pubkey-recoverable signature
|
2018-04-26 05:45:26 +03:00
|
|
|
sig, err := btcec.SignCompact(
|
|
|
|
btcec.S256(), n.privKey, hash, isCompressedKey,
|
|
|
|
)
|
2017-04-20 05:28:10 +03:00
|
|
|
if err != nil {
|
2017-09-05 18:56:36 +03:00
|
|
|
return nil, fmt.Errorf("can't sign the hash: %v", err)
|
2017-04-20 05:28:10 +03:00
|
|
|
}
|
|
|
|
|
2017-04-29 14:44:29 +03:00
|
|
|
return sig, nil
|
2017-04-20 05:28:10 +03:00
|
|
|
}
|
|
|
|
|
2017-04-14 21:08:56 +03:00
|
|
|
// A compile time check to ensure that nodeSigner implements the MessageSigner
|
|
|
|
// interface.
|
|
|
|
var _ lnwallet.MessageSigner = (*nodeSigner)(nil)
|