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