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.

154 lines
4.1 KiB

  1. package cryptostore
  2. import (
  3. "strings"
  4. crypto "github.com/tendermint/go-crypto"
  5. keys "github.com/tendermint/go-crypto/keys"
  6. )
  7. // Manager combines encyption and storage implementation to provide
  8. // a full-featured key manager
  9. type Manager struct {
  10. es encryptedStorage
  11. codec keys.Codec
  12. }
  13. func New(coder Encoder, store keys.Storage, codec keys.Codec) Manager {
  14. return Manager{
  15. es: encryptedStorage{
  16. coder: coder,
  17. store: store,
  18. },
  19. codec: codec,
  20. }
  21. }
  22. // exists just to make sure we fulfill the Signer interface
  23. func (s Manager) assertSigner() keys.Signer {
  24. return s
  25. }
  26. // exists just to make sure we fulfill the Manager interface
  27. func (s Manager) assertKeyManager() keys.Manager {
  28. return s
  29. }
  30. // Create adds a new key to the storage engine, returning error if
  31. // another key already stored under this name
  32. //
  33. // algo must be a supported go-crypto algorithm:
  34. //
  35. func (s Manager) Create(name, passphrase, algo string) (keys.Info, string, error) {
  36. gen, err := getGenerator(algo)
  37. if err != nil {
  38. return keys.Info{}, "", err
  39. }
  40. key := gen.Generate()
  41. err = s.es.Put(name, passphrase, key)
  42. if err != nil {
  43. return keys.Info{}, "", err
  44. }
  45. seed, err := s.codec.BytesToWords(key.Bytes())
  46. phrase := strings.Join(seed, " ")
  47. return info(name, key), phrase, err
  48. }
  49. func (s Manager) Recover(name, passphrase, seedphrase string) (keys.Info, error) {
  50. words := strings.Split(strings.TrimSpace(seedphrase), " ")
  51. data, err := s.codec.WordsToBytes(words)
  52. if err != nil {
  53. return keys.Info{}, err
  54. }
  55. key, err := crypto.PrivKeyFromBytes(data)
  56. if err != nil {
  57. return keys.Info{}, err
  58. }
  59. // d00d, it worked! create the bugger....
  60. err = s.es.Put(name, passphrase, key)
  61. return info(name, key), err
  62. }
  63. // List loads the keys from the storage and enforces alphabetical order
  64. func (s Manager) List() (keys.Infos, error) {
  65. res, err := s.es.List()
  66. res.Sort()
  67. return res, err
  68. }
  69. // Get returns the public information about one key
  70. func (s Manager) Get(name string) (keys.Info, error) {
  71. _, info, err := s.es.store.Get(name)
  72. return info, err
  73. }
  74. // Sign will modify the Signable in order to attach a valid signature with
  75. // this public key
  76. //
  77. // If no key for this name, or the passphrase doesn't match, returns an error
  78. func (s Manager) Sign(name, passphrase string, tx keys.Signable) error {
  79. key, _, err := s.es.Get(name, passphrase)
  80. if err != nil {
  81. return err
  82. }
  83. sig := key.Sign(tx.SignBytes())
  84. pubkey := key.PubKey()
  85. return tx.Sign(pubkey, sig)
  86. }
  87. // Export decodes the private key with the current password, encodes
  88. // it with a secure one-time password and generates a sequence that can be
  89. // Imported by another Manager
  90. //
  91. // This is designed to copy from one device to another, or provide backups
  92. // during version updates.
  93. func (s Manager) Export(name, oldpass, transferpass string) ([]byte, error) {
  94. key, _, err := s.es.Get(name, oldpass)
  95. if err != nil {
  96. return nil, err
  97. }
  98. res, err := s.es.coder.Encrypt(key, transferpass)
  99. return res, err
  100. }
  101. // Import accepts bytes generated by Export along with the same transferpass
  102. // If they are valid, it stores the password under the given name with the
  103. // new passphrase.
  104. func (s Manager) Import(name, newpass, transferpass string, data []byte) error {
  105. key, err := s.es.coder.Decrypt(data, transferpass)
  106. if err != nil {
  107. return err
  108. }
  109. return s.es.Put(name, newpass, key)
  110. }
  111. // Delete removes key forever, but we must present the
  112. // proper passphrase before deleting it (for security)
  113. func (s Manager) Delete(name, passphrase string) error {
  114. // verify we have the proper password before deleting
  115. _, _, err := s.es.Get(name, passphrase)
  116. if err != nil {
  117. return err
  118. }
  119. return s.es.Delete(name)
  120. }
  121. // Update changes the passphrase with which a already stored key is encoded.
  122. //
  123. // oldpass must be the current passphrase used for encoding, newpass will be
  124. // the only valid passphrase from this time forward
  125. func (s Manager) Update(name, oldpass, newpass string) error {
  126. key, _, err := s.es.Get(name, oldpass)
  127. if err != nil {
  128. return err
  129. }
  130. // we must delete first, as Putting over an existing name returns an error
  131. s.Delete(name, oldpass)
  132. return s.es.Put(name, newpass, key)
  133. }