From 694a10e0a786e03fb1038e0ed7b45e37152848df Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Tue, 13 Jan 2015 21:03:01 -0800 Subject: [PATCH] PrivKey is just []byte --- account/priv_account.go | 14 ++++---------- account/privkey.go | 17 ++++++++--------- cmd/gen_account.go | 16 ++++++---------- cmd/gen_validator.go | 4 +++- rpc/accounts.go | 5 +++++ state/priv_validator.go | 6 ++---- 6 files changed, 28 insertions(+), 34 deletions(-) diff --git a/account/priv_account.go b/account/priv_account.go index 4b4107d8e..f9179ec84 100644 --- a/account/priv_account.go +++ b/account/priv_account.go @@ -1,8 +1,6 @@ package account import ( - "github.com/tendermint/go-ed25519" - . "github.com/tendermint/tendermint/common" ) @@ -14,15 +12,11 @@ type PrivAccount struct { // Generates a new account with private key. func GenPrivAccount() *PrivAccount { - privKey := CRandBytes(32) - pubKey := PubKeyEd25519(ed25519.MakePubKey(privKey)) + privKey := PrivKeyEd25519(CRandBytes(32)) return &PrivAccount{ - pubKey.Address(), - pubKey, - PrivKeyEd25519{ - PubKey: pubKey, - PrivKey: privKey, - }, + Address: privKey.PubKey().Address(), + PubKey: privKey.PubKey(), + PrivKey: privKey, } } diff --git a/account/privkey.go b/account/privkey.go index eee63f527..87d634acb 100644 --- a/account/privkey.go +++ b/account/privkey.go @@ -10,6 +10,7 @@ import ( // PrivKey is part of PrivAccount and state.PrivValidator. type PrivKey interface { Sign(msg []byte) Signature + PubKey() PubKey } // Types of PrivKey implementations @@ -26,24 +27,22 @@ var _ = RegisterInterface( //------------------------------------- // Implements PrivKey -type PrivKeyEd25519 struct { - PubKey []byte - PrivKey []byte -} +type PrivKeyEd25519 []byte func (key PrivKeyEd25519) TypeByte() byte { return PrivKeyTypeEd25519 } func (key PrivKeyEd25519) ValidateBasic() error { - if len(key.PubKey) != ed25519.PublicKeySize { - return errors.New("Invalid PrivKeyEd25519 pubkey size") - } - if len(key.PrivKey) != ed25519.PrivateKeySize { + if len(key) != ed25519.PrivateKeySize { return errors.New("Invalid PrivKeyEd25519 privkey size") } return nil } func (key PrivKeyEd25519) Sign(msg []byte) Signature { - signature := ed25519.SignMessage(msg, key.PrivKey, key.PubKey) + signature := ed25519.SignMessage(msg, key, ed25519.MakePubKey(key)) return SignatureEd25519(signature) } + +func (key PrivKeyEd25519) PubKey() PubKey { + return PubKeyEd25519(ed25519.MakePubKey(key)) +} diff --git a/cmd/gen_account.go b/cmd/gen_account.go index fe8301813..d7f4b2d46 100644 --- a/cmd/gen_account.go +++ b/cmd/gen_account.go @@ -1,24 +1,20 @@ package main import ( - "encoding/hex" "fmt" - . "github.com/tendermint/tendermint/account" + "github.com/tendermint/tendermint/account" . "github.com/tendermint/tendermint/binary" ) func gen_account() { + privAccount := account.GenPrivAccount() + privAccountJSONBytes := JSONBytes(privAccount) + fmt.Printf(`Generated a new account! - // TODO: uh, write better logic. - // Generate private account - privAccount := GenPrivAccount() +%v - fmt.Printf(`Generated account: -Account Public Key: %v -Account Private Key: %v `, - hex.EncodeToString(BinaryBytes(privAccount.PubKey)), - hex.EncodeToString(BinaryBytes(privAccount.PrivKey)), + string(privAccountJSONBytes), ) } diff --git a/cmd/gen_validator.go b/cmd/gen_validator.go index 8b5e3e943..7f2b08fc5 100644 --- a/cmd/gen_validator.go +++ b/cmd/gen_validator.go @@ -5,12 +5,14 @@ import ( "github.com/tendermint/tendermint/config" "github.com/tendermint/tendermint/state" + + . "github.com/tendermint/tendermint/binary" ) func gen_validator() { privValidator := state.GenPrivValidator() - privValidatorJSONBytes := privValidator.JSONBytes() + privValidatorJSONBytes := JSONBytes(privValidator) fmt.Printf(`Generated a new validator! Paste the following JSON into your %v file diff --git a/rpc/accounts.go b/rpc/accounts.go index 2f0e29799..12dcd7b7d 100644 --- a/rpc/accounts.go +++ b/rpc/accounts.go @@ -33,6 +33,11 @@ func GetAccountHandler(w http.ResponseWriter, r *http.Request) { state := consensusState.GetState() account_ := state.GetAccount(address) + if account_ == nil { + WriteAPIResponse(w, API_OK, struct{}{}) + return + } + WriteAPIResponse(w, API_OK, struct { Account *account.Account }{account_}) diff --git a/state/priv_validator.go b/state/priv_validator.go index ac11f5c83..7d235c40f 100644 --- a/state/priv_validator.go +++ b/state/priv_validator.go @@ -59,7 +59,7 @@ func GenPrivValidator() *PrivValidator { privKeyBytes := CRandBytes(32) pubKeyBytes := ed25519.MakePubKey(privKeyBytes) pubKey := PubKeyEd25519(pubKeyBytes) - privKey := PrivKeyEd25519{pubKeyBytes, privKeyBytes} + privKey := PrivKeyEd25519(privKeyBytes) return &PrivValidator{ Address: pubKey.Address(), PubKey: pubKey, @@ -91,15 +91,13 @@ func (privVal *PrivValidator) Save() { } func (privVal *PrivValidator) save() { - jsonBytes := privVal.JSONBytes() + jsonBytes := JSONBytes(privVal) err := ioutil.WriteFile(privVal.filename, jsonBytes, 0700) if err != nil { panic(err) } } -func (privVal *PrivValidator) JSONBytes() []byte { return JSONBytes(privVal) } - // TODO: test func (privVal *PrivValidator) SignVote(vote *Vote) error { privVal.mtx.Lock()