2015-07-07 03:54:22 +03:00
|
|
|
// Copyright 2014 The go-ethereum Authors
|
2015-07-22 19:48:40 +03:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-07 03:54:22 +03:00
|
|
|
//
|
2015-07-23 19:35:11 +03:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-07 03:54:22 +03:00
|
|
|
// 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.
|
|
|
|
//
|
2015-07-22 19:48:40 +03:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-07 03:54:22 +03:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 19:48:40 +03:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-07 03:54:22 +03:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 19:48:40 +03:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2014-12-31 16:39:33 +02:00
|
|
|
|
2016-03-02 14:57:15 +02:00
|
|
|
package accounts
|
2014-12-31 16:39:33 +02:00
|
|
|
|
|
|
|
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-01-07 17:06:26 +02:00
|
|
|
type keyStorePlain struct {
|
2014-12-31 16:39:33 +02:00
|
|
|
keysDirPath string
|
|
|
|
}
|
|
|
|
|
2016-03-02 14:57:15 +02: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) {
|
2016-03-02 14:57:15 +02:00
|
|
|
return generateNewKeyDefault(ks, rand, auth)
|
2014-12-31 16:39:33 +02:00
|
|
|
}
|
|
|
|
|
2016-03-02 14:57:15 +02: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
|
|
|
}
|
|
|
|
|
2016-02-16 23:28:11 +02:00
|
|
|
func (ks keyStorePlain) GetKey(keyAddr common.Address, auth string) (*Key, error) {
|
|
|
|
keyjson, err := getKeyFile(ks.keysDirPath, keyAddr)
|
2014-12-31 16:39:33 +02:00
|
|
|
if err != nil {
|
2016-02-16 23:28:11 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
key := new(Key)
|
|
|
|
if err := json.Unmarshal(keyjson, key); err != nil {
|
|
|
|
return nil, err
|
2014-12-31 16:39:33 +02:00
|
|
|
}
|
2016-02-16 23:28:11 +02:00
|
|
|
return key, nil
|
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)
|
|
|
|
}
|
2015-07-09 15:55:09 +03:00
|
|
|
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)
|
2015-07-03 00:58:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|