go-pulse/crypto/key_store_plain.go

153 lines
4.3 KiB
Go
Raw Normal View History

/*
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 (
"encoding/hex"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
2015-06-21 21:17:17 +00:00
"sort"
"syscall"
"time"
"github.com/ethereum/go-ethereum/common"
)
// TODO: rename to KeyStore when replacing existing KeyStore
type KeyStore2 interface {
// create new key using io.Reader entropy source and optionally using auth string
GenerateNewKey(io.Reader, string) (*Key, error)
GetKey(common.Address, string) (*Key, error) // key from addr and auth string
GetKeyAddresses() ([]common.Address, error) // get all addresses
StoreKey(*Key, string) error // store key optionally using auth string
DeleteKey(common.Address, string) error // delete key by addr and auth string
}
type keyStorePlain struct {
keysDirPath string
}
func NewKeyStorePlain(path string) KeyStore2 {
return &keyStorePlain{path}
}
func (ks keyStorePlain) GenerateNewKey(rand io.Reader, auth string) (key *Key, err error) {
return GenerateNewKeyDefault(ks, rand, auth)
}
func GenerateNewKeyDefault(ks KeyStore2, rand io.Reader, auth string) (key *Key, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("GenerateNewKey error: %v", r)
}
}()
key = NewKey(rand)
err = ks.StoreKey(key, auth)
return key, err
}
func (ks keyStorePlain) GetKey(keyAddr common.Address, auth string) (key *Key, err error) {
fileContent, err := GetKeyFile(ks.keysDirPath, keyAddr)
if err != nil {
return nil, err
}
key = new(Key)
err = json.Unmarshal(fileContent, key)
return key, err
}
func (ks keyStorePlain) GetKeyAddresses() (addresses []common.Address, err error) {
return GetKeyAddresses(ks.keysDirPath)
}
func (ks keyStorePlain) StoreKey(key *Key, auth string) (err error) {
keyJSON, err := json.Marshal(key)
if err != nil {
return err
}
err = WriteKeyFile(key.Address, ks.keysDirPath, keyJSON)
return err
}
func (ks keyStorePlain) DeleteKey(keyAddr common.Address, auth string) (err error) {
keyDirPath := filepath.Join(ks.keysDirPath, keyAddr.Hex())
err = os.RemoveAll(keyDirPath)
return err
}
func GetKeyFile(keysDirPath string, keyAddr common.Address) (fileContent []byte, err error) {
fileName := hex.EncodeToString(keyAddr[:])
return ioutil.ReadFile(filepath.Join(keysDirPath, fileName, fileName))
}
func WriteKeyFile(addr common.Address, keysDirPath string, content []byte) (err error) {
addrHex := hex.EncodeToString(addr[:])
keyDirPath := filepath.Join(keysDirPath, addrHex)
keyFilePath := filepath.Join(keyDirPath, addrHex)
err = os.MkdirAll(keyDirPath, 0700) // read, write and dir search for user
if err != nil {
return err
}
return ioutil.WriteFile(keyFilePath, content, 0600) // read, write for user
}
func GetKeyAddresses(keysDirPath string) (addresses []common.Address, err error) {
fileInfos, err := ioutil.ReadDir(keysDirPath)
if err != nil {
return nil, err
}
2015-06-21 21:17:17 +00:00
var kfis keyFileInfos
for _, fileInfo := range fileInfos {
2015-06-21 21:17:17 +00:00
stat := fileInfo.Sys().(*syscall.Stat_t)
ctime := time.Unix(int64(stat.Ctimespec.Sec), int64(stat.Ctimespec.Nsec))
kfis = append(kfis, keyFileInfo{fileInfo.Name(), ctime})
}
sort.Sort(kfis)
for _, kfi := range kfis {
address, err := hex.DecodeString(kfi.name)
if err != nil {
continue
}
addresses = append(addresses, common.BytesToAddress(address))
}
return addresses, err
}
2015-06-21 21:17:17 +00:00
type keyFileInfo struct {
name string
ctime time.Time
}
type keyFileInfos []keyFileInfo
func (a keyFileInfos) Len() int { return len(a) }
func (a keyFileInfos) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a keyFileInfos) Less(i, j int) bool {
return a[i].ctime.Before(a[j].ctime)
}