2020-04-28 10:06:20 +02:00
|
|
|
package keychain
|
|
|
|
|
2021-09-23 16:54:26 +02:00
|
|
|
import (
|
2022-02-23 14:48:00 +01:00
|
|
|
"github.com/btcsuite/btcd/btcec/v2"
|
|
|
|
"github.com/btcsuite/btcd/btcec/v2/ecdsa"
|
2021-09-23 16:54:26 +02:00
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
|
|
)
|
2020-04-28 10:06:20 +02:00
|
|
|
|
2021-09-23 16:54:29 +02:00
|
|
|
func NewPubKeyMessageSigner(pubKey *btcec.PublicKey, keyLoc KeyLocator,
|
2021-09-23 16:54:27 +02:00
|
|
|
signer MessageSignerRing) *PubKeyMessageSigner {
|
2020-04-28 10:06:20 +02:00
|
|
|
|
2021-09-23 16:54:27 +02:00
|
|
|
return &PubKeyMessageSigner{
|
2021-09-23 16:54:29 +02:00
|
|
|
pubKey: pubKey,
|
|
|
|
keyLoc: keyLoc,
|
2020-04-28 10:06:20 +02:00
|
|
|
digestSigner: signer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-23 16:54:27 +02:00
|
|
|
type PubKeyMessageSigner struct {
|
2021-09-23 16:54:29 +02:00
|
|
|
pubKey *btcec.PublicKey
|
|
|
|
keyLoc KeyLocator
|
2021-09-23 16:54:27 +02:00
|
|
|
digestSigner MessageSignerRing
|
2020-04-28 10:06:20 +02:00
|
|
|
}
|
|
|
|
|
2021-09-23 16:54:27 +02:00
|
|
|
func (p *PubKeyMessageSigner) PubKey() *btcec.PublicKey {
|
2021-09-23 16:54:29 +02:00
|
|
|
return p.pubKey
|
2020-04-28 10:06:20 +02:00
|
|
|
}
|
|
|
|
|
2021-09-23 16:54:30 +02:00
|
|
|
func (p *PubKeyMessageSigner) KeyLocator() KeyLocator {
|
|
|
|
return p.keyLoc
|
|
|
|
}
|
|
|
|
|
2021-09-23 16:54:27 +02:00
|
|
|
func (p *PubKeyMessageSigner) SignMessage(message []byte,
|
2022-02-23 14:48:00 +01:00
|
|
|
doubleHash bool) (*ecdsa.Signature, error) {
|
2020-04-28 10:06:20 +02:00
|
|
|
|
2021-09-23 16:54:29 +02:00
|
|
|
return p.digestSigner.SignMessage(p.keyLoc, message, doubleHash)
|
2020-04-28 10:06:20 +02:00
|
|
|
}
|
|
|
|
|
2021-09-23 16:54:27 +02:00
|
|
|
func (p *PubKeyMessageSigner) SignMessageCompact(msg []byte,
|
|
|
|
doubleHash bool) ([]byte, error) {
|
2020-04-28 10:06:20 +02:00
|
|
|
|
2021-09-23 16:54:29 +02:00
|
|
|
return p.digestSigner.SignMessageCompact(p.keyLoc, msg, doubleHash)
|
2020-04-28 10:06:20 +02:00
|
|
|
}
|
|
|
|
|
2021-09-23 16:54:30 +02:00
|
|
|
func NewPrivKeyMessageSigner(privKey *btcec.PrivateKey,
|
|
|
|
keyLoc KeyLocator) *PrivKeyMessageSigner {
|
|
|
|
|
|
|
|
return &PrivKeyMessageSigner{
|
|
|
|
privKey: privKey,
|
|
|
|
keyLoc: keyLoc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-23 16:54:27 +02:00
|
|
|
type PrivKeyMessageSigner struct {
|
2021-09-23 16:54:30 +02:00
|
|
|
keyLoc KeyLocator
|
|
|
|
privKey *btcec.PrivateKey
|
2020-04-28 10:06:20 +02:00
|
|
|
}
|
|
|
|
|
2021-09-23 16:54:27 +02:00
|
|
|
func (p *PrivKeyMessageSigner) PubKey() *btcec.PublicKey {
|
2021-09-23 16:54:30 +02:00
|
|
|
return p.privKey.PubKey()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PrivKeyMessageSigner) KeyLocator() KeyLocator {
|
|
|
|
return p.keyLoc
|
2020-04-28 10:06:20 +02:00
|
|
|
}
|
|
|
|
|
2021-09-23 16:54:27 +02:00
|
|
|
func (p *PrivKeyMessageSigner) SignMessage(msg []byte,
|
2022-02-23 14:48:00 +01:00
|
|
|
doubleHash bool) (*ecdsa.Signature, error) {
|
2020-04-28 10:06:20 +02:00
|
|
|
|
2021-09-23 16:54:26 +02:00
|
|
|
var digest []byte
|
|
|
|
if doubleHash {
|
|
|
|
digest = chainhash.DoubleHashB(msg)
|
|
|
|
} else {
|
|
|
|
digest = chainhash.HashB(msg)
|
|
|
|
}
|
2022-02-23 14:48:00 +01:00
|
|
|
return ecdsa.Sign(p.privKey, digest), nil
|
2020-04-28 10:06:20 +02:00
|
|
|
}
|
|
|
|
|
2021-09-23 16:54:27 +02:00
|
|
|
func (p *PrivKeyMessageSigner) SignMessageCompact(msg []byte,
|
|
|
|
doubleHash bool) ([]byte, error) {
|
2020-04-28 10:06:20 +02:00
|
|
|
|
2021-09-23 16:54:27 +02:00
|
|
|
var digest []byte
|
|
|
|
if doubleHash {
|
|
|
|
digest = chainhash.DoubleHashB(msg)
|
|
|
|
} else {
|
|
|
|
digest = chainhash.HashB(msg)
|
|
|
|
}
|
2022-02-23 14:48:00 +01:00
|
|
|
return ecdsa.SignCompact(p.privKey, digest, true)
|
2020-04-28 10:06:20 +02:00
|
|
|
}
|
|
|
|
|
2021-09-23 16:54:27 +02:00
|
|
|
var _ SingleKeyMessageSigner = (*PubKeyMessageSigner)(nil)
|
|
|
|
var _ SingleKeyMessageSigner = (*PrivKeyMessageSigner)(nil)
|