tendermint/pub_key.go

147 lines
3.5 KiB
Go
Raw Normal View History

2015-10-25 13:45:13 -07:00
package crypto
2015-10-25 13:42:49 -07:00
import (
"bytes"
"crypto/sha256"
2017-12-27 14:37:37 -08:00
"fmt"
2015-10-25 13:42:49 -07:00
2016-04-19 01:02:31 -07:00
secp256k1 "github.com/btcsuite/btcd/btcec"
2015-10-25 13:42:49 -07:00
"github.com/tendermint/ed25519"
"github.com/tendermint/ed25519/extra25519"
2017-12-27 14:37:37 -08:00
cmn "github.com/tendermint/tmlibs/common"
2015-10-25 13:45:13 -07:00
"golang.org/x/crypto/ripemd160"
2015-10-25 13:42:49 -07:00
)
2017-12-27 14:37:37 -08:00
// An address is a []byte, but hex-encoded even in JSON.
// []byte leaves us the option to change the address length.
2018-01-06 13:05:50 -08:00
// Use an alias so Unmarshal methods (with ptr receivers) are available too.
type Address = cmn.HexBytes
2017-12-27 14:37:37 -08:00
2017-04-09 00:32:54 -07:00
func PubKeyFromBytes(pubKeyBytes []byte) (pubKey PubKey, err error) {
err = cdc.UnmarshalBinary(pubKeyBytes, &pubKey)
return
2017-04-09 00:32:54 -07:00
}
2017-03-29 06:17:06 -07:00
2017-04-09 00:32:54 -07:00
//----------------------------------------
2017-03-29 06:17:06 -07:00
type PubKey interface {
2017-12-27 14:37:37 -08:00
Address() Address
2016-03-15 11:11:54 -07:00
Bytes() []byte
2015-10-25 13:42:49 -07:00
VerifyBytes(msg []byte, sig Signature) bool
2016-03-13 09:40:15 -07:00
Equals(PubKey) bool
2015-10-25 13:42:49 -07:00
}
//-------------------------------------
var _ PubKey = PubKeyEd25519{}
// Implements PubKeyInner
2015-10-25 13:42:49 -07:00
type PubKeyEd25519 [32]byte
2017-12-27 14:37:37 -08:00
func (pubKey PubKeyEd25519) Address() Address {
2016-03-15 15:58:43 -07:00
// append type byte
2015-10-25 13:42:49 -07:00
hasher := ripemd160.New()
hasher.Write(pubKey.Bytes()) // does not error
2017-12-27 14:37:37 -08:00
return Address(hasher.Sum(nil))
2015-10-25 13:42:49 -07:00
}
2016-03-15 11:11:54 -07:00
func (pubKey PubKeyEd25519) Bytes() []byte {
bz, err := cdc.MarshalBinary(pubKey)
if err != nil {
panic(err)
}
return bz
2016-03-15 11:11:54 -07:00
}
2015-10-25 13:42:49 -07:00
func (pubKey PubKeyEd25519) VerifyBytes(msg []byte, sig_ Signature) bool {
2017-02-22 14:43:26 -08:00
// make sure we use the same algorithm to sign
sig, ok := sig_.(SignatureEd25519)
2015-10-25 13:42:49 -07:00
if !ok {
return false
}
pubKeyBytes := [32]byte(pubKey)
sigBytes := [64]byte(sig)
return ed25519.Verify(&pubKeyBytes, msg, &sigBytes)
}
// For use with golang/crypto/nacl/box
// If error, returns nil.
func (pubKey PubKeyEd25519) ToCurve25519() *[32]byte {
keyCurve25519, pubKeyBytes := new([32]byte), [32]byte(pubKey)
ok := extra25519.PublicKeyToCurve25519(keyCurve25519, &pubKeyBytes)
if !ok {
return nil
}
return keyCurve25519
}
func (pubKey PubKeyEd25519) String() string {
2017-12-27 14:37:37 -08:00
return fmt.Sprintf("PubKeyEd25519{%X}", pubKey[:])
2015-10-25 13:42:49 -07:00
}
func (pubKey PubKeyEd25519) Equals(other PubKey) bool {
if otherEd, ok := other.(PubKeyEd25519); ok {
2015-10-25 13:42:49 -07:00
return bytes.Equal(pubKey[:], otherEd[:])
} else {
return false
}
}
2016-04-19 01:02:31 -07:00
//-------------------------------------
var _ PubKey = PubKeySecp256k1{}
// Implements PubKey.
// Compressed pubkey (just the x-cord),
// prefixed with 0x02 or 0x03, depending on the y-cord.
type PubKeySecp256k1 [33]byte
2016-04-19 01:02:31 -07:00
// Implements Bitcoin style addresses: RIPEMD160(SHA256(pubkey))
2017-12-27 14:37:37 -08:00
func (pubKey PubKeySecp256k1) Address() Address {
hasherSHA256 := sha256.New()
hasherSHA256.Write(pubKey[:]) // does not error
sha := hasherSHA256.Sum(nil)
hasherRIPEMD160 := ripemd160.New()
hasherRIPEMD160.Write(sha) // does not error
2017-12-27 14:37:37 -08:00
return Address(hasherRIPEMD160.Sum(nil))
2016-04-19 01:02:31 -07:00
}
func (pubKey PubKeySecp256k1) Bytes() []byte {
bz, err := cdc.MarshalBinary(pubKey)
if err != nil {
panic(err)
}
return bz
2016-04-19 01:02:31 -07:00
}
func (pubKey PubKeySecp256k1) VerifyBytes(msg []byte, sig_ Signature) bool {
2017-02-22 14:43:26 -08:00
// and assert same algorithm to sign and verify
sig, ok := sig_.(SignatureSecp256k1)
2016-04-19 01:02:31 -07:00
if !ok {
return false
}
2017-02-22 14:43:26 -08:00
pub__, err := secp256k1.ParsePubKey(pubKey[:], secp256k1.S256())
2017-02-22 14:43:26 -08:00
if err != nil {
return false
}
2016-04-19 01:02:31 -07:00
sig__, err := secp256k1.ParseDERSignature(sig[:], secp256k1.S256())
if err != nil {
return false
}
return sig__.Verify(Sha256(msg), pub__)
}
func (pubKey PubKeySecp256k1) String() string {
2017-12-27 14:37:37 -08:00
return fmt.Sprintf("PubKeySecp256k1{%X}", pubKey[:])
2016-04-19 01:02:31 -07:00
}
func (pubKey PubKeySecp256k1) Equals(other PubKey) bool {
if otherSecp, ok := other.(PubKeySecp256k1); ok {
2016-04-19 01:26:40 -07:00
return bytes.Equal(pubKey[:], otherSecp[:])
2016-04-19 01:02:31 -07:00
} else {
return false
}
}