2016-04-14 19:18:24 +03:00
|
|
|
// Copyright 2015 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
|
|
|
|
2017-01-24 12:49:20 +03:00
|
|
|
package keystore
|
2014-12-31 16:39:33 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2015-05-12 15:24:11 +03:00
|
|
|
"path/filepath"
|
2015-06-22 00:17:17 +03:00
|
|
|
|
|
|
|
"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-03 02:15:42 +02:00
|
|
|
func (ks keyStorePlain) GetKey(addr common.Address, filename, auth string) (*Key, error) {
|
|
|
|
fd, err := os.Open(filename)
|
2014-12-31 16:39:33 +02:00
|
|
|
if err != nil {
|
2016-02-16 23:28:11 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-03 02:15:42 +02:00
|
|
|
defer fd.Close()
|
2016-02-16 23:28:11 +02:00
|
|
|
key := new(Key)
|
2016-03-03 02:15:42 +02:00
|
|
|
if err := json.NewDecoder(fd).Decode(key); err != nil {
|
2016-02-16 23:28:11 +02:00
|
|
|
return nil, err
|
2014-12-31 16:39:33 +02:00
|
|
|
}
|
2016-03-03 02:15:42 +02:00
|
|
|
if key.Address != addr {
|
|
|
|
return nil, fmt.Errorf("key content mismatch: have address %x, want %x", key.Address, addr)
|
2015-07-03 00:58:00 +03:00
|
|
|
}
|
2016-03-03 02:15:42 +02:00
|
|
|
return key, nil
|
2014-12-31 16:39:33 +02:00
|
|
|
}
|
|
|
|
|
2016-03-03 02:15:42 +02:00
|
|
|
func (ks keyStorePlain) StoreKey(filename string, key *Key, auth string) error {
|
|
|
|
content, err := json.Marshal(key)
|
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
|
|
|
}
|
2016-03-03 02:15:42 +02:00
|
|
|
return writeKeyFile(filename, content)
|
2014-12-31 16:39:33 +02:00
|
|
|
}
|
2015-01-25 03:07:20 +02:00
|
|
|
|
2016-03-03 02:15:42 +02:00
|
|
|
func (ks keyStorePlain) JoinPath(filename string) string {
|
|
|
|
if filepath.IsAbs(filename) {
|
|
|
|
return filename
|
2015-01-25 03:07:20 +02:00
|
|
|
}
|
2018-05-03 11:37:56 +03:00
|
|
|
return filepath.Join(ks.keysDirPath, filename)
|
2015-01-25 03:07:20 +02:00
|
|
|
}
|