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.

70 lines
1.8 KiB

9 years ago
  1. package account
  2. import (
  3. "github.com/tendermint/tendermint/Godeps/_workspace/src/github.com/tendermint/ed25519"
  4. "github.com/tendermint/tendermint/Godeps/_workspace/src/github.com/tendermint/ed25519/extra25519"
  5. "github.com/tendermint/tendermint/wire"
  6. . "github.com/tendermint/tendermint/common"
  7. )
  8. // PrivKey is part of PrivAccount and state.PrivValidator.
  9. type PrivKey interface {
  10. Sign(msg []byte) Signature
  11. PubKey() PubKey
  12. }
  13. // Types of PrivKey implementations
  14. const (
  15. PrivKeyTypeEd25519 = byte(0x01)
  16. )
  17. // for wire.readReflect
  18. var _ = wire.RegisterInterface(
  19. struct{ PrivKey }{},
  20. wire.ConcreteType{PrivKeyEd25519{}, PrivKeyTypeEd25519},
  21. )
  22. //-------------------------------------
  23. // Implements PrivKey
  24. type PrivKeyEd25519 [64]byte
  25. func (key PrivKeyEd25519) Sign(msg []byte) Signature {
  26. privKeyBytes := [64]byte(key)
  27. signatureBytes := ed25519.Sign(&privKeyBytes, msg)
  28. return SignatureEd25519(*signatureBytes)
  29. }
  30. func (privKey PrivKeyEd25519) PubKey() PubKey {
  31. privKeyBytes := [64]byte(privKey)
  32. return PubKeyEd25519(*ed25519.MakePublicKey(&privKeyBytes))
  33. }
  34. func (privKey PrivKeyEd25519) ToCurve25519() *[32]byte {
  35. keyCurve25519 := new([32]byte)
  36. privKeyBytes := [64]byte(privKey)
  37. extra25519.PrivateKeyToCurve25519(keyCurve25519, &privKeyBytes)
  38. return keyCurve25519
  39. }
  40. func (privKey PrivKeyEd25519) String() string {
  41. return Fmt("PrivKeyEd25519{*****}")
  42. }
  43. // Deterministically generates new priv-key bytes from key.
  44. func (key PrivKeyEd25519) Generate(index int) PrivKeyEd25519 {
  45. newBytes := wire.BinarySha256(struct {
  46. PrivKey [64]byte
  47. Index int
  48. }{key, index})
  49. var newKey [64]byte
  50. copy(newKey[:], newBytes)
  51. return PrivKeyEd25519(newKey)
  52. }
  53. func GenPrivKeyEd25519() PrivKeyEd25519 {
  54. privKeyBytes := new([64]byte)
  55. copy(privKeyBytes[:32], CRandBytes(32))
  56. ed25519.MakePublicKey(privKeyBytes)
  57. return PrivKeyEd25519(*privKeyBytes)
  58. }