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.

49 lines
1.1 KiB

  1. package cryptostore
  2. import (
  3. crypto "github.com/tendermint/go-crypto"
  4. keys "github.com/tendermint/go-keys"
  5. )
  6. // encryptedStorage needs passphrase to get private keys
  7. type encryptedStorage struct {
  8. coder Encoder
  9. store keys.Storage
  10. }
  11. func (es encryptedStorage) Put(name, pass string, key crypto.PrivKey) error {
  12. secret, err := es.coder.Encrypt(key, pass)
  13. if err != nil {
  14. return err
  15. }
  16. ki := info(name, key)
  17. return es.store.Put(name, secret, ki)
  18. }
  19. func (es encryptedStorage) Get(name, pass string) (crypto.PrivKey, keys.Info, error) {
  20. secret, info, err := es.store.Get(name)
  21. if err != nil {
  22. return nil, info, err
  23. }
  24. key, err := es.coder.Decrypt(secret, pass)
  25. return key, info, err
  26. }
  27. func (es encryptedStorage) List() (keys.Infos, error) {
  28. return es.store.List()
  29. }
  30. func (es encryptedStorage) Delete(name string) error {
  31. return es.store.Delete(name)
  32. }
  33. // info hardcodes the encoding of keys
  34. func info(name string, key crypto.PrivKey) keys.Info {
  35. pub := key.PubKey()
  36. return keys.Info{
  37. Name: name,
  38. Address: pub.Address(),
  39. PubKey: crypto.PubKeyS{pub},
  40. }
  41. }