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.

61 lines
1.7 KiB

10 years ago
9 years ago
10 years ago
10 years ago
  1. package account
  2. import (
  3. "github.com/tendermint/tendermint/Godeps/_workspace/src/github.com/tendermint/ed25519"
  4. "github.com/tendermint/tendermint/binary"
  5. . "github.com/tendermint/tendermint/common"
  6. )
  7. type PrivAccount struct {
  8. Address []byte `json:"address"`
  9. PubKey PubKey `json:"pub_key"`
  10. PrivKey PrivKey `json:"priv_key"`
  11. }
  12. // Generates a new account with private key.
  13. func GenPrivAccount() *PrivAccount {
  14. privKeyBytes := new([64]byte)
  15. copy(privKeyBytes[:32], CRandBytes(32))
  16. pubKeyBytes := ed25519.MakePublicKey(privKeyBytes)
  17. pubKey := PubKeyEd25519(pubKeyBytes[:])
  18. privKey := PrivKeyEd25519(privKeyBytes[:])
  19. return &PrivAccount{
  20. Address: pubKey.Address(),
  21. PubKey: pubKey,
  22. PrivKey: privKey,
  23. }
  24. }
  25. // Generates a new account with private key from SHA256 hash of a secret
  26. func GenPrivAccountFromSecret(secret []byte) *PrivAccount {
  27. privKey32 := binary.BinarySha256(secret)
  28. privKeyBytes := new([64]byte)
  29. copy(privKeyBytes[:32], privKey32)
  30. pubKeyBytes := ed25519.MakePublicKey(privKeyBytes)
  31. pubKey := PubKeyEd25519(pubKeyBytes[:])
  32. privKey := PrivKeyEd25519(privKeyBytes[:])
  33. return &PrivAccount{
  34. Address: pubKey.Address(),
  35. PubKey: pubKey,
  36. PrivKey: privKey,
  37. }
  38. }
  39. func GenPrivAccountFromKey(privKeyBytes [64]byte) *PrivAccount {
  40. pubKeyBytes := ed25519.MakePublicKey(&privKeyBytes)
  41. pubKey := PubKeyEd25519(pubKeyBytes[:])
  42. privKey := PrivKeyEd25519(privKeyBytes[:])
  43. return &PrivAccount{
  44. Address: pubKey.Address(),
  45. PubKey: pubKey,
  46. PrivKey: privKey,
  47. }
  48. }
  49. func (privAccount *PrivAccount) Sign(chainID string, o Signable) Signature {
  50. return privAccount.PrivKey.Sign(SignBytes(chainID, o))
  51. }
  52. func (privAccount *PrivAccount) String() string {
  53. return Fmt("PrivAccount{%X}", privAccount.Address)
  54. }