mirror of
https://github.com/tendermint/tendermint.git
synced 2026-07-29 11:32:56 +00:00
type Ed25519[Signature|PubKey] struct{[]byte} -> []byte
This commit is contained in:
@@ -16,9 +16,7 @@ func GenPrivAccount() *PrivAccount {
|
||||
privKey := CRandBytes(32)
|
||||
pubKey := ed25519.MakePubKey(privKey)
|
||||
return &PrivAccount{
|
||||
PubKeyEd25519{
|
||||
PubKey: pubKey,
|
||||
},
|
||||
PubKeyEd25519(pubKey),
|
||||
PrivKeyEd25519{
|
||||
PubKey: pubKey,
|
||||
PrivKey: privKey,
|
||||
|
||||
+1
-1
@@ -59,5 +59,5 @@ func (key PrivKeyEd25519) ValidateBasic() error {
|
||||
|
||||
func (key PrivKeyEd25519) Sign(msg []byte) Signature {
|
||||
signature := ed25519.SignMessage(msg, key.PrivKey, key.PubKey)
|
||||
return SignatureEd25519{signature}
|
||||
return SignatureEd25519(signature)
|
||||
}
|
||||
|
||||
+7
-8
@@ -19,7 +19,7 @@ type PubKey interface {
|
||||
// Types of PubKey implementations
|
||||
const (
|
||||
PubKeyTypeNil = byte(0x00)
|
||||
PubKeyTypeEd25519 = byte(0x02)
|
||||
PubKeyTypeEd25519 = byte(0x01)
|
||||
)
|
||||
|
||||
//-------------------------------------
|
||||
@@ -60,16 +60,15 @@ func (key PubKeyNil) VerifyBytes(msg []byte, sig_ Signature) bool {
|
||||
//-------------------------------------
|
||||
|
||||
// Implements PubKey
|
||||
type PubKeyEd25519 struct {
|
||||
PubKey []byte
|
||||
}
|
||||
type PubKeyEd25519 []byte
|
||||
|
||||
func (key PubKeyEd25519) TypeByte() byte { return PubKeyTypeEd25519 }
|
||||
|
||||
func (key PubKeyEd25519) Address() []byte { return BinaryRipemd160(key.PubKey) }
|
||||
// TODO: Or should this just be BinaryRipemd160(key)? (The difference is the TypeByte.)
|
||||
func (key PubKeyEd25519) Address() []byte { return BinaryRipemd160([]byte(key)) }
|
||||
|
||||
func (key PubKeyEd25519) ValidateBasic() error {
|
||||
if len(key.PubKey) != ed25519.PublicKeySize {
|
||||
if len(key) != ed25519.PublicKeySize {
|
||||
return errors.New("Invalid PubKeyEd25519 key size")
|
||||
}
|
||||
return nil
|
||||
@@ -82,8 +81,8 @@ func (key PubKeyEd25519) VerifyBytes(msg []byte, sig_ Signature) bool {
|
||||
}
|
||||
v1 := &ed25519.Verify{
|
||||
Message: msg,
|
||||
PubKey: key.PubKey,
|
||||
Signature: []byte(sig.Bytes),
|
||||
PubKey: key,
|
||||
Signature: sig,
|
||||
}
|
||||
return ed25519.VerifyBatch([]*ed25519.Verify{v1})
|
||||
}
|
||||
|
||||
@@ -40,23 +40,21 @@ var _ = RegisterType(&TypeInfo{
|
||||
//-------------------------------------
|
||||
|
||||
// Implements Signature
|
||||
type SignatureEd25519 struct {
|
||||
Bytes []byte
|
||||
}
|
||||
type SignatureEd25519 []byte
|
||||
|
||||
func (sig SignatureEd25519) TypeByte() byte { return SignatureTypeEd25519 }
|
||||
|
||||
func (sig SignatureEd25519) ValidateBasic() error {
|
||||
if len(sig.Bytes) != ed25519.SignatureSize {
|
||||
if len(sig) != ed25519.SignatureSize {
|
||||
return errors.New("Invalid SignatureEd25519 signature size")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (sig SignatureEd25519) IsZero() bool {
|
||||
return len(sig.Bytes) == 0
|
||||
return len(sig) == 0
|
||||
}
|
||||
|
||||
func (sig SignatureEd25519) String() string {
|
||||
return fmt.Sprintf("%X", Fingerprint(sig.Bytes))
|
||||
return fmt.Sprintf("%X", Fingerprint(sig))
|
||||
}
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
package account
|
||||
|
||||
import (
|
||||
. "github.com/tendermint/tendermint/common"
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
ed25519 "github.com/tendermint/go-ed25519"
|
||||
. "github.com/tendermint/tendermint/binary"
|
||||
. "github.com/tendermint/tendermint/common"
|
||||
)
|
||||
|
||||
func TestSignAndValidate(t *testing.T) {
|
||||
@@ -21,9 +25,44 @@ func TestSignAndValidate(t *testing.T) {
|
||||
}
|
||||
|
||||
// Mutate the signature, just one bit.
|
||||
sig.(SignatureEd25519).Bytes[0] ^= byte(0x01)
|
||||
sig.(SignatureEd25519)[0] ^= byte(0x01)
|
||||
|
||||
if pubKey.VerifyBytes(msg, sig) {
|
||||
t.Errorf("Account message signature verification should have failed but passed instead")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBinaryDecode(t *testing.T) {
|
||||
|
||||
privAccount := GenPrivAccount()
|
||||
pubKey := privAccount.PubKey
|
||||
privKey := privAccount.PrivKey
|
||||
|
||||
msg := CRandBytes(128)
|
||||
sig := privKey.Sign(msg)
|
||||
t.Logf("msg: %X, sig: %X", msg, sig)
|
||||
|
||||
buf, n, err := new(bytes.Buffer), new(int64), new(error)
|
||||
WriteBinary(sig, buf, n, err)
|
||||
if *err != nil {
|
||||
t.Fatalf("Failed to write Signature: %v", err)
|
||||
}
|
||||
|
||||
if len(buf.Bytes()) != ed25519.SignatureSize+2 {
|
||||
// 1 byte TypeByte, 1 byte length, 64 bytes signature bytes
|
||||
t.Fatalf("Unexpected signature write size: %v", len(buf.Bytes()))
|
||||
}
|
||||
if buf.Bytes()[0] != SignatureTypeEd25519 {
|
||||
t.Fatalf("Unexpected signature type byte")
|
||||
}
|
||||
|
||||
sig2, ok := ReadBinary(SignatureEd25519{}, buf, n, err).(SignatureEd25519)
|
||||
if !ok || *err != nil {
|
||||
t.Fatalf("Failed to read Signature: %v", err)
|
||||
}
|
||||
|
||||
// Test the signature
|
||||
if !pubKey.VerifyBytes(msg, sig2) {
|
||||
t.Errorf("Account message signature verification failed")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user