mirror of
https://github.com/fluencelabs/tendermint
synced 2025-06-25 18:51:39 +00:00
Use new go-wire; PubKey etc are interfaces; Keybase refactor
This commit is contained in:
86
pub_key.go
86
pub_key.go
@ -8,8 +8,6 @@ import (
|
||||
secp256k1 "github.com/btcsuite/btcd/btcec"
|
||||
"github.com/tendermint/ed25519"
|
||||
"github.com/tendermint/ed25519/extra25519"
|
||||
"github.com/tendermint/go-wire"
|
||||
data "github.com/tendermint/go-wire/data"
|
||||
cmn "github.com/tendermint/tmlibs/common"
|
||||
"golang.org/x/crypto/ripemd160"
|
||||
)
|
||||
@ -20,56 +18,44 @@ import (
|
||||
type Address = cmn.HexBytes
|
||||
|
||||
func PubKeyFromBytes(pubKeyBytes []byte) (pubKey PubKey, err error) {
|
||||
if err := wire.ReadBinaryBytes(pubKeyBytes, &pubKey); err != nil {
|
||||
return PubKey{}, err
|
||||
}
|
||||
return pubKey, nil
|
||||
err = cdc.UnmarshalBinary(pubKeyBytes, &pubKey)
|
||||
return
|
||||
}
|
||||
|
||||
//----------------------------------------
|
||||
|
||||
// DO NOT USE THIS INTERFACE.
|
||||
// You probably want to use PubKey
|
||||
// +gen wrapper:"PubKey,Impl[PubKeyEd25519,PubKeySecp256k1],ed25519,secp256k1"
|
||||
type PubKeyInner interface {
|
||||
AssertIsPubKeyInner()
|
||||
type PubKey interface {
|
||||
Address() Address
|
||||
Bytes() []byte
|
||||
KeyString() string
|
||||
VerifyBytes(msg []byte, sig Signature) bool
|
||||
Equals(PubKey) bool
|
||||
Wrap() PubKey
|
||||
}
|
||||
|
||||
//-------------------------------------
|
||||
|
||||
var _ PubKeyInner = PubKeyEd25519{}
|
||||
var _ PubKey = PubKeyEd25519{}
|
||||
|
||||
// Implements PubKeyInner
|
||||
type PubKeyEd25519 [32]byte
|
||||
|
||||
func (pubKey PubKeyEd25519) AssertIsPubKeyInner() {}
|
||||
|
||||
func (pubKey PubKeyEd25519) Address() Address {
|
||||
w, n, err := new(bytes.Buffer), new(int), new(error)
|
||||
wire.WriteBinary(pubKey[:], w, n, err)
|
||||
if *err != nil {
|
||||
panic(*err)
|
||||
}
|
||||
// append type byte
|
||||
encodedPubkey := append([]byte{TypeEd25519}, w.Bytes()...)
|
||||
hasher := ripemd160.New()
|
||||
hasher.Write(encodedPubkey) // does not error
|
||||
hasher.Write(pubKey.Bytes()) // does not error
|
||||
return Address(hasher.Sum(nil))
|
||||
}
|
||||
|
||||
func (pubKey PubKeyEd25519) Bytes() []byte {
|
||||
return wire.BinaryBytes(PubKey{pubKey})
|
||||
bz, err := cdc.MarshalBinary(pubKey)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return bz
|
||||
}
|
||||
|
||||
func (pubKey PubKeyEd25519) VerifyBytes(msg []byte, sig_ Signature) bool {
|
||||
// make sure we use the same algorithm to sign
|
||||
sig, ok := sig_.Unwrap().(SignatureEd25519)
|
||||
sig, ok := sig_.(SignatureEd25519)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
@ -78,17 +64,6 @@ 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 {
|
||||
@ -104,14 +79,8 @@ func (pubKey PubKeyEd25519) String() string {
|
||||
return fmt.Sprintf("PubKeyEd25519{%X}", pubKey[:])
|
||||
}
|
||||
|
||||
// Must return the full bytes in hex.
|
||||
// Used for map keying, etc.
|
||||
func (pubKey PubKeyEd25519) KeyString() string {
|
||||
return fmt.Sprintf("%X", pubKey[:])
|
||||
}
|
||||
|
||||
func (pubKey PubKeyEd25519) Equals(other PubKey) bool {
|
||||
if otherEd, ok := other.Unwrap().(PubKeyEd25519); ok {
|
||||
if otherEd, ok := other.(PubKeyEd25519); ok {
|
||||
return bytes.Equal(pubKey[:], otherEd[:])
|
||||
} else {
|
||||
return false
|
||||
@ -120,15 +89,13 @@ func (pubKey PubKeyEd25519) Equals(other PubKey) bool {
|
||||
|
||||
//-------------------------------------
|
||||
|
||||
var _ PubKeyInner = PubKeySecp256k1{}
|
||||
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
|
||||
|
||||
func (pubKey PubKeySecp256k1) AssertIsPubKeyInner() {}
|
||||
|
||||
// Implements Bitcoin style addresses: RIPEMD160(SHA256(pubkey))
|
||||
func (pubKey PubKeySecp256k1) Address() Address {
|
||||
hasherSHA256 := sha256.New()
|
||||
@ -141,12 +108,16 @@ func (pubKey PubKeySecp256k1) Address() Address {
|
||||
}
|
||||
|
||||
func (pubKey PubKeySecp256k1) Bytes() []byte {
|
||||
return wire.BinaryBytes(PubKey{pubKey})
|
||||
bz, err := cdc.MarshalBinary(pubKey)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return bz
|
||||
}
|
||||
|
||||
func (pubKey PubKeySecp256k1) VerifyBytes(msg []byte, sig_ Signature) bool {
|
||||
// and assert same algorithm to sign and verify
|
||||
sig, ok := sig_.Unwrap().(SignatureSecp256k1)
|
||||
sig, ok := sig_.(SignatureSecp256k1)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
@ -162,29 +133,12 @@ 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.Sprintf("PubKeySecp256k1{%X}", pubKey[:])
|
||||
}
|
||||
|
||||
// Must return the full bytes in hex.
|
||||
// Used for map keying, etc.
|
||||
func (pubKey PubKeySecp256k1) KeyString() string {
|
||||
return fmt.Sprintf("%X", pubKey[:])
|
||||
}
|
||||
|
||||
func (pubKey PubKeySecp256k1) Equals(other PubKey) bool {
|
||||
if otherSecp, ok := other.Unwrap().(PubKeySecp256k1); ok {
|
||||
if otherSecp, ok := other.(PubKeySecp256k1); ok {
|
||||
return bytes.Equal(pubKey[:], otherSecp[:])
|
||||
} else {
|
||||
return false
|
||||
|
Reference in New Issue
Block a user