key_store_plain.go 3.59 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
	This file is part of go-ethereum

	go-ethereum is free software: you can redistribute it and/or modify
	it under the terms of the GNU Lesser General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	go-ethereum is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with go-ethereum.  If not, see <http://www.gnu.org/licenses/>.
*/
/**
 * @authors
 * 	Gustav Simonsson <gustav.simonsson@gmail.com>
 * @date 2015
 *
 */

package crypto

import (
27
	"encoding/hex"
28 29
	"encoding/json"
	"fmt"
30
	"io"
31 32 33 34 35 36 37
	"io/ioutil"
	"os"
	"path"
)

// TODO: rename to KeyStore when replacing existing KeyStore
type KeyStore2 interface {
38 39
	// create new key using io.Reader entropy source and optionally using auth string
	GenerateNewKey(io.Reader, string) (*Key, error)
40 41 42 43
	GetKey([]byte, string) (*Key, error) // key from addr and auth string
	GetKeyAddresses() ([][]byte, error)  // get all addresses
	StoreKey(*Key, string) error         // store key optionally using auth string
	DeleteKey([]byte, string) error      // delete key by addr and auth string
44 45
}

46
type keyStorePlain struct {
47 48 49
	keysDirPath string
}

50
func NewKeyStorePlain(path string) KeyStore2 {
51
	return &keyStorePlain{path}
52 53
}

54 55
func (ks keyStorePlain) GenerateNewKey(rand io.Reader, auth string) (key *Key, err error) {
	return GenerateNewKeyDefault(ks, rand, auth)
56 57
}

58
func GenerateNewKeyDefault(ks KeyStore2, rand io.Reader, auth string) (key *Key, err error) {
59 60 61 62 63
	defer func() {
		if r := recover(); r != nil {
			err = fmt.Errorf("GenerateNewKey error: %v", r)
		}
	}()
64
	key = NewKey(rand)
65
	err = ks.StoreKey(key, auth)
66
	return key, err
67 68
}

69 70
func (ks keyStorePlain) GetKey(keyAddr []byte, auth string) (key *Key, err error) {
	fileContent, err := GetKeyFile(ks.keysDirPath, keyAddr)
71
	if err != nil {
72
		return nil, err
73 74 75 76
	}

	key = new(Key)
	err = json.Unmarshal(fileContent, key)
77
	return key, err
78 79
}

80 81 82 83
func (ks keyStorePlain) GetKeyAddresses() (addresses [][]byte, err error) {
	return GetKeyAddresses(ks.keysDirPath)
}

84
func (ks keyStorePlain) StoreKey(key *Key, auth string) (err error) {
85 86
	keyJSON, err := json.Marshal(key)
	if err != nil {
87
		return err
88
	}
89
	err = WriteKeyFile(key.Address, ks.keysDirPath, keyJSON)
90
	return err
91 92
}

93 94
func (ks keyStorePlain) DeleteKey(keyAddr []byte, auth string) (err error) {
	keyDirPath := path.Join(ks.keysDirPath, hex.EncodeToString(keyAddr))
95
	err = os.RemoveAll(keyDirPath)
96
	return err
97 98
}

99 100 101
func GetKeyFile(keysDirPath string, keyAddr []byte) (fileContent []byte, err error) {
	fileName := hex.EncodeToString(keyAddr)
	return ioutil.ReadFile(path.Join(keysDirPath, fileName, fileName))
102 103
}

104 105 106 107
func WriteKeyFile(addr []byte, keysDirPath string, content []byte) (err error) {
	addrHex := hex.EncodeToString(addr)
	keyDirPath := path.Join(keysDirPath, addrHex)
	keyFilePath := path.Join(keyDirPath, addrHex)
108 109
	err = os.MkdirAll(keyDirPath, 0700) // read, write and dir search for user
	if err != nil {
110
		return err
111
	}
112
	return ioutil.WriteFile(keyFilePath, content, 0600) // read, write for user
113
}
114 115 116 117 118 119 120 121

func GetKeyAddresses(keysDirPath string) (addresses [][]byte, err error) {
	fileInfos, err := ioutil.ReadDir(keysDirPath)
	if err != nil {
		return nil, err
	}
	addresses = make([][]byte, len(fileInfos))
	for i, fileInfo := range fileInfos {
122 123 124 125 126
		address, err := hex.DecodeString(fileInfo.Name())
		if err != nil {
			continue
		}
		addresses[i] = address
127 128 129
	}
	return addresses, err
}