Browse Source

PrivKey is just []byte

pull/39/head
Jae Kwon 10 years ago
parent
commit
694a10e0a7
6 changed files with 28 additions and 34 deletions
  1. +4
    -10
      account/priv_account.go
  2. +8
    -9
      account/privkey.go
  3. +6
    -10
      cmd/gen_account.go
  4. +3
    -1
      cmd/gen_validator.go
  5. +5
    -0
      rpc/accounts.go
  6. +2
    -4
      state/priv_validator.go

+ 4
- 10
account/priv_account.go View File

@ -1,8 +1,6 @@
package account package account
import ( import (
"github.com/tendermint/go-ed25519"
. "github.com/tendermint/tendermint/common" . "github.com/tendermint/tendermint/common"
) )
@ -14,15 +12,11 @@ type PrivAccount struct {
// Generates a new account with private key. // Generates a new account with private key.
func GenPrivAccount() *PrivAccount { func GenPrivAccount() *PrivAccount {
privKey := CRandBytes(32)
pubKey := PubKeyEd25519(ed25519.MakePubKey(privKey))
privKey := PrivKeyEd25519(CRandBytes(32))
return &PrivAccount{ return &PrivAccount{
pubKey.Address(),
pubKey,
PrivKeyEd25519{
PubKey: pubKey,
PrivKey: privKey,
},
Address: privKey.PubKey().Address(),
PubKey: privKey.PubKey(),
PrivKey: privKey,
} }
} }


+ 8
- 9
account/privkey.go View File

@ -10,6 +10,7 @@ import (
// PrivKey is part of PrivAccount and state.PrivValidator. // PrivKey is part of PrivAccount and state.PrivValidator.
type PrivKey interface { type PrivKey interface {
Sign(msg []byte) Signature Sign(msg []byte) Signature
PubKey() PubKey
} }
// Types of PrivKey implementations // Types of PrivKey implementations
@ -26,24 +27,22 @@ var _ = RegisterInterface(
//------------------------------------- //-------------------------------------
// Implements PrivKey // Implements PrivKey
type PrivKeyEd25519 struct {
PubKey []byte
PrivKey []byte
}
type PrivKeyEd25519 []byte
func (key PrivKeyEd25519) TypeByte() byte { return PrivKeyTypeEd25519 } func (key PrivKeyEd25519) TypeByte() byte { return PrivKeyTypeEd25519 }
func (key PrivKeyEd25519) ValidateBasic() error { 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 errors.New("Invalid PrivKeyEd25519 privkey size")
} }
return nil return nil
} }
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, ed25519.MakePubKey(key))
return SignatureEd25519(signature) return SignatureEd25519(signature)
} }
func (key PrivKeyEd25519) PubKey() PubKey {
return PubKeyEd25519(ed25519.MakePubKey(key))
}

+ 6
- 10
cmd/gen_account.go View File

@ -1,24 +1,20 @@
package main package main
import ( import (
"encoding/hex"
"fmt" "fmt"
. "github.com/tendermint/tendermint/account"
"github.com/tendermint/tendermint/account"
. "github.com/tendermint/tendermint/binary" . "github.com/tendermint/tendermint/binary"
) )
func gen_account() { 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),
) )
} }

+ 3
- 1
cmd/gen_validator.go View File

@ -5,12 +5,14 @@ import (
"github.com/tendermint/tendermint/config" "github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/state" "github.com/tendermint/tendermint/state"
. "github.com/tendermint/tendermint/binary"
) )
func gen_validator() { func gen_validator() {
privValidator := state.GenPrivValidator() privValidator := state.GenPrivValidator()
privValidatorJSONBytes := privValidator.JSONBytes()
privValidatorJSONBytes := JSONBytes(privValidator)
fmt.Printf(`Generated a new validator! fmt.Printf(`Generated a new validator!
Paste the following JSON into your %v file Paste the following JSON into your %v file


+ 5
- 0
rpc/accounts.go View File

@ -33,6 +33,11 @@ func GetAccountHandler(w http.ResponseWriter, r *http.Request) {
state := consensusState.GetState() state := consensusState.GetState()
account_ := state.GetAccount(address) account_ := state.GetAccount(address)
if account_ == nil {
WriteAPIResponse(w, API_OK, struct{}{})
return
}
WriteAPIResponse(w, API_OK, struct { WriteAPIResponse(w, API_OK, struct {
Account *account.Account Account *account.Account
}{account_}) }{account_})


+ 2
- 4
state/priv_validator.go View File

@ -59,7 +59,7 @@ 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(privKeyBytes)
return &PrivValidator{ return &PrivValidator{
Address: pubKey.Address(), Address: pubKey.Address(),
PubKey: pubKey, PubKey: pubKey,
@ -91,15 +91,13 @@ func (privVal *PrivValidator) Save() {
} }
func (privVal *PrivValidator) save() { func (privVal *PrivValidator) save() {
jsonBytes := privVal.JSONBytes()
jsonBytes := JSONBytes(privVal)
err := ioutil.WriteFile(privVal.filename, jsonBytes, 0700) err := ioutil.WriteFile(privVal.filename, jsonBytes, 0700)
if err != nil { if err != nil {
panic(err) panic(err)
} }
} }
func (privVal *PrivValidator) JSONBytes() []byte { return JSONBytes(privVal) }
// TODO: test // TODO: test
func (privVal *PrivValidator) SignVote(vote *Vote) error { func (privVal *PrivValidator) SignVote(vote *Vote) error {
privVal.mtx.Lock() privVal.mtx.Lock()


Loading…
Cancel
Save