mirror of
https://github.com/fluencelabs/tendermint
synced 2025-06-24 10:11:48 +00:00
Use new go-wire; PubKey etc are interfaces; Keybase refactor
This commit is contained in:
88
priv_key.go
88
priv_key.go
@ -7,89 +7,60 @@ import (
|
||||
"github.com/tendermint/ed25519"
|
||||
"github.com/tendermint/ed25519/extra25519"
|
||||
"github.com/tendermint/go-wire"
|
||||
data "github.com/tendermint/go-wire/data"
|
||||
. "github.com/tendermint/tmlibs/common"
|
||||
)
|
||||
|
||||
func PrivKeyFromBytes(privKeyBytes []byte) (privKey PrivKey, err error) {
|
||||
err = wire.ReadBinaryBytes(privKeyBytes, &privKey)
|
||||
if err == nil {
|
||||
// add support for a ValidateKey method on PrivKeys
|
||||
// to make sure they load correctly
|
||||
val, ok := privKey.Unwrap().(validatable)
|
||||
if ok {
|
||||
err = val.ValidateKey()
|
||||
}
|
||||
}
|
||||
err = cdc.UnmarshalBinary(privKeyBytes, &privKey)
|
||||
return
|
||||
}
|
||||
|
||||
// validatable is an optional interface for keys that want to
|
||||
// check integrity
|
||||
type validatable interface {
|
||||
ValidateKey() error
|
||||
}
|
||||
|
||||
//----------------------------------------
|
||||
|
||||
// DO NOT USE THIS INTERFACE.
|
||||
// You probably want to use PrivKey
|
||||
// +gen wrapper:"PrivKey,Impl[PrivKeyEd25519,PrivKeySecp256k1],ed25519,secp256k1"
|
||||
type PrivKeyInner interface {
|
||||
AssertIsPrivKeyInner()
|
||||
type PrivKey interface {
|
||||
Bytes() []byte
|
||||
Sign(msg []byte) Signature
|
||||
PubKey() PubKey
|
||||
Equals(PrivKey) bool
|
||||
Wrap() PrivKey
|
||||
}
|
||||
|
||||
//-------------------------------------
|
||||
|
||||
var _ PrivKeyInner = PrivKeyEd25519{}
|
||||
var _ PrivKey = PrivKeyEd25519{}
|
||||
|
||||
// Implements PrivKey
|
||||
type PrivKeyEd25519 [64]byte
|
||||
|
||||
func (privKey PrivKeyEd25519) AssertIsPrivKeyInner() {}
|
||||
|
||||
func (privKey PrivKeyEd25519) Bytes() []byte {
|
||||
return wire.BinaryBytes(PrivKey{privKey})
|
||||
bz, err := cdc.MarshalBinary(privKey)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return bz
|
||||
}
|
||||
|
||||
func (privKey PrivKeyEd25519) Sign(msg []byte) Signature {
|
||||
privKeyBytes := [64]byte(privKey)
|
||||
signatureBytes := ed25519.Sign(&privKeyBytes, msg)
|
||||
return SignatureEd25519(*signatureBytes).Wrap()
|
||||
return SignatureEd25519(*signatureBytes)
|
||||
}
|
||||
|
||||
func (privKey PrivKeyEd25519) PubKey() PubKey {
|
||||
privKeyBytes := [64]byte(privKey)
|
||||
pubBytes := *ed25519.MakePublicKey(&privKeyBytes)
|
||||
return PubKeyEd25519(pubBytes).Wrap()
|
||||
return PubKeyEd25519(pubBytes)
|
||||
}
|
||||
|
||||
// Equals - you probably don't need to use this.
|
||||
// Runs in constant time based on length of the keys.
|
||||
func (privKey PrivKeyEd25519) Equals(other PrivKey) bool {
|
||||
if otherEd, ok := other.Unwrap().(PrivKeyEd25519); ok {
|
||||
if otherEd, ok := other.(PrivKeyEd25519); ok {
|
||||
return subtle.ConstantTimeCompare(privKey[:], otherEd[:]) == 1
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func (p PrivKeyEd25519) MarshalJSON() ([]byte, error) {
|
||||
return data.Encoder.Marshal(p[:])
|
||||
}
|
||||
|
||||
func (p *PrivKeyEd25519) UnmarshalJSON(enc []byte) error {
|
||||
var ref []byte
|
||||
err := data.Encoder.Unmarshal(&ref, enc)
|
||||
copy(p[:], ref)
|
||||
return err
|
||||
}
|
||||
|
||||
func (privKey PrivKeyEd25519) ToCurve25519() *[32]byte {
|
||||
keyCurve25519 := new([32]byte)
|
||||
privKeyBytes := [64]byte(privKey)
|
||||
@ -97,16 +68,22 @@ func (privKey PrivKeyEd25519) ToCurve25519() *[32]byte {
|
||||
return keyCurve25519
|
||||
}
|
||||
|
||||
/*
|
||||
func (privKey PrivKeyEd25519) String() string {
|
||||
return Fmt("PrivKeyEd25519{*****}")
|
||||
}
|
||||
*/
|
||||
|
||||
// Deterministically generates new priv-key bytes from key.
|
||||
func (privKey PrivKeyEd25519) Generate(index int) PrivKeyEd25519 {
|
||||
newBytes := wire.BinarySha256(struct {
|
||||
bz, err := wire.MarshalBinary(struct {
|
||||
PrivKey [64]byte
|
||||
Index int
|
||||
}{privKey, index})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
newBytes := Sha256(bz)
|
||||
var newKey [64]byte
|
||||
copy(newKey[:], newBytes)
|
||||
return PrivKeyEd25519(newKey)
|
||||
@ -131,15 +108,17 @@ func GenPrivKeyEd25519FromSecret(secret []byte) PrivKeyEd25519 {
|
||||
|
||||
//-------------------------------------
|
||||
|
||||
var _ PrivKeyInner = PrivKeySecp256k1{}
|
||||
var _ PrivKey = PrivKeySecp256k1{}
|
||||
|
||||
// Implements PrivKey
|
||||
type PrivKeySecp256k1 [32]byte
|
||||
|
||||
func (privKey PrivKeySecp256k1) AssertIsPrivKeyInner() {}
|
||||
|
||||
func (privKey PrivKeySecp256k1) Bytes() []byte {
|
||||
return wire.BinaryBytes(PrivKey{privKey})
|
||||
bz, err := cdc.MarshalBinary(privKey)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return bz
|
||||
}
|
||||
|
||||
func (privKey PrivKeySecp256k1) Sign(msg []byte) Signature {
|
||||
@ -148,40 +127,31 @@ func (privKey PrivKeySecp256k1) Sign(msg []byte) Signature {
|
||||
if err != nil {
|
||||
PanicSanity(err)
|
||||
}
|
||||
return SignatureSecp256k1(sig__.Serialize()).Wrap()
|
||||
return SignatureSecp256k1(sig__.Serialize())
|
||||
}
|
||||
|
||||
func (privKey PrivKeySecp256k1) PubKey() PubKey {
|
||||
_, pub__ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKey[:])
|
||||
var pub PubKeySecp256k1
|
||||
copy(pub[:], pub__.SerializeCompressed())
|
||||
return pub.Wrap()
|
||||
return pub
|
||||
}
|
||||
|
||||
// Equals - you probably don't need to use this.
|
||||
// Runs in constant time based on length of the keys.
|
||||
func (privKey PrivKeySecp256k1) Equals(other PrivKey) bool {
|
||||
if otherSecp, ok := other.Unwrap().(PrivKeySecp256k1); ok {
|
||||
if otherSecp, ok := other.(PrivKeySecp256k1); ok {
|
||||
return subtle.ConstantTimeCompare(privKey[:], otherSecp[:]) == 1
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func (p PrivKeySecp256k1) MarshalJSON() ([]byte, error) {
|
||||
return data.Encoder.Marshal(p[:])
|
||||
}
|
||||
|
||||
func (p *PrivKeySecp256k1) UnmarshalJSON(enc []byte) error {
|
||||
var ref []byte
|
||||
err := data.Encoder.Unmarshal(&ref, enc)
|
||||
copy(p[:], ref)
|
||||
return err
|
||||
}
|
||||
|
||||
/*
|
||||
func (privKey PrivKeySecp256k1) String() string {
|
||||
return Fmt("PrivKeySecp256k1{*****}")
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
// Deterministically generates new priv-key bytes from key.
|
||||
|
Reference in New Issue
Block a user