tendermint/account/pub_key.go

91 lines
2.5 KiB
Go
Raw Normal View History

package account
import (
"bytes"
2015-06-09 23:17:19 -04:00
"github.com/tendermint/tendermint/Godeps/_workspace/src/github.com/tendermint/ed25519"
2015-07-14 17:52:54 -07:00
"github.com/tendermint/tendermint/Godeps/_workspace/src/github.com/tendermint/ed25519/extra25519"
2015-07-19 14:40:53 -04:00
"github.com/tendermint/tendermint/Godeps/_workspace/src/golang.org/x/crypto/ripemd160"
2015-07-25 15:45:45 -07:00
"github.com/tendermint/tendermint/wire"
2015-04-01 17:30:16 -07:00
. "github.com/tendermint/tendermint/common"
)
// PubKey is part of Account and Validator.
type PubKey interface {
Address() []byte
VerifyBytes(msg []byte, sig Signature) bool
}
// Types of PubKey implementations
const (
PubKeyTypeEd25519 = byte(0x01)
)
2015-07-25 15:45:45 -07:00
// for wire.readReflect
var _ = wire.RegisterInterface(
struct{ PubKey }{},
2015-07-25 15:45:45 -07:00
wire.ConcreteType{PubKeyEd25519{}, PubKeyTypeEd25519},
)
//-------------------------------------
// Implements PubKey
2015-07-17 17:19:16 -04:00
type PubKeyEd25519 [32]byte
2015-07-19 14:40:53 -04:00
// TODO: Slicing the array gives us length prefixing but loses the type byte.
// Revisit if we add more pubkey types.
// For now, we artificially append the type byte in front to give us backwards
// compatibility for when the pubkey wasn't fixed length array
func (pubKey PubKeyEd25519) Address() []byte {
w, n, err := new(bytes.Buffer), new(int64), new(error)
2015-07-25 15:45:45 -07:00
wire.WriteBinary(pubKey[:], w, n, err)
2015-07-19 14:40:53 -04:00
if *err != nil {
2015-07-19 23:42:52 +00:00
PanicCrisis(*err)
2015-07-19 14:40:53 -04:00
}
// append type byte
encodedPubkey := append([]byte{1}, w.Bytes()...)
hasher := ripemd160.New()
hasher.Write(encodedPubkey) // does not error
return hasher.Sum(nil)
}
2015-06-17 00:16:58 -04:00
// TODO: Consider returning a reason for failure, or logging a runtime type mismatch.
func (pubKey PubKeyEd25519) VerifyBytes(msg []byte, sig_ Signature) bool {
sig, ok := sig_.(SignatureEd25519)
if !ok {
2015-06-17 00:16:58 -04:00
return false
}
2015-07-17 17:19:16 -04:00
pubKeyBytes := [32]byte(pubKey)
sigBytes := [64]byte(sig)
return ed25519.Verify(&pubKeyBytes, msg, &sigBytes)
}
2015-01-16 00:31:34 -08:00
2015-07-14 17:52:54 -07:00
// For use with golang/crypto/nacl/box
// If error, returns nil.
func (pubKey PubKeyEd25519) ToCurve25519() *[32]byte {
2015-07-17 17:19:16 -04:00
keyCurve25519, pubKeyBytes := new([32]byte), [32]byte(pubKey)
ok := extra25519.PublicKeyToCurve25519(keyCurve25519, &pubKeyBytes)
2015-07-14 17:52:54 -07:00
if !ok {
return nil
}
return keyCurve25519
}
func (pubKey PubKeyEd25519) String() string {
2015-07-17 17:19:16 -04:00
return Fmt("PubKeyEd25519{%X}", pubKey[:])
2015-01-16 00:31:34 -08:00
}
2015-07-15 14:31:03 -07:00
// Must return the full bytes in hex.
// Used for map keying, etc.
func (pubKey PubKeyEd25519) KeyString() string {
2015-07-17 17:19:16 -04:00
return Fmt("%X", pubKey[:])
2015-07-15 14:31:03 -07:00
}
func (pubKey PubKeyEd25519) Equals(other PubKey) bool {
2015-07-17 17:19:16 -04:00
if otherEd, ok := other.(PubKeyEd25519); ok {
return bytes.Equal(pubKey[:], otherEd[:])
} else {
return false
}
}