|
|
@ -7,6 +7,7 @@ import ( |
|
|
|
"github.com/tendermint/ed25519" |
|
|
|
"github.com/tendermint/ed25519/extra25519" |
|
|
|
. "github.com/tendermint/go-common" |
|
|
|
data "github.com/tendermint/go-data" |
|
|
|
"github.com/tendermint/go-wire" |
|
|
|
"golang.org/x/crypto/ripemd160" |
|
|
|
) |
|
|
@ -24,14 +25,35 @@ type PubKey interface { |
|
|
|
const ( |
|
|
|
PubKeyTypeEd25519 = byte(0x01) |
|
|
|
PubKeyTypeSecp256k1 = byte(0x02) |
|
|
|
PubKeyNameEd25519 = "ed25519" |
|
|
|
PubKeyNameSecp256k1 = "secp256k1" |
|
|
|
) |
|
|
|
|
|
|
|
// for wire.readReflect
|
|
|
|
var _ = wire.RegisterInterface( |
|
|
|
struct{ PubKey }{}, |
|
|
|
wire.ConcreteType{PubKeyEd25519{}, PubKeyTypeEd25519}, |
|
|
|
wire.ConcreteType{PubKeySecp256k1{}, PubKeyTypeSecp256k1}, |
|
|
|
) |
|
|
|
var pubKeyMapper data.Mapper |
|
|
|
|
|
|
|
// register both public key types with go-data (and thus go-wire)
|
|
|
|
func init() { |
|
|
|
pubKeyMapper = data.NewMapper(PubKeyS{}). |
|
|
|
RegisterInterface(PubKeyEd25519{}, PubKeyNameEd25519, PubKeyTypeEd25519). |
|
|
|
RegisterInterface(PubKeySecp256k1{}, PubKeyNameSecp256k1, PubKeyTypeSecp256k1) |
|
|
|
} |
|
|
|
|
|
|
|
// PubKeyS add json serialization to PubKey
|
|
|
|
type PubKeyS struct { |
|
|
|
PubKey |
|
|
|
} |
|
|
|
|
|
|
|
func (p PubKeyS) MarshalJSON() ([]byte, error) { |
|
|
|
return pubKeyMapper.ToJSON(p.PubKey) |
|
|
|
} |
|
|
|
|
|
|
|
func (p *PubKeyS) UnmarshalJSON(data []byte) (err error) { |
|
|
|
parsed, err := pubKeyMapper.FromJSON(data) |
|
|
|
if err == nil { |
|
|
|
p.PubKey = parsed.(PubKey) |
|
|
|
} |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
func PubKeyFromBytes(pubKeyBytes []byte) (pubKey PubKey, err error) { |
|
|
|
err = wire.ReadBinaryBytes(pubKeyBytes, &pubKey) |
|
|
@ -70,6 +92,17 @@ func (pubKey PubKeyEd25519) VerifyBytes(msg []byte, sig_ Signature) bool { |
|
|
|
return ed25519.Verify(&pubKeyBytes, msg, &sigBytes) |
|
|
|
} |
|
|
|
|
|
|
|
func (p PubKeyEd25519) MarshalJSON() ([]byte, error) { |
|
|
|
return data.Encoder.Marshal(p[:]) |
|
|
|
} |
|
|
|
|
|
|
|
func (p *PubKeyEd25519) UnmarshalJSON(enc []byte) error { |
|
|
|
var ref []byte |
|
|
|
err := data.Encoder.Unmarshal(&ref, enc) |
|
|
|
copy(p[:], ref) |
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
// For use with golang/crypto/nacl/box
|
|
|
|
// If error, returns nil.
|
|
|
|
func (pubKey PubKeyEd25519) ToCurve25519() *[32]byte { |
|
|
@ -137,6 +170,17 @@ func (pubKey PubKeySecp256k1) VerifyBytes(msg []byte, sig_ Signature) bool { |
|
|
|
return sig__.Verify(Sha256(msg), pub__) |
|
|
|
} |
|
|
|
|
|
|
|
func (p PubKeySecp256k1) MarshalJSON() ([]byte, error) { |
|
|
|
return data.Encoder.Marshal(p[:]) |
|
|
|
} |
|
|
|
|
|
|
|
func (p *PubKeySecp256k1) UnmarshalJSON(enc []byte) error { |
|
|
|
var ref []byte |
|
|
|
err := data.Encoder.Unmarshal(&ref, enc) |
|
|
|
copy(p[:], ref) |
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
func (pubKey PubKeySecp256k1) String() string { |
|
|
|
return Fmt("PubKeySecp256k1{%X}", pubKey[:]) |
|
|
|
} |
|
|
|