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.

77 lines
2.1 KiB

10 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. func (pA *PrivAccount) Generate(index int) *PrivAccount {
  13. newPrivKey := pA.PrivKey.(PrivKeyEd25519).Generate(index)
  14. newPubKey := newPrivKey.PubKey()
  15. newAddress := newPubKey.Address()
  16. return &PrivAccount{
  17. Address: newAddress,
  18. PubKey: newPubKey,
  19. PrivKey: newPrivKey,
  20. }
  21. }
  22. func (pA *PrivAccount) Sign(chainID string, o Signable) Signature {
  23. return pA.PrivKey.Sign(SignBytes(chainID, o))
  24. }
  25. func (pA *PrivAccount) String() string {
  26. return Fmt("PrivAccount{%X}", pA.Address)
  27. }
  28. //----------------------------------------
  29. // Generates a new account with private key.
  30. func GenPrivAccount() *PrivAccount {
  31. privKeyBytes := new([64]byte)
  32. copy(privKeyBytes[:32], CRandBytes(32))
  33. pubKeyBytes := ed25519.MakePublicKey(privKeyBytes)
  34. pubKey := PubKeyEd25519(*pubKeyBytes)
  35. privKey := PrivKeyEd25519(*privKeyBytes)
  36. return &PrivAccount{
  37. Address: pubKey.Address(),
  38. PubKey: pubKey,
  39. PrivKey: privKey,
  40. }
  41. }
  42. // Generates a new account with private key from SHA256 hash of a secret
  43. func GenPrivAccountFromSecret(secret []byte) *PrivAccount {
  44. privKey32 := binary.BinarySha256(secret) // Not Ripemd160 because we want 32 bytes.
  45. privKeyBytes := new([64]byte)
  46. copy(privKeyBytes[:32], privKey32)
  47. pubKeyBytes := ed25519.MakePublicKey(privKeyBytes)
  48. pubKey := PubKeyEd25519(*pubKeyBytes)
  49. privKey := PrivKeyEd25519(*privKeyBytes)
  50. return &PrivAccount{
  51. Address: pubKey.Address(),
  52. PubKey: pubKey,
  53. PrivKey: privKey,
  54. }
  55. }
  56. func GenPrivAccountFromPrivKeyBytes(privKeyBytes *[64]byte) *PrivAccount {
  57. if len(privKeyBytes) != 64 {
  58. PanicSanity(Fmt("Expected 64 bytes but got %v", len(privKeyBytes)))
  59. }
  60. pubKeyBytes := ed25519.MakePublicKey(privKeyBytes)
  61. pubKey := PubKeyEd25519(*pubKeyBytes)
  62. privKey := PrivKeyEd25519(*privKeyBytes)
  63. return &PrivAccount{
  64. Address: pubKey.Address(),
  65. PubKey: pubKey,
  66. PrivKey: privKey,
  67. }
  68. }