diff --git a/account/priv_account.go b/account/priv_account.go index b84fc7b16..a8c8532b2 100644 --- a/account/priv_account.go +++ b/account/priv_account.go @@ -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, diff --git a/account/privkey.go b/account/privkey.go index c220492b6..2c48368fd 100644 --- a/account/privkey.go +++ b/account/privkey.go @@ -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) } diff --git a/account/pubkey.go b/account/pubkey.go index 5c3874542..f7dbc9cf3 100644 --- a/account/pubkey.go +++ b/account/pubkey.go @@ -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}) } diff --git a/account/signature.go b/account/signature.go index a6c6b83cc..6f55aa448 100644 --- a/account/signature.go +++ b/account/signature.go @@ -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)) } diff --git a/account/signature_test.go b/account/signature_test.go index ec6234845..3cba030aa 100644 --- a/account/signature_test.go +++ b/account/signature_test.go @@ -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") + } +} diff --git a/block/block.go b/block/block.go index e3533dca9..60fb2615e 100644 --- a/block/block.go +++ b/block/block.go @@ -169,7 +169,7 @@ func (commit Commit) IsZero() bool { } 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)) } //------------------------------------- diff --git a/cmd/gen_tx.go b/cmd/gen_tx.go index a99f44c81..f93d7e419 100644 --- a/cmd/gen_tx.go +++ b/cmd/gen_tx.go @@ -59,10 +59,11 @@ func gen_tx() { // Get source pubkey srcPubKeyBytes := getByteSliceFromBase64("Enter source pubkey: ") 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 { Exit(Fmt("Invalid PubKey. Error: %v", err)) } + srcPubKey := srcPubKey_.(account_.PubKey) // Get the state of the account. var srcAccount *account_.Account @@ -113,10 +114,11 @@ func gen_tx() { // Get source privkey (for signing) srcPrivKeyBytes := getByteSliceFromBase64("Enter source privkey (for signing): ") 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 { Exit(Fmt("Invalid PrivKey. Error: %v", err)) } + srcPrivKey := srcPrivKey_.(account_.PrivKey) // Sign tx.Inputs[0].Signature = srcPrivKey.Sign(account_.SignBytes(tx)) diff --git a/consensus/pol_test.go b/consensus/pol_test.go index a4a636406..d523965f2 100644 --- a/consensus/pol_test.go +++ b/consensus/pol_test.go @@ -72,7 +72,7 @@ func TestVerifyInvalidVote(t *testing.T) { } for i := 0; i < 7; i++ { polVoteSig := signAddPOLVoteSignature(privValidators[i], valSet, voteProto, pol) - polVoteSig.Signature.Bytes[0] += byte(0x01) // mutated! + polVoteSig.Signature[0] += byte(0x01) // mutated! } // Check that validation fails. @@ -119,7 +119,7 @@ func TestVerifyInvalidCommits(t *testing.T) { } for i := 0; i < 7; i++ { polVoteSig := signAddPOLVoteSignature(privValidators[i], valSet, voteProto, pol) - polVoteSig.Signature.Bytes[0] += byte(0x01) + polVoteSig.Signature[0] += byte(0x01) } // Check that validation fails. diff --git a/rpc/mempool.go b/rpc/mempool.go index 82d0d5fff..f42472cb1 100644 --- a/rpc/mempool.go +++ b/rpc/mempool.go @@ -2,6 +2,8 @@ package rpc import ( "bytes" + "encoding/json" + "fmt" "net/http" "github.com/tendermint/tendermint/block" @@ -21,7 +23,6 @@ func MempoolHandler(w http.ResponseWriter, r *http.Request) { if err != nil { ReturnJSON(API_INVALID_PARAM, Fmt("Invalid tx_bytes: %v", err)) } - // XXX Oops, I need to cast later like this, everywhere. tx := tx_.(block.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)) } + jsonBytes, err := json.MarshalIndent(tx, "", " ") + if err != nil { + panic(err) + } + fmt.Println(">>", string(jsonBytes)) + 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 */ diff --git a/state/priv_validator.go b/state/priv_validator.go index 1de4e96f5..575ba5bee 100644 --- a/state/priv_validator.go +++ b/state/priv_validator.go @@ -61,7 +61,7 @@ type PrivValidator struct { func GenPrivValidator() *PrivValidator { privKeyBytes := CRandBytes(32) pubKeyBytes := ed25519.MakePubKey(privKeyBytes) - pubKey := PubKeyEd25519{pubKeyBytes} + pubKey := PubKeyEd25519(pubKeyBytes) privKey := PrivKeyEd25519{pubKeyBytes, privKeyBytes} return &PrivValidator{ Address: pubKey.Address(), diff --git a/state/validator_set_test.go b/state/validator_set_test.go index 819ded7b3..ee1034676 100644 --- a/state/validator_set_test.go +++ b/state/validator_set_test.go @@ -11,7 +11,7 @@ import ( func randValidator_() *Validator { return &Validator{ Address: RandBytes(20), - PubKey: PubKeyEd25519{RandBytes(64)}, + PubKey: PubKeyEd25519(RandBytes(64)), BondHeight: uint(RandUint32()), VotingPower: RandUint64(), Accum: int64(RandUint64()),