type Ed25519[Signature|PubKey] struct{[]byte} -> []byte

This commit is contained in:
Jae Kwon
2015-01-03 20:24:02 -08:00
parent f02ba63412
commit 7a8a0fefc7
11 changed files with 72 additions and 29 deletions
+1 -3
View File
@@ -16,9 +16,7 @@ func GenPrivAccount() *PrivAccount {
privKey := CRandBytes(32) privKey := CRandBytes(32)
pubKey := ed25519.MakePubKey(privKey) pubKey := ed25519.MakePubKey(privKey)
return &PrivAccount{ return &PrivAccount{
PubKeyEd25519{ PubKeyEd25519(pubKey),
PubKey: pubKey,
},
PrivKeyEd25519{ PrivKeyEd25519{
PubKey: pubKey, PubKey: pubKey,
PrivKey: privKey, PrivKey: privKey,
+1 -1
View File
@@ -59,5 +59,5 @@ func (key PrivKeyEd25519) ValidateBasic() error {
func (key PrivKeyEd25519) Sign(msg []byte) Signature { func (key PrivKeyEd25519) Sign(msg []byte) Signature {
signature := ed25519.SignMessage(msg, key.PrivKey, key.PubKey) signature := ed25519.SignMessage(msg, key.PrivKey, key.PubKey)
return SignatureEd25519{signature} return SignatureEd25519(signature)
} }
+7 -8
View File
@@ -19,7 +19,7 @@ type PubKey interface {
// Types of PubKey implementations // Types of PubKey implementations
const ( const (
PubKeyTypeNil = byte(0x00) PubKeyTypeNil = byte(0x00)
PubKeyTypeEd25519 = byte(0x02) PubKeyTypeEd25519 = byte(0x01)
) )
//------------------------------------- //-------------------------------------
@@ -60,16 +60,15 @@ func (key PubKeyNil) VerifyBytes(msg []byte, sig_ Signature) bool {
//------------------------------------- //-------------------------------------
// Implements PubKey // Implements PubKey
type PubKeyEd25519 struct { type PubKeyEd25519 []byte
PubKey []byte
}
func (key PubKeyEd25519) TypeByte() byte { return PubKeyTypeEd25519 } 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 { func (key PubKeyEd25519) ValidateBasic() error {
if len(key.PubKey) != ed25519.PublicKeySize { if len(key) != ed25519.PublicKeySize {
return errors.New("Invalid PubKeyEd25519 key size") return errors.New("Invalid PubKeyEd25519 key size")
} }
return nil return nil
@@ -82,8 +81,8 @@ func (key PubKeyEd25519) VerifyBytes(msg []byte, sig_ Signature) bool {
} }
v1 := &ed25519.Verify{ v1 := &ed25519.Verify{
Message: msg, Message: msg,
PubKey: key.PubKey, PubKey: key,
Signature: []byte(sig.Bytes), Signature: sig,
} }
return ed25519.VerifyBatch([]*ed25519.Verify{v1}) return ed25519.VerifyBatch([]*ed25519.Verify{v1})
} }
+4 -6
View File
@@ -40,23 +40,21 @@ var _ = RegisterType(&TypeInfo{
//------------------------------------- //-------------------------------------
// Implements Signature // Implements Signature
type SignatureEd25519 struct { type SignatureEd25519 []byte
Bytes []byte
}
func (sig SignatureEd25519) TypeByte() byte { return SignatureTypeEd25519 } func (sig SignatureEd25519) TypeByte() byte { return SignatureTypeEd25519 }
func (sig SignatureEd25519) ValidateBasic() error { func (sig SignatureEd25519) ValidateBasic() error {
if len(sig.Bytes) != ed25519.SignatureSize { if len(sig) != ed25519.SignatureSize {
return errors.New("Invalid SignatureEd25519 signature size") return errors.New("Invalid SignatureEd25519 signature size")
} }
return nil return nil
} }
func (sig SignatureEd25519) IsZero() bool { func (sig SignatureEd25519) IsZero() bool {
return len(sig.Bytes) == 0 return len(sig) == 0
} }
func (sig SignatureEd25519) String() string { func (sig SignatureEd25519) String() string {
return fmt.Sprintf("%X", Fingerprint(sig.Bytes)) return fmt.Sprintf("%X", Fingerprint(sig))
} }
+41 -2
View File
@@ -1,8 +1,12 @@
package account package account
import ( import (
. "github.com/tendermint/tendermint/common" "bytes"
"testing" "testing"
ed25519 "github.com/tendermint/go-ed25519"
. "github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/common"
) )
func TestSignAndValidate(t *testing.T) { func TestSignAndValidate(t *testing.T) {
@@ -21,9 +25,44 @@ func TestSignAndValidate(t *testing.T) {
} }
// Mutate the signature, just one bit. // Mutate the signature, just one bit.
sig.(SignatureEd25519).Bytes[0] ^= byte(0x01) sig.(SignatureEd25519)[0] ^= byte(0x01)
if pubKey.VerifyBytes(msg, sig) { if pubKey.VerifyBytes(msg, sig) {
t.Errorf("Account message signature verification should have failed but passed instead") 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")
}
}
+1 -1
View File
@@ -169,7 +169,7 @@ func (commit Commit) IsZero() bool {
} }
func (commit Commit) String() string { func (commit Commit) String() string {
return fmt.Sprintf("Commit{A:%X R:%v %X}", commit.Address, commit.Round, Fingerprint(commit.Signature.Bytes)) return fmt.Sprintf("Commit{A:%X R:%v %X}", commit.Address, commit.Round, Fingerprint(commit.Signature))
} }
//------------------------------------- //-------------------------------------
+4 -2
View File
@@ -59,10 +59,11 @@ func gen_tx() {
// Get source pubkey // Get source pubkey
srcPubKeyBytes := getByteSliceFromBase64("Enter source pubkey: ") srcPubKeyBytes := getByteSliceFromBase64("Enter source pubkey: ")
r, n, err := bytes.NewReader(srcPubKeyBytes), new(int64), new(error) r, n, err := bytes.NewReader(srcPubKeyBytes), new(int64), new(error)
srcPubKey := account_.PubKeyDecoder(r, n, err).(account_.PubKey) srcPubKey_ := account_.PubKeyDecoder(r, n, err)
if *err != nil { if *err != nil {
Exit(Fmt("Invalid PubKey. Error: %v", err)) Exit(Fmt("Invalid PubKey. Error: %v", err))
} }
srcPubKey := srcPubKey_.(account_.PubKey)
// Get the state of the account. // Get the state of the account.
var srcAccount *account_.Account var srcAccount *account_.Account
@@ -113,10 +114,11 @@ func gen_tx() {
// Get source privkey (for signing) // Get source privkey (for signing)
srcPrivKeyBytes := getByteSliceFromBase64("Enter source privkey (for signing): ") srcPrivKeyBytes := getByteSliceFromBase64("Enter source privkey (for signing): ")
r, n, err = bytes.NewReader(srcPrivKeyBytes), new(int64), new(error) r, n, err = bytes.NewReader(srcPrivKeyBytes), new(int64), new(error)
srcPrivKey := account_.PrivKeyDecoder(r, n, err).(account_.PrivKey) srcPrivKey_ := account_.PrivKeyDecoder(r, n, err)
if *err != nil { if *err != nil {
Exit(Fmt("Invalid PrivKey. Error: %v", err)) Exit(Fmt("Invalid PrivKey. Error: %v", err))
} }
srcPrivKey := srcPrivKey_.(account_.PrivKey)
// Sign // Sign
tx.Inputs[0].Signature = srcPrivKey.Sign(account_.SignBytes(tx)) tx.Inputs[0].Signature = srcPrivKey.Sign(account_.SignBytes(tx))
+2 -2
View File
@@ -72,7 +72,7 @@ func TestVerifyInvalidVote(t *testing.T) {
} }
for i := 0; i < 7; i++ { for i := 0; i < 7; i++ {
polVoteSig := signAddPOLVoteSignature(privValidators[i], valSet, voteProto, pol) polVoteSig := signAddPOLVoteSignature(privValidators[i], valSet, voteProto, pol)
polVoteSig.Signature.Bytes[0] += byte(0x01) // mutated! polVoteSig.Signature[0] += byte(0x01) // mutated!
} }
// Check that validation fails. // Check that validation fails.
@@ -119,7 +119,7 @@ func TestVerifyInvalidCommits(t *testing.T) {
} }
for i := 0; i < 7; i++ { for i := 0; i < 7; i++ {
polVoteSig := signAddPOLVoteSignature(privValidators[i], valSet, voteProto, pol) polVoteSig := signAddPOLVoteSignature(privValidators[i], valSet, voteProto, pol)
polVoteSig.Signature.Bytes[0] += byte(0x01) polVoteSig.Signature[0] += byte(0x01)
} }
// Check that validation fails. // Check that validation fails.
+9 -2
View File
@@ -2,6 +2,8 @@ package rpc
import ( import (
"bytes" "bytes"
"encoding/json"
"fmt"
"net/http" "net/http"
"github.com/tendermint/tendermint/block" "github.com/tendermint/tendermint/block"
@@ -21,7 +23,6 @@ func MempoolHandler(w http.ResponseWriter, r *http.Request) {
if err != nil { if err != nil {
ReturnJSON(API_INVALID_PARAM, Fmt("Invalid tx_bytes: %v", err)) ReturnJSON(API_INVALID_PARAM, Fmt("Invalid tx_bytes: %v", err))
} }
// XXX Oops, I need to cast later like this, everywhere.
tx := tx_.(block.Tx) tx := tx_.(block.Tx)
err = mempoolReactor.BroadcastTx(tx) err = mempoolReactor.BroadcastTx(tx)
@@ -29,9 +30,15 @@ func MempoolHandler(w http.ResponseWriter, r *http.Request) {
ReturnJSON(API_ERROR, Fmt("Error broadcasting transaction: %v", err)) ReturnJSON(API_ERROR, Fmt("Error broadcasting transaction: %v", err))
} }
jsonBytes, err := json.MarshalIndent(tx, "", " ")
if err != nil {
panic(err)
}
fmt.Println(">>", string(jsonBytes))
ReturnJSON(API_OK, Fmt("Broadcasted tx: %X", tx)) ReturnJSON(API_OK, Fmt("Broadcasted tx: %X", tx))
} }
/* /*
curl -H 'content-type: text/plain;' http://127.0.0.1:8888/mempool?tx_bytes=0101146070FF17C39B2B0A64CA2BC431328037FA0F47606400000000000000000140D209A7CD4E2E7C5E4B17815AB93029960AF66D3428DE7B085EBDBACD84A31F58562EFF0AC4EC7151B071DE82417110C94FFEE862A3740624D7A8C1874AFCF50402206BD490C212E701A2136EEEA04F06FA4F287EE47E2B7A9B5D62EDD84CD6AD975301146070FF17C39B2B0A64CA2BC431328037FA0F47FF6400000000000000 curl -H 'content-type: text/plain;' http://127.0.0.1:8888/mempool?tx_bytes=0101146070FF17C39B2B0A64CA2BC431328037FA0F4760640000000000000001014025BE0AD9344DA24BC12FCB84903F88AB9B82107F414A0B570048CDEF7EDDD5AC96B34F0405B7A75B761447F8E6C899343F1EDB160B4C4FAAE92D5881D0023A0701206BD490C212E701A2136EEEA04F06FA4F287EE47E2B7A9B5D62EDD84CD6AD975301146070FF17C39B2B0A64CA2BC431328037FA0F47FF6400000000000000
*/ */
+1 -1
View File
@@ -61,7 +61,7 @@ type PrivValidator struct {
func GenPrivValidator() *PrivValidator { func GenPrivValidator() *PrivValidator {
privKeyBytes := CRandBytes(32) privKeyBytes := CRandBytes(32)
pubKeyBytes := ed25519.MakePubKey(privKeyBytes) pubKeyBytes := ed25519.MakePubKey(privKeyBytes)
pubKey := PubKeyEd25519{pubKeyBytes} pubKey := PubKeyEd25519(pubKeyBytes)
privKey := PrivKeyEd25519{pubKeyBytes, privKeyBytes} privKey := PrivKeyEd25519{pubKeyBytes, privKeyBytes}
return &PrivValidator{ return &PrivValidator{
Address: pubKey.Address(), Address: pubKey.Address(),
+1 -1
View File
@@ -11,7 +11,7 @@ import (
func randValidator_() *Validator { func randValidator_() *Validator {
return &Validator{ return &Validator{
Address: RandBytes(20), Address: RandBytes(20),
PubKey: PubKeyEd25519{RandBytes(64)}, PubKey: PubKeyEd25519(RandBytes(64)),
BondHeight: uint(RandUint32()), BondHeight: uint(RandUint32()),
VotingPower: RandUint64(), VotingPower: RandUint64(),
Accum: int64(RandUint64()), Accum: int64(RandUint64()),