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
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,
}
}


+ 8
- 9
account/privkey.go View File

@ -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))
}

+ 6
- 10
cmd/gen_account.go View File

@ -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),
)
}

+ 3
- 1
cmd/gen_validator.go View File

@ -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


+ 5
- 0
rpc/accounts.go View File

@ -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_})


+ 2
- 4
state/priv_validator.go View File

@ -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()


Loading…
Cancel
Save