You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.1 KiB

10 years ago
10 years ago
10 years ago
  1. package account
  2. import (
  3. "github.com/tendermint/ed25519"
  4. . "github.com/tendermint/tendermint/common"
  5. )
  6. type PrivAccount struct {
  7. Address []byte `json:"address"`
  8. PubKey PubKey `json:"pub_key"`
  9. PrivKey PrivKey `json:"priv_key"`
  10. }
  11. // Generates a new account with private key.
  12. func GenPrivAccount() *PrivAccount {
  13. privKeyBytes := new([64]byte)
  14. copy(privKeyBytes[:32], CRandBytes(32))
  15. pubKeyBytes := ed25519.MakePublicKey(privKeyBytes)
  16. pubKey := PubKeyEd25519(pubKeyBytes[:])
  17. privKey := PrivKeyEd25519(privKeyBytes[:])
  18. return &PrivAccount{
  19. Address: pubKey.Address(),
  20. PubKey: pubKey,
  21. PrivKey: privKey,
  22. }
  23. }
  24. func GenPrivAccountFromKey(privKeyBytes [64]byte) *PrivAccount {
  25. pubKeyBytes := ed25519.MakePublicKey(&privKeyBytes)
  26. pubKey := PubKeyEd25519(pubKeyBytes[:])
  27. privKey := PrivKeyEd25519(privKeyBytes[:])
  28. return &PrivAccount{
  29. Address: pubKey.Address(),
  30. PubKey: pubKey,
  31. PrivKey: privKey,
  32. }
  33. }
  34. func (privAccount *PrivAccount) Sign(o Signable) Signature {
  35. return privAccount.PrivKey.Sign(SignBytes(o))
  36. }
  37. func (privAccount *PrivAccount) String() string {
  38. return Fmt("PrivAccount{%X}", privAccount.Address)
  39. }