2014-12-09 18:49:04 -08:00
|
|
|
package account
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
2015-01-18 06:34:01 -08:00
|
|
|
"github.com/tendermint/ed25519"
|
2015-01-14 20:34:53 -08:00
|
|
|
"github.com/tendermint/tendermint/binary"
|
2015-01-16 00:31:34 -08:00
|
|
|
. "github.com/tendermint/tendermint/common"
|
2014-12-09 18:49:04 -08:00
|
|
|
)
|
|
|
|
|
2014-12-22 17:38:16 -08:00
|
|
|
// PubKey is part of Account and Validator.
|
2014-12-09 18:49:04 -08:00
|
|
|
type PubKey interface {
|
2015-01-18 06:34:01 -08:00
|
|
|
TypeByte() byte
|
|
|
|
IsNil() bool
|
2014-12-09 18:49:04 -08:00
|
|
|
Address() []byte
|
|
|
|
VerifyBytes(msg []byte, sig Signature) bool
|
|
|
|
}
|
|
|
|
|
2014-12-22 17:38:16 -08:00
|
|
|
// Types of PubKey implementations
|
2014-12-09 18:49:04 -08:00
|
|
|
const (
|
2014-12-28 00:44:56 -08:00
|
|
|
PubKeyTypeNil = byte(0x00)
|
2015-01-03 20:24:02 -08:00
|
|
|
PubKeyTypeEd25519 = byte(0x01)
|
2014-12-09 18:49:04 -08:00
|
|
|
)
|
|
|
|
|
2014-12-22 17:38:16 -08:00
|
|
|
// for binary.readReflect
|
2015-01-14 20:34:53 -08:00
|
|
|
var _ = binary.RegisterInterface(
|
2015-01-04 17:33:18 -08:00
|
|
|
struct{ PubKey }{},
|
2015-01-14 20:34:53 -08:00
|
|
|
binary.ConcreteType{PubKeyNil{}},
|
|
|
|
binary.ConcreteType{PubKeyEd25519{}},
|
2015-01-04 17:33:18 -08:00
|
|
|
)
|
2014-12-09 18:49:04 -08:00
|
|
|
|
|
|
|
//-------------------------------------
|
|
|
|
|
2014-12-22 17:38:16 -08:00
|
|
|
// Implements PubKey
|
2014-12-28 00:44:56 -08:00
|
|
|
type PubKeyNil struct{}
|
2014-12-16 05:40:17 -08:00
|
|
|
|
2014-12-28 00:44:56 -08:00
|
|
|
func (key PubKeyNil) TypeByte() byte { return PubKeyTypeNil }
|
2014-12-09 18:49:04 -08:00
|
|
|
|
2015-01-18 06:34:01 -08:00
|
|
|
func (key PubKeyNil) IsNil() bool { return true }
|
|
|
|
|
2014-12-28 00:44:56 -08:00
|
|
|
func (key PubKeyNil) Address() []byte {
|
|
|
|
panic("PubKeyNil has no address")
|
2014-12-09 18:49:04 -08:00
|
|
|
}
|
|
|
|
|
2014-12-28 00:44:56 -08:00
|
|
|
func (key PubKeyNil) VerifyBytes(msg []byte, sig_ Signature) bool {
|
|
|
|
panic("PubKeyNil cannot verify messages")
|
2014-12-09 18:49:04 -08:00
|
|
|
}
|
|
|
|
|
2015-01-18 06:34:01 -08:00
|
|
|
func (key PubKeyEd25519) ValidateBasic() error {
|
|
|
|
if len(key) != ed25519.PublicKeySize {
|
|
|
|
return errors.New("Invalid PubKeyEd25519 key size")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-16 00:31:34 -08:00
|
|
|
func (key PubKeyNil) String() string {
|
|
|
|
return "PubKeyNil{}"
|
|
|
|
}
|
|
|
|
|
2014-12-09 18:49:04 -08:00
|
|
|
//-------------------------------------
|
|
|
|
|
2014-12-22 17:38:16 -08:00
|
|
|
// Implements PubKey
|
2015-01-03 20:24:02 -08:00
|
|
|
type PubKeyEd25519 []byte
|
2014-12-09 18:49:04 -08:00
|
|
|
|
2015-01-18 06:34:01 -08:00
|
|
|
func (pubKey PubKeyEd25519) TypeByte() byte { return PubKeyTypeEd25519 }
|
2014-12-09 18:49:04 -08:00
|
|
|
|
2015-01-18 06:34:01 -08:00
|
|
|
func (pubKey PubKeyEd25519) IsNil() bool { return false }
|
2014-12-09 18:49:04 -08:00
|
|
|
|
2015-01-18 06:34:01 -08:00
|
|
|
// TODO: Or should this just be BinaryRipemd160(key)? (The difference is the TypeByte.)
|
|
|
|
func (pubKey PubKeyEd25519) Address() []byte { return binary.BinaryRipemd160(pubKey) }
|
2014-12-09 18:49:04 -08:00
|
|
|
|
2015-01-18 06:34:01 -08:00
|
|
|
func (pubKey PubKeyEd25519) VerifyBytes(msg []byte, sig_ Signature) bool {
|
2014-12-09 18:49:04 -08:00
|
|
|
sig, ok := sig_.(SignatureEd25519)
|
|
|
|
if !ok {
|
|
|
|
panic("PubKeyEd25519 expects an SignatureEd25519 signature")
|
|
|
|
}
|
2015-01-18 06:34:01 -08:00
|
|
|
pubKeyBytes := new([32]byte)
|
|
|
|
copy(pubKeyBytes[:], pubKey)
|
|
|
|
sigBytes := new([64]byte)
|
|
|
|
copy(sigBytes[:], sig)
|
|
|
|
return ed25519.Verify(pubKeyBytes, msg, sigBytes)
|
2014-12-09 18:49:04 -08:00
|
|
|
}
|
2015-01-16 00:31:34 -08:00
|
|
|
|
2015-01-18 06:34:01 -08:00
|
|
|
func (pubKey PubKeyEd25519) String() string {
|
2015-01-20 15:45:20 -08:00
|
|
|
return Fmt("PubKeyEd25519{%X}", []byte(pubKey))
|
2015-01-16 00:31:34 -08:00
|
|
|
}
|