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.

63 lines
1.7 KiB

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/Godeps/_workspace/src/github.com/tendermint/ed25519/extra25519"
  5. "github.com/tendermint/tendermint/binary"
  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 binary.readReflect
  18. var _ = binary.RegisterInterface(
  19. struct{ PrivKey }{},
  20. binary.ConcreteType{PrivKeyEd25519{}, PrivKeyTypeEd25519},
  21. )
  22. //-------------------------------------
  23. // Implements PrivKey
  24. type PrivKeyEd25519 []byte
  25. func (privKey PrivKeyEd25519) Sign(msg []byte) Signature {
  26. pubKey := privKey.PubKey().(PubKeyEd25519)
  27. privKeyBytes := new([64]byte)
  28. copy(privKeyBytes[:32], privKey[:])
  29. copy(privKeyBytes[32:], pubKey[:])
  30. signatureBytes := ed25519.Sign(privKeyBytes, msg)
  31. return SignatureEd25519(signatureBytes[:])
  32. }
  33. func (privKey PrivKeyEd25519) PubKey() PubKey {
  34. privKeyBytes := new([64]byte)
  35. copy(privKeyBytes[:], privKey[:])
  36. return PubKeyEd25519(ed25519.MakePublicKey(privKeyBytes)[:])
  37. }
  38. func (privKey PrivKeyEd25519) ToCurve25519() *[32]byte {
  39. keyEd25519, keyCurve25519 := new([64]byte), new([32]byte)
  40. copy(keyEd25519[:], privKey)
  41. extra25519.PrivateKeyToCurve25519(keyCurve25519, keyEd25519)
  42. return keyCurve25519
  43. }
  44. func (privKey PrivKeyEd25519) String() string {
  45. return Fmt("PrivKeyEd25519{*****}")
  46. }
  47. func GenPrivKeyEd25519() PrivKeyEd25519 {
  48. privKeyBytes := new([64]byte)
  49. copy(privKeyBytes[:32], CRandBytes(32))
  50. ed25519.MakePublicKey(privKeyBytes)
  51. return PrivKeyEd25519(privKeyBytes[:])
  52. }