key_store_plain.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. This file is part of go-ethereum
  3. go-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. go-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /**
  15. * @authors
  16. * Gustav Simonsson <gustav.simonsson@gmail.com>
  17. * @date 2015
  18. *
  19. */
  20. package crypto
  21. import (
  22. "encoding/hex"
  23. "encoding/json"
  24. "fmt"
  25. "io"
  26. "io/ioutil"
  27. "os"
  28. "path/filepath"
  29. )
  30. // TODO: rename to KeyStore when replacing existing KeyStore
  31. type KeyStore2 interface {
  32. // create new key using io.Reader entropy source and optionally using auth string
  33. GenerateNewKey(io.Reader, string) (*Key, error)
  34. GetKey([]byte, string) (*Key, error) // key from addr and auth string
  35. GetKeyAddresses() ([][]byte, error) // get all addresses
  36. StoreKey(*Key, string) error // store key optionally using auth string
  37. DeleteKey([]byte, string) error // delete key by addr and auth string
  38. }
  39. type keyStorePlain struct {
  40. keysDirPath string
  41. }
  42. func NewKeyStorePlain(path string) KeyStore2 {
  43. return &keyStorePlain{path}
  44. }
  45. func (ks keyStorePlain) GenerateNewKey(rand io.Reader, auth string) (key *Key, err error) {
  46. return GenerateNewKeyDefault(ks, rand, auth)
  47. }
  48. func GenerateNewKeyDefault(ks KeyStore2, rand io.Reader, auth string) (key *Key, err error) {
  49. defer func() {
  50. if r := recover(); r != nil {
  51. err = fmt.Errorf("GenerateNewKey error: %v", r)
  52. }
  53. }()
  54. key = NewKey(rand)
  55. err = ks.StoreKey(key, auth)
  56. return key, err
  57. }
  58. func (ks keyStorePlain) GetKey(keyAddr []byte, auth string) (key *Key, err error) {
  59. fileContent, err := GetKeyFile(ks.keysDirPath, keyAddr)
  60. if err != nil {
  61. return nil, err
  62. }
  63. key = new(Key)
  64. err = json.Unmarshal(fileContent, key)
  65. return key, err
  66. }
  67. func (ks keyStorePlain) GetKeyAddresses() (addresses [][]byte, err error) {
  68. return GetKeyAddresses(ks.keysDirPath)
  69. }
  70. func (ks keyStorePlain) StoreKey(key *Key, auth string) (err error) {
  71. keyJSON, err := json.Marshal(key)
  72. if err != nil {
  73. return err
  74. }
  75. err = WriteKeyFile(key.Address, ks.keysDirPath, keyJSON)
  76. return err
  77. }
  78. func (ks keyStorePlain) DeleteKey(keyAddr []byte, auth string) (err error) {
  79. keyDirPath := filepath.Join(ks.keysDirPath, hex.EncodeToString(keyAddr))
  80. err = os.RemoveAll(keyDirPath)
  81. return err
  82. }
  83. func GetKeyFile(keysDirPath string, keyAddr []byte) (fileContent []byte, err error) {
  84. fileName := hex.EncodeToString(keyAddr)
  85. return ioutil.ReadFile(filepath.Join(keysDirPath, fileName, fileName))
  86. }
  87. func WriteKeyFile(addr []byte, keysDirPath string, content []byte) (err error) {
  88. addrHex := hex.EncodeToString(addr)
  89. keyDirPath := filepath.Join(keysDirPath, addrHex)
  90. keyFilePath := filepath.Join(keyDirPath, addrHex)
  91. err = os.MkdirAll(keyDirPath, 0700) // read, write and dir search for user
  92. if err != nil {
  93. return err
  94. }
  95. return ioutil.WriteFile(keyFilePath, content, 0600) // read, write for user
  96. }
  97. func GetKeyAddresses(keysDirPath string) (addresses [][]byte, err error) {
  98. fileInfos, err := ioutil.ReadDir(keysDirPath)
  99. if err != nil {
  100. return nil, err
  101. }
  102. for _, fileInfo := range fileInfos {
  103. address, err := hex.DecodeString(fileInfo.Name())
  104. if err != nil {
  105. continue
  106. }
  107. addresses = append(addresses, address)
  108. }
  109. return addresses, err
  110. }