2015-07-07 03:54:22 +03:00
|
|
|
// Copyright 2014 The go-ethereum Authors
|
|
|
|
// 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 Lesser 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/>.
|
2014-12-31 16:39:33 +02:00
|
|
|
|
|
|
|
package crypto
|
|
|
|
|
|
|
|
import (
|
2015-01-25 03:07:20 +02:00
|
|
|
"encoding/hex"
|
2014-12-31 16:39:33 +02:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2015-01-15 18:45:45 +02:00
|
|
|
"io"
|
2014-12-31 16:39:33 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2015-05-12 15:24:11 +03:00
|
|
|
"path/filepath"
|
2015-06-22 00:17:17 +03:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2014-12-31 16:39:33 +02:00
|
|
|
)
|
|
|
|
|
2015-07-03 00:58:00 +03:00
|
|
|
type KeyStore interface {
|
2015-01-15 18:45:45 +02:00
|
|
|
// create new key using io.Reader entropy source and optionally using auth string
|
|
|
|
GenerateNewKey(io.Reader, string) (*Key, error)
|
2015-07-03 00:58:00 +03:00
|
|
|
GetKey(common.Address, string) (*Key, error) // get key from addr and auth string
|
2015-04-02 22:14:25 +03:00
|
|
|
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
|
2015-07-03 06:56:20 +03:00
|
|
|
Cleanup(keyAddr common.Address) (err error)
|
2014-12-31 16:39:33 +02:00
|
|
|
}
|
|
|
|
|
2015-01-07 17:06:26 +02:00
|
|
|
type keyStorePlain struct {
|
2014-12-31 16:39:33 +02:00
|
|
|
keysDirPath string
|
|
|
|
}
|
|
|
|
|
2015-07-03 00:58:00 +03:00
|
|
|
func NewKeyStorePlain(path string) KeyStore {
|
2015-01-19 23:12:22 +02:00
|
|
|
return &keyStorePlain{path}
|
2015-01-07 17:06:26 +02:00
|
|
|
}
|
|
|
|
|
2015-01-15 18:45:45 +02:00
|
|
|
func (ks keyStorePlain) GenerateNewKey(rand io.Reader, auth string) (key *Key, err error) {
|
|
|
|
return GenerateNewKeyDefault(ks, rand, auth)
|
2014-12-31 16:39:33 +02:00
|
|
|
}
|
|
|
|
|
2015-07-03 00:58:00 +03:00
|
|
|
func GenerateNewKeyDefault(ks KeyStore, rand io.Reader, auth string) (key *Key, err error) {
|
2014-12-31 16:39:33 +02:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
err = fmt.Errorf("GenerateNewKey error: %v", r)
|
|
|
|
}
|
|
|
|
}()
|
2015-01-15 18:45:45 +02:00
|
|
|
key = NewKey(rand)
|
2014-12-31 16:39:33 +02:00
|
|
|
err = ks.StoreKey(key, auth)
|
2015-01-07 17:06:26 +02:00
|
|
|
return key, err
|
2014-12-31 16:39:33 +02:00
|
|
|
}
|
|
|
|
|
2015-04-02 22:14:25 +03:00
|
|
|
func (ks keyStorePlain) GetKey(keyAddr common.Address, auth string) (key *Key, err error) {
|
2015-07-03 00:58:00 +03:00
|
|
|
key = new(Key)
|
|
|
|
err = getKey(ks.keysDirPath, keyAddr, key)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func getKey(keysDirPath string, keyAddr common.Address, content interface{}) (err error) {
|
|
|
|
fileContent, err := getKeyFile(keysDirPath, keyAddr)
|
2014-12-31 16:39:33 +02:00
|
|
|
if err != nil {
|
2015-07-03 00:58:00 +03:00
|
|
|
return
|
2014-12-31 16:39:33 +02:00
|
|
|
}
|
2015-07-03 00:58:00 +03:00
|
|
|
return json.Unmarshal(fileContent, content)
|
2014-12-31 16:39:33 +02:00
|
|
|
}
|
|
|
|
|
2015-04-02 22:14:25 +03:00
|
|
|
func (ks keyStorePlain) GetKeyAddresses() (addresses []common.Address, err error) {
|
2015-07-03 00:58:00 +03:00
|
|
|
return getKeyAddresses(ks.keysDirPath)
|
2015-01-25 03:07:20 +02:00
|
|
|
}
|
|
|
|
|
2015-07-03 06:56:20 +03:00
|
|
|
func (ks keyStorePlain) Cleanup(keyAddr common.Address) (err error) {
|
|
|
|
return cleanup(ks.keysDirPath, keyAddr)
|
|
|
|
}
|
|
|
|
|
2015-01-07 17:06:26 +02:00
|
|
|
func (ks keyStorePlain) StoreKey(key *Key, auth string) (err error) {
|
2014-12-31 16:39:33 +02:00
|
|
|
keyJSON, err := json.Marshal(key)
|
|
|
|
if err != nil {
|
2015-07-03 00:58:00 +03:00
|
|
|
return
|
2014-12-31 16:39:33 +02:00
|
|
|
}
|
2015-07-03 00:58:00 +03:00
|
|
|
err = writeKeyFile(key.Address, ks.keysDirPath, keyJSON)
|
|
|
|
return
|
2014-12-31 16:39:33 +02:00
|
|
|
}
|
|
|
|
|
2015-04-02 22:14:25 +03:00
|
|
|
func (ks keyStorePlain) DeleteKey(keyAddr common.Address, auth string) (err error) {
|
2015-07-03 00:58:00 +03:00
|
|
|
return deleteKey(ks.keysDirPath, keyAddr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteKey(keysDirPath string, keyAddr common.Address) (err error) {
|
2015-07-03 06:56:20 +03:00
|
|
|
var path string
|
|
|
|
path, err = getKeyFilePath(keysDirPath, keyAddr)
|
2015-07-03 00:58:00 +03:00
|
|
|
if err == nil {
|
2015-07-03 06:56:20 +03:00
|
|
|
addrHex := hex.EncodeToString(keyAddr[:])
|
|
|
|
if path == filepath.Join(keysDirPath, addrHex, addrHex) {
|
|
|
|
path = filepath.Join(keysDirPath, addrHex)
|
|
|
|
}
|
|
|
|
err = os.RemoveAll(path)
|
2015-07-03 00:58:00 +03:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func getKeyFilePath(keysDirPath string, keyAddr common.Address) (keyFilePath string, err error) {
|
|
|
|
addrHex := hex.EncodeToString(keyAddr[:])
|
|
|
|
matches, err := filepath.Glob(filepath.Join(keysDirPath, fmt.Sprintf("*--%s", addrHex)))
|
|
|
|
if len(matches) > 0 {
|
|
|
|
if err == nil {
|
|
|
|
keyFilePath = matches[len(matches)-1]
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
keyFilePath = filepath.Join(keysDirPath, addrHex, addrHex)
|
|
|
|
_, err = os.Stat(keyFilePath)
|
|
|
|
return
|
2014-12-31 16:39:33 +02:00
|
|
|
}
|
|
|
|
|
2015-07-03 06:56:20 +03:00
|
|
|
func cleanup(keysDirPath string, keyAddr common.Address) (err error) {
|
|
|
|
fileInfos, err := ioutil.ReadDir(keysDirPath)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var paths []string
|
|
|
|
account := hex.EncodeToString(keyAddr[:])
|
|
|
|
for _, fileInfo := range fileInfos {
|
|
|
|
path := filepath.Join(keysDirPath, fileInfo.Name())
|
|
|
|
if len(path) >= 40 {
|
|
|
|
addr := path[len(path)-40 : len(path)]
|
|
|
|
if addr == account {
|
|
|
|
if path == filepath.Join(keysDirPath, addr, addr) {
|
|
|
|
path = filepath.Join(keysDirPath, addr)
|
|
|
|
}
|
|
|
|
paths = append(paths, path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(paths) > 1 {
|
|
|
|
for i := 0; err == nil && i < len(paths)-1; i++ {
|
|
|
|
err = os.RemoveAll(paths[i])
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-07-03 00:58:00 +03:00
|
|
|
func getKeyFile(keysDirPath string, keyAddr common.Address) (fileContent []byte, err error) {
|
|
|
|
var keyFilePath string
|
|
|
|
keyFilePath, err = getKeyFilePath(keysDirPath, keyAddr)
|
|
|
|
if err == nil {
|
|
|
|
fileContent, err = ioutil.ReadFile(keyFilePath)
|
|
|
|
}
|
|
|
|
return
|
2014-12-31 16:39:33 +02:00
|
|
|
}
|
|
|
|
|
2015-07-03 00:58:00 +03:00
|
|
|
func writeKeyFile(addr common.Address, keysDirPath string, content []byte) (err error) {
|
|
|
|
filename := keyFileName(addr)
|
|
|
|
// read, write and dir search for user
|
|
|
|
err = os.MkdirAll(keysDirPath, 0700)
|
2014-12-31 16:39:33 +02:00
|
|
|
if err != nil {
|
2015-01-07 17:06:26 +02:00
|
|
|
return err
|
2014-12-31 16:39:33 +02:00
|
|
|
}
|
2015-07-03 00:58:00 +03:00
|
|
|
// read, write for user
|
|
|
|
return ioutil.WriteFile(filepath.Join(keysDirPath, filename), content, 0600)
|
2014-12-31 16:39:33 +02:00
|
|
|
}
|
2015-01-25 03:07:20 +02:00
|
|
|
|
2015-07-03 00:58:00 +03:00
|
|
|
// keyFilePath implements the naming convention for keyfiles:
|
|
|
|
// UTC--<created_at UTC ISO8601>-<address hex>
|
|
|
|
func keyFileName(keyAddr common.Address) string {
|
|
|
|
ts := time.Now().UTC()
|
|
|
|
return fmt.Sprintf("UTC--%s--%s", toISO8601(ts), hex.EncodeToString(keyAddr[:]))
|
|
|
|
}
|
|
|
|
|
|
|
|
func toISO8601(t time.Time) string {
|
|
|
|
var tz string
|
|
|
|
name, offset := t.Zone()
|
|
|
|
if name == "UTC" {
|
|
|
|
tz = "Z"
|
|
|
|
} else {
|
|
|
|
tz = fmt.Sprintf("%03d00", offset/3600)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%04d-%02d-%02dT%02d:%02d:%02d.%09d%s", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), tz)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getKeyAddresses(keysDirPath string) (addresses []common.Address, err error) {
|
2015-01-25 03:07:20 +02:00
|
|
|
fileInfos, err := ioutil.ReadDir(keysDirPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-04-23 06:52:41 +03:00
|
|
|
for _, fileInfo := range fileInfos {
|
2015-07-03 00:58:00 +03:00
|
|
|
filename := fileInfo.Name()
|
|
|
|
if len(filename) >= 40 {
|
|
|
|
addr := filename[len(filename)-40 : len(filename)]
|
|
|
|
address, err := hex.DecodeString(addr)
|
|
|
|
if err == nil {
|
|
|
|
addresses = append(addresses, common.BytesToAddress(address))
|
|
|
|
}
|
2015-02-24 19:03:10 +02:00
|
|
|
}
|
2015-01-25 03:07:20 +02:00
|
|
|
}
|
|
|
|
return addresses, err
|
|
|
|
}
|